1 16 package org.apache.catalina.storeconfig; 17 18 import java.io.PrintWriter ; 19 20 import javax.management.MBeanServer ; 21 import javax.management.MalformedObjectNameException ; 22 import javax.management.ObjectName ; 23 24 import org.apache.catalina.Context; 25 import org.apache.catalina.Host; 26 import org.apache.catalina.Server; 27 import org.apache.catalina.ServerFactory; 28 import org.apache.catalina.Service; 29 import org.apache.catalina.core.StandardContext; 30 import org.apache.catalina.mbeans.MBeanUtils; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 41 public class StoreConfig implements IStoreConfig { 42 private static Log log = LogFactory.getLog(StoreConfig.class); 43 44 private String serverFilename = "conf/server.xml"; 45 46 private StoreRegistry registry; 47 48 53 public String getServerFilename() { 54 return serverFilename; 55 } 56 57 62 public void setServerFilename(String string) { 63 serverFilename = string; 64 } 65 66 72 public StoreRegistry getRegistry() { 73 return registry; 74 } 75 76 81 public void setRegistry(StoreRegistry aRegistry) { 82 registry = aRegistry; 83 } 84 85 90 public synchronized void storeConfig() { 91 store(ServerFactory.getServer()); 92 } 93 94 104 public synchronized void storeServer(String aServerName, boolean backup, 105 boolean externalAllowed) throws MalformedObjectNameException { 106 if (aServerName == null || aServerName.length() == 0) { 107 if (log.isErrorEnabled()) 108 log.error("Please, call with a correct server ObjectName!"); 109 return; 110 } 111 MBeanServer mserver = MBeanUtils.createServer(); 112 ObjectName objectName = new ObjectName (aServerName); 113 if (mserver.isRegistered(objectName)) { 114 try { 115 Server aServer = (Server ) mserver.getAttribute(objectName, 116 "managedResource"); 117 StoreDescription desc = null; 118 desc = getRegistry().findDescription(StandardContext.class); 119 if (desc != null) { 120 boolean oldSeparate = desc.isStoreSeparate(); 121 boolean oldBackup = desc.isBackup(); 122 boolean oldExternalAllowed = desc.isExternalAllowed(); 123 try { 124 desc.setStoreSeparate(true); 125 desc.setBackup(backup); 126 desc.setExternalAllowed(externalAllowed); 127 store((Server ) aServer); 128 } finally { 129 desc.setStoreSeparate(oldSeparate); 130 desc.setBackup(oldBackup); 131 desc.setExternalAllowed(oldExternalAllowed); 132 } 133 } else 134 store((Server ) aServer); 135 } catch (Exception e) { 136 if (log.isInfoEnabled()) 137 log.info("Object " + aServerName 138 + " is no a Server instance or store exception", e); 139 } 140 } else if (log.isInfoEnabled()) 141 log.info("Server " + aServerName + " not found!"); 142 } 143 144 153 public synchronized void storeContext(String aContextName, boolean backup, 154 boolean externalAllowed) throws MalformedObjectNameException { 155 if (aContextName == null || aContextName.length() == 0) { 156 if (log.isErrorEnabled()) 157 log.error("Please, call with a correct context ObjectName!"); 158 return; 159 } 160 MBeanServer mserver = MBeanUtils.createServer(); 161 ObjectName objectName = new ObjectName (aContextName); 162 if (mserver.isRegistered(objectName)) { 163 try { 164 Context aContext = (Context) mserver.getAttribute(objectName, 165 "managedResource"); 166 String configFile = aContext.getConfigFile(); 167 if (configFile != null) { 168 try { 169 StoreDescription desc = null; 170 desc = getRegistry().findDescription( 171 aContext.getClass()); 172 if (desc != null) { 173 boolean oldSeparate = desc.isStoreSeparate(); 174 boolean oldBackup = desc.isBackup(); 175 boolean oldExternalAllowed = desc 176 .isExternalAllowed(); 177 try { 178 desc.setStoreSeparate(true); 179 desc.setBackup(backup); 180 desc.setExternalAllowed(externalAllowed); 181 desc.getStoreFactory() 182 .store(null, -2, aContext); 183 } finally { 184 desc.setStoreSeparate(oldSeparate); 185 desc.setBackup(oldBackup); 186 desc.setBackup(oldExternalAllowed); 187 } 188 } 189 } catch (Exception e) { 190 log.error(e); 191 } 192 } else 193 log.error("Missing configFile at Context " 194 + aContext.getPath() + " to store!"); 195 } catch (Exception e) { 196 if (log.isInfoEnabled()) 197 log 198 .info( 199 "Object " 200 + aContextName 201 + " is no a context instance or store exception", 202 e); 203 } 204 } else if (log.isInfoEnabled()) 205 log.info("Context " + aContextName + " not found!"); 206 } 207 208 213 public synchronized void store(Server aServer) { 214 215 StoreFileMover mover = new StoreFileMover(System 216 .getProperty("catalina.base"), getServerFilename(), 217 getRegistry().getEncoding()); 218 try { 220 PrintWriter writer = mover.getWriter(); 221 222 try { 223 store(writer, -2, aServer); 224 } finally { 225 try { 227 writer.flush(); 228 } catch (Exception e) { 229 log.error(e); 230 } 231 try { 232 writer.close(); 233 } catch (Exception e) { 234 throw (e); 235 } 236 } 237 mover.move(); 238 } catch (Exception e) { 239 log.error(e); 240 } 241 } 242 243 248 public synchronized void store(Context aContext) { 249 String configFile = aContext.getConfigFile(); 250 if (configFile != null) { 251 try { 252 StoreDescription desc = null; 253 desc = getRegistry().findDescription(aContext.getClass()); 254 if (desc != null) { 255 boolean old = desc.isStoreSeparate(); 256 try { 257 desc.setStoreSeparate(true); 258 desc.getStoreFactory().store(null, -2, aContext); 259 } finally { 260 desc.setStoreSeparate(old); 261 } 262 } 263 } catch (Exception e) { 264 log.error(e); 265 } 266 } else 267 log.error("Missing configFile at Context " + aContext.getPath()); 268 269 } 270 271 277 public synchronized void store(PrintWriter aWriter, int indent, 278 Context aContext) { 279 boolean oldSeparate = true; 280 StoreDescription desc = null; 281 try { 282 desc = getRegistry().findDescription(aContext.getClass()); 283 oldSeparate = desc.isStoreSeparate(); 284 desc.setStoreSeparate(false); 285 desc.getStoreFactory().store(aWriter, indent, aContext); 286 } catch (Exception e) { 287 log.error(e); 288 } finally { 289 if (desc != null) 290 desc.setStoreSeparate(oldSeparate); 291 } 292 } 293 294 300 public synchronized void store(PrintWriter aWriter, int indent, Host aHost) { 301 try { 302 StoreDescription desc = getRegistry().findDescription( 303 aHost.getClass()); 304 desc.getStoreFactory().store(aWriter, indent, aHost); 305 } catch (Exception e) { 306 log.error(e); 307 } 308 } 309 310 316 public synchronized void store(PrintWriter aWriter, int indent, 317 Service aService) { 318 try { 319 StoreDescription desc = getRegistry().findDescription( 320 aService.getClass()); 321 desc.getStoreFactory().store(aWriter, indent, aService); 322 } catch (Exception e) { 323 log.error(e); 324 } 325 } 326 327 335 public synchronized void store(PrintWriter writer, int indent, 336 Server aServer) { 337 try { 338 StoreDescription desc = getRegistry().findDescription( 339 aServer.getClass()); 340 desc.getStoreFactory().store(writer, indent, aServer); 341 } catch (Exception e) { 342 log.error(e); 343 } 344 } 345 346 } | Popular Tags |