You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rg...@apache.org on 2016/11/02 23:52:24 UTC

svn commit: r1767804 - in /qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server: ./ model/ util/urlstreamhandler/classpath/ util/urlstreamhandler/data/

Author: rgodfrey
Date: Wed Nov  2 23:52:23 2016
New Revision: 1767804

URL: http://svn.apache.org/viewvc?rev=1767804&view=rev
Log:
QPID-7486 : Add support for classpath: URLs to allow (default) locations to be in terms of resources found within the classpath

Added:
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/classpath/
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/classpath/Handler.java
      - copied, changed from r1767790, qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java
Modified:
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/SystemLauncher.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractSystemConfig.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/SystemConfig.java
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/SystemLauncher.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/SystemLauncher.java?rev=1767804&r1=1767803&r2=1767804&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/SystemLauncher.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/SystemLauncher.java Wed Nov  2 23:52:23 2016
@@ -21,10 +21,12 @@
 package org.apache.qpid.server;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.security.Principal;
 import java.security.PrivilegedExceptionAction;
@@ -58,13 +60,23 @@ import org.apache.qpid.server.model.Syst
 import org.apache.qpid.server.plugin.PluggableFactoryLoader;
 import org.apache.qpid.server.plugin.SystemConfigFactory;
 import org.apache.qpid.server.security.auth.TaskPrincipal;
