You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2018/11/30 10:33:50 UTC

svn commit: r1847796 - in /tomcat/trunk/java/org/apache/catalina/storeconfig: LocalStrings.properties StoreConfig.java StoreConfigLifecycleListener.java StoreLoader.java

Author: remm
Date: Fri Nov 30 10:33:49 2018
New Revision: 1847796

URL: http://svn.apache.org/viewvc?rev=1847796&view=rev
Log:
Started with a few i18n, but in the end fully refactored the storeconfig registry loading.

Modified:
    tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties
    tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java
    tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfigLifecycleListener.java
    tomcat/trunk/java/org/apache/catalina/storeconfig/StoreLoader.java

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=1847796&r1=1847795&r2=1847796&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties [UTF-8] (original)
+++ tomcat/trunk/java/org/apache/catalina/storeconfig/LocalStrings.properties [UTF-8] Fri Nov 30 10:33:49 2018
@@ -13,10 +13,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+config.emptyObjectName=Invalid null or empty object name
+config.missingContextFile=Missing configuration file of context [{0}] to store
+config.objectNameNotFound=Object name [{0}] not found
 config.storeContextError=Error storing context [{0}]
 config.storeServerError=Error storing server
 
 factory.storeNoDescriptor=Descriptor for element class [{0}] not configured!
 factory.storeTag=store tag [{0}] ( Object: [{1}] )
 
+storeConfigListener.loadError=Error loading StoreConfig
 storeConfigListener.notServer=The listener was added to component other than the Server and will therefore be ignored
+storeConfigListener.registerError=Error registering StoreConfig MBean

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=1847796&r1=1847795&r2=1847796&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfig.java Fri Nov 30 10:33:49 2018
@@ -29,6 +29,7 @@ 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.catalina.startup.Bootstrap;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.res.StringManager;
@@ -119,8 +120,7 @@ public class StoreConfig implements ISto
     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!");
+            log.error(sm.getString("config.emptyObjectName"));
             return;
         }
         MBeanServer mserver = MBeanUtils.createServer();
@@ -149,12 +149,11 @@ public class StoreConfig implements ISto
                     store(aServer);
                 }
             } catch (Exception e) {
-                if (log.isInfoEnabled())
-                    log.info("Object " + aServerName
-                            + " is no a Server instance or store exception", e);
+                log.error(sm.getString("config.storeServerError"), e);
             }
-        } else if (log.isInfoEnabled())
-            log.info("Server " + aServerName + " not found!");
+        } else {
+            log.info(sm.getString("config.objectNameNotFound", aServerName));
+        }
     }
 
     /**
@@ -171,8 +170,7 @@ public class StoreConfig implements ISto
     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!");
+            log.error(sm.getString("config.emptyObjectName"));
             return;
         }
         MBeanServer mserver = MBeanUtils.createServer();
@@ -183,44 +181,35 @@ public class StoreConfig implements ISto
                         "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);
-                            }
+                    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!");
+                } else {
+                    log.error(sm.getString("config.missingContextFile", aContext.getPath()));
+                }
             } catch (Exception e) {
-                if (log.isInfoEnabled())
-                    log
-                            .info(
-                                    "Object "
-                                            + aContextName
-                                            + " is no a context instance or store exception",
-                                    e);
+                log.error(sm.getString("config.storeContextError", aContextName), e);
             }
-        } else if (log.isInfoEnabled())
-            log.info("Context " + aContextName + " not found!");
+        } else {
+            log.info(sm.getString("config.objectNameNotFound", aContextName));
+        }
     }
 
     /**
@@ -232,9 +221,8 @@ public class StoreConfig implements ISto
      */
     @Override
     public synchronized boolean store(Server aServer) {
-        StoreFileMover mover = new StoreFileMover(System
-                .getProperty("catalina.base"), getServerFilename(),
-                getRegistry().getEncoding());
+        StoreFileMover mover = new StoreFileMover(Bootstrap.getCatalinaBase(),
+                getServerFilename(), getRegistry().getEncoding());
         // Open an output writer for the new configuration file
         try {
             try (PrintWriter writer = mover.getWriter()) {
@@ -272,7 +260,7 @@ public class StoreConfig implements ISto
                 log.error(sm.getString("config.storeContextError", aContext.getName()), e);
             }
         } else {
-            log.error("Missing configFile at Context " + aContext.getPath());
+            log.error(sm.getString("config.missingContextFile", aContext.getPath()));
         }
         return false;
     }

