KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > model > ModelManagerImp


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.model;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.concurrent.ConcurrentHashMap JavaDoc;
22
23 import com.jdon.controller.model.ModelIF;
24 import com.jdon.model.cache.ModelCacheManager;
25 import com.jdon.model.config.PoolParameter;
26 import com.jdon.model.factory.ModelHandlerClassBuilder;
27 import com.jdon.model.handler.HandlerObjectFactory;
28 import com.jdon.util.Debug;
29
30
31
32 /**
33  * ModelManager implemention
34  * @author banq
35  */

36 public class ModelManagerImp implements ModelManager{
37
38   public final static String JavaDoc module = ModelManagerImp.class.getName();
39
40   private ModelHandlerClassBuilder modelFactory;
41   private ModelCacheManager modelCacheManager;
42   private HandlerObjectFactory handlerObjectFactory;
43   
44   private Map JavaDoc modelPool = new ConcurrentHashMap JavaDoc();;
45   private int poolSize;
46   private final static int MODEL_INSTANCE_COUNT = 200;
47   
48   public ModelManagerImp(ModelHandlerClassBuilder modelXmlLoader,
49                         ModelCacheManager modelCacheManager,
50                         HandlerObjectFactory handlerObjectFactory,
51                         PoolParameter poolParameter) {
52     this.modelFactory = modelXmlLoader;
53     this.modelCacheManager = modelCacheManager;
54     this.handlerObjectFactory = handlerObjectFactory;
55     this.poolSize = poolParameter.getModelPoolSize();
56   }
57
58   /**
59    * borrow a Handler instance from Modelhandler pool
60    */

61   public ModelHandler borrowtHandlerObject(String JavaDoc formName) {
62     ModelHandler modelHandler = null;
63     try {
64       modelHandler = handlerObjectFactory.borrowHandlerObject(formName);
65       modelHandler.setModelMapping(modelFactory.getModelMapping(formName));
66     } catch (Exception JavaDoc ex) {
67       Debug.logError("[JdonFramework]can't get the modelHandler for the formName " + formName,
68                      module);
69       returnHandlerObject(modelHandler);
70     }
71     return modelHandler;
72   }
73
74   /**
75    * return the Handler instance.
76    *
77    */

78   public void returnHandlerObject(ModelHandler modelHandler) {
79     if (modelHandler == null) return;
80     try {
81         handlerObjectFactory.returnHandlerObject(modelHandler);
82     } catch (Exception JavaDoc ex) {
83       Debug.logError("[JdonFramework] return modelHandler error" + ex, module);
84     }
85
86   }
87
88   /**
89    * get a Model instance from the model pool
90    * the model poo size is decided by the PoolParameter configured in container.xml
91    *
92    */

93   public ModelIF getModelObject(String JavaDoc formName) {
94       ModelIF model = null;
95       try {
96           String JavaDoc poolKey = (modelFactory.getModelMapping(formName)).getClassName();
97           Debug.logVerbose("[JdonFramework]--> get Model object " + poolKey, module);
98
99           LinkedList JavaDoc list = (LinkedList JavaDoc) modelPool.get(poolKey);
100           if ( (list == null) || (list.isEmpty())) {
101             Debug.logVerbose("[JdonFramework]--> create Model object " + poolSize,
102                              module);
103             int count = 0;
104             list = new LinkedList JavaDoc();
105             while (count < poolSize) {
106               model = makeModelObject(formName);
107               list.add(model);
108               count++;
109             }
110             modelPool.put(poolKey, list);
111           }
112           model = (ModelIF) list.removeFirst();
113     } catch (Exception JavaDoc ex) {
114         Debug.logError("[JdonFramework]getModelObject error: " + ex, module);
115     }
116     return model;
117   }
118
119  
120   /**
121    * create model instance from the model class that read from the xml configure.
122    * @param formName
123    * @return
124    * @throws Exception
125    */

126   private ModelIF makeModelObject(String JavaDoc formName) {
127       ModelIF object = null;
128       Class JavaDoc modelClass = null;
129       try {
130         modelClass = (Class JavaDoc) modelFactory.getModelClasses(formName);
131         if (modelClass == null) {
132           throw new Exception JavaDoc(
133               " not found the model in config xml, formName=" + formName);
134         }
135         object = (ModelIF) modelClass.newInstance();
136       } catch (Exception JavaDoc e) {
137         Debug.logError("[JdonFramework]--> call Model: " + modelClass + " error:" + e, module);
138       }
139       return object;
140   }
141   
142   /**
143    * add the model to the cache
144    * @param modelKey
145    * @param model
146    */

147   public void addCache(ModelKey modelKey, ModelIF model) {
148     if ((modelKey == null) || (modelKey.getDataKey() == null))
149       return;
150
151     if (modelKey.getModelClassName() != null)
152       modelCacheManager.setCache2(modelKey.getDataKey(),
153                           modelKey.getModelClassName().getName(), model);
154     else
155       modelCacheManager.setCache(modelKey.getDataKey(),
156                          modelKey.getFormName(), model);
157
158   }
159
160   /**
161    * add the model to the cache
162    */

163   public void addCache(Object JavaDoc key, String JavaDoc className, ModelIF model) {
164
165     if (key == null)
166       return;
167     modelCacheManager.setCache2(key, className, model);
168
169   }
170
171   /**
172    * get the model instance from the cache
173    */

174
175   public ModelIF getCache(ModelKey modelKey) {
176     if (modelKey.getModelClassName() != null)
177       return modelCacheManager.getCache2(modelKey.getDataKey(),
178                                  modelKey.getModelClassName().getName());
179     else
180       return modelCacheManager.getCache(modelKey.getDataKey(),
181                                 modelKey.getFormName());
182
183   }
184
185   /**
186    * get the model instance from the cache
187    */

188
189   public ModelIF getCache(Object JavaDoc key, String JavaDoc className) {
190     return modelCacheManager.getCache2(key, className);
191
192   }
193
194   /**
195    * remove the model instance from the cache
196    */

197
198   public void removeCache(Object JavaDoc dataKey) throws Exception JavaDoc {
199     modelCacheManager.removeCache(dataKey);
200   }
201   
202   /**
203    * clear all models in the cache.
204    */

205   public void clearCache(){
206     modelCacheManager.clearCache();
207   }
208
209   public boolean isNull(String JavaDoc s) {
210     boolean isNull = false;
211     if (s == null)
212       isNull = true;
213     else if (s.equals(""))
214       isNull = true;
215     else if (s.equals("null"))
216       isNull = true;
217     return isNull;
218   }
219
220
221
222 }
223
Popular Tags