001: /*
002: * Copyright 2005 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: package org.directwebremoting.struts;
017:
018: import java.lang.reflect.Method;
019:
020: import javax.servlet.ServletContext;
021: import javax.servlet.http.HttpServletRequest;
022:
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.commons.logging.Log;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.config.ModuleConfig;
027: import org.apache.struts.util.RequestUtils;
028: import org.directwebremoting.WebContext;
029: import org.directwebremoting.WebContextFactory;
030: import org.directwebremoting.create.AbstractCreator;
031: import org.directwebremoting.extend.Creator;
032: import org.directwebremoting.util.FakeHttpServletRequest;
033: import org.directwebremoting.util.LocalUtil;
034: import org.directwebremoting.util.Messages;
035:
036: /**
037: * StrutsCreator
038: * @author Ariel O. Falduto
039: * @author Joe Walker [joe at getahead dot ltd dot uk]
040: */
041: public class StrutsCreator extends AbstractCreator implements Creator {
042: /**
043: *
044: */
045: public StrutsCreator() {
046: try {
047: Class<?> moduleUtilsClass = LocalUtil
048: .classForName("org.apache.struts.util.ModuleUtils");
049: getInstanceMethod = moduleUtilsClass
050: .getMethod("getInstance");
051: getModuleNameMethod = moduleUtilsClass
052: .getMethod("getModuleName", String.class,
053: ServletContext.class);
054: getModuleConfigMethod = moduleUtilsClass.getMethod(
055: "getModuleConfig", String.class,
056: ServletContext.class);
057:
058: log.debug("Using Struts 1.2 based ModuleUtils code");
059: } catch (Exception ex) {
060: getInstanceMethod = null;
061: getModuleNameMethod = null;
062: getModuleConfigMethod = null;
063:
064: log
065: .debug("Failed to find Struts 1.2 ModuleUtils code. Falling back to 1.1 based code");
066: }
067: }
068:
069: /**
070: * Struts formBean to be retrived
071: * @param formBean Struts bean form related.
072: */
073: public void setFormBean(String formBean) {
074: this .formBean = formBean;
075: }
076:
077: /* (non-Javadoc)
078: * @see org.directwebremoting.Creator#getType()
079: */
080: public Class<?> getType() {
081: synchronized (this ) {
082: if (moduleConfig == null) {
083: WebContext wc = WebContextFactory.get();
084:
085: if (getInstanceMethod != null) {
086: try {
087: // ModuleUtils utils = ModuleUtils.getInstance();
088: Object utils = getInstanceMethod.invoke(null);
089:
090: // String moduleName = utils.getModuleName("/", wc.getServletContext());
091: String moduleName = (String) getModuleNameMethod
092: .invoke(utils, "/", wc
093: .getServletContext());
094:
095: // moduleConfig = utils.getModuleConfig(moduleName, wc.getServletContext());
096: moduleConfig = (ModuleConfig) getModuleConfigMethod
097: .invoke(utils, moduleName, wc
098: .getServletContext());
099: } catch (Exception ex) {
100: throw new IllegalArgumentException(ex
101: .getMessage());
102: }
103: } else {
104: HttpServletRequest request = wc
105: .getHttpServletRequest();
106: if (request == null) {
107: log
108: .warn("Using a FakeHttpServletRequest as part of setup");
109: request = new FakeHttpServletRequest();
110: }
111:
112: moduleConfig = RequestUtils.getModuleConfig(
113: request, wc.getServletContext());
114: }
115: }
116: }
117:
118: try {
119: return LocalUtil.classForName(moduleConfig
120: .findFormBeanConfig(formBean).getType());
121: } catch (ClassNotFoundException ex) {
122: throw new IllegalArgumentException(Messages.getString(
123: "Creator.ClassNotFound", moduleConfig
124: .findFormBeanConfig(formBean).getType()));
125: }
126: }
127:
128: /* (non-Javadoc)
129: * @see org.directwebremoting.Creator#getInstance()
130: */
131: public Object getInstance() throws InstantiationException {
132: // fills for the first time the moduleConfig
133: ActionForm formInstance = (ActionForm) WebContextFactory.get()
134: .getSession().getAttribute(formBean);
135: if (formInstance == null) {
136: throw new InstantiationException(Messages
137: .getString("Creator.IllegalAccess"));
138: }
139:
140: return formInstance;
141: }
142:
143: /**
144: * The FormBean that we lookup to call methods on
145: */
146: private String formBean = null;
147:
148: /**
149: * moduleConfig allows us to do the lookup
150: */
151: private ModuleConfig moduleConfig = null;
152:
153: /**
154: * Reflection access to 1.2 code for compatibility with 1.1
155: */
156: private Method getInstanceMethod;
157:
158: /**
159: * Reflection access to 1.2 code for compatibility with 1.1
160: */
161: private Method getModuleNameMethod;
162:
163: /**
164: * Reflection access to 1.2 code for compatibility with 1.1
165: */
166: private Method getModuleConfigMethod;
167:
168: /**
169: * The log stream
170: */
171: private static final Log log = LogFactory
172: .getLog(StrutsCreator.class);
173: }
|