1 16 17 package org.apache.catalina.storeconfig; 18 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.OutputStreamWriter ; 23 import java.io.PrintWriter ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import javax.naming.directory.DirContext ; 28 29 import org.apache.catalina.Container; 30 import org.apache.catalina.Context; 31 import org.apache.catalina.Engine; 32 import org.apache.catalina.Host; 33 import org.apache.catalina.Lifecycle; 34 import org.apache.catalina.LifecycleListener; 35 import org.apache.catalina.Loader; 36 import org.apache.catalina.Manager; 37 import org.apache.catalina.Pipeline; 38 import org.apache.catalina.Realm; 39 import org.apache.catalina.Valve; 40 import org.apache.catalina.core.StandardContext; 41 import org.apache.catalina.deploy.ApplicationParameter; 42 import org.apache.catalina.deploy.NamingResources; 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.apache.naming.resources.ProxyDirContext; 46 47 57 public class StandardContextSF extends StoreFactoryBase { 58 59 private static Log log = LogFactory.getLog(StandardContextSF.class); 60 61 68 public void store(PrintWriter aWriter, int indent, Object aContext) 69 throws Exception { 70 71 if (aContext instanceof StandardContext) { 72 StoreDescription desc = getRegistry().findDescription( 73 aContext.getClass()); 74 if (desc.isStoreSeparate()) { 75 String configFile = ((StandardContext) aContext) 76 .getConfigFile(); 77 if (configFile != null) { 78 if (desc.isExternalAllowed()) { 79 if (desc.isBackup()) 80 storeWithBackup((StandardContext) aContext); 81 else 82 storeContextSeparate(aWriter, indent, 83 (StandardContext) aContext); 84 return; 85 } 86 } 87 } 88 } 89 super.store(aWriter, indent, aContext); 90 91 } 92 93 102 protected void storeContextSeparate(PrintWriter aWriter, int indent, 103 StandardContext aContext) throws Exception { 104 String configFile = aContext.getConfigFile(); 105 PrintWriter writer = null; 106 if (configFile != null) { 107 File config = new File (configFile); 108 if (!config.isAbsolute()) { 109 config = new File (System.getProperty("catalina.base"), 110 configFile); 111 } 112 if (log.isInfoEnabled()) 113 log.info("Store Context " + aContext.getPath() 114 + " separate at file " + config); 115 try { 116 writer = new PrintWriter (new OutputStreamWriter ( 117 new FileOutputStream (config), getRegistry() 118 .getEncoding())); 119 storeXMLHead(writer); 120 super.store(writer, -2, aContext); 121 } finally { 122 if (writer != null) { 123 try { 124 writer.flush(); 125 } catch (Exception e) { 126 ; 127 } 128 try { 129 writer.close(); 130 } catch (Throwable t) { 131 ; 132 } 133 } 134 } 135 } else { 136 super.store(aWriter, indent, aContext); 137 } 138 } 139 140 146 protected void storeWithBackup(StandardContext aContext) throws Exception { 147 StoreFileMover mover = getConfigFileWriter((Context ) aContext); 148 if (mover != null) { 149 if (log.isInfoEnabled()) 150 log.info("Store Context " + aContext.getPath() 151 + " separate with backup (at file " 152 + mover.getConfigSave() + " )"); 153 PrintWriter writer = mover.getWriter(); 154 try { 155 storeXMLHead(writer); 156 super.store(writer, -2, aContext); 157 } finally { 158 try { 160 writer.flush(); 161 } catch (Exception e) { 162 log.error(e); 163 } 164 try { 165 writer.close(); 166 } catch (Exception e) { 167 throw (e); 168 } 169 } 170 mover.move(); 171 } 172 } 173 174 181 protected StoreFileMover getConfigFileWriter(Context context) 182 throws IOException { 183 String configFile = context.getConfigFile(); 184 PrintWriter writer = null; 185 StoreFileMover mover = null; 186 if (configFile != null) { 187 File config = new File (configFile); 188 if (!config.isAbsolute()) { 189 config = new File (System.getProperty("catalina.base"), 190 configFile); 191 } 192 mover = new StoreFileMover("", config.getCanonicalPath(), 194 getRegistry().getEncoding()); 195 } 196 return mover; 197 } 198 199 212 public void storeChilds(PrintWriter aWriter, int indent, Object aContext, 213 StoreDescription parentDesc) throws Exception { 214 if (aContext instanceof StandardContext) { 215 StandardContext context = (StandardContext) aContext; 216 if (context instanceof Lifecycle) { 218 LifecycleListener listeners[] = context 219 .findLifecycleListeners(); 220 storeElementArray(aWriter, indent, listeners); 221 } 222 if (context instanceof Pipeline) { 224 Valve valves[] = ((Pipeline) context).getValves(); 225 storeElementArray(aWriter, indent, valves); 226 } 227 228 Loader loader = context.getLoader(); 230 storeElement(aWriter, indent, loader); 231 232 Manager manager = context.getManager(); 234 storeElement(aWriter, indent, manager); 235 236 Realm realm = context.getRealm(); 238 if (realm != null) { 239 Realm parentRealm = null; 240 if (context.getParent() != null) { 242 parentRealm = context.getParent().getRealm(); 243 } 244 if (realm != parentRealm) { 245 storeElement(aWriter, indent, realm); 246 247 } 248 } 249 DirContext resources = context.getResources(); 251 if (resources instanceof ProxyDirContext) 252 resources = ((ProxyDirContext) resources).getDirContext(); 253 storeElement(aWriter, indent, resources); 254 255 String iListeners[] = context.findInstanceListeners(); 257 getStoreAppender().printTagArray(aWriter, "InstanceListener", 258 indent + 2, iListeners); 259 260 String wLifecycles[] = context.findWrapperLifecycles(); 262 getStoreAppender().printTagArray(aWriter, "WrapperListener", 263 indent + 2, wLifecycles); 264 String wListeners[] = context.findWrapperListeners(); 266 getStoreAppender().printTagArray(aWriter, "WrapperLifecycle", 267 indent + 2, wListeners); 268 269 ApplicationParameter[] appParams = context 271 .findApplicationParameters(); 272 storeElementArray(aWriter, indent, appParams); 273 274 NamingResources nresources = context.getNamingResources(); 276 storeElement(aWriter, indent, nresources); 277 278 String [] wresources = context.findWatchedResources(); 280 wresources = filterWatchedResources(context, wresources); 281 getStoreAppender().printTagArray(aWriter, "WatchedResource", 282 indent + 2, wresources); 283 } 284 } 285 286 290 protected File configBase(Context context) { 291 292 File file = new File (System.getProperty("catalina.base"), "conf"); 293 Container host = (Host) context.getParent(); 294 295 if ((host != null) && (host instanceof Host)) { 296 Container engine = host.getParent(); 297 if ((engine != null) && (engine instanceof Engine)) { 298 file = new File (file, engine.getName()); 299 } 300 file = new File (file, host.getName()); 301 try { 302 file = file.getCanonicalFile(); 303 } catch (IOException e) { 304 log.error(e); 305 } 306 } 307 return (file); 308 309 } 310 311 322 protected String [] filterWatchedResources(StandardContext context, 323 String [] wresources) throws IOException { 324 File configBase = configBase(context); 325 String confContext = new File (System.getProperty("catalina.base"), 326 "conf/context.xml").getCanonicalPath(); 327 String confHostDefault = new File (configBase, "context.xml.default") 328 .getCanonicalPath(); 329 String configFile = context.getConfigFile(); 330 331 List resource = new ArrayList (); 332 for (int i = 0; i < wresources.length; i++) { 333 334 if (wresources[i].equals(confContext)) 335 continue; 336 if (wresources[i].equals(confHostDefault)) 337 continue; 338 if (wresources[i].equals(configFile)) 339 continue; 340 resource.add(wresources[i]); 341 } 342 return (String []) resource.toArray(new String [resource.size()]); 343 } 344 345 } | Popular Tags |