KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > DefaultCreatorManager


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.directwebremoting.impl;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.directwebremoting.WebContext;
27 import org.directwebremoting.WebContextFactory;
28 import org.directwebremoting.extend.Creator;
29 import org.directwebremoting.extend.CreatorManager;
30 import org.directwebremoting.util.LocalUtil;
31 import org.directwebremoting.util.Logger;
32 import org.directwebremoting.util.Messages;
33
34 /**
35  * A class to manage the types of creators and the instansiated creators.
36  * @author Joe Walker [joe at getahead dot ltd dot uk]
37  */

38 public class DefaultCreatorManager implements CreatorManager
39 {
40     /**
41      * Set the debug status
42      * @param debug The new debug setting
43      */

44     public void setDebug(boolean debug)
45     {
46         this.debug = debug;
47     }
48
49     /* (non-Javadoc)
50      * @see org.directwebremoting.CreatorManager#isDebug()
51      */

52     public boolean isDebug()
53     {
54         return debug;
55     }
56
57     /* (non-Javadoc)
58      * @see org.directwebremoting.CreatorManager#addCreatorType(java.lang.String, java.lang.String)
59      */

60     public void addCreatorType(String JavaDoc typeName, String JavaDoc className)
61     {
62         if (!LocalUtil.isJavaIdentifier(typeName))
63         {
64             log.error("Illegal identifier: '" + typeName + "'");
65             return;
66         }
67
68         Class JavaDoc clazz = LocalUtil.classForName(typeName, className, Creator.class);
69         if (clazz != null)
70         {
71             log.debug("- adding creator type: " + typeName + " = " + clazz);
72             creatorTypes.put(typeName, clazz);
73         }
74     }
75
76     /* (non-Javadoc)
77      * @see org.directwebremoting.CreatorManager#addCreator(java.lang.String, java.lang.String, java.util.Map)
78      */

79     public void addCreator(String JavaDoc scriptName, String JavaDoc typeName, Map JavaDoc params) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, IllegalArgumentException JavaDoc
80     {
81         if (!LocalUtil.isJavaIdentifier(scriptName))
82         {
83             log.error("Illegal identifier: '" + scriptName + "'");
84             return;
85         }
86
87         Class JavaDoc clazz = (Class JavaDoc) creatorTypes.get(typeName);
88         if (clazz == null)
89         {
90             log.error("Missing creator: " + typeName + " (while initializing creator for: " + scriptName + ".js)");
91             return;
92         }
93
94         Creator creator = (Creator) clazz.newInstance();
95
96         LocalUtil.setParams(creator, params, ignore);
97         creator.setProperties(params);
98
99         // add the creator for the script name
100
addCreator(scriptName, creator);
101     }
102
103     /* (non-Javadoc)
104      * @see org.directwebremoting.CreatorManager#addCreator(java.lang.String, org.directwebremoting.Creator)
105      */

106     public void addCreator(String JavaDoc scriptName, Creator creator) throws IllegalArgumentException JavaDoc
107     {
108         // Check that we don't have this one already
109
Creator other = (Creator) creators.get(scriptName);
110         if (other != null)
111         {
112             throw new IllegalArgumentException JavaDoc(Messages.getString("DefaultCreatorManager.DuplicateName", scriptName, other.getType().getName(), creator));
113         }
114
115         // Check that it can at least tell us what type of thing we will be getting
116
try
117         {
118             Class JavaDoc test = creator.getType();
119             if (test == null)
120             {
121                 log.error("Creator: '" + creator + "' for " + scriptName + ".js is returning null for type queries.");
122             }
123             else
124             {
125                 log.debug("- adding creator: " + LocalUtil.getShortClassName(creator.getClass()) + " for " + scriptName);
126                 creators.put(scriptName, creator);
127             }
128         }
129         catch (NoClassDefFoundError JavaDoc ex)
130         {
131             log.error("Missing class for creator '" + creator + "'. Cause: " + ex.getMessage());
132         }
133         catch (Exception JavaDoc ex)
134         {
135             log.error("Error loading class for creator '" + creator + "'.", ex);
136         }
137
138         // If this is application scope then it might make sense to create one
139
// now rather than wait for first use. Otherwise this job is done by
140
// DefaultRemoter.execute(Call call)
141
if (initApplicationScopeCreatorsAtStartup && creator.getScope().equals(Creator.APPLICATION))
142         {
143             try
144             {
145                 WebContext webcx = WebContextFactory.get();
146                 Object JavaDoc object = creator.getInstance();
147                 webcx.getServletContext().setAttribute(creator.getJavascript(), object);
148
149                 log.debug("Created new " + creator.getJavascript() + ", stored in application.");
150             }
151             catch (InstantiationException JavaDoc ex)
152             {
153                 log.warn("Failed to create " + creator.getJavascript(), ex);
154                 log.debug("Maybe it will succeed when the application is in flight. If so you should probably set initApplicationScopeCreatorsAtStartup=false in web.xml");
155             }
156         }
157     }
158
159     /* (non-Javadoc)
160      * @see org.directwebremoting.CreatorManager#getCreatorNames()
161      */

162     public Collection JavaDoc getCreatorNames() throws SecurityException JavaDoc
163     {
164         if (!debug)
165         {
166             throw new SecurityException JavaDoc();
167         }
168
169         return Collections.unmodifiableSet(creators.keySet());
170     }
171
172     /* (non-Javadoc)
173      * @see org.directwebremoting.CreatorManager#getCreator(java.lang.String)
174      */

175     public Creator getCreator(String JavaDoc scriptName) throws SecurityException JavaDoc
176     {
177         Creator creator = (Creator) creators.get(scriptName);
178         if (creator == null)
179         {
180             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("Names of known classes are: ");
181             for (Iterator JavaDoc it = creators.keySet().iterator(); it.hasNext();)
182             {
183                 String JavaDoc key = (String JavaDoc) it.next();
184                 buffer.append(key);
185                 buffer.append(' ');
186             }
187
188             log.warn(buffer.toString());
189             throw new SecurityException JavaDoc(Messages.getString("DefaultCreatorManager.MissingName", scriptName));
190         }
191
192         return creator;
193     }
194
195     /* (non-Javadoc)
196      * @see org.directwebremoting.CreatorManager#setCreators(java.util.Map)
197      */

198     public void setCreators(Map JavaDoc creators)
199     {
200         this.creators = creators;
201     }
202
203     /**
204      * The log stream
205      */

206     private static final Logger log = Logger.getLogger(DefaultCreatorManager.class);
207
208     /**
209      * The list of the available creators
210      */

211     private Map JavaDoc creatorTypes = new HashMap JavaDoc();
212
213     /**
214      * The list of the configured creators
215      */

216     private Map JavaDoc creators = new HashMap JavaDoc();
217
218     /**
219      * Are we in debug mode?
220      */

221     private boolean debug = false;
222
223     /**
224      * Do we do full-create on startup?
225      */

226     private boolean initApplicationScopeCreatorsAtStartup = false;
227
228     /**
229      * The properties that we don't warn about if they don't exist.
230      * @see DefaultCreatorManager#addCreator(String, String, Map)
231      */

232     private static List JavaDoc ignore = Arrays.asList(new String JavaDoc[] { "creator", "class" });
233
234     /**
235      * Do we do full-create on startup?
236      * @return true if we are doing full-create
237      */

238     public boolean isInitApplicationScopeCreatorsAtStartup()
239     {
240         return initApplicationScopeCreatorsAtStartup;
241     }
242
243     /**
244      * Do we do full-create on startup?
245      * @param initApplicationScopeCreatorsAtStartup true for full create
246      */

247     public void setInitApplicationScopeCreatorsAtStartup(boolean initApplicationScopeCreatorsAtStartup)
248     {
249         this.initApplicationScopeCreatorsAtStartup = initApplicationScopeCreatorsAtStartup;
250     }
251 }
252
Popular Tags