Modified: tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfigLifecycleListener.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfigLifecycleListener.java?rev=1847796&r1=1847795&r2=1847796&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfigLifecycleListener.java (original)
+++ tomcat/trunk/java/org/apache/catalina/storeconfig/StoreConfigLifecycleListener.java Fri Nov 30 10:33:49 2018
@@ -85,17 +85,12 @@ public class StoreConfigLifecycleListene
             Class<?> clazz = Class.forName(getStoreConfigClass(), true, this
                     .getClass().getClassLoader());
             storeConfig = (IStoreConfig) clazz.getConstructor().newInstance();
-            if (null == getStoreRegistry())
-                // default Loading
-                loader.load();
-            else
-                // load a special file registry (url)
-                loader.load(getStoreRegistry());
+            loader.load(getStoreRegistry());
             // use the loader Registry
             storeConfig.setRegistry(loader.getRegistry());
             storeConfig.setServer(server);
         } catch (Exception e) {
-            log.error("createMBean load", e);
+            log.error(sm.getString("storeConfigListener.loadError"), e);
             return;
         }
         try {
@@ -103,7 +98,7 @@ public class StoreConfigLifecycleListene
             oname = new ObjectName("Catalina:type=StoreConfig" );
             registry.registerComponent(storeConfig, oname, "StoreConfig");
         } catch (Exception ex) {
-            log.error("createMBean register MBean", ex);
+            log.error(sm.getString("storeConfigListener.registerError"), ex);
         }
     }
 

