Author: tjwatson
Date: Wed Jul 16 20:58:56 2014
New Revision: 1611188

URL: http://svn.apache.org/r1611188
Log:
ARIES-1227 - Allow PerserService to parse successfully when namespace handlers 
are missing

Added:
    
aries/trunk/blueprint/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/ParserServiceIgnoreUnknownNamespaceHandlerTest.java
Modified:
    aries/trunk/blueprint/blueprint-core/pom.xml
    
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java
    
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java
    aries/trunk/blueprint/blueprint-parser/pom.xml
    
aries/trunk/blueprint/blueprint-parser/src/main/java/org/apache/aries/blueprint/parser/Parser.java

Modified: aries/trunk/blueprint/blueprint-core/pom.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/pom.xml?rev=1611188&r1=1611187&r2=1611188&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/pom.xml (original)
+++ aries/trunk/blueprint/blueprint-core/pom.xml Wed Jul 16 20:58:56 2014
@@ -91,7 +91,7 @@
         </aries.osgi.include.resource>
         
<blueprint.annotation.api.version>1.0.0</blueprint.annotation.api.version>
         <blueprint.api.version>1.0.0</blueprint.api.version>
-        <blueprint.parser.version>1.2.1</blueprint.parser.version>
+        <blueprint.parser.version>1.3.0-SNAPSHOT</blueprint.parser.version>
         <proxy.api.version>1.0.0</proxy.api.version>
         <proxy.impl.version>1.0.3</proxy.impl.version>
         <quiesce.api.version>1.0.0</quiesce.api.version>

Modified: 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java?rev=1611188&r1=1611187&r2=1611188&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java
 Wed Jul 16 20:58:56 2014
@@ -134,9 +134,11 @@ public class BlueprintExtender implement
         });
         proxyManager.open();
         
+        // Determine if the ParserService should ignore unknown namespace 
handlers
+        boolean ignoreUnknownNamespaceHandlers = 
Boolean.parseBoolean(ctx.getProperty("org.apache.aries.blueprint.parser.service.ignore.unknown.namespace.handlers"));
         // Create and publish a ParserService
         parserServiceReg = ctx.registerService(ParserService.class.getName(), 
-            new ParserServiceImpl (handlers), 
+            new ParserServiceImpl (handlers, ignoreUnknownNamespaceHandlers), 
             new Hashtable<String, Object>());
 
         // Create and publish a BlueprintContainerService

Modified: 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java?rev=1611188&r1=1611187&r2=1611188&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java
 Wed Jul 16 20:58:56 2014
@@ -34,13 +34,15 @@ import org.xml.sax.SAXException;
 
 public class ParserServiceImpl implements ParserService {
 
-       NamespaceHandlerRegistry _namespaceHandlerRegistry;
-       
-       public ParserServiceImpl (NamespaceHandlerRegistry nhr) { 
-               _namespaceHandlerRegistry = nhr;
-       }
-       
-       public ComponentDefinitionRegistry parse(URL url, Bundle clientBundle) 
throws Exception {
+       final NamespaceHandlerRegistry _namespaceHandlerRegistry;
+    final boolean _ignoreUnknownNamespaceHandlers;
+
+  public ParserServiceImpl (NamespaceHandlerRegistry nhr, boolean 
ignoreUnknownNamespaceHandlers) { 
+    _namespaceHandlerRegistry = nhr;
+    _ignoreUnknownNamespaceHandlers = ignoreUnknownNamespaceHandlers;
+  }
+
+  public ComponentDefinitionRegistry parse(URL url, Bundle clientBundle) 
throws Exception {
     return parse (url, clientBundle, false);
   }
 
@@ -56,7 +58,7 @@ public class ParserServiceImpl implement
   }
   
        public ComponentDefinitionRegistry parse(List<URL> urls, Bundle 
clientBundle, boolean validate) throws Exception {
-         Parser parser = new Parser();   
+         Parser parser = new Parser(null, _ignoreUnknownNamespaceHandlers);   
          parser.parse(urls);
          return validateAndPopulate (parser, clientBundle, validate);
        }
@@ -66,7 +68,7 @@ public class ParserServiceImpl implement
   }
   
   public ComponentDefinitionRegistry parse(InputStream is, Bundle 
clientBundle, boolean validate) throws Exception {
-    Parser parser = new Parser();
+    Parser parser = new Parser(null, _ignoreUnknownNamespaceHandlers);
     parser.parse(is);
     return validateAndPopulate (parser, clientBundle, validate);
   }

