01: /*
02: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.romaframework.aspect.view.form;
18:
19: import org.apache.commons.logging.Log;
20: import org.apache.commons.logging.LogFactory;
21: import org.apache.commons.pool.KeyedObjectPool;
22: import org.apache.commons.pool.impl.GenericKeyedObjectPool;
23: import org.apache.commons.pool.impl.GenericObjectPool;
24: import org.romaframework.aspect.view.ViewException;
25: import org.romaframework.core.flow.ObjectContext;
26: import org.romaframework.core.schema.SchemaField;
27: import org.romaframework.core.schema.SchemaObject;
28:
29: /**
30: * Factory and pool of forms to maximize form instance reusing.
31: *
32: * @author Luca Garulli (luca.garulli@assetdata.it)
33: */
34: public class FormPool {
35:
36: /**
37: * Set the maximum active forms the pool can manage before to be "exhausted" and wait for a new form instance.
38: *
39: * @param iMaxActiveFormInstances
40: */
41: public void setMaxActiveFormInstances(int iMaxActiveFormInstances) {
42: maxActiveFormInstances = iMaxActiveFormInstances;
43: }
44:
45: public void init() {
46: pool = new GenericKeyedObjectPool(new FormFactory(),
47: maxActiveFormInstances,
48: GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 0);
49: }
50:
51: public ContentForm getForm(SchemaObject iSchema,
52: SchemaField iField, Object iUserObject) {
53: ContentForm form;
54: try {
55: form = (ContentForm) pool.borrowObject(iSchema);
56: form.setMetaDataField(iField);
57: form.setContent(iUserObject);
58: } catch (Exception e) {
59: throw new ViewException(e);
60: }
61:
62: return form;
63: }
64:
65: public void releaseForm(ContentForm iFormInstance) {
66: try {
67: // REMOVE THE ASSOCIATION FROM THE USER OBJECT AND THE FORM
68: ObjectContext.getInstance().removeObjectFormAssociation(
69: iFormInstance.getContent());
70:
71: pool.returnObject(iFormInstance.getSchemaInstance(),
72: iFormInstance);
73: } catch (Exception e) {
74: log.error("[FormPool.releaseForm] Error", e);
75: }
76: }
77:
78: public KeyedObjectPool getPool() {
79: return pool;
80: }
81:
82: private KeyedObjectPool pool;
83:
84: private int maxActiveFormInstances = DEF_ACTIVES;
85: public static final int DEF_ACTIVES = 0;
86:
87: private static Log log = LogFactory.getLog(FormPool.class);
88: }
|