KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > struts > StrutsCreator


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.struts;
17
18 import java.lang.reflect.Method JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.config.ModuleConfig;
25 import org.apache.struts.util.RequestUtils;
26 import org.directwebremoting.WebContext;
27 import org.directwebremoting.WebContextFactory;
28 import org.directwebremoting.create.AbstractCreator;
29 import org.directwebremoting.extend.Creator;
30 import org.directwebremoting.util.FakeHttpServletRequest;
31 import org.directwebremoting.util.LocalUtil;
32 import org.directwebremoting.util.Logger;
33 import org.directwebremoting.util.Messages;
34
35 /**
36  * StrutsCreator
37  * @author Ariel O. Falduto
38  * @author Joe Walker [joe at getahead dot ltd dot uk]
39  */

40 public class StrutsCreator extends AbstractCreator implements Creator
41 {
42     /**
43      *
44      */

45     public StrutsCreator()
46     {
47         try
48         {
49             moduleUtilsClass = LocalUtil.classForName("org.apache.struts.util.ModuleUtils");
50             getInstanceMethod = moduleUtilsClass.getMethod("getInstance", new Class JavaDoc[0]);
51             getModuleNameMethod = moduleUtilsClass.getMethod("getModuleName", new Class JavaDoc[] { String JavaDoc.class, ServletContext JavaDoc.class });
52             getModuleConfigMethod = moduleUtilsClass.getMethod("getModuleConfig", new Class JavaDoc[] { String JavaDoc.class, ServletContext JavaDoc.class });
53
54             log.debug("Using Struts 1.2 based ModuleUtils code");
55         }
56         catch (Exception JavaDoc ex)
57         {
58             moduleUtilsClass = null;
59             getInstanceMethod = null;
60             getModuleNameMethod = null;
61             getModuleConfigMethod = null;
62
63             log.debug("Failed to find Struts 1.2 ModuleUtils code. Falling back to 1.1 based code");
64         }
65     }
66
67     /**
68      * Struts formBean to be retrived
69      * @param formBean Struts bean form related.
70      */

71     public void setFormBean(String JavaDoc formBean)
72     {
73         this.formBean = formBean;
74     }
75
76     /* (non-Javadoc)
77      * @see org.directwebremoting.Creator#getType()
78      */

79     public Class JavaDoc getType()
80     {
81         synchronized (this)
82         {
83             if (moduleConfig == null)
84             {
85                 WebContext wc = WebContextFactory.get();
86
87                 if (getInstanceMethod != null)
88                 {
89                     try
90                     {
91                         // ModuleUtils utils = ModuleUtils.getInstance();
92
Object JavaDoc utils = getInstanceMethod.invoke(null, new Object JavaDoc[0]);
93
94                         // String moduleName = utils.getModuleName("/", wc.getServletContext());
95
String JavaDoc moduleName = (String JavaDoc) getModuleNameMethod.invoke(utils, new Object JavaDoc[] { "/", wc.getServletContext() });
96
97                         // moduleConfig = utils.getModuleConfig(moduleName, wc.getServletContext());
98
moduleConfig = (ModuleConfig) getModuleConfigMethod.invoke(utils, new Object JavaDoc[] { moduleName, wc.getServletContext() });
99                     }
100                     catch (Exception JavaDoc ex)
101                     {
102                         throw new IllegalArgumentException JavaDoc(ex.getMessage());
103                     }
104                 }
105                 else
106                 {
107                     HttpServletRequest JavaDoc request = wc.getHttpServletRequest();
108                     if (request == null)
109                     {
110                         log.warn("Using a FakeHttpServletRequest as part of setup");
111                         request = new FakeHttpServletRequest();
112                     }
113
114                     moduleConfig = RequestUtils.getModuleConfig(request, wc.getServletContext());
115                 }
116             }
117         }
118
119         try
120         {
121             return LocalUtil.classForName(moduleConfig.findFormBeanConfig(formBean).getType());
122         }
123         catch (ClassNotFoundException JavaDoc ex)
124         {
125             throw new IllegalArgumentException JavaDoc(Messages.getString("Creator.ClassNotFound", moduleConfig.findFormBeanConfig(formBean).getType()));
126         }
127     }
128
129     /* (non-Javadoc)
130      * @see org.directwebremoting.Creator#getInstance()
131      */

132     public Object JavaDoc getInstance() throws InstantiationException JavaDoc
133     {
134         // fills for the first time the moduleConfig
135
ActionForm formInstance = (ActionForm) WebContextFactory.get().getSession().getAttribute(formBean);
136         if (formInstance == null)
137         {
138             throw new InstantiationException JavaDoc(Messages.getString("Creator.IllegalAccess"));
139         }
140
141         return formInstance;
142     }
143
144     /**
145      * The FormBean that we lookup to call methods on
146      */

147     private String JavaDoc formBean;
148
149     /**
150      * moduleConfig allows us to do the lookup
151      */

152     private ModuleConfig moduleConfig;
153
154     /**
155      * Reflection access to 1.2 code for compatibility with 1.1
156      */

157     private Class JavaDoc moduleUtilsClass;
158
159     /**
160      * Reflection access to 1.2 code for compatibility with 1.1
161      */

162     private Method JavaDoc getInstanceMethod;
163
164     /**
165      * Reflection access to 1.2 code for compatibility with 1.1
166      */

167     private Method JavaDoc getModuleNameMethod;
168
169     /**
170      * Reflection access to 1.2 code for compatibility with 1.1
171      */

172     private Method JavaDoc getModuleConfigMethod;
173
174     /**
175      * The log stream
176      */

177     private static final Logger log = Logger.getLogger(StrutsCreator.class);
178 }
179
Popular Tags