Added: 
aries/trunk/blueprint/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/ParserServiceIgnoreUnknownNamespaceHandlerTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/ParserServiceIgnoreUnknownNamespaceHandlerTest.java?rev=1611188&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/ParserServiceIgnoreUnknownNamespaceHandlerTest.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-itests/src/test/java/org/apache/aries/blueprint/itests/ParserServiceIgnoreUnknownNamespaceHandlerTest.java
 Wed Jul 16 20:58:56 2014
@@ -0,0 +1,93 @@
+/**
+ * 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.
+ */
+package org.apache.aries.blueprint.itests;
+
+import java.io.InputStream;
+import java.net.URL;
+
+import javax.inject.Inject;
+
+import org.apache.aries.blueprint.itests.cm.service.Foo;
+import org.apache.aries.blueprint.itests.cm.service.FooFactory;
+import org.apache.aries.blueprint.itests.cm.service.FooInterface;
+import org.apache.aries.blueprint.services.ParserService;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+import org.ops4j.pax.exam.CoreOptions;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.ProbeBuilder;
+import org.ops4j.pax.exam.TestProbeBuilder;
+import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+
+
+public class ParserServiceIgnoreUnknownNamespaceHandlerTest extends 
AbstractBlueprintIntegrationTest {
+       private static final String CM_BUNDLE = "org.apache.aries.blueprint.cm";
+       private static final String TEST_BUNDLE = 
"org.apache.aries.blueprint.cm.test.b1";
+       
+       @ProbeBuilder
+       public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
+               probe.setHeader(Constants.EXPORT_PACKAGE, 
Foo.class.getPackage().getName());
+       probe.setHeader(Constants.IMPORT_PACKAGE, 
Foo.class.getPackage().getName());
+               return probe;
+       }
+
+    @org.ops4j.pax.exam.Configuration
+    public Option[] config() {
+       InputStream testBundle = TinyBundles.bundle()
+               .add(FooInterface.class)
+               .add(Foo.class)
+               .add(FooFactory.class)
+               .add("OSGI-INF/blueprint/context.xml", 
+                               getResource("ManagedServiceFactoryTest.xml"))
+               .set(Constants.BUNDLE_SYMBOLICNAME, TEST_BUNDLE)
+               .set(Constants.EXPORT_PACKAGE, Foo.class.getPackage().getName())
+               .set(Constants.IMPORT_PACKAGE, Foo.class.getPackage().getName())
+               .build(TinyBundles.withBnd());
+       return new Option[] {
+                       baseOptions(),
+                
CoreOptions.frameworkProperty("org.apache.aries.blueprint.parser.service.ignore.unknown.namespace.handlers").value("true"),
+                       Helper.blueprintBundles(),
+                       CoreOptions.keepCaches(),
+                       CoreOptions.streamBundle(testBundle)
+       };
+    }
+
+       @Before
+       public void stopCM() throws BundleException {
+               context().getBundleByName(CM_BUNDLE).stop();
+       }
+
+       @After
+       public void startCM() throws BundleException {
+               context().getBundleByName(CM_BUNDLE).start();
+       }
+
+    @Test
+    public void testIgnoreTrue() throws Exception {
+        ParserService parserService = 
context().getService(ParserService.class);
+        URL blueprintXML = 
context().getBundleByName(TEST_BUNDLE).getEntry("OSGI-INF/blueprint/context.xml");
+        // ensure there is no error parsing while CM is stopped
+        parserService.parse(blueprintXML, 
context().getBundleByName(TEST_BUNDLE));
+    }
+}

