KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > model > ModelManager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.guiframework.model;
25
26 import com.iplanet.jato.ModelTypeMap;
27 import com.iplanet.jato.RequestContext;
28 import com.iplanet.jato.model.DefaultModel;
29 import com.iplanet.jato.model.Model;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34
35 /**
36  * The purpose of this class is to extend the JATO model manager and add the
37  * ability to register a user-instantiated model in the Manager. This is
38  * necessary because Lockhart models do not provide a means of creating some
39  * of their models via the default constructor (which is a requirement of the
40  * JATO ModelManager).
41  */

42 public class ModelManager extends com.iplanet.jato.ModelManager {
43
44     /**
45      *
46      *
47      * @param requestContext
48      * The request context of the current request
49      *
50      * @param typeMap
51      * An application-specific map that associates <code>Model</code>
52      * interfaces with corresponding <code>Model</code> implementation
53      * classes.
54      */

55     public ModelManager(RequestContext requestContext, ModelTypeMap typeMap) {
56     super(requestContext, typeMap);
57     }
58
59
60     /**
61      * This method overrides the superclass method in order to check a local
62      * Map of instances first and to capture the created model instance if a
63      * local one is not found. The reason we have to do it this way is
64      * because JATO did not provide access to the their Map of instances.
65      */

66     public Model getModel(Class JavaDoc cls, String JavaDoc name, boolean lookInSession, boolean storeInSession) {
67         // Check the instance Map
68
Model model = (Model)getInstanceMap().get(name);
69     if (model != null) {
70         return model;
71     }
72
73     // Not cached, get new...
74
model = super.getModel(cls, name, lookInSession, storeInSession);
75
76     // Save for next time...
77
if (model != null) {
78         if (model instanceof DefaultModel) {
79             // Screws up model.clear() if set to true
80
((DefaultModel)model).setUseDefaultValues(false);
81         }
82         getInstanceMap().put(name, model);
83     }
84
85     // Return the model.
86
return model;
87     }
88
89
90     /**
91      * This is a new method, and the purpose of writing this class. This
92      * method allows Models created elsewhere to be registered here. This
93      * will allow ModelManager.getModel(...) to return us our model (yea!).
94      * This was needed because Lockhart does not allow their models to be
95      * created via the default constructor making them incompatible with
96      * JATO's ModelManager.
97      *
98      * @param name The instance name this instance of the model should be
99      * known by.
100      * @param model The model instance.
101      */

102     public void registerModel(String JavaDoc name, Model model) {
103         if (name == null) {
104         throw new IllegalArgumentException JavaDoc(
105         "You cannot register a Model with a (null) name!");
106     }
107     getInstanceMap().put(name, model);
108     }
109
110
111     /**
112      * While we're at it, might as well provide a contains method...
113      */

114     public boolean contains(String JavaDoc name) {
115     return getInstanceMap().containsKey(name);
116     }
117
118
119     /**
120      * This method returns the Map of instances.
121      */

122     protected Map getInstanceMap() {
123     return _instanceMap;
124     }
125
126
127     /**
128      * This method sets the Map of instances.
129      */

130     protected void setInstanceMap(Map map) {
131     _instanceMap = map;
132     }
133
134
135     private Map _instanceMap = new HashMap JavaDoc();
136 }
137
Popular Tags