[jboss-cvs] JBossAS SVN: r86078 - in projects/jboss-deployers/trunk: deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/annotations and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 18 19:07:44 EDT 2009


Author: alesj
Date: 2009-03-18 19:07:44 -0400 (Wed, 18 Mar 2009)
New Revision: 86078

Added:
   projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SchemaHelper.java
Modified:
   projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractParsingDeployerWithOutput.java
   projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXBDeployer.java
   projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXPDeployer.java
   projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SecurityActions.java
   projects/jboss-deployers/trunk/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/annotations/ScanningMetaDataDeployer.java
   projects/jboss-deployers/trunk/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/deployer/validate/test/DeployersValidateInputTestCase.java
Log:
[JBDEPLOY-177, JBDEPLOY-178, JBDEPLOY-179, JBDEPLOY-180, JBDEPLOY-181];
expose attachment key, add schema validation, add validation event handler, use VFSInputSource.

Modified: projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractParsingDeployerWithOutput.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractParsingDeployerWithOutput.java	2009-03-18 21:52:30 UTC (rev 86077)
+++ projects/jboss-deployers/trunk/deployers-spi/src/main/java/org/jboss/deployers/spi/deployer/helpers/AbstractParsingDeployerWithOutput.java	2009-03-18 23:07:44 UTC (rev 86078)
@@ -49,6 +49,9 @@
    /** The jar extension */
    private String jarExtension;
 
+   /** The attachment key */
+   private String attachmentKey;
+
    /** Include the deployment file */
    private boolean includeDeploymentFile = false;
 
@@ -66,6 +69,7 @@
       if (output == null)
          throw new IllegalArgumentException("Null output");
       setOutput(output);
+      setAttachmentKey(output.getName());
    }
    
    @SuppressWarnings("unchecked")
