Author: gnodet
Date: Sun Jan 13 09:54:55 2008
New Revision: 611610

URL: http://svn.apache.org/viewvc?rev=611610&view=rev
Log:
SMX4NMR-5: write a jbi protocol url handler to install the jbi artifact, patch 
provided by Jeff Yu

Added:
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Connection.java
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/JBIDeploymentListener.java
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Transformer.java
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/URLHandler.java
Removed:
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/JBIDeploymentListener.java
Modified:
    servicemix/smx4/nmr/trunk/bundles/   (props changed)
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/FileUtil.java
    
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml

Propchange: servicemix/smx4/nmr/trunk/bundles/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Jan 13 09:54:55 2008
@@ -0,0 +1,7 @@
+target
+*.iml
+*.ipr
+*.iws
+.classpath
+.project
+.settings

Added: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Connection.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Connection.java?rev=611610&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Connection.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Connection.java
 Sun Jan 13 09:54:55 2008
@@ -0,0 +1,72 @@
+/**
+ *
+ * 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.servicemix.jbi.deployer.handler;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.jbi.deployer.impl.FileUtil;
+
+
+public class Connection extends URLConnection {
+       
+       private static Log logger = LogFactory.getLog(Connection.class);
+       
+       private URLHandler urlHandler;
+       
+       public Connection(URL url, URLHandler urlHandler) {
+               super(url);
+               this.urlHandler = urlHandler;
+       }
+
+
+       @Override
+       public void connect() throws IOException {
+               
+       }
+       
+       @Override
+       public InputStream getInputStream() throws IOException {
+               try {
+                       connect();
+                       InputStream targetInputStream = 
urlHandler.getJbiArtifactURL().openConnection().getInputStream();
+                       File jbiZipFile = File.createTempFile("jbi", ".zip");
+                       FileOutputStream jbiZip = new 
FileOutputStream(jbiZipFile);
+                       
+                       FileUtil.copyInputStream(targetInputStream, jbiZip);
+                       jbiZip.close();
+                       targetInputStream.close();
+                       
+                       File jbiBundle = File.createTempFile("jbi", ".jar");
+                       Transformer.transformToOSGiBundle(jbiZipFile, 
jbiBundle);
+                       return new FileInputStream(jbiBundle);
+               } catch (Exception e) {
+                       logger.error("Error opening jbi protocol artifact", e);
+                       throw (IOException) new IOException("Error opening jbi 
protocol artifact").initCause(e);
+               }
+       }
+       
+
+}

Added: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/JBIDeploymentListener.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/JBIDeploymentListener.java?rev=611610&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/JBIDeploymentListener.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/JBIDeploymentListener.java
 Sun Jan 13 09:54:55 2008
@@ -0,0 +1,78 @@
+/*
+ * 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.servicemix.jbi.deployer.handler;
+
+import java.io.File;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.servicemix.jbi.deployer.impl.FileUtil;
+import org.apache.servicemix.runtime.filemonitor.DeploymentListener;
+
+
+public class JBIDeploymentListener implements DeploymentListener {
+       
+       private static final Log Logger = 
LogFactory.getLog(JBIDeploymentListener.class);
+       
+       public boolean canHandle(File artifact) {
+               try {
+            // Accept jars and zips
+            if (!artifact.getName().endsWith(".zip") &&
+                !artifact.getName().endsWith(".jar")) {
+                return false;
+            }
+                       JarFile jar = new JarFile(artifact);
+                       JarEntry entry = jar.getJarEntry("META-INF/jbi.xml");
+            // Only handle JBI artifacts
+            if (entry == null) {
+                               return false;
+                       }
+            // Only handle non OSGi bundles
+            Manifest m = jar.getManifest();
+            if (m.getMainAttributes().getValue(new 
Attributes.Name("Bundle-SymbolicName")) != null &&
+                m.getMainAttributes().getValue(new 
Attributes.Name("Bundle-Version")) != null) {
+                return false;
+            }
+            return true;
+               } catch (Exception e) {
+                       return false;
+               }
+       }
+
+       
+       public File handle(File artifact, File tmpDir) {
+               try{
+                       String bundleName = artifact.getName().substring(0, 
artifact.getName().length() -4 ) + ".jar";
+                       File destFile = new File(tmpDir, bundleName);
+                       if (destFile.exists()) {
+                               destFile.delete();
+                       }                       
+                       Transformer.transformToOSGiBundle(artifact, destFile);
+                       return destFile;
+                       
+               } catch (Exception e) {
+                       Logger.error("Failed in transforming the JBI artifact 
to be OSGified. error is: " + e);
+                       return null;
+               }
+       }
+
+
+}

Added: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Transformer.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Transformer.java?rev=611610&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Transformer.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/Transformer.java
 Sun Jan 13 09:54:55 2008
@@ -0,0 +1,75 @@
+/*
+ * 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.servicemix.jbi.deployer.handler;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import org.apache.servicemix.jbi.deployer.descriptor.Descriptor;
+import org.apache.servicemix.jbi.deployer.descriptor.DescriptorFactory;
+import org.apache.servicemix.jbi.deployer.impl.FileUtil;
+
+/**
+ * Helper class to transform JBI artifacts into OSGi bundles
+ */
+public class Transformer {
+
+    public static void transformToOSGiBundle(File jbiArtifact, File jbiBundle) 
throws Exception {
+       JarFile jar = new JarFile(jbiArtifact);
+        Manifest m = jar.getManifest();
+        JarEntry jarEntry = jar.getJarEntry("META-INF/jbi.xml");
+        InputStream is = jar.getInputStream(jarEntry);
+        Descriptor desc = DescriptorFactory.buildDescriptor(is);
+
+        String version = 
m.getMainAttributes().getValue("Implementation-Version");
+        String name = m.getMainAttributes().getValue("Implementation-Title");
+
+        if (desc.getComponent() != null) {
+            name = desc.getComponent().getIdentification().getName();
+        } else if (desc.getSharedLibrary() != null) {
+               name = desc.getSharedLibrary().getIdentification().getName();
+        } else if (desc.getServiceAssembly() != null) {
+               name = desc.getServiceAssembly().getIdentification().getName();
+        }
+
+        m.getMainAttributes().put(new Attributes.Name("Bundle-SymbolicName"), 
name);
+        m.getMainAttributes().put(new Attributes.Name("Bundle-Version"), 
version);
+
+               JarInputStream jis = new JarInputStream(new 
FileInputStream(jbiArtifact));
+               JarOutputStream jos = new JarOutputStream(new 
FileOutputStream(jbiBundle), m);
+
+               JarEntry entry = jis.getNextJarEntry();
+               while (entry != null) {
+                   jos.putNextEntry(entry);
+                   FileUtil.copyInputStream(jis, jos);
+                   jos.closeEntry();
+                   entry = jis.getNextJarEntry();
+               }
+
+               jos.close();
+               jis.close();
+    }
+
+}

