001: /*
002: * Copyright 2005-2006 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.directwebremoting.spring;
018:
019: import java.util.List;
020: import java.util.Map;
021: import java.util.Properties;
022: import java.util.Map.Entry;
023:
024: import org.directwebremoting.AjaxFilter;
025: import org.directwebremoting.Container;
026: import org.directwebremoting.extend.AccessControl;
027: import org.directwebremoting.extend.AjaxFilterManager;
028: import org.directwebremoting.extend.Configurator;
029: import org.directwebremoting.extend.ConverterManager;
030: import org.directwebremoting.extend.Creator;
031: import org.directwebremoting.extend.CreatorManager;
032: import org.directwebremoting.impl.SignatureParser;
033: import org.directwebremoting.util.LocalUtil;
034: import org.directwebremoting.util.Messages;
035: import org.springframework.util.StringUtils;
036:
037: /**
038: * @author Joe Walker [joe at getahead dot ltd dot uk]
039: */
040: public class SpringConfigurator implements Configurator {
041: /* (non-Javadoc)
042: * @see org.directwebremoting.Configurator#configure(org.directwebremoting.Container)
043: */
044: public void configure(Container container) {
045: AccessControl accessControl = container
046: .getBean(AccessControl.class);
047: AjaxFilterManager ajaxFilterManager = container
048: .getBean(AjaxFilterManager.class);
049: ConverterManager converterManager = container
050: .getBean(ConverterManager.class);
051: CreatorManager creatorManager = container
052: .getBean(CreatorManager.class);
053:
054: // Configure the creator types
055: if (creatorTypes != null) {
056: for (Entry<String, String> entry : creatorTypes.entrySet()) {
057: String typeName = entry.getKey();
058: String className = entry.getValue();
059:
060: creatorManager.addCreatorType(typeName, className);
061: }
062: }
063:
064: // Configure the converter types
065: if (converterTypes != null) {
066: for (Entry<String, String> entry : converterTypes
067: .entrySet()) {
068: String typeName = entry.getKey();
069: String className = entry.getValue();
070:
071: converterManager.addConverterType(typeName, className);
072: }
073: }
074:
075: // Configure the creators
076: if (creators != null) {
077: try {
078: for (Entry<String, CreatorConfig> entry : creators
079: .entrySet()) {
080: String scriptName = entry.getKey();
081: CreatorConfig creatorConfig = entry.getValue();
082:
083: if (creatorConfig.getCreator() != null) {
084: Creator creator = creatorConfig.getCreator();
085: creatorManager.addCreator(scriptName, creator);
086: } else {
087: String creatorName = creatorConfig
088: .getCreatorType();
089: Map<String, String> params = creatorConfig
090: .getParams();
091: creatorManager.addCreator(scriptName,
092: creatorName, params);
093: }
094:
095: for (String exclude : creatorConfig.getExcludes()) {
096: accessControl.addExcludeRule(scriptName,
097: exclude);
098: }
099:
100: for (String include : creatorConfig.getIncludes()) {
101: accessControl.addIncludeRule(scriptName,
102: include);
103: }
104:
105: Properties auth = creatorConfig.getAuth();
106: for (Entry<Object, Object> aentry : auth.entrySet()) {
107: String methodName = (String) aentry.getKey();
108: String role = (String) aentry.getValue();
109: accessControl.addRoleRestriction(scriptName,
110: methodName, role);
111: }
112:
113: List<?> filters = creatorConfig.getFilters();
114: for (Object obj : filters) {
115: if (obj instanceof String) {
116: String filterName = (String) obj;
117:
118: AjaxFilter filter = LocalUtil
119: .classNewInstance(filterName,
120: filterName,
121: AjaxFilter.class);
122: if (filter != null) {
123: ajaxFilterManager.addAjaxFilter(filter,
124: scriptName);
125: }
126: } else if (obj instanceof AjaxFilter) {
127: AjaxFilter filter = (AjaxFilter) obj;
128: ajaxFilterManager.addAjaxFilter(filter,
129: scriptName);
130: } else {
131: throw new IllegalArgumentException(
132: Messages
133: .getString(
134: "SpringConfigurator.InvalidFilter",
135: scriptName, obj));
136: }
137: }
138: }
139: } catch (Exception ex) {
140: throw new IllegalArgumentException(ex);
141: }
142: }
143:
144: // Configure the converters
145: if (converters != null) {
146: try {
147: for (Entry<String, ConverterConfig> entry : converters
148: .entrySet()) {
149: String match = entry.getKey();
150: ConverterConfig converterConfig = entry.getValue();
151:
152: Map<String, String> params = converterConfig
153: .getParams();
154: if (!converterConfig.getIncludes().isEmpty()) {
155: params
156: .put(
157: "include",
158: StringUtils
159: .collectionToCommaDelimitedString(converterConfig
160: .getIncludes()));
161: }
162:
163: if (!converterConfig.getExcludes().isEmpty()) {
164: params
165: .put(
166: "exclude",
167: StringUtils
168: .collectionToCommaDelimitedString(converterConfig
169: .getExcludes()));
170: }
171:
172: // params.put("force", Boolean.valueOf(converterConfig.isForce()));
173: if (StringUtils.hasText(converterConfig
174: .getJavascriptClassName())) {
175: params.put("javascript", converterConfig
176: .getJavascriptClassName());
177: }
178: converterManager.addConverter(match,
179: converterConfig.getType(), params);
180: }
181: } catch (Exception ex) {
182: throw new IllegalArgumentException(
183: Messages
184: .getString("SpringConfigurator.ConfigureConverterError"));
185: }
186: }
187:
188: // Configure the signatures
189: if (StringUtils.hasText(signatures)) {
190: SignatureParser sigp = new SignatureParser(
191: converterManager, creatorManager);
192: sigp.parse(signatures);
193: }
194: }
195:
196: /**
197: * Setter for the map of Creator types
198: * @param creatorTypes The new creator types map
199: */
200: public void setCreatorTypes(Map<String, String> creatorTypes) {
201: this .creatorTypes = creatorTypes;
202: }
203:
204: /**
205: * Setter for the map of Converter types
206: * @param converterTypes The new creator types map
207: */
208: public void setConverterTypes(Map<String, String> converterTypes) {
209: this .converterTypes = converterTypes;
210: }
211:
212: /**
213: * Setter for the map of real Creators
214: * @param creators The new creator map
215: */
216: public void setCreators(Map<String, CreatorConfig> creators) {
217: this .creators = creators;
218: }
219:
220: /**
221: * Setter for the map of real Converter
222: * @param converters The new creator map
223: */
224: public void setConverters(Map<String, ConverterConfig> converters) {
225: this .converters = converters;
226: }
227:
228: /**
229: * @param signatures the signatures to set
230: */
231: public void setSignatures(String signatures) {
232: this .signatures = signatures;
233: }
234:
235: /**
236: * @return the signatures
237: */
238: public String getSignatures() {
239: return signatures;
240: }
241:
242: /**
243: * The map of Converter types
244: */
245: private Map<String, String> creatorTypes;
246:
247: /**
248: * The map of Converter types
249: */
250: private Map<String, String> converterTypes;
251:
252: /**
253: * The map of real Creators
254: */
255: private Map<String, CreatorConfig> creators;
256:
257: /**
258: * The map of real Converter
259: */
260: private Map<String, ConverterConfig> converters;
261:
262: /**
263: * The string of Signatures
264: */
265: private String signatures;
266: }
|