+import org.apache.qpid.server.util.urlstreamhandler.classpath.Handler;
 
 public class SystemLauncher
 {
+
     private static final Logger LOGGER = LoggerFactory.getLogger(SystemLauncher.class);
-    public static final SystemLauncherListener.DefaultSystemLauncherListener DEFAULT_SYSTEM_LAUNCHER_LISTENER =
+    private static final String DEFAULT_INITIAL_PROPERTIES_LOCATION = "classpath:system.properties";
+
+    private static final SystemLauncherListener.DefaultSystemLauncherListener DEFAULT_SYSTEM_LAUNCHER_LISTENER =
             new SystemLauncherListener.DefaultSystemLauncherListener();
 
+    static
+    {
+        Handler.register();
+    }
+
+
     private EventLogger _eventLogger;
     private final TaskExecutor _taskExecutor = new TaskExecutorImpl();
 
@@ -103,20 +115,32 @@ public class SystemLauncher
         URL initialPropertiesLocation;
         if(initialProperties == null)
         {
-            initialPropertiesLocation = SystemLauncher.class.getClassLoader().getResource("system.properties");
+            initialPropertiesLocation = new URL(DEFAULT_INITIAL_PROPERTIES_LOCATION);
         }
         else
         {
-            initialPropertiesLocation = (new File(initialProperties)).toURI().toURL();
+            try
+            {
+                initialPropertiesLocation = new URL(initialProperties);
+            }
+            catch (MalformedURLException e)
+            {
+                initialPropertiesLocation = new File(initialProperties).toURI().toURL();
+
+            }
         }
 
         Properties props = new Properties(CommonProperties.asProperties());
-        if(initialPropertiesLocation != null)
-        {
 
-            try(InputStream inStream = initialPropertiesLocation.openStream())
+        try(InputStream inStream = initialPropertiesLocation.openStream())
+        {
+            props.load(inStream);
+        }
+        catch (FileNotFoundException e)
+        {
+            if(initialProperties != null)
             {
-                props.load(inStream);
+                throw e;
             }
         }
 

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractSystemConfig.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractSystemConfig.java?rev=1767804&r1=1767803&r2=1767804&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractSystemConfig.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/AbstractSystemConfig.java Wed Nov  2 23:52:23 2016
@@ -66,6 +66,7 @@ import org.apache.qpid.server.store.pref
 import org.apache.qpid.server.store.preferences.PreferenceStoreAttributes;
 import org.apache.qpid.server.store.preferences.PreferenceStoreFactoryService;
 import org.apache.qpid.server.util.ServerScopedRuntimeException;
+import org.apache.qpid.server.util.urlstreamhandler.classpath.Handler;
 
 public abstract class AbstractSystemConfig<X extends SystemConfig<X>>
         extends AbstractConfiguredObject<X> implements SystemConfig<X>, DynamicModel
@@ -112,6 +113,11 @@ public abstract class AbstractSystemConf
 
     private final Thread _shutdownHook = new Thread(new ShutdownService(), "QpidBrokerShutdownHook");
 
+    static
+    {
+        Handler.register();
+    }
+
     public AbstractSystemConfig(final TaskExecutor taskExecutor,
                                 final EventLogger eventLogger,
                                 final Principal systemPrincipal,
@@ -275,8 +281,7 @@ public abstract class AbstractSystemConf
 
     private Container<?> initateStoreAndRecovery() throws IOException
     {
-        ConfiguredObjectRecord[] initialRecords = convertToConfigurationRecords(getInitialConfigurationLocation()
-                                                                               );
+        ConfiguredObjectRecord[] initialRecords = convertToConfigurationRecords(getInitialConfigurationLocation());
         final DurableConfigurationStore store = getConfigurationStore();
         final List<ConfiguredObjectRecord> records = new ArrayList<>();
 

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/SystemConfig.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/SystemConfig.java?rev=1767804&r1=1767803&r2=1767804&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/SystemConfig.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/model/SystemConfig.java Wed Nov  2 23:52:23 2016
@@ -85,7 +85,7 @@ public interface SystemConfig<X extends
     String DEFAULT_INITIAL_CONFIG_NAME = "initial-config.json";
 
     @ManagedContextDefault(name="qpid.initialConfigurationLocation")
-    String DEFAULT_INITIAL_CONFIG_LOCATION = SystemConfig.class.getClassLoader().getResource(DEFAULT_INITIAL_CONFIG_NAME).toExternalForm();
+    String DEFAULT_INITIAL_CONFIG_LOCATION = "classpath:"+DEFAULT_INITIAL_CONFIG_NAME;
 
     @ManagedAttribute(defaultValue = "${qpid.initialConfigurationLocation}")
     String getInitialConfigurationLocation();

Copied: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/classpath/Handler.java (from r1767790, qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java)
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/classpath/Handler.java?p2=qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/classpath/Handler.java&p1=qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java&r1=1767790&r2=1767804&rev=1767804&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/classpath/Handler.java Wed Nov  2 23:52:23 2016
@@ -18,20 +18,14 @@
  * under the License.
  *
  */
-package org.apache.qpid.server.util.urlstreamhandler.data;
+package org.apache.qpid.server.util.urlstreamhandler.classpath;
 
-import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
-import java.net.URLDecoder;
 import java.net.URLStreamHandler;
-import java.nio.charset.StandardCharsets;
-
-import org.apache.qpid.util.Strings;
 
 public class Handler extends URLStreamHandler
 {
@@ -41,99 +35,40 @@ public class Handler extends URLStreamHa
     @Override
     protected URLConnection openConnection(final URL u) throws IOException
     {
-        return new DataUrlConnection(u);
-    }
-
-    public synchronized static void register()
-    {
-        if(!_registered)
+        String externalForm = u.toExternalForm();
+        if(externalForm.startsWith("classpath:"))
         {
-            String registeredPackages = System.getProperty(PROTOCOL_HANDLER_PROPERTY);
-            String thisPackage = Handler.class.getPackage().getName();
-            String packageToRegister = thisPackage.substring(0, thisPackage.lastIndexOf('.') );
-            System.setProperty(PROTOCOL_HANDLER_PROPERTY,
-                               registeredPackages == null
-                                       ? packageToRegister
-                                       : packageToRegister + "|" + registeredPackages);
-
-            _registered = true;
-        }
-
-
-
-    }
-
-    private static class DataUrlConnection extends URLConnection
-    {
-        private final byte[] _content;
-        private final String _contentType;
-        private final boolean _base64;
-
-        public DataUrlConnection(final URL u) throws IOException
-        {
-            super(u);
-            String externalForm = u.toExternalForm();
-            if(externalForm.startsWith("data:"))
-            {
-                String[] parts = externalForm.substring(5).split(",",2);
-                _base64 = parts[0].endsWith(";base64");
-                if(_base64)
-                {
-                    _content = Strings.decodeBase64(parts[1]);
-                }
-                else
-                {
-                    try
-                    {
-                        _content = URLDecoder.decode(parts[1], StandardCharsets.US_ASCII.name()).getBytes(StandardCharsets.US_ASCII);
-                    }
-                    catch (UnsupportedEncodingException e)
-                    {
-                        throw new IOException(e);
-                    }
-                }
-                String mediaType = (_base64
-                        ? parts[0].substring(0,parts[0].length()-";base64".length())
-                        : parts[0]).split(";")[0];
-
-                _contentType = "".equals(mediaType) ? "text/plain" : mediaType;
-            }
-            else
+            String path = externalForm.substring(10);
+            URL resourceUrl = getClass().getClassLoader().getResource(path);
+            if(resourceUrl == null)
             {
-                throw new MalformedURLException("'"+externalForm+"' does not start with 'data:'");
+                throw new FileNotFoundException("No such resource found in the classpath: " + path);
             }
+            return resourceUrl.openConnection();
         }
-
-
-
-        @Override
-        public void connect() throws IOException
+        else
         {
-
-        }
-
-        @Override
-        public int getContentLength()
-        {
-            return _content.length;
-        }
-
-        @Override
-        public String getContentType()
-        {
-            return _contentType;
+            throw new MalformedURLException("'"+externalForm+"' does not start with 'classpath:'");
         }
+    }
 
-        @Override
-        public String getContentEncoding()
+    public static void register()
+    {
+        synchronized (System.getProperties())
         {
-            return _base64 ? "base64" : null;
-        }
+            if (!_registered)
+            {
+                String registeredPackages = System.getProperty(PROTOCOL_HANDLER_PROPERTY);
+                String thisPackage = Handler.class.getPackage().getName();
+                String packageToRegister = thisPackage.substring(0, thisPackage.lastIndexOf('.'));
+                System.setProperty(PROTOCOL_HANDLER_PROPERTY,
+                                   registeredPackages == null
+                                           ? packageToRegister
+                                           : packageToRegister + "|" + registeredPackages);
 
-        @Override
-        public InputStream getInputStream() throws IOException
-        {
-            return new ByteArrayInputStream(_content);
+                _registered = true;
+            }
         }
     }
+
 }

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java?rev=1767804&r1=1767803&r2=1767804&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/util/urlstreamhandler/data/Handler.java Wed Nov  2 23:52:23 2016
@@ -44,23 +44,25 @@ public class Handler extends URLStreamHa
         return new DataUrlConnection(u);
     }
 
-    public synchronized static void register()
+    public static void register()
     {
-        if(!_registered)
+        synchronized (System.getProperties())
         {
-            String registeredPackages = System.getProperty(PROTOCOL_HANDLER_PROPERTY);
-            String thisPackage = Handler.class.getPackage().getName();
-            String packageToRegister = thisPackage.substring(0, thisPackage.lastIndexOf('.') );
-            System.setProperty(PROTOCOL_HANDLER_PROPERTY,
-                               registeredPackages == null
-                                       ? packageToRegister
-                                       : packageToRegister + "|" + registeredPackages);
+            if (!_registered)
+            {
+                String registeredPackages = System.getProperty(PROTOCOL_HANDLER_PROPERTY);
+                String thisPackage = Handler.class.getPackage().getName();
+                String packageToRegister = thisPackage.substring(0, thisPackage.lastIndexOf('.'));
+                System.setProperty(PROTOCOL_HANDLER_PROPERTY,
+                                   registeredPackages == null
+                                           ? packageToRegister
+                                           : packageToRegister + "|" + registeredPackages);
 
-            _registered = true;
+                _registered = true;
+            }
         }
 
 
-
     }
 
     private static class DataUrlConnection extends URLConnection



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org