1 22 23 package de.laures.cewolf; 24 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.ServletContext ; 31 32 38 public class Configuration { 39 40 public static final String KEY = Configuration.class.getName(); 41 private static final String DEFAULT_OVERLIB_URL = "overlib.js"; 42 private static final String DEFAULT_STORAGE = "de.laures.cewolf.storage.TransientSessionStorage"; 43 44 private String overlibURL = DEFAULT_OVERLIB_URL; 45 private boolean debugged = false; 46 47 private String storageClassName = DEFAULT_STORAGE; 48 private Storage storage = null; 49 private Map parameters = new HashMap (); 50 51 52 protected Configuration(ServletContext ctx) { 53 ctx.log("configuring cewolf app.."); 54 ctx.setAttribute(KEY, this); 55 56 ServletConfig config = (ServletConfig ) ctx.getAttribute(CewolfRenderer.INIT_CONFIG); 58 if (config != null) 59 { 60 Enumeration initParams = config.getInitParameterNames(); 61 try { 62 while (initParams.hasMoreElements()) { 63 String param = (String ) initParams.nextElement(); 64 String value = config.getInitParameter(param); 65 if ("debug".equalsIgnoreCase(param)) { 66 debugged = Boolean.valueOf(value).booleanValue(); 67 } else if ("overliburl".equalsIgnoreCase(param)) { 68 overlibURL = value; 69 } else if ("storage".equalsIgnoreCase(param)) { 70 storageClassName = value; 71 } else { 72 ctx.log(param + " parameter is ignored."); 73 } 74 parameters.put(param,value); 75 } 76 } catch (Throwable t) { 77 ctx.log("Error in Cewolf config.", t); 78 } 79 } 80 else { 81 ctx.log("Cewolf Misconfiguration. You should add a <load-on-startup> tag " 82 + "to your web.xml for the Cewolf rendering servlet.\n" 83 + "A default Configuration will be used if not."); 84 } 85 86 try { 87 initStorage(ctx); 88 } catch (CewolfException ex) { 89 ctx.log("exception during storage init from class " + storageClassName); 90 ctx.log("using " + DEFAULT_STORAGE); 91 storageClassName = DEFAULT_STORAGE; 92 try { 93 initStorage(ctx); 94 } catch(CewolfException cwex){ 95 cwex.printStackTrace(); 96 throw new RuntimeException (storageClassName + ".init() threw exception."); 97 } 98 } 99 ctx.log("using storage class " + storageClassName); 100 ctx.log("using overlibURL " + overlibURL); 101 ctx.log("debugging is turned " + (debugged ? "on" : "off")); 102 ctx.log("...done."); 103 } 104 105 private void initStorage(ServletContext ctx) throws CewolfException { 106 try { 107 storage = (Storage)Class.forName(storageClassName).newInstance(); 108 } catch(Exception ex){ 109 ex.printStackTrace(); 110 throw new CewolfException(ex.getMessage()); 111 } 112 storage.init(ctx); 113 } 114 115 private Configuration() { 116 } 117 118 125 public static Configuration getInstance(ServletContext ctx) { 126 Configuration config = null; 127 config = (Configuration) ctx.getAttribute(KEY); 128 129 if (config == null) 130 { 131 ctx.log("No Configuration for this context. Initializing."); 132 config = new Configuration(ctx); 133 ctx.setAttribute(KEY, config); 134 } 135 136 return config; 137 } 138 139 144 public boolean isDebugged() { 145 return debugged; 146 } 147 148 154 public String getOverlibURL() { 155 return overlibURL; 156 } 157 158 public Storage getStorage() { 159 return storage; 160 } 161 162 166 public Map getParameters() { 167 return parameters; 168 } 169 } 170 | Popular Tags |