1 16 package org.directwebremoting.create; 17 18 import java.io.File ; 19 import java.io.RandomAccessFile ; 20 21 import javax.servlet.ServletContext ; 22 23 import org.apache.bsf.BSFException; 24 import org.apache.bsf.BSFManager; 25 import org.directwebremoting.WebContext; 26 import org.directwebremoting.WebContextFactory; 27 import org.directwebremoting.extend.Creator; 28 import org.directwebremoting.util.LocalUtil; 29 import org.directwebremoting.util.Logger; 30 import org.directwebremoting.util.Messages; 31 32 37 public class ScriptedCreator extends AbstractCreator implements Creator 38 { 39 42 public ScriptedCreator() 43 { 44 setCacheable(false); 49 } 50 51 55 public String getLanguage() 56 { 57 return language; 58 } 59 60 63 public void setLanguage(String language) 64 { 65 this.language = language; 72 } 73 74 78 public boolean isReloadable() 79 { 80 return reloadable; 81 } 82 83 90 public void setReloadable(boolean reloadable) 91 { 92 this.reloadable = reloadable; 93 94 if (reloadable) 95 { 96 setCacheable(false); 97 } 98 } 99 100 105 public boolean isUseDynamicClasses() 106 { 107 return useDynamicClasses; 108 } 109 110 115 public void setUseDynamicClasses(boolean useDynamicClasses) 116 { 117 this.useDynamicClasses = useDynamicClasses; 118 } 119 120 123 public String getScriptPath() 124 { 125 return scriptPath; 126 } 127 128 131 public void setScriptPath(String scriptPath) 132 { 133 if (scriptSrc != null) 134 { 135 throw new IllegalArgumentException (Messages.getString("ScriptCreator.MultipleScript")); 136 } 137 138 this.scriptPath = scriptPath; 139 } 140 141 144 private boolean scriptUpdated() 145 { 146 if (null == scriptPath) 147 { 148 return false; 149 } 150 151 ServletContext sc = WebContextFactory.get().getServletContext(); 152 File scriptFile = new File (sc.getRealPath(scriptPath)); 153 if (scriptModified < scriptFile.lastModified()) 154 { 155 log.debug("Script has been updated."); 156 clazz = null; return true; 158 } 159 160 return false; 161 } 162 163 167 public String getScript() throws InstantiationException 168 { 169 if (scriptSrc != null) 170 { 171 return scriptSrc; 172 } 173 174 if (scriptPath == null) 175 { 176 throw new InstantiationException (Messages.getString("ScriptedCreator.MissingScript")); 177 } 178 179 if (cachedScript != null && (!reloadable || !scriptUpdated())) 180 { 181 return cachedScript; 182 } 183 184 log.debug("Loading Script from Path: " + scriptPath); 186 RandomAccessFile in = null; 187 188 try 189 { 190 ServletContext sc = WebContextFactory.get().getServletContext(); 191 File scriptFile = new File (sc.getRealPath(scriptPath)); 192 193 scriptModified = scriptFile.lastModified(); 198 in = new RandomAccessFile (scriptFile, "r"); 199 byte bytes[] = new byte[(int) in.length()]; 200 in.readFully(bytes); 201 cachedScript = new String (bytes); 202 203 return cachedScript; 204 } 205 catch (Exception ex) 206 { 207 log.error(ex.getMessage(), ex); 208 throw new InstantiationException (Messages.getString("ScriptCreator.MissingScript")); 209 } 210 finally 211 { 212 LocalUtil.close(in); 213 } 214 } 215 216 219 public void setScript(String scriptSrc) 220 { 221 if (scriptPath != null) 222 { 223 throw new IllegalArgumentException (Messages.getString("ScriptCreator.MultipleScript")); 224 } 225 226 if (scriptSrc == null || scriptSrc.trim().length() == 0) 227 { 228 throw new IllegalArgumentException (Messages.getString("ScriptedCreator.MissingScript")); 229 } 230 231 this.scriptSrc = scriptSrc; 232 } 233 234 238 public void setClass(String classname) 239 { 240 try 241 { 242 this.clazz = LocalUtil.classForName(classname); 243 } 244 catch (ClassNotFoundException ex) 245 { 246 throw new IllegalArgumentException (Messages.getString("Creator.ClassNotFound", classname)); 247 } 248 } 249 250 253 public Class getType() 254 { 255 if (clazz == null || (reloadable && scriptUpdated())) 256 { 257 try 258 { 259 clazz = getInstance().getClass(); 260 } 261 catch (InstantiationException ex) 262 { 263 log.error("Failed to instansiate object to detect type.", ex); 264 return Object .class; 265 } 266 } 267 268 return clazz; 269 } 270 271 274 public Object getInstance() throws InstantiationException 275 { 276 try 277 { 278 if (useDynamicClasses && clazz != null) 279 { 280 return clazz.newInstance(); 281 } 282 283 BSFManager bsfman = new BSFManager(); 284 285 try 286 { 287 WebContext context = WebContextFactory.get(); 288 bsfman.declareBean("context", context, context.getClass()); 289 } 290 catch (BSFException ex) 291 { 292 log.warn("Failed to register WebContext with scripting engine: " + ex.getMessage()); 293 } 294 295 return bsfman.eval(language, (null == scriptPath ? "dwr.xml" : scriptPath), 0, 0, getScript()); 296 } 297 catch (Exception ex) 298 { 299 log.error("Error executing script", ex); 300 throw new InstantiationException (Messages.getString("Creator.IllegalAccess")); 301 } 302 } 303 304 307 private static final Logger log = Logger.getLogger(ScriptedCreator.class); 308 309 312 private Class clazz = null; 313 314 317 private String language = null; 318 319 322 private String scriptSrc = null; 323 324 327 private String scriptPath = null; 328 329 333 private boolean reloadable = true; 334 335 339 private boolean useDynamicClasses = false; 340 341 344 private long scriptModified = -1; 345 346 349 private String cachedScript; 350 } 351 | Popular Tags |