@@ -98,10 +102,10 @@
     */
    public void setName(String name)
    {
-      if (names != null)
-         throw new IllegalArgumentException("Names already set.");
-
-      this.names = Collections.singleton(name);
+      if (name != null)
+         names = Collections.singleton(name);
+      else
+         names = null;
    }
 
    /**
@@ -255,8 +259,29 @@
    {
       return unit.getAttachment(key, getOutput());
    }
-   
+
    /**
+    * Get attachment key.
+    * By default it's output's fqn classname.
+    *
+    * @return the attachment key.
+    */
+   protected String getAttachmentKey()
+   {
+      return attachmentKey;
+   }
+
+   /**
+    * Set attachment key.
+    *
+    * @param attachmentKey the attachment key
+    */
+   public void setAttachmentKey(String attachmentKey)
+   {
+      this.attachmentKey = attachmentKey;
+   }
+
+   /**
     * Create some meta data. Calls createMetaData(unit, name, suffix, getDeploymentType().getName()).
     * 
     * @param unit the deployment unit
@@ -266,7 +291,7 @@
     */
    protected void createMetaData(DeploymentUnit unit, String name, String suffix) throws DeploymentException
    {
-      createMetaData(unit, name, suffix, getOutput().getName());
+      createMetaData(unit, name, suffix, getAttachmentKey());
    }
    
    /**
@@ -279,7 +304,7 @@
     */
    protected void createMetaData(DeploymentUnit unit, Set<String> names, String suffix) throws DeploymentException
    {
-      createMetaData(unit, names, suffix, getOutput().getName());
+      createMetaData(unit, names, suffix, getAttachmentKey());
    }
 
    /**

Modified: projects/jboss-deployers/trunk/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/annotations/ScanningMetaDataDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/annotations/ScanningMetaDataDeployer.java	2009-03-18 21:52:30 UTC (rev 86077)
+++ projects/jboss-deployers/trunk/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/annotations/ScanningMetaDataDeployer.java	2009-03-18 23:07:44 UTC (rev 86078)
@@ -22,9 +22,7 @@
 package org.jboss.deployers.vfs.plugins.annotations;
 
 import org.jboss.deployers.plugins.annotations.AbstractScanningMetaData;
-import org.jboss.deployers.spi.DeploymentException;
 import org.jboss.deployers.spi.annotations.ScanningMetaData;
-import org.jboss.deployers.structure.spi.DeploymentUnit;
 import org.jboss.deployers.vfs.spi.deployer.SchemaResolverDeployer;
 
 /**
@@ -40,11 +38,6 @@
       setName("jboss-scanning.xml");
       setRegisterWithJBossXB(true);
       addOutput(ScanningMetaData.class);
+      setAttachmentKey(ScanningMetaData.class.getName());
    }
-
-   @Override
-   protected void createMetaData(DeploymentUnit unit, String name, String suffix) throws DeploymentException
-   {
-      createMetaData(unit, name, suffix, ScanningMetaData.class.getName());
-   }
 }

Modified: projects/jboss-deployers/trunk/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/deployer/validate/test/DeployersValidateInputTestCase.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/deployer/validate/test/DeployersValidateInputTestCase.java	2009-03-18 21:52:30 UTC (rev 86077)
+++ projects/jboss-deployers/trunk/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/deployer/validate/test/DeployersValidateInputTestCase.java	2009-03-18 23:07:44 UTC (rev 86078)
@@ -22,6 +22,8 @@
 package org.jboss.test.deployers.vfs.deployer.validate.test;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 import junit.framework.Test;
 import org.jboss.deployers.spi.DeploymentException;
@@ -61,20 +63,17 @@
       TestXmlDeployer xmlDeployer = new TestXmlDeployer();
       xmlDeployer.create();
 
-      AbstractVFSParsingDeployer<?>[] deployers = new AbstractVFSParsingDeployer<?>[]
-            {
-                  new Properties2BeansDeployer(),
-                  new MockBshDeployer(),
-//                  new TestJaxbDeployer(),
-                  xmlDeployer,
-                  new SchemaResolverDeployer<Object>(Object.class),
-            };
+      Map<AbstractVFSParsingDeployer<?>, Class<? extends Exception>> map = new HashMap<AbstractVFSParsingDeployer<?>, Class<? extends Exception>>();
+      map.put(new Properties2BeansDeployer(), IOException.class);
+      map.put(new MockBshDeployer(), IOException.class);
+      map.put(xmlDeployer, RuntimeException.class);
+      map.put(new SchemaResolverDeployer<Object>(Object.class), JBossXBException.class);
 
       VirtualFile root = new MyVirtualFile();
       AbstractVFSDeploymentContext context = new MyVFSDeploymentContext(root, "");
       DeploymentUnit unit = context.getDeploymentUnit();
 
-      for(AbstractVFSParsingDeployer<?> deployer : deployers)
+      for(AbstractVFSParsingDeployer<?> deployer : map.keySet())
       {
          // set name to "" to match in deployment
          deployer.setName("");
@@ -87,8 +86,10 @@
          {
             assertInstanceOf(e, DeploymentException.class);
             Throwable cause = e.getCause();
-            if (IOException.class.isInstance(cause) == false && JBossXBException.class.isInstance(cause) == false)
+            if (map.get(deployer).isInstance(cause) == false)
+            {
                fail("Illegal exception cause: " + cause);
+            }
          }
       }
    }

Modified: projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXBDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXBDeployer.java	2009-03-18 21:52:30 UTC (rev 86077)
+++ projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXBDeployer.java	2009-03-18 23:07:44 UTC (rev 86078)
@@ -21,34 +21,43 @@
 */
 package org.jboss.deployers.vfs.spi.deployer;
 
-import java.io.InputStream;
 import java.util.Map;
-
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.ValidationEventHandler;
+import javax.xml.bind.helpers.DefaultValidationEventHandler;
+import javax.xml.validation.Schema;
 
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
+import org.jboss.virtual.VFSInputSource;
 import org.jboss.virtual.VirtualFile;
 import org.xml.sax.InputSource;
 
 /**
  * JAXBDeployer.
- * 
- * @param <T> the expected type 
+ *
+ * @param <T> the expected type
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public abstract class JAXBDeployer<T> extends AbstractVFSParsingDeployer<T>
 {
-   /** The JAXBContext */ 
+   /** The JAXBContext */
    private JAXBContext context;
 
    /** The properties */
    private Map<String, Object> properties;
-   
+
+   /** The schema location */
+   private String schemaLocation;
+
+   /** The validation event handler */
+   private ValidationEventHandler validationEventHandler = new DefaultValidationEventHandler();
+
    /**
     * Create a new JAXBDeployer.
-    * 
+    *
     * @param output the output
     * @throws IllegalArgumentException for a null output
     */
