1 16 package org.directwebremoting.struts; 17 18 import java.lang.reflect.Method ; 19 20 import javax.servlet.ServletContext ; 21 import javax.servlet.http.HttpServletRequest ; 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 40 public class StrutsCreator extends AbstractCreator implements Creator 41 { 42 45 public StrutsCreator() 46 { 47 try 48 { 49 moduleUtilsClass = LocalUtil.classForName("org.apache.struts.util.ModuleUtils"); 50 getInstanceMethod = moduleUtilsClass.getMethod("getInstance", new Class [0]); 51 getModuleNameMethod = moduleUtilsClass.getMethod("getModuleName", new Class [] { String .class, ServletContext .class }); 52 getModuleConfigMethod = moduleUtilsClass.getMethod("getModuleConfig", new Class [] { String .class, ServletContext .class }); 53 54 log.debug("Using Struts 1.2 based ModuleUtils code"); 55 } 56 catch (Exception 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 71 public void setFormBean(String formBean) 72 { 73 this.formBean = formBean; 74 } 75 76 79 public Class 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 Object utils = getInstanceMethod.invoke(null, new Object [0]); 93 94 String moduleName = (String ) getModuleNameMethod.invoke(utils, new Object [] { "/", wc.getServletContext() }); 96 97 moduleConfig = (ModuleConfig) getModuleConfigMethod.invoke(utils, new Object [] { moduleName, wc.getServletContext() }); 99 } 100 catch (Exception ex) 101 { 102 throw new IllegalArgumentException (ex.getMessage()); 103 } 104 } 105 else 106 { 107 HttpServletRequest 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 ex) 124 { 125 throw new IllegalArgumentException (Messages.getString("Creator.ClassNotFound", moduleConfig.findFormBeanConfig(formBean).getType())); 126 } 127 } 128 129 132 public Object getInstance() throws InstantiationException  133 { 134 ActionForm formInstance = (ActionForm) WebContextFactory.get().getSession().getAttribute(formBean); 136 if (formInstance == null) 137 { 138 throw new InstantiationException (Messages.getString("Creator.IllegalAccess")); 139 } 140 141 return formInstance; 142 } 143 144 147 private String formBean; 148 149 152 private ModuleConfig moduleConfig; 153 154 157 private Class moduleUtilsClass; 158 159 162 private Method getInstanceMethod; 163 164 167 private Method getModuleNameMethod; 168 169 172 private Method getModuleConfigMethod; 173 174 177 private static final Logger log = Logger.getLogger(StrutsCreator.class); 178 } 179
| Popular Tags
|