[jboss-cvs] JBossAS SVN: r67653 - in trunk/ejb3/src/main/org/jboss/ejb3: deployers and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Nov 30 05:47:32 EST 2007


Author: wolfc
Date: 2007-11-30 05:47:31 -0500 (Fri, 30 Nov 2007)
New Revision: 67653

Added:
   trunk/ejb3/src/main/org/jboss/ejb3/protocol/
   trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/
   trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/Handler.java
   trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarInputStream.java
   trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarURLConnection.java
Modified:
   trunk/ejb3/src/main/org/jboss/ejb3/deployers/PersistenceUnitParsingDeployer.java
Log:
EJBTHREE-1138: Created jarjar URL stream handler

Modified: trunk/ejb3/src/main/org/jboss/ejb3/deployers/PersistenceUnitParsingDeployer.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/deployers/PersistenceUnitParsingDeployer.java	2007-11-30 09:21:33 UTC (rev 67652)
+++ trunk/ejb3/src/main/org/jboss/ejb3/deployers/PersistenceUnitParsingDeployer.java	2007-11-30 10:47:31 UTC (rev 67653)
@@ -32,6 +32,7 @@
 import org.jboss.deployers.vfs.spi.deployer.AbstractVFSParsingDeployer;
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
 import org.jboss.ejb3.metadata.jpa.spec.PersistenceUnitsMetaData;
+import org.jboss.ejb3.protocol.jarjar.Handler;
 import org.jboss.logging.Logger;
 import org.jboss.virtual.VirtualFile;
 