@@ -59,7 +68,7 @@
 
    /**
     * Get the properties.
-    * 
+    *
     * @return the properties.
     */
    public Map<String, Object> getProperties()
@@ -69,7 +78,7 @@
 
    /**
     * Set the properties.
-    * 
+    *
     * @param properties the properties.
     */
    public void setProperties(Map<String, Object> properties)
@@ -78,47 +87,83 @@
    }
 
    /**
+    * Set schema location.
+    *
+    * @param schemaLocation the schema location
+    */
+   public void setSchemaLocation(String schemaLocation)
+   {
+      this.schemaLocation = schemaLocation;
+   }
+
+   /**
+    * Set the validation event handler.
+    *
+    * @param validationEventHandler the validation event handler
+    */
+   public void setValidationEventHandler(ValidationEventHandler validationEventHandler)
+   {
+      this.validationEventHandler = validationEventHandler;
+   }
+
+   /**
     * Create lifecycle
-    * 
+    *
     * @throws Exception for any problem
     */
    public void create() throws Exception
    {
+      context = createContext();
+   }
+
+   /**
+    * Create context.
+    *
+    * @return new context instance
+    * @throws Exception for any error
+    */
+   protected JAXBContext createContext() throws Exception
+   {
       if (properties != null)
-         context = JAXBContext.newInstance(new Class[] { getOutput() }, properties);
+         return JAXBContext.newInstance(classesToBeBound(), properties);
       else
-         context = JAXBContext.newInstance(getOutput());
+         return JAXBContext.newInstance(classesToBeBound());
    }
 
    /**
+    * Get classes to be bound.
+    *
+    * @return the classes to be bound
+    */
+   protected Class<?>[] classesToBeBound()
+   {
+      return new Class<?>[]{getOutput()};
+   }
+
+   /**
     * Destroy lifecycle
     */
    public void destroy()
    {
       context = null;
    }
-   
+
    @Override
    protected T parse(VFSDeploymentUnit unit, VirtualFile file, T root) throws Exception
    {
+      if (file == null)
+         throw new IllegalArgumentException("Null file");
+
+      log.debug("Parsing: " + file.getName());
+
       Unmarshaller unmarshaller = context.createUnmarshaller();
-      InputStream is = openStreamAndValidate(file);
-      try
-      {
-         InputSource source = new InputSource(is);
-         source.setSystemId(file.toURI().toString());
-         Object o = unmarshaller.unmarshal(source);
-         return getOutput().cast(o);
-      }
-      finally
-      {
-         try
-         {
-            is.close();
-         }
-         catch (Exception ignored)
-         {
-         }
-      }
+      unmarshaller.setEventHandler(validationEventHandler);
+      Schema schema = SchemaHelper.getSchema(schemaLocation);
+      if (schema != null)
+         unmarshaller.setSchema(schema);
+
+      InputSource source = new VFSInputSource(file);
+      Object result = unmarshaller.unmarshal(source);
+      return getOutput().cast(result);
    }
 }

Modified: projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXPDeployer.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXPDeployer.java	2009-03-18 21:52:30 UTC (rev 86077)
+++ projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXPDeployer.java	2009-03-18 23:07:44 UTC (rev 86078)
@@ -21,13 +21,13 @@
 */
 package org.jboss.deployers.vfs.spi.deployer;
 
-import java.io.InputStream;
-
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.validation.Schema;
 
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
 import org.jboss.util.xml.JBossEntityResolver;
+import org.jboss.virtual.VFSInputSource;
 import org.jboss.virtual.VirtualFile;
 import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
@@ -55,7 +55,10 @@
    
    /** The document builder factory */
    private DocumentBuilderFactory documentBuilderFactory;
