1 16 package org.directwebremoting.impl; 17 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.directwebremoting.WebContext; 27 import org.directwebremoting.WebContextFactory; 28 import org.directwebremoting.extend.Creator; 29 import org.directwebremoting.extend.CreatorManager; 30 import org.directwebremoting.util.LocalUtil; 31 import org.directwebremoting.util.Logger; 32 import org.directwebremoting.util.Messages; 33 34 38 public class DefaultCreatorManager implements CreatorManager 39 { 40 44 public void setDebug(boolean debug) 45 { 46 this.debug = debug; 47 } 48 49 52 public boolean isDebug() 53 { 54 return debug; 55 } 56 57 60 public void addCreatorType(String typeName, String className) 61 { 62 if (!LocalUtil.isJavaIdentifier(typeName)) 63 { 64 log.error("Illegal identifier: '" + typeName + "'"); 65 return; 66 } 67 68 Class clazz = LocalUtil.classForName(typeName, className, Creator.class); 69 if (clazz != null) 70 { 71 log.debug("- adding creator type: " + typeName + " = " + clazz); 72 creatorTypes.put(typeName, clazz); 73 } 74 } 75 76 79 public void addCreator(String scriptName, String typeName, Map params) throws InstantiationException , IllegalAccessException , IllegalArgumentException 80 { 81 if (!LocalUtil.isJavaIdentifier(scriptName)) 82 { 83 log.error("Illegal identifier: '" + scriptName + "'"); 84 return; 85 } 86 87 Class clazz = (Class ) creatorTypes.get(typeName); 88 if (clazz == null) 89 { 90 log.error("Missing creator: " + typeName + " (while initializing creator for: " + scriptName + ".js)"); 91 return; 92 } 93 94 Creator creator = (Creator) clazz.newInstance(); 95 96 LocalUtil.setParams(creator, params, ignore); 97 creator.setProperties(params); 98 99 addCreator(scriptName, creator); 101 } 102 103 106 public void addCreator(String scriptName, Creator creator) throws IllegalArgumentException 107 { 108 Creator other = (Creator) creators.get(scriptName); 110 if (other != null) 111 { 112 throw new IllegalArgumentException (Messages.getString("DefaultCreatorManager.DuplicateName", scriptName, other.getType().getName(), creator)); 113 } 114 115 try 117 { 118 Class test = creator.getType(); 119 if (test == null) 120 { 121 log.error("Creator: '" + creator + "' for " + scriptName + ".js is returning null for type queries."); 122 } 123 else 124 { 125 log.debug("- adding creator: " + LocalUtil.getShortClassName(creator.getClass()) + " for " + scriptName); 126 creators.put(scriptName, creator); 127 } 128 } 129 catch (NoClassDefFoundError ex) 130 { 131 log.error("Missing class for creator '" + creator + "'. Cause: " + ex.getMessage()); 132 } 133 catch (Exception ex) 134 { 135 log.error("Error loading class for creator '" + creator + "'.", ex); 136 } 137 138 if (initApplicationScopeCreatorsAtStartup && creator.getScope().equals(Creator.APPLICATION)) 142 { 143 try 144 { 145 WebContext webcx = WebContextFactory.get(); 146 Object object = creator.getInstance(); 147 webcx.getServletContext().setAttribute(creator.getJavascript(), object); 148 149 log.debug("Created new " + creator.getJavascript() + ", stored in application."); 150 } 151 catch (InstantiationException ex) 152 { 153 log.warn("Failed to create " + creator.getJavascript(), ex); 154 log.debug("Maybe it will succeed when the application is in flight. If so you should probably set initApplicationScopeCreatorsAtStartup=false in web.xml"); 155 } 156 } 157 } 158 159 162 public Collection getCreatorNames() throws SecurityException 163 { 164 if (!debug) 165 { 166 throw new SecurityException (); 167 } 168 169 return Collections.unmodifiableSet(creators.keySet()); 170 } 171 172 175 public Creator getCreator(String scriptName) throws SecurityException 176 { 177 Creator creator = (Creator) creators.get(scriptName); 178 if (creator == null) 179 { 180 StringBuffer buffer = new StringBuffer ("Names of known classes are: "); 181 for (Iterator it = creators.keySet().iterator(); it.hasNext();) 182 { 183 String key = (String ) it.next(); 184 buffer.append(key); 185 buffer.append(' '); 186 } 187 188 log.warn(buffer.toString()); 189 throw new SecurityException (Messages.getString("DefaultCreatorManager.MissingName", scriptName)); 190 } 191 192 return creator; 193 } 194 195 198 public void setCreators(Map creators) 199 { 200 this.creators = creators; 201 } 202 203 206 private static final Logger log = Logger.getLogger(DefaultCreatorManager.class); 207 208 211 private Map creatorTypes = new HashMap (); 212 213 216 private Map creators = new HashMap (); 217 218 221 private boolean debug = false; 222 223 226 private boolean initApplicationScopeCreatorsAtStartup = false; 227 228 232 private static List ignore = Arrays.asList(new String [] { "creator", "class" }); 233 234 238 public boolean isInitApplicationScopeCreatorsAtStartup() 239 { 240 return initApplicationScopeCreatorsAtStartup; 241 } 242 243 247 public void setInitApplicationScopeCreatorsAtStartup(boolean initApplicationScopeCreatorsAtStartup) 248 { 249 this.initApplicationScopeCreatorsAtStartup = initApplicationScopeCreatorsAtStartup; 250 } 251 } 252 | Popular Tags |