@@ -51,6 +52,11 @@
 {
    private static final Logger log = Logger.getLogger(PersistenceUnitParsingDeployer.class);
    
+   static
+   {
+      Handler.init();
+   }
+   
    public PersistenceUnitParsingDeployer()
    {
       super(PersistenceUnitsMetaData.class);
@@ -90,7 +96,11 @@
    protected PersistenceUnitsMetaData parse(VFSDeploymentUnit unit, VirtualFile file, PersistenceUnitsMetaData root)
          throws Exception
    {
-      URL persistenceUnitRootUrl = file.getParent().getParent().toURL();
+      VirtualFile persistenceRoot = file.getParent().getParent();
+      //URL persistenceUnitRootUrl = persistenceRoot.toURL();
+      // FIXME: is this a supported hack?
+      URL persistenceUnitRootUrl = new URL("jarjar:" + persistenceRoot.getHandler().toURL());
+      
       URL persistenceXmlUrl = file.toURL();
       PersistenceUnitsMetaData metaData = new PersistenceUnitsMetaData(persistenceUnitRootUrl, PersistenceXmlLoader.deploy(persistenceXmlUrl, new HashMap<String, String>(),
             new EJB3DTDEntityResolver(), PersistenceUnitTransactionType.JTA));

Added: trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/Handler.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/Handler.java	                        (rev 0)
+++ trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/Handler.java	2007-11-30 10:47:31 UTC (rev 67653)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, 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.ejb3.protocol.jarjar;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ * The jarjar URL stream handler allows for directories within
+ * a jarjar to be served as a jar file.
+ * 
+ * So the URL spec becomes: jarjar:jar:file:myjar.jar!/somedirectory/
+ * 
+ * It's meant to be used by the PersistenceUnitDeploment to comply
+ * with javax.persistence.spi.PersistentenceUnitInfo.getPersistenceUnitRootUrl()
+ *
+ * @see javax.persistence.spi.PersistenceUnitInfo#getPersistenceUnitRootUrl()
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class Handler extends URLStreamHandler
+{
+   static
+   {
+      init();
+   }
+
+   public static void init()
+   {
+      String pkg = "org.jboss.ejb3.protocol";
+      String pkgs = System.getProperty("java.protocol.handler.pkgs");
+      if (pkgs == null || pkgs.trim().length() == 0)
+         System.setProperty("java.protocol.handler.pkgs", pkg);
+      else if (!pkgs.contains(pkg))
+      {
+         pkgs += "|" + pkg;
+         System.setProperty("java.protocol.handler.pkgs", pkgs);
+      }
+   }
+   
+   /* (non-Javadoc)
+    * @see java.net.URLStreamHandler#openConnection(java.net.URL)
+    */
+   @Override
+   protected URLConnection openConnection(URL u) throws IOException
+   {
+      return new JarJarURLConnection(u);
+   }
+
+}


Property changes on: trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/Handler.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarInputStream.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarInputStream.java	                        (rev 0)
+++ trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarInputStream.java	2007-11-30 10:47:31 UTC (rev 67653)
@@ -0,0 +1,172 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, 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.ejb3.protocol.jarjar;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import org.jboss.logging.Logger;
+
+/**
+ * Provide an input stream in jar format which is build up
+ * using a subset of a jar file.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class JarJarInputStream extends InputStream
+{
+   private static final Logger log = Logger.getLogger(JarJarInputStream.class);
+   
+   private PipedInputStream pipedInputStream;
+   private PipedOutputStream pipedOutputStream;
+   
+   private Thread writingThread;
+   
+   /**
+    * @param jarFile        the jar file to read from
+    * @param rootEntry      the root entry at which the new jar file starts
+    * @throws IOException
+    */
+   protected JarJarInputStream(final JarFile jarFile, final JarEntry rootEntry) throws IOException
+   {
+      this.pipedInputStream = new PipedInputStream();
+      this.pipedOutputStream = new PipedOutputStream(pipedInputStream);
+      
+      Manifest mf = jarFile.getManifest();
+      final JarOutputStream jos;
+      if(mf != null)
+         jos = new JarOutputStream(pipedOutputStream, mf);
+      else
+         jos = new JarOutputStream(pipedOutputStream);
+      
+      this.writingThread = new Thread()
+      {
+         @Override
+         public void run()
+         {
+            try
+            {
+               Enumeration<JarEntry> entries = jarFile.entries();
+               while(entries.hasMoreElements())
+               {
+                  JarEntry entry = entries.nextElement();
+                  if (isRelativeTo(rootEntry, entry))
+                  {
+                     InputStream in = jarFile.getInputStream(entry);
+                     try
+                     {
+                        JarEntry newEntry = createEntry(rootEntry, entry);
+                        jos.putNextEntry(newEntry);
+
+                        copy(in, jos, entry.getSize());
+                        log.trace("emitted " + newEntry);
+
+                        jos.closeEntry();
+                     }
+                     finally
+                     {
+                        in.close();
+                     }
+                  }
+               }
+               jos.flush();
+               jos.finish();
+            }
+            catch(IOException e)
+            {
+               // FIXME: How to handle this?
+               log.warn(e.getMessage(), e);
+            }
+         }
+      };
+      writingThread.start();
+   }
+
+   @Override
+   public void close() throws IOException
+   {
+      pipedOutputStream.close();
+      pipedInputStream.close();
+      // TODO: rejoin the thread?
+      // writingThread.interrupt();
+      // writingThread.join(5000);
+      super.close();
+   }
+   
+   protected void copy(InputStream in, OutputStream out, long size) throws IOException
+   {
+      byte buf[] = new byte[65536];
+      
+      while(size > 0)
+      {
+         int len = in.read(buf);
+         if(len < 0)
+            throw new EOFException("Unexpected EOF");
+         out.write(buf, 0, len);
+         size -= len;
+      }
+   }
+   
+   protected JarEntry createEntry(JarEntry rootEntry, JarEntry template)
+   {
+      if(rootEntry == null)
+         return new JarEntry(template);
+      
+      String name = template.getName().substring(rootEntry.getName().length());
+      JarEntry entry = new JarEntry(name);
+      entry.setComment(template.getComment());
+      entry.setSize(template.getSize());
+      entry.setTime(template.getTime());
+      return entry;
+   }
+   
+   protected boolean isRelativeTo(JarEntry rootEntry, JarEntry current)
+   {
+      if(rootEntry == null)
+         return true;
+      
+      if(current.getName().startsWith(rootEntry.getName()) && !current.getName().equals(rootEntry.getName()))
+         return true;
+      
+      return false;
+   }
+   
+   /* (non-Javadoc)
+    * @see java.io.InputStream#read()
+    */
+   @Override
+   public int read() throws IOException
+   {
+      return pipedInputStream.read();
+   }
+
+}


Property changes on: trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarInputStream.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Added: trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarURLConnection.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarURLConnection.java	                        (rev 0)
+++ trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarURLConnection.java	2007-11-30 10:47:31 UTC (rev 67653)
@@ -0,0 +1,84 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, 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.ejb3.protocol.jarjar;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import org.jboss.logging.Logger;
+
+/**
+ * Connect to a jarjar.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class JarJarURLConnection extends URLConnection
+{
+   private static final Logger log = Logger.getLogger(JarJarURLConnection.class);
+   
+   private URL delegate;
+   private JarFile jarFile;
+   private JarEntry rootEntry;
+   
+   protected JarJarURLConnection(URL url)
+   {
+      super(url);
+   }
+
+   @Override
+   public void connect() throws IOException
+   {
+      if(connected) return;
+      
+      delegate = new URL(getURL().toString().substring(7));
+      log.debug("delegate " + delegate);
+      JarURLConnection conn = (JarURLConnection) delegate.openConnection();
+      this.jarFile = conn.getJarFile();
+      log.trace("jar file " + jarFile);
+      this.rootEntry = conn.getJarEntry();
+      log.trace("root entry " + rootEntry);
+      
+      connected = true;
+   }
+
+   @Override
+   public InputStream getInputStream() throws IOException
+   {
+      if(!connected) connect();
+      
+      String spec = delegate.toString();
+      URL jarFileURL;
+      if(rootEntry != null)
+         jarFileURL = new URL(spec.substring(0, spec.length() - rootEntry.getName().length()));
+      else
+         jarFileURL = delegate;
+      log.debug("jar file url " + jarFileURL);
+      
+      return new JarJarInputStream(jarFile, rootEntry);
+   }
+}


Property changes on: trunk/ejb3/src/main/org/jboss/ejb3/protocol/jarjar/JarJarURLConnection.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native




More information about the jboss-cvs-commits mailing list