KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > spring > SpringConfigurator


1 /*
2  * Copyright 2005-2006 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
17 package org.directwebremoting.spring;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import org.directwebremoting.AjaxFilter;
25 import org.directwebremoting.Container;
26 import org.directwebremoting.extend.AccessControl;
27 import org.directwebremoting.extend.AjaxFilterManager;
28 import org.directwebremoting.extend.Configurator;
29 import org.directwebremoting.extend.ConverterManager;
30 import org.directwebremoting.extend.Creator;
31 import org.directwebremoting.extend.CreatorManager;
32 import org.directwebremoting.impl.SignatureParser;
33 import org.directwebremoting.util.LocalUtil;
34 import org.directwebremoting.util.Messages;
35 import org.springframework.util.StringUtils;
36
37 /**
38  * @author Joe Walker [joe at getahead dot ltd dot uk]
39  */

40 public class SpringConfigurator implements Configurator
41 {
42     /* (non-Javadoc)
43      * @see org.directwebremoting.Configurator#configure(org.directwebremoting.Container)
44      */

45     public void configure(Container container)
46     {
47         AccessControl accessControl = (AccessControl) container.getBean(AccessControl.class.getName());
48         AjaxFilterManager ajaxFilterManager = (AjaxFilterManager) container.getBean(AjaxFilterManager.class.getName());
49         ConverterManager converterManager = (ConverterManager) container.getBean(ConverterManager.class.getName());
50         CreatorManager creatorManager = (CreatorManager) container.getBean(CreatorManager.class.getName());
51
52         // Configure the creator types
53
if (creatorTypes != null)
54         {
55             for (Iterator JavaDoc it = creatorTypes.entrySet().iterator(); it.hasNext();)
56             {
57                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
58                 String JavaDoc typeName = (String JavaDoc) entry.getKey();
59                 String JavaDoc className = (String JavaDoc) entry.getValue();
60
61                 creatorManager.addCreatorType(typeName, className);
62             }
63         }
64
65         // Configure the converter types
66
if (converterTypes != null)
67         {
68             for (Iterator JavaDoc it = converterTypes.entrySet().iterator(); it.hasNext();)
69             {
70                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
71                 String JavaDoc typeName = (String JavaDoc) entry.getKey();
72                 String JavaDoc className = (String JavaDoc) entry.getValue();
73
74                 converterManager.addConverterType(typeName, className);
75             }
76         }
77
78         // Configure the creators
79
if (creators != null)
80         {
81             try
82             {
83                 for (Iterator JavaDoc it = creators.entrySet().iterator(); it.hasNext();)
84                 {
85                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
86                     String JavaDoc scriptName = (String JavaDoc) entry.getKey();
87                     CreatorConfig creatorConfig = (CreatorConfig) entry.getValue();
88
89                     if (creatorConfig.getCreator() != null)
90                     {
91                         Creator creator = creatorConfig.getCreator();
92                         creatorManager.addCreator(scriptName, creator);
93                     }
94                     else
95                     {
96                         String JavaDoc creatorName = creatorConfig.getCreatorType();
97                         Map JavaDoc params = creatorConfig.getParams();
98                         creatorManager.addCreator(scriptName, creatorName, params);
99                     }
100
101                     List JavaDoc excludes = creatorConfig.getExcludes();
102                     for (Iterator JavaDoc eit = excludes.iterator(); eit.hasNext();)
103                     {
104                         String JavaDoc exclude = (String JavaDoc) eit.next();
105                         accessControl.addExcludeRule(scriptName, exclude);
106                     }
107
108                     List JavaDoc includes = creatorConfig.getIncludes();
109                     for (Iterator JavaDoc iit = includes.iterator(); iit.hasNext();)
110                     {
111                         String JavaDoc include = (String JavaDoc) iit.next();
112                         accessControl.addIncludeRule(scriptName, include);
113                     }
114
115                     Properties JavaDoc auth = creatorConfig.getAuth();
116                     for (Iterator JavaDoc ait = auth.entrySet().iterator(); ait.hasNext();)
117                     {
118                         Map.Entry JavaDoc aentry = (Map.Entry JavaDoc) ait.next();
119                         String JavaDoc methodName = (String JavaDoc) aentry.getKey();
120                         String JavaDoc role = (String JavaDoc) aentry.getValue();
121                         accessControl.addRoleRestriction(scriptName, methodName, role);
122                     }
123
124                     List JavaDoc filters = creatorConfig.getFilters();
125                     for (Iterator JavaDoc fit = filters.iterator(); fit.hasNext();)
126                     {
127                         Object JavaDoc obj = fit.next();
128                         if (obj instanceof String JavaDoc)
129                         {
130                             String JavaDoc filterName = (String JavaDoc) obj;
131
132                             AjaxFilter filter = (AjaxFilter) LocalUtil.classNewInstance(filterName, filterName, AjaxFilter.class);
133                             if (filter != null)
134                             {
135                                 ajaxFilterManager.addAjaxFilter(filter, scriptName);
136                             }
137                         }
138                         else if (obj instanceof AjaxFilter)
139                         {
140                             AjaxFilter filter = (AjaxFilter) obj;
141                             ajaxFilterManager.addAjaxFilter(filter, scriptName);
142                         }
143                         else
144                         {
145                             throw new IllegalArgumentException JavaDoc(Messages.getString("SpringConfigurator.InvalidFilter", scriptName, obj));
146                         }
147                     }
148                 }
149             }
150             catch (Exception JavaDoc ex)
151             {
152                 ex.printStackTrace();
153                 throw new IllegalArgumentException JavaDoc(ex.toString());
154             }
155         }
156
157         // Configure the converters
158
if (converters != null)
159         {
160             try
161             {
162                 for (Iterator JavaDoc it = converters.entrySet().iterator(); it.hasNext();)
163                 {
164                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
165                     String JavaDoc match = (String JavaDoc) entry.getKey();
166                     ConverterConfig converterConfig = (ConverterConfig) entry.getValue();
167                     Map JavaDoc params = converterConfig.getParams();
168                     if (converterConfig.getIncludes().size() > 0)
169                     {
170                         params.put("include", StringUtils.collectionToCommaDelimitedString(
171                                 converterConfig.getIncludes()));
172                     }
173
174                     if (converterConfig.getExcludes().size() > 0)
175                     {
176                         params.put("exclude", StringUtils.collectionToCommaDelimitedString(
177                                 converterConfig.getExcludes()));
178                     }
179
180                     // params.put("force", Boolean.valueOf(converterConfig.isForce()));
181
if (StringUtils.hasText(converterConfig.getJavascriptClassName())) {
182                         params.put("javascript", converterConfig.getJavascriptClassName());
183                     }
184                     converterManager.addConverter(match, converterConfig.getType(), params);
185                 }
186             }
187             catch (Exception JavaDoc ex)
188             {
189                 throw new IllegalArgumentException JavaDoc(Messages.getString("SpringConfigurator.ConfigureConverterError"));
190             }
191         }
192
193         // Configure the signatures
194
if (StringUtils.hasText(signatures)) {
195             SignatureParser sigp = new SignatureParser(converterManager, creatorManager);
196             sigp.parse(signatures);
197         }
198     }
199
200     /**
201      * Setter for the map of Creator types
202      * @param creatorTypes The new creator types map
203      */

204     public void setCreatorTypes(Map JavaDoc creatorTypes)
205     {
206         this.creatorTypes = creatorTypes;
207     }
208
209     /**
210      * Setter for the map of Converter types
211      * @param converterTypes The new creator types map
212      */

213     public void setConverterTypes(Map JavaDoc converterTypes)
214     {
215         this.converterTypes = converterTypes;
216     }
217
218     /**
219      * Setter for the map of real Creators
220      * @param creators The new creator map
221      */

222     public void setCreators(Map JavaDoc creators)
223     {
224         this.creators = creators;
225     }
226
227     /**
228      * Setter for the map of real Converter
229      * @param converters The new creator map
230      */

231     public void setConverters(Map JavaDoc converters)
232     {
233         this.converters = converters;
234     }
235
236     /**
237      * @param signatures the signatures to set
238      */

239     public void setSignatures(String JavaDoc signatures)
240     {
241         this.signatures = signatures;
242     }
243
244     /**
245      * @return the signatures
246      */

247     public String JavaDoc getSignatures()
248     {
249         return signatures;
250     }
251
252     /**
253      * The map of Converter types
254      */

255     private Map JavaDoc creatorTypes;
256
257     /**
258      * The map of Converter types
259      */

260     private Map JavaDoc converterTypes;
261
262     /**
263      * The map of real Creators
264      */

265     private Map JavaDoc creators;
266
267     /**
268      * The map of real Converter
269      */

270     private Map JavaDoc converters;
271
272     /**
273      * The string of Signatures
274      */

275     private String JavaDoc signatures;
276 }
Popular Tags