tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r831467 - in /tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor: DefaultValidatingXMLInputFactory.java ExtensibleStAXArtifactProcessor.java
Date Fri, 30 Oct 2009 22:08:26 GMT
Author: rfeng
Date: Fri Oct 30 22:08:25 2009
New Revision: 831467

URL: http://svn.apache.org/viewvc?rev=831467&view=rev
Log:
Set TCCL for SchemaFactory

Modified:
    tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java
    tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java

Modified: tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java?rev=831467&r1=831466&r2=831467&view=diff
==============================================================================
--- tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java
(original)
+++ tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/DefaultValidatingXMLInputFactory.java
Fri Oct 30 22:08:25 2009
@@ -39,6 +39,7 @@
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
 import javax.xml.stream.EventFilter;
 import javax.xml.stream.StreamFilter;
 import javax.xml.stream.XMLEventReader;
@@ -49,6 +50,7 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.util.XMLEventAllocator;
 import javax.xml.transform.Source;
+import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
@@ -56,9 +58,11 @@
 import org.apache.tuscany.sca.assembly.xsd.Constants;
 import org.apache.tuscany.sca.common.xml.XMLDocumentHelper;
 import org.apache.tuscany.sca.common.xml.stax.StAXHelper;
+import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.core.FactoryExtensionPoint;
 import org.apache.tuscany.sca.core.UtilityExtensionPoint;
+import org.apache.tuscany.sca.extensibility.ClassLoaderContext;
 import org.apache.tuscany.sca.monitor.Monitor;
 import org.apache.tuscany.sca.monitor.MonitorFactory;
 import org.w3c.dom.DOMImplementation;
@@ -75,7 +79,7 @@
  * @version $Rev$ $Date$
  */
 public class DefaultValidatingXMLInputFactory extends ValidatingXMLInputFactory implements
LSResourceResolver {
-
+    private ExtensionPointRegistry registry;
     private XMLInputFactory inputFactory;
     private DocumentBuilderFactory documentBuilderFactory;
     private DOMImplementationLS ls;
@@ -87,6 +91,7 @@
     private StAXHelper helper;
 
     public DefaultValidatingXMLInputFactory(ExtensionPointRegistry registry) {
+        this.registry = registry;
         FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class);
         this.inputFactory = factoryExtensionPoint.getFactory(XMLInputFactory.class);
         this.documentBuilderFactory = factoryExtensionPoint.getFactory(DocumentBuilderFactory.class);
@@ -102,9 +107,11 @@
      * @param inputFactory
      * @param schemas
      */
+    // FOR Test only
     public DefaultValidatingXMLInputFactory(XMLInputFactory inputFactory, ValidationSchemaExtensionPoint
schemas) {
         this.inputFactory = inputFactory;
         this.schemas = schemas;
+        this.registry = new DefaultExtensionPointRegistry();
     }
     
 
@@ -188,8 +195,7 @@
             }
             final Collection<? extends Source> sources = aggregate(urls);         
  
 
-            // Create an aggregated validation schemas from all the XSDs
-            final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+            final SchemaFactory schemaFactory = newSchemaFactory();
             DOMImplementation impl = null;
             try {
                 impl = documentBuilderFactory.newDocumentBuilder().getDOMImplementation();
@@ -225,6 +231,30 @@
         }
     }
 
+    /**
+     * For OSGi:
+     * Create a SchemaFactory in the context of service provider classloaders
+     * @return
+     */
+    private SchemaFactory newSchemaFactory() {
+        ClassLoader cl =
+            ClassLoaderContext.setContextClassLoader(getClass().getClassLoader(),
+                                                     registry.getServiceDiscovery(),
+                                                     SchemaFactory.class,
+                                                     TransformerFactory.class,
+                                                     SAXParserFactory.class,
+                                                     DocumentBuilderFactory.class
+                                                     );
+        try {
+            // Create an aggregated validation schemas from all the XSDs
+            return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        } finally {
+            if (cl != null) {
+                Thread.currentThread().setContextClassLoader(cl);
+            }
+        }
+    }
+
     @Override
     public XMLEventReader createFilteredReader(XMLEventReader arg0, EventFilter arg1) throws
XMLStreamException {
         return inputFactory.createFilteredReader(arg0, arg1);

Modified: tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java?rev=831467&r1=831466&r2=831467&view=diff
==============================================================================
--- tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java
(original)
+++ tuscany/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/ExtensibleStAXArtifactProcessor.java
Fri Oct 30 22:08:25 2009
@@ -33,6 +33,7 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
+import org.apache.tuscany.sca.assembly.Extension;
 import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.core.FactoryExtensionPoint;
@@ -56,7 +57,6 @@
     private XMLInputFactory inputFactory;
     private XMLOutputFactory outputFactory;
     private StAXArtifactProcessorExtensionPoint processors;
-    
 
     /**
      * Constructs a new ExtensibleStAXArtifactProcessor.
@@ -172,7 +172,6 @@
 
     @SuppressWarnings("unchecked")
     public void write(Object model, XMLStreamWriter outputSource, ProcessorContext context)
throws ContributionWriteException, XMLStreamException {
-        Monitor monitor = context.getMonitor();
         // Delegate to the processor associated with the model type
         if (model != null) {
             StAXArtifactProcessor processor = processors.getProcessor(model.getClass());
@@ -182,11 +181,8 @@
                 if (logger.isLoggable(Level.WARNING)) {
                     logger.warning("No StAX processor is configured to handle " + model.getClass());
                 }
-                if (!XMLStreamReader.class.isInstance(model)) {
-                    warning(monitor, "NoStaxProcessor", processors, model.getClass());
-                }
                 StAXArtifactProcessor anyElementProcessor = processors.getProcessor(ANY_ELEMENT);
-                if (anyElementProcessor != null) {
+                if ((model instanceof Extension) && anyElementProcessor != null)
{
                     anyElementProcessor.write(model, outputSource, context);
                 }
             }



Mime
View raw message