Author: dejanb
Date: Thu Jun 18 11:14:40 2009
New Revision: 786017

URL: http://svn.apache.org/viewvc?rev=786017&view=rev
Log:
jaxb - improving uri handling

Added:
    
activemq/sandbox/activemq-flow/activemq-broker/src/test/resources/org/apache/activemq/apollo/jaxb/testUris.xml
Modified:
    
activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/jaxb/JAXBBrokerFactory.java
    
activemq/sandbox/activemq-flow/activemq-broker/src/test/java/org/apache/activemq/apollo/jaxb/JAXBConfigTest.java

Modified: 
activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/jaxb/JAXBBrokerFactory.java
URL: 
http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/jaxb/JAXBBrokerFactory.java?rev=786017&r1=786016&r2=786017&view=diff
==============================================================================
--- 
activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/jaxb/JAXBBrokerFactory.java
 (original)
+++ 
activemq/sandbox/activemq-flow/activemq-broker/src/main/java/org/apache/activemq/apollo/jaxb/JAXBBrokerFactory.java
 Thu Jun 18 11:14:40 2009
@@ -16,14 +16,16 @@
  */
 package org.apache.activemq.apollo.jaxb;
 
+import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
 
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.UnmarshalException;
 import javax.xml.bind.Unmarshaller;
 
-import org.apache.activemq.apollo.broker.BrokerFactory;
 import org.apache.activemq.apollo.broker.Broker;
+import org.apache.activemq.apollo.broker.BrokerFactory;
 import org.apache.activemq.util.URISupport;
 
 public class JAXBBrokerFactory implements BrokerFactory.Handler {
@@ -36,15 +38,21 @@
                brokerURI = URISupport.stripScheme(brokerURI);
                String scheme = brokerURI.getScheme();
                if( scheme==null || "file".equals(scheme) ) {
-                       configURL = URISupport.changeScheme(brokerURI, 
"file").toURL();
+                       configURL = 
URISupport.changeScheme(URISupport.stripScheme(brokerURI), "file").toURL();
                } else if( "classpath".equals(scheme) ) {
                        configURL = 
Thread.currentThread().getContextClassLoader().getResource(brokerURI.getSchemeSpecificPart());
                } else {
                        configURL = URISupport.changeScheme(brokerURI, 
scheme).toURL();
-               }               
-               
-               BrokerXml xml = (BrokerXml) unmarshaller.unmarshal(configURL);
-               return xml.createMessageBroker();
+               }
+               if (configURL == null) {
+                       throw new IOException("Cannot create broker from 
non-existent URI: " + brokerURI);
+               }
+               try {
+                       BrokerXml xml = (BrokerXml) 
unmarshaller.unmarshal(configURL);
+                       return xml.createMessageBroker();
+               } catch (UnmarshalException e) {
+                       throw new IOException("Cannot create broker from URI: " 
+ brokerURI + ", reason: " + e.getCause());
+               }       
        }
 
 

Modified: 
activemq/sandbox/activemq-flow/activemq-broker/src/test/java/org/apache/activemq/apollo/jaxb/JAXBConfigTest.java
URL: 
http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-broker/src/test/java/org/apache/activemq/apollo/jaxb/JAXBConfigTest.java?rev=786017&r1=786016&r2=786017&view=diff
==============================================================================
--- 
activemq/sandbox/activemq-flow/activemq-broker/src/test/java/org/apache/activemq/apollo/jaxb/JAXBConfigTest.java
 (original)
+++ 
activemq/sandbox/activemq-flow/activemq-broker/src/test/java/org/apache/activemq/apollo/jaxb/JAXBConfigTest.java
 Thu Jun 18 11:14:40 2009
@@ -17,6 +17,7 @@
 package org.apache.activemq.apollo.jaxb;
 
 
+import java.io.IOException;
 import java.net.URI;
 import java.util.ArrayList;
 
@@ -57,6 +58,44 @@
                assertTrue( 
broker.getDefaultVirtualHost().getDatabase().getStore() instanceof MemoryStore 
);
                
        }
+       
+       public void testUris() throws Exception {
+               boolean failed = false;
+               // non-existent classpath
+               try {
+                       URI uri = new 
URI("jaxb:classpath:org/apache/activemq/apollo/jaxb/" + getName()+"-fail.xml");
+                       BrokerFactory.createBroker(uri);
+               } catch (IOException e) {
+                       failed = true;
+               }
+               if (!failed) {
+                       fail("Creating broker from non-existing url does not 
throw an exception!");
+               }
+               failed = false;
+               //non-existent file
+               try {
+                       URI uri = new 
URI("jaxb:file:/org/apache/activemq/apollo/jaxb/" + getName()+"-fail.xml");
+                       BrokerFactory.createBroker(uri);
+               } catch (IOException e) {
+                       failed = true;
+               }
+               if (!failed) {
+                       fail("Creating broker from non-existing url does not 
throw an exception!");
+               }
+               //non-existent url
+               try {
+                       URI uri = new URI("jaxb:http://localhost/"; + 
getName()+".xml");
+                       BrokerFactory.createBroker(uri);
+               } catch (IOException e) {
+                       failed = true;
+               }
+               if (!failed) {
+                       fail("Creating broker from non-existing url does not 
throw an exception!");
+               }               
+               // regular file
+               URI uri = new URI("jaxb:" + 
Thread.currentThread().getContextClassLoader().getResource("org/apache/activemq/apollo/jaxb/"
 + getName() + ".xml"));
+               BrokerFactory.createBroker(uri);
+       }
 
     protected Broker createBroker() throws Exception {
        URI uri = new URI("jaxb:classpath:org/apache/activemq/apollo/jaxb/" + 
getName()+".xml");

Added: 
activemq/sandbox/activemq-flow/activemq-broker/src/test/resources/org/apache/activemq/apollo/jaxb/testUris.xml
URL: 
http://svn.apache.org/viewvc/activemq/sandbox/activemq-flow/activemq-broker/src/test/resources/org/apache/activemq/apollo/jaxb/testUris.xml?rev=786017&view=auto
==============================================================================
--- 
activemq/sandbox/activemq-flow/activemq-broker/src/test/resources/org/apache/activemq/apollo/jaxb/testUris.xml
 (added)
+++ 
activemq/sandbox/activemq-flow/activemq-broker/src/test/resources/org/apache/activemq/apollo/jaxb/testUris.xml
 Thu Jun 18 11:14:40 2009
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+   
+    http://www.apache.org/licenses/LICENSE-2.0
+   
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<broker xmlns="http://activemq.apache.org/schema/activemq/apollo";>
+</broker>
\ No newline at end of file


Reply via email to