Added: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/URLHandler.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/URLHandler.java?rev=611610&view=auto
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/URLHandler.java
 (added)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/handler/URLHandler.java
 Sun Jan 13 09:54:55 2008
@@ -0,0 +1,53 @@
+/**
+ *
+ * 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.servicemix.jbi.deployer.handler;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.osgi.service.url.AbstractURLStreamHandlerService;
+
+
+public class URLHandler extends AbstractURLStreamHandlerService {
+       
+       private static Log logger = LogFactory.getLog(URLHandler.class);
+       
+       private static String SYNTAX = "jbi: jbi-jar-uri";
+       
+       private URL jbiArtifactURL;
+       
+       @Override
+       public URLConnection openConnection(URL url) throws IOException {
+               if (url.getPath() == null || url.getPath().trim().length() == 
0) {
+                       throw new MalformedURLException ("Path can not be null 
or empty. Syntax: " + SYNTAX );
+               }
+               jbiArtifactURL = new URL(url.getPath());
+               
+               logger.debug("JBI artifact URL is: [" + jbiArtifactURL + "]");
+               return new Connection(url, this);
+       }
+       
+       public URL getJbiArtifactURL() {
+               return jbiArtifactURL;
+       }
+
+}

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/FileUtil.java
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/FileUtil.java?rev=611610&r1=611609&r2=611610&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/FileUtil.java
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/impl/FileUtil.java
 Sun Jan 13 09:54:55 2008
@@ -25,11 +25,17 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
-import java.util.Enumeration;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
 import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
-import java.util.zip.ZipOutputStream;
+
+import org.apache.servicemix.jbi.deployer.descriptor.Descriptor;
+import org.apache.servicemix.jbi.deployer.descriptor.DescriptorFactory;
 
 /**
  * File utilities
@@ -71,7 +77,6 @@
             out.write(buffer, 0, len);
             len = in.read(buffer);
         }
-        out.close();
     }
 
     /**
@@ -132,7 +137,9 @@
                 throw new IOException("Could not create directory: " + 
file.getParentFile());
             }
             if (!entry.isDirectory()) {
-                copyInputStream(zip, new BufferedOutputStream(new 
FileOutputStream(file)));
+               BufferedOutputStream bos = new BufferedOutputStream(new 
FileOutputStream(file)); 
+                copyInputStream(zip, bos);
+                bos.close();
             } else {
                 if (!buildDirectory(file)) {
                     throw new IOException("Could not create directory: " + 
file);

Modified: 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml
URL: 
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml?rev=611610&r1=611609&r2=611610&view=diff
==============================================================================
--- 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml
 (original)
+++ 
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/resources/META-INF/spring/servicemix-jbi-deployer.xml
 Sun Jan 13 09:54:55 2008
@@ -34,7 +34,16 @@
     </bean>
 
     <!-- Deployment listener -->
-    <bean id="listener" 
class="org.apache.servicemix.jbi.deployer.impl.JBIDeploymentListener" />
+    <bean id="listener" 
class="org.apache.servicemix.jbi.deployer.handler.JBIDeploymentListener" />
+    
+    <!-- JBI Handler -->
+    <bean id="jbiHandler" 
class="org.apache.servicemix.jbi.deployer.handler.URLHandler" />
+
+    <osgi:service ref="jbiHandler" 
interface="org.osgi.service.url.URLStreamHandlerService">
+       <osgi:service-properties>
+            <entry key="url.handler.protocol" value="jbi"/>
+        </osgi:service-properties>
+    </osgi:service>
 
     <osgi:service ref="listener" 
interface="org.apache.servicemix.runtime.filemonitor.DeploymentListener" />
 


Reply via email to