Modified: tomcat/trunk/java/org/apache/catalina/storeconfig/StoreLoader.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/storeconfig/StoreLoader.java?rev=1847796&r1=1847795&r2=1847796&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/storeconfig/StoreLoader.java (original)
+++ tomcat/trunk/java/org/apache/catalina/storeconfig/StoreLoader.java Fri Nov 30 10:33:49 2018
@@ -16,16 +16,13 @@
  */
 package org.apache.catalina.storeconfig;
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 
-import org.apache.juli.logging.Log;
-import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.digester.Digester;
-import org.xml.sax.SAXException;
+import org.apache.tomcat.util.file.ConfigFileLoader;
+import org.apache.tomcat.util.file.ConfigurationSource.Resource;
 
 /**
  * <b>XML Format </b>
@@ -73,7 +70,6 @@ import org.xml.sax.SAXException;
  * </ul>
  */
 public class StoreLoader {
-    private static Log log = LogFactory.getLog(StoreLoader.class);
 
     /**
      * The <code>Digester</code> instance used to parse registry descriptors.
@@ -105,7 +101,6 @@ public class StoreLoader {
      * @return the XML digester that will be used to parse the configuration
      */
     protected static Digester createDigester() {
-        long t1 = System.currentTimeMillis();
         // Initialize the digester
         Digester digester = new Digester();
         digester.setValidating(false);
@@ -115,8 +110,7 @@ public class StoreLoader {
         digester.addObjectCreate("Registry",
                 "org.apache.catalina.storeconfig.StoreRegistry", "className");
         digester.addSetProperties("Registry");
-        digester
-                .addObjectCreate("Registry/Description",
+        digester.addObjectCreate("Registry/Description",
                         "org.apache.catalina.storeconfig.StoreDescription",
                         "className");
         digester.addSetProperties("Registry/Description");
@@ -132,147 +126,40 @@ public class StoreLoader {
         digester.addCallMethod("Registry/Description/TransientChild",
                 "addTransientChild", 0);
 
-        long t2 = System.currentTimeMillis();
-        if (log.isDebugEnabled())
-            log.debug("Digester for server-registry.xml created " + (t2 - t1));
         return digester;
 
     }
 
     /**
-     * Find main configuration file.
-     * @param aFile File name, absolute or relative
-     *  to <code>${catalina.base}/conf</code>, if not specified
-     *  <code>server-registry.xml</code> is used
-     * @return The file
-     */
-    protected File serverFile(String aFile) {
-
-        if (aFile == null || aFile.length() < 1)
-            aFile = "server-registry.xml";
-        File file = new File(aFile);
-        if (!file.isAbsolute())
-            file = new File(System.getProperty("catalina.base") + "/conf",
-                    aFile);
-        try {
-            file = file.getCanonicalFile();
-        } catch (IOException e) {
-            log.error(e);
-        }
-        return file;
-    }
-
-    /**
-     * Load main configuration file from external source.
+     * Load registry configuration.
      *
-     * @param aURL URL to the configuration file
-     */
-    public void load(String aURL) {
-        synchronized (digester) {
-            File aRegistryFile = serverFile(aURL);
-            try {
-                registry = (StoreRegistry) digester.parse(aRegistryFile);
-                registryResource = aRegistryFile.toURI().toURL();
-            } catch (IOException e) {
-                log.error(e);
-            } catch (SAXException e) {
-                log.error(e);
-            }
-        }
-
-    }
-
-    /**
-     * Load from defaults
-     * <ul>
-     * <li>System Property URL catalina.storeregistry</li>
-     * <li>File ${catalina.base}/conf/server-registry.xml</li>
-     * <li>class resource org/apache/catalina/storeconfig/server-registry.xml
-     * </li>
-     * </ul>
+     * @param path Path to the configuration file, may be null to use the default
+     *  name server-registry.xml
      */
-    public void load() {
-
-        InputStream is = null;
-        registryResource = null ;
-        try {
-            String configUrl = getConfigUrl();
-            if (configUrl != null) {
-                is = (new URL(configUrl)).openStream();
-                if (log.isInfoEnabled())
-                    log.info("Find registry server-registry.xml from system property at url "
-                            + configUrl);
-                registryResource = new URL(configUrl);
-            }
-        } catch (Throwable t) {
-            // Ignore
-        }
-        if (is == null) {
-            try {
-                File home = new File(getCatalinaBase());
-                File conf = new File(home, "conf");
-                File reg = new File(conf, "server-registry.xml");
-                is = new FileInputStream(reg);
-                if (log.isInfoEnabled())
-                    log.info("Find registry server-registry.xml at file "
-                            + reg.getCanonicalPath());
-                registryResource = reg.toURI().toURL();
-            } catch (Throwable t) {
-                // Ignore
-            }
-        }
-        if (is == null) {
-            try {
-                is = StoreLoader.class
-                        .getResourceAsStream("/org/apache/catalina/storeconfig/server-registry.xml");
-                if (log.isDebugEnabled())
-                    log.debug("Find registry server-registry.xml at classpath resource");
-                registryResource = StoreLoader.class
-                    .getResource("/org/apache/catalina/storeconfig/server-registry.xml");
-
-            } catch (Throwable t) {
-                // Ignore
+    public void load(String path) throws Exception {
+        try (Resource resource = (path == null) ?
+                ConfigFileLoader.getSource().getConfResource("server-registry.xml")
+                : ConfigFileLoader.getSource().getResource(path);
+                InputStream is = resource.getInputStream()) {
+            registryResource = resource.getURI().toURL();
+            synchronized (digester) {
+                registry = (StoreRegistry) digester.parse(is);
             }
-        }
-        if (is != null) {
-            try {
-                synchronized (digester) {
-                    registry = (StoreRegistry) digester.parse(is);
-                }
-            } catch (Throwable t) {
-                log.error(t);
-            } finally {
-                try {
-                    is.close();
-                } catch (IOException e) {
+        } catch (IOException e) {
+            // Try default classloader location
+            try (InputStream is = StoreLoader.class
+                    .getResourceAsStream("/org/apache/catalina/storeconfig/server-registry.xml")) {
+                if (is != null) {
+                    registryResource = StoreLoader.class
+                            .getResource("/org/apache/catalina/storeconfig/server-registry.xml");
+                    synchronized (digester) {
+                        registry = (StoreRegistry) digester.parse(is);
+                    }
+                } else {
+                    throw e;
                 }
             }
         }
-        if (is == null) {
-            log.error("Failed to load server-registry.xml");
-        }
-    }
-
-    /**
-     * @return the catalina.home environment variable.
-     */
-    private static String getCatalinaHome() {
-        return System.getProperty("catalina.home", System
-                .getProperty("user.dir"));
-    }
-
-    /**
-     * @return the catalina.base environment variable.
-     */
-    private static String getCatalinaBase() {
-        return System.getProperty("catalina.base", getCatalinaHome());
-    }
-
-    /**
-     * @return the configuration URL.
-     */
-    private static String getConfigUrl() {
-        return System.getProperty("catalina.storeconfig");
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org