Modified: aries/trunk/blueprint/blueprint-parser/pom.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-parser/pom.xml?rev=1611188&r1=1611187&r2=1611188&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-parser/pom.xml (original)
+++ aries/trunk/blueprint/blueprint-parser/pom.xml Wed Jul 16 20:58:56 2014
@@ -32,7 +32,7 @@
     <artifactId>blueprint-parser</artifactId>
     <packaging>jar</packaging>
     <name>Apache Aries Blueprint Parser</name>
-    <version>1.2.2-SNAPSHOT</version>
+    <version>1.3.0-SNAPSHOT</version>
     <description>
         This bundle contains the blueprint parser in a plain jar.
     </description>

Modified: 
aries/trunk/blueprint/blueprint-parser/src/main/java/org/apache/aries/blueprint/parser/Parser.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-parser/src/main/java/org/apache/aries/blueprint/parser/Parser.java?rev=1611188&r1=1611187&r2=1611188&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-parser/src/main/java/org/apache/aries/blueprint/parser/Parser.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-parser/src/main/java/org/apache/aries/blueprint/parser/Parser.java
 Wed Jul 16 20:58:56 2014
@@ -36,8 +36,10 @@ import javax.xml.parsers.DocumentBuilder
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.Validator;
+
 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
 import org.apache.aries.blueprint.NamespaceHandler;
+import org.apache.aries.blueprint.ParserContext;
 import org.apache.aries.blueprint.reflect.BeanArgumentImpl;
 import org.apache.aries.blueprint.reflect.BeanMetadataImpl;
 import org.apache.aries.blueprint.reflect.BeanPropertyImpl;
@@ -175,11 +177,31 @@ public class Parser {
     public static final String ACTIVATION_DEFAULT = ACTIVATION_EAGER;
     
     private static DocumentBuilderFactory documentBuilderFactory;
+    private static final NamespaceHandler missingNamespace = new 
NamespaceHandler() {
+        @Override
+        public Metadata parse(Element element, ParserContext context) {
+            return null;
+        }
+        @Override
+        public URL getSchemaLocation(String namespace) {
+            return null;
+        }
+        @Override
+        public Set<Class> getManagedClasses() {
+            return null;
+        }
+        @Override
+        public ComponentMetadata decorate(Node node, ComponentMetadata 
component,
+                ParserContext context) {
+            return component;
+        }
+    };
 
     private final List<Document> documents = new ArrayList<Document>();
     private ComponentDefinitionRegistry registry;
     private NamespaceHandlerSet handlers;
-    private String idPrefix = "component-";
+    private final String idPrefix;
+    private final boolean ignoreUnknownNamespaces;
     private final Set<String> ids = new HashSet<String>();
     private int idCounter;
     private String defaultTimeout;
@@ -187,10 +209,17 @@ public class Parser {
     private String defaultActivation;
     private Set<URI> namespaces;
 
-    public Parser() {}
+    public Parser() {
+      this(null);
+    }
 
     public Parser(String idPrefix) {
-        this.idPrefix = idPrefix;
+      this(idPrefix, false);
+    }
+
+    public Parser(String idPrefix, boolean ignoreUnknownNamespaces) {
+      this.idPrefix = idPrefix == null ? "component-" : idPrefix;
+      this.ignoreUnknownNamespaces = ignoreUnknownNamespaces;
     }
 
     /**
@@ -1290,6 +1319,9 @@ public class Parser {
         }
         NamespaceHandler handler = this.handlers.getNamespaceHandler(uri);
         if (handler == null) {
+            if (ignoreUnknownNamespaces) {
+                return missingNamespace;
+            }
             throw new ComponentDefinitionException("Unsupported node 
namespace: " + uri);
         }
         return handler;


Reply via email to