Author: remm Date: Thu Jun 26 10:18:07 2014 New Revision: 1605724 URL: http://svn.apache.org/r1605724 Log: Improve exception routing so that a thrown exception interrupts the store process (avoid server.xml overwrite with a broken one).
Modified: tomcat/trunk/java/org/apache/catalina/storeconfig/IStoreConfig.java tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/catalina/storeconfig/IStoreConfig.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/storeconfig/IStoreConfig.java?rev=1605724&r1=1605723&r2=1605724&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/storeconfig/IStoreConfig.java (original) +++ tomcat/trunk/java/org/apache/catalina/storeconfig/IStoreConfig.java Thu Jun 26 10:18:07 2014 @@ -61,7 +61,7 @@ public interface IStoreConfig { * @exception Exception * if an exception occurs while storing */ - void storeConfig() throws Exception; + void storeConfig(); /** * Store the specified Server properties. @@ -72,7 +72,7 @@ public interface IStoreConfig { * @exception Exception * if an exception occurs while storing */ - void store(Server aServer) throws Exception; + boolean store(Server aServer); /** * Store the specified Server properties. @@ -84,7 +84,7 @@ public interface IStoreConfig { * @param aServer * Object to be stored */ - void store(PrintWriter aWriter, int indent, Server aServer); + void store(PrintWriter aWriter, int indent, Server aServer) throws Exception; /** * Store the specified Service properties. @@ -96,7 +96,7 @@ public interface IStoreConfig { * @param aService * Object to be stored */ - void store(PrintWriter aWriter, int indent, Service aService); + void store(PrintWriter aWriter, int indent, Service aService) throws Exception; /** * Store the specified Host properties. @@ -108,7 +108,7 @@ public interface IStoreConfig { * @param aHost * Object to be stored */ - void store(PrintWriter aWriter, int indent, Host aHost); + void store(PrintWriter aWriter, int indent, Host aHost) throws Exception; /** * Store the specified Context properties. @@ -116,7 +116,7 @@ public interface IStoreConfig { * @param aContext * Object to be stored */ - void store(Context aContext); + boolean store(Context aContext); /** * Store the specified Context properties. @@ -128,5 +128,5 @@ public interface IStoreConfig { * @param aContext * Object to be stored */ - void store(PrintWriter aWriter, int indent, Context aContext); + void store(PrintWriter aWriter, int indent, Context aContext) throws Exception; } \ No newline at end of file Modified: tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties?rev=1605724&r1=1605723&r2=1605724&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties Thu Jun 26 10:18:07 2014 @@ -15,3 +15,5 @@ factory.storeTag=store tag {0} ( Object: {1} ) factory.storeNoDescriptor=Descriptor for element class {0} not configured! +config.storeServerError=Error storing server +config.storeContextError=Error storing context {0} Modified: tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java?rev=1605724&r1=1605723&r2=1605724&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java (original) +++ tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java Thu Jun 26 10:18:07 2014 @@ -19,18 +19,13 @@ package org.apache.catalina.storeconfig; import java.io.PrintWriter; import java.net.URL; -import javax.management.MBeanServer; -import javax.management.MalformedObjectNameException; -import javax.management.ObjectName; - import org.apache.catalina.Context; import org.apache.catalina.Host; import org.apache.catalina.Server; import org.apache.catalina.Service; -import org.apache.catalina.core.StandardContext; -import org.apache.catalina.mbeans.MBeanUtils; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; +import org.apache.tomcat.util.res.StringManager; /** * Store Server/Service/Host/Context at file or PrintWriter. Default server.xml @@ -38,6 +33,8 @@ import org.apache.juli.logging.LogFactor */ public class StoreConfig implements IStoreConfig { private static Log log = LogFactory.getLog(StoreConfig.class); + protected static final StringManager sm = StringManager + .getManager(Constants.Package); private String serverFilename = "conf/server.xml"; @@ -98,133 +95,17 @@ public class StoreConfig implements ISto * Store current Server. */ @Override - public synchronized void storeConfig() { + public void storeConfig() { store(server); } /** - * Store Server from Object Name (Catalina:type=Server) - * - * @param aServerName - * Server ObjectName - * @param backup - * @param externalAllowed - * s * - * @throws MalformedObjectNameException - */ - public synchronized void storeServer(String aServerName, boolean backup, - boolean externalAllowed) throws MalformedObjectNameException { - if (aServerName == null || aServerName.length() == 0) { - if (log.isErrorEnabled()) - log.error("Please, call with a correct server ObjectName!"); - return; - } - MBeanServer mserver = MBeanUtils.createServer(); - ObjectName objectName = new ObjectName(aServerName); - if (mserver.isRegistered(objectName)) { - try { - Server aServer = (Server) mserver.getAttribute(objectName, - "managedResource"); - StoreDescription desc = null; - desc = getRegistry().findDescription(StandardContext.class); - if (desc != null) { - boolean oldSeparate = desc.isStoreSeparate(); - boolean oldBackup = desc.isBackup(); - boolean oldExternalAllowed = desc.isExternalAllowed(); - try { - desc.setStoreSeparate(true); - desc.setBackup(backup); - desc.setExternalAllowed(externalAllowed); - store(aServer); - } finally { - desc.setStoreSeparate(oldSeparate); - desc.setBackup(oldBackup); - desc.setExternalAllowed(oldExternalAllowed); - } - } else { - store(aServer); - } - } catch (Exception e) { - if (log.isInfoEnabled()) - log.info("Object " + aServerName - + " is no a Server instance or store exception", e); - } - } else if (log.isInfoEnabled()) - log.info("Server " + aServerName + " not found!"); - } - - /** - * Store a Context from ObjectName - * - * @param aContextName - * MBean ObjectName - * @param backup - * @param externalAllowed - * @throws MalformedObjectNameException - */ - public synchronized void storeContext(String aContextName, boolean backup, - boolean externalAllowed) throws MalformedObjectNameException { - if (aContextName == null || aContextName.length() == 0) { - if (log.isErrorEnabled()) - log.error("Please, call with a correct context ObjectName!"); - return; - } - MBeanServer mserver = MBeanUtils.createServer(); - ObjectName objectName = new ObjectName(aContextName); - if (mserver.isRegistered(objectName)) { - try { - Context aContext = (Context) mserver.getAttribute(objectName, - "managedResource"); - URL configFile = aContext.getConfigFile(); - if (configFile != null) { - try { - StoreDescription desc = null; - desc = getRegistry().findDescription( - aContext.getClass()); - if (desc != null) { - boolean oldSeparate = desc.isStoreSeparate(); - boolean oldBackup = desc.isBackup(); - boolean oldExternalAllowed = desc - .isExternalAllowed(); - try { - desc.setStoreSeparate(true); - desc.setBackup(backup); - desc.setExternalAllowed(externalAllowed); - desc.getStoreFactory() - .store(null, -2, aContext); - } finally { - desc.setStoreSeparate(oldSeparate); - desc.setBackup(oldBackup); - desc.setBackup(oldExternalAllowed); - } - } - } catch (Exception e) { - log.error(e); - } - } else - log.error("Missing configFile at Context " - + aContext.getPath() + " to store!"); - } catch (Exception e) { - if (log.isInfoEnabled()) - log - .info( - "Object " - + aContextName - + " is no a context instance or store exception", - e); - } - } else if (log.isInfoEnabled()) - log.info("Context " + aContextName + " not found!"); - } - - /** * Write the configuration information for this entire <code>Server</code> * out to the server.xml configuration file. * */ @Override - public synchronized void store(Server aServer) { - + public synchronized boolean store(Server aServer) { StoreFileMover mover = new StoreFileMover(System .getProperty("catalina.base"), getServerFilename(), getRegistry().getEncoding()); @@ -234,9 +115,11 @@ public class StoreConfig implements ISto store(writer, -2, aServer); } mover.move(); + return true; } catch (Exception e) { - log.error(e); + log.error(sm.getString("config.storeServerError"), e); } + return false; } /* @@ -245,7 +128,7 @@ public class StoreConfig implements ISto * @see org.apache.catalina.config.IStoreConfig#store(org.apache.catalina.Context) */ @Override - public synchronized void store(Context aContext) { + public synchronized boolean store(Context aContext) { URL configFile = aContext.getConfigFile(); if (configFile != null) { try { @@ -260,12 +143,14 @@ public class StoreConfig implements ISto desc.setStoreSeparate(old); } } + return true; } catch (Exception e) { - log.error(e); + log.error(sm.getString("config.storeContextError", aContext.getName()), e); } - } else + } else { log.error("Missing configFile at Context " + aContext.getPath()); - + } + return false; } /* @@ -275,8 +160,8 @@ public class StoreConfig implements ISto * int, org.apache.catalina.Context) */ @Override - public synchronized void store(PrintWriter aWriter, int indent, - Context aContext) { + public void store(PrintWriter aWriter, int indent, + Context aContext) throws Exception { boolean oldSeparate = true; StoreDescription desc = null; try { @@ -284,8 +169,6 @@ public class StoreConfig implements ISto oldSeparate = desc.isStoreSeparate(); desc.setStoreSeparate(false); desc.getStoreFactory().store(aWriter, indent, aContext); - } catch (Exception e) { - log.error(e); } finally { if (desc != null) desc.setStoreSeparate(oldSeparate); @@ -299,14 +182,11 @@ public class StoreConfig implements ISto * int, org.apache.catalina.Host) */ @Override - public synchronized void store(PrintWriter aWriter, int indent, Host aHost) { - try { - StoreDescription desc = getRegistry().findDescription( - aHost.getClass()); - desc.getStoreFactory().store(aWriter, indent, aHost); - } catch (Exception e) { - log.error(e); - } + public void store(PrintWriter aWriter, int indent, Host aHost) + throws Exception { + StoreDescription desc = getRegistry().findDescription( + aHost.getClass()); + desc.getStoreFactory().store(aWriter, indent, aHost); } /* @@ -316,15 +196,11 @@ public class StoreConfig implements ISto * int, org.apache.catalina.Service) */ @Override - public synchronized void store(PrintWriter aWriter, int indent, - Service aService) { - try { - StoreDescription desc = getRegistry().findDescription( - aService.getClass()); - desc.getStoreFactory().store(aWriter, indent, aService); - } catch (Exception e) { - log.error(e); - } + public void store(PrintWriter aWriter, int indent, + Service aService) throws Exception { + StoreDescription desc = getRegistry().findDescription( + aService.getClass()); + desc.getStoreFactory().store(aWriter, indent, aService); } /** @@ -336,15 +212,11 @@ public class StoreConfig implements ISto * @param aServer */ @Override - public synchronized void store(PrintWriter writer, int indent, - Server aServer) { - try { - StoreDescription desc = getRegistry().findDescription( - aServer.getClass()); - desc.getStoreFactory().store(writer, indent, aServer); - } catch (Exception e) { - log.error(e); - } + public void store(PrintWriter writer, int indent, + Server aServer) throws Exception { + StoreDescription desc = getRegistry().findDescription( + aServer.getClass()); + desc.getStoreFactory().store(writer, indent, aServer); } } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1605724&r1=1605723&r2=1605724&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Thu Jun 26 10:18:07 2014 @@ -84,6 +84,10 @@ <bug>56665</bug>: Correct the generation of the effective web.xml when elements contain an empty string as value. (violetagg) </fix> + <fix> + Fix storeconfig exception routing issues, so that a major problem + should avoid configuration overwrite. (remm) + </fix> </changelog> </subsection> <subsection name="Jasper"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org