-   
+
+   /** The schema location */
+   private String schemaLocation;
+
    /**
     * Create a new JAXPDeployer.
     * 
@@ -108,6 +111,16 @@
    }
 
    /**
+    * Set schema location.
+    *
+    * @param schemaLocation the schema location
+    */
+   public void setSchemaLocation(String schemaLocation)
+   {
+      this.schemaLocation = schemaLocation;
+   }
+
+   /**
     * Get the documentBuilderFactory.
     * 
     * @return the documentBuilderFactory.
@@ -117,6 +130,7 @@
    {
       if (documentBuilderFactory == null)
          throw new IllegalStateException("Document builder factory has not been constructed");
+
       return documentBuilderFactory;
    }
 
@@ -130,6 +144,11 @@
       documentBuilderFactory = DocumentBuilderFactory.newInstance();
       documentBuilderFactory.setNamespaceAware(useNamespaceAwareParser);
       documentBuilderFactory.setValidating(validateDTDs);
+      Schema schema = SchemaHelper.getSchema(schemaLocation);
+      if (schema != null)
+      {
+         documentBuilderFactory.setSchema(schema);
+      }
    }
 
    /**
@@ -186,25 +205,11 @@
 
       log.debug("Parsing: " + file.getName());
       
-      DocumentBuilder parser = getDocumentBuilderFactory().newDocumentBuilder();
-      InputStream is = openStreamAndValidate(file);
-      try
-      {
-         InputSource source = new InputSource(is);
-         source.setSystemId(file.toURI().toString());
-         parser.setEntityResolver(new JBossEntityResolver());
-         return parser.parse(source);
-      }
-      finally
-      {
-         try
-         {
-            is.close();
-         }
-         catch (Exception ignored)
-         {
-         }
-      }
+      InputSource source = new VFSInputSource(file);
+      DocumentBuilderFactory factory = getDocumentBuilderFactory();
+      DocumentBuilder parser = factory.newDocumentBuilder();
+      parser.setEntityResolver(new JBossEntityResolver());
+      return parser.parse(source);
    }
 
    /**

Copied: projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SchemaHelper.java (from rev 85889, projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/JAXBDeployer.java)
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SchemaHelper.java	                        (rev 0)
+++ projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SchemaHelper.java	2009-03-18 23:07:44 UTC (rev 86078)
@@ -0,0 +1,58 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.deployers.vfs.spi.deployer;
+
+import java.net.URL;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import javax.xml.XMLConstants;
+
+import org.xml.sax.SAXException;
+
+/**
+ * Handle Schema.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class SchemaHelper
+{
+   /**
+    * Get schema.
+    *
+    * @param schemaLocation the schema location
+    * @return schema or null if schema location is null
+    * @throws SAXException for any error
+    */
+   static Schema getSchema(String schemaLocation) throws SAXException
+   {
+      if (schemaLocation == null)
+         return null;
+      
+      ClassLoader tcl = SecurityActions.getContextClassLoader();
+      URL schemaURL = tcl.getResource(schemaLocation);
+      if(schemaURL == null)
+         throw new IllegalStateException("Schema URL is null:" + schemaLocation);
+
+      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+      return factory.newSchema(schemaURL);
+   }
+}
\ No newline at end of file

Modified: projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SecurityActions.java
===================================================================
--- projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SecurityActions.java	2009-03-18 21:52:30 UTC (rev 86077)
+++ projects/jboss-deployers/trunk/deployers-vfs-spi/src/main/java/org/jboss/deployers/vfs/spi/deployer/SecurityActions.java	2009-03-18 23:07:44 UTC (rev 86078)
@@ -27,11 +27,13 @@
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
+import java.security.PrivilegedAction;
 
 import org.jboss.virtual.VirtualFile;
 
 /**
  * @author Scott.Stark at jboss.org
+ * @author Ales.Justin at jboss.org
  * @version $Revision: 60921 $
  */
 public class SecurityActions
@@ -87,4 +89,26 @@
       else
          return FileActions.NON_PRIVILEGED.openStream(f);
    }
+
+   static ClassLoader getContextClassLoader()
+   {
+      if (System.getSecurityManager() == null)
+      {
+         return Thread.currentThread().getContextClassLoader();
+      }
+      else
+      {
+         return AccessController.doPrivileged(GetContextClassLoader.INSTANCE);
+      }
+   }
+
+   static class GetContextClassLoader implements PrivilegedAction<ClassLoader>
+   {
+      static GetContextClassLoader INSTANCE = new GetContextClassLoader();
+
+      public ClassLoader run()
+      {
+         return Thread.currentThread().getContextClassLoader();
+      }
+   }
 }




More information about the jboss-cvs-commits mailing list