1 /**
2 * Copyright 2005-2015 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.krad.datadictionary.uif;
17
18 import org.kuali.rice.krad.uif.view.View;
19
20 import java.util.Stack;
21
22 /**
23 * Holds preloaded view instances up to a configured size
24 *
25 * <p>
26 * The initial creation of the view object from Spring can be expensive in certain cases. To help with this, views
27 * can be preloaded with this pool class. When a request for a new view instance is made, a check will be done first
28 * to see if there is a pool and if so pull the already loaded view
29 * </p>
30 *
31 * @author Kuali Rice Team (rice.collab@kuali.org)
32 * @see UifDictionaryIndex#getViewById(java.lang.String)
33 */
34 public class UifViewPool {
35 private int maxSize;
36 private Stack<View> views;
37
38 public UifViewPool() {
39 maxSize = 1;
40 views = new Stack<View>();
41 }
42
43 public UifViewPool(int maxSize) {
44 this.maxSize = maxSize;
45 views = new Stack<View>();
46 }
47
48 /**
49 * Maximum number of view instances the pool can hold
50 *
51 * <p>
52 * On initial startup of the application (during dictionary loading), view instances will be loaded and
53 * filled in a pool up to the max size configuration. The default is to preload one view, and each time
54 * the view is retrieved it is replaced. If a request is made before the view is replaced, the view is rebuilt
55 * from Spring. Therefore the performance gain is not present. For views with high concurrency, this property
56 * can be tweaked as needed. Please note larger pool sizes cost more in memory storage and application
57 * start up time
58 * </p>
59 *
60 * @return int max pool size
61 */
62 public int getMaxSize() {
63 return maxSize;
64 }
65
66 /**
67 * Setter for the pool max size
68 *
69 * @param maxSize
70 */
71 public void setMaxSize(int maxSize) {
72 this.maxSize = maxSize;
73 }
74
75 /**
76 * Adds a view instance to the pool
77 *
78 * @param view - view instance to add
79 */
80 public void addViewInstance(View view) {
81 views.push(view);
82 }
83
84 /**
85 * Retrieves a view instance from the pool and removes the instance
86 *
87 * @return View instance
88 */
89 public View getViewInstance() {
90 return views.pop();
91 }
92
93 /**
94 * Retrieves a view instance from the pool without removing it
95 *
96 * @return instance of a View
97 */
98 public View getViewSharedInstance() {
99 return views.peek();
100 }
101
102 /**
103 * Indicates whether the pool is full (number of view instances equals configured max size)
104 *
105 * @return boolean true if pool is full, else if not
106 */
107 public boolean isFull() {
108 return views.size() == maxSize;
109 }
110
111 /**
112 * Indicates whether the pool is empty (contains no view instances)
113 *
114 * <p>
115 * When the pool is empty, no view instances may be retrieved until the pool requires view instances.
116 * The calling code may choose to wait for a period of time and check again, or request a view instance from
117 * Spring
118 * </p>
119 *
120 * @return boolean true if the pool is empty, false if not
121 */
122 public boolean isEmpty() {
123 return (views == null) || (views.size() == 0);
124 }
125 }