KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > dwr > SpringConfigurator


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.dwr;
25
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.directwebremoting.Container;
33 import org.directwebremoting.extend.AccessControl;
34 import org.directwebremoting.extend.Configurator;
35 import org.directwebremoting.extend.ConverterManager;
36 import org.directwebremoting.extend.Creator;
37 import org.directwebremoting.extend.CreatorManager;
38 import org.directwebremoting.impl.SignatureParser;
39 import org.directwebremoting.spring.BeanCreator;
40 import org.springframework.beans.factory.BeanCreationException;
41 import org.springframework.util.StringUtils;
42
43 public class SpringConfigurator implements Configurator {
44
45     private Map JavaDoc serviceBeans;
46
47     private Class JavaDoc[] serviceInterfaces;
48
49     private Properties JavaDoc converterTypes;
50
51     private String JavaDoc signatures;
52
53     /**
54      * Sets a map of beans to be exported keyed by their script name.
55      * @param serviceBeans Map of beans to export
56      */

57     public void setServiceBeans(Map JavaDoc serviceBeans) {
58         this.serviceBeans = serviceBeans;
59     }
60
61     /**
62      * Sets the interfaces to be exported. This is a convenient way
63      * to control which methods should be exposed to the client. This is
64      * especially useful when your service beans are AOP proxies.
65      * If no interfaces are configured only the default access rules apply.
66      * @param serviceInterfaces Interfaces to export
67      */

68     public void setServiceInterfaces(Class JavaDoc[] serviceInterfaces) {
69         this.serviceInterfaces = serviceInterfaces;
70     }
71
72     public void setConverterTypes(Properties JavaDoc converterTypes) {
73         this.converterTypes = converterTypes;
74     }
75
76     public void setSignatures(String JavaDoc signatures) {
77         this.signatures = signatures;
78     }
79
80     public void configure(Container container) {
81         ConverterManager converterManager = (ConverterManager)
82                 container.getBean(ConverterManager.class.getName());
83
84         if (converterTypes != null) {
85             Iterator JavaDoc it = converterTypes.entrySet().iterator();
86             while (it.hasNext()) {
87                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
88                 String JavaDoc match = (String JavaDoc) entry.getKey();
89                 String JavaDoc type = (String JavaDoc) entry.getValue();
90                 try {
91                     converterManager.addConverter(match, type, Collections.EMPTY_MAP);
92                 }
93                 catch (IllegalArgumentException JavaDoc e) {
94                     throw new BeanCreationException("Error adding converter", e);
95                 }
96                 catch (InstantiationException JavaDoc e) {
97                     throw new BeanCreationException("Error adding converter", e);
98                 }
99                 catch (IllegalAccessException JavaDoc e) {
100                     throw new BeanCreationException("Error adding converter", e);
101                 }
102             }
103         }
104
105         CreatorManager creatorManager = (CreatorManager)
106                 container.getBean(CreatorManager.class.getName());
107
108
109         if (serviceBeans != null) {
110             Iterator JavaDoc it = serviceBeans.entrySet().iterator();
111             while (it.hasNext()) {
112                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
113                 String JavaDoc scriptName = (String JavaDoc) entry.getKey();
114                 BeanCreator creator = new BeanCreator();
115                 creator.setScope(Creator.APPLICATION);
116                 creator.setBean(entry.getValue());
117                 creator.afterPropertiesSet();
118                 creatorManager.addCreator(scriptName, creator);
119             }
120         }
121
122         if (serviceInterfaces != null) {
123             AccessControl accessControl = (AccessControl) container.getBean(
124                 AccessControl.class.getName());
125
126             for (int i = 0; i < serviceInterfaces.length; i++) {
127                 includeMethods(accessControl, serviceInterfaces[i]);
128             }
129         }
130
131         if (StringUtils.hasText(signatures)) {
132             SignatureParser sigp = new SignatureParser(converterManager, creatorManager);
133             sigp.parse(signatures);
134         }
135     }
136
137     private void includeMethods(AccessControl accessControl,
138             Class JavaDoc serviceInterface) {
139
140         Iterator JavaDoc it = serviceBeans.entrySet().iterator();
141         while (it.hasNext()) {
142             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
143             if (serviceInterface.isInstance(entry.getValue())) {
144                 String JavaDoc scriptName = (String JavaDoc) entry.getKey();
145                 includeMethods(accessControl, serviceInterface, scriptName);
146             }
147         }
148     }
149
150     private void includeMethods(AccessControl accessControl,
151             Class JavaDoc serviceInterface, String JavaDoc scriptName) {
152
153         Method JavaDoc[] methods = serviceInterface.getDeclaredMethods();
154         for (int i = 0; i < methods.length; i++) {
155             accessControl.addIncludeRule(scriptName, methods[i].getName());
156         }
157     }
158
159 }
160
Popular Tags