1 16 package org.apache.catalina.storeconfig; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.URL ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.tomcat.util.digester.Digester; 27 import org.xml.sax.SAXException ; 28 29 74 public class StoreLoader { 75 private static Log log = LogFactory.getLog(StoreLoader.class); 76 77 80 protected static Digester digester = createDigester(); 81 82 private StoreRegistry registry; 83 84 private URL registryResource ; 85 86 89 public StoreRegistry getRegistry() { 90 return registry; 91 } 92 93 97 public void setRegistry(StoreRegistry registry) { 98 this.registry = registry; 99 } 100 101 105 protected static Digester createDigester() { 106 long t1 = System.currentTimeMillis(); 107 Digester digester = new Digester(); 109 digester.setValidating(false); 110 digester.setClassLoader(StoreRegistry.class.getClassLoader()); 111 112 digester.addObjectCreate("Registry", 114 "org.apache.catalina.storeconfig.StoreRegistry", "className"); 115 digester.addSetProperties("Registry"); 116 digester 117 .addObjectCreate("Registry/Description", 118 "org.apache.catalina.storeconfig.StoreDescription", 119 "className"); 120 digester.addSetProperties("Registry/Description"); 121 digester.addRule("Registry/Description", new StoreFactoryRule( 122 "org.apache.catalina.storeconfig.StoreFactoryBase", 123 "storeFactoryClass", 124 "org.apache.catalina.storeconfig.StoreAppender", 125 "storeAppenderClass")); 126 digester.addSetNext("Registry/Description", "registerDescription", 127 "org.apache.catalina.storeconfig.StoreDescription"); 128 digester.addCallMethod("Registry/Description/TransientAttribute", 129 "addTransientAttribute", 0); 130 digester.addCallMethod("Registry/Description/TransientChild", 131 "addTransientChild", 0); 132 133 long t2 = System.currentTimeMillis(); 134 if (log.isDebugEnabled()) 135 log.debug("Digester for server-registry.xml created " + (t2 - t1)); 136 return (digester); 137 138 } 139 140 145 protected File serverFile(String aFile) { 146 147 if (aFile == null || (aFile != null && aFile.length() < 1)) 148 aFile = "server-registry.xml"; 149 File file = new File (aFile); 150 if (!file.isAbsolute()) 151 file = new File (System.getProperty("catalina.base") + "/conf", 152 aFile); 153 try { 154 file = file.getCanonicalFile(); 155 } catch (IOException e) { 156 log.error(e); 157 } 158 return (file); 159 } 160 161 166 public void load(String aURL) { 167 synchronized (digester) { 168 File aRegistryFile = serverFile(aURL); 169 try { 170 registry = (StoreRegistry) digester.parse(aRegistryFile); 171 registryResource = aRegistryFile.toURL(); 172 } catch (IOException e) { 173 log.error(e); 174 } catch (SAXException e) { 175 log.error(e); 176 } 177 } 178 179 } 180 181 190 public void load() { 191 192 InputStream is = null; 193 Throwable error = null; 194 registryResource = null ; 195 try { 196 String configUrl = getConfigUrl(); 197 if (configUrl != null) { 198 is = (new URL (configUrl)).openStream(); 199 if (log.isInfoEnabled()) 200 log 201 .info("Find registry server-registry.xml from system property at url " 202 + configUrl); 203 ; 204 registryResource = new URL (configUrl); 205 } 206 } catch (Throwable t) { 207 } 209 if (is == null) { 210 try { 211 File home = new File (getCatalinaBase()); 212 File conf = new File (home, "conf"); 213 File reg = new File (conf, "server-registry.xml"); 214 is = new FileInputStream (reg); 215 if (log.isInfoEnabled()) 216 log.info("Find registry server-registry.xml at file " 217 + reg.getCanonicalPath()); 218 ; 219 registryResource = reg.toURL() ; 220 } catch (Throwable t) { 221 } 223 } 224 if (is == null) { 225 try { 226 is = StoreLoader.class 227 .getResourceAsStream("/org/apache/catalina/storeconfig/server-registry.xml"); 228 if (log.isInfoEnabled()) 229 log 230 .info("Find registry server-registry.xml at classpath resource"); 231 registryResource = StoreLoader.class 232 .getResource("/org/apache/catalina/storeconfig/server-registry.xml"); 233 234 } catch (Throwable t) { 235 } 237 } 238 if (is != null) { 239 try { 240 synchronized (digester) { 241 registry = (StoreRegistry) digester.parse(is); 242 } 243 } catch (Throwable t) { 244 error = t; 245 } finally { 246 try { 247 is.close(); 248 } catch (IOException e) { 249 } 250 } 251 } 252 if ((is == null) || (error != null)) { 253 log.error(error); 254 } 255 } 256 257 260 private static String getCatalinaHome() { 261 return System.getProperty("catalina.home", System 262 .getProperty("user.dir")); 263 } 264 265 268 private static String getCatalinaBase() { 269 return System.getProperty("catalina.base", getCatalinaHome()); 270 } 271 272 275 private static String getConfigUrl() { 276 return System.getProperty("catalina.storeconfig"); 277 } 278 279 280 281 284 public URL getRegistryResource() { 285 return registryResource; 286 } 287 } | Popular Tags |