FELIX-2047: Support for WAR files

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@906878 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/assembly/pom.xml b/karaf/assembly/pom.xml
index cdf13cc..3c0b1bd 100644
--- a/karaf/assembly/pom.xml
+++ b/karaf/assembly/pom.xml
@@ -68,6 +68,10 @@
             <artifactId>org.apache.felix.karaf.deployer.features</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.felix.karaf.deployer</groupId>
+            <artifactId>org.apache.felix.karaf.deployer.war</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.apache.felix.karaf.features</groupId>
             <artifactId>org.apache.felix.karaf.features.core</artifactId>
         </dependency>
@@ -224,10 +228,6 @@
             <artifactId>pax-url-wrap</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.geronimo.specs</groupId>
-            <artifactId>geronimo-servlet_2.5_spec</artifactId>
-        </dependency>
-        <dependency>
             <groupId>org.apache.sshd</groupId>
             <artifactId>sshd-core</artifactId>
         </dependency>
@@ -247,6 +247,56 @@
             <groupId>org.apache.geronimo.blueprint</groupId>
             <artifactId>geronimo-blueprint</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-servlet_2.5_spec</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.url</groupId>
+            <artifactId>pax-url-wrap</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.servicemix.bundles</groupId>
+            <artifactId>org.apache.servicemix.bundles.jetty-bundle</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.web</groupId>
+            <artifactId>pax-web-api</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.web</groupId>
+            <artifactId>pax-web-spi</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.web</groupId>
+            <artifactId>pax-web-runtime</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.web</groupId>
+            <artifactId>pax-web-jetty</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.web</groupId>
+            <artifactId>pax-web-jsp</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.web</groupId>
+            <artifactId>pax-web-extender-war</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.web</groupId>
+            <artifactId>pax-web-extender-whiteboard</artifactId>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
     <build>
         <resources>
diff --git a/karaf/assembly/src/main/filtered-resources/features.xml b/karaf/assembly/src/main/filtered-resources/features.xml
index 650e9ef..abe73a7 100644
--- a/karaf/assembly/src/main/filtered-resources/features.xml
+++ b/karaf/assembly/src/main/filtered-resources/features.xml
@@ -52,6 +52,14 @@
         <bundle>mvn:org.ops4j.pax.web/pax-web-runtime/${pax.web.version}</bundle>
         <bundle>mvn:org.ops4j.pax.web/pax-web-jetty/${pax.web.version}</bundle>
     </feature>
+    <feature name="war" version="${version}">
+        <feature version="${version}">http</feature>
+        <bundle>mvn:org.ops4j.pax.web/pax-web-jsp/${pax.web.version}</bundle>
+        <bundle>mvn:org.ops4j.pax.web/pax-web-extender-war/${pax.web.version}</bundle>
+        <bundle>mvn:org.ops4j.pax.web/pax-web-extender-whiteboard/${pax.web.version}</bundle>
+        <bundle>mvn:org.ops4j.pax.url/pax-url-war/${pax.url.version}</bundle>
+        <bundle>mvn:org.apache.felix.karaf.deployer/org.apache.felix.karaf.deployer.war/${version}</bundle>
+    </feature>
     <feature name="webconsole" version="${version}">
         <feature version="${version}">http</feature>
         <config name="org.apache.felix.webconsole.internal.servlet.OsgiManager">
@@ -59,9 +67,7 @@
           password=karaf
         </config>
         <bundle>mvn:org.apache.felix/org.apache.felix.metatype/${felix.metatype.version}</bundle>
-        <!-- TODO: add this bundle when upgrading to webconsole 1.2.12
         <bundle>mvn:org.apache.felix.karaf.webconsole/org.apache.felix.karaf.webconsole.branding/${version}</bundle>
-        -->
         <bundle>mvn:org.apache.felix/org.apache.felix.webconsole/${felix.webconsole.version}</bundle>
         <bundle>mvn:org.apache.felix.karaf.webconsole/org.apache.felix.karaf.webconsole.admin/${version}</bundle>
         <bundle>mvn:org.apache.felix.karaf.webconsole/org.apache.felix.karaf.webconsole.features/${version}</bundle>
diff --git a/karaf/deployer/pom.xml b/karaf/deployer/pom.xml
index 686ee79..6f3ca81 100644
--- a/karaf/deployer/pom.xml
+++ b/karaf/deployer/pom.xml
@@ -36,6 +36,7 @@
         <module>spring</module>
         <module>blueprint</module>
         <module>features</module>
+        <module>war</module>
     </modules>
 
 </project>
diff --git a/karaf/deployer/war/pom.xml b/karaf/deployer/war/pom.xml
new file mode 100644
index 0000000..f6413c9
--- /dev/null
+++ b/karaf/deployer/war/pom.xml
@@ -0,0 +1,79 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <!--
+
+        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.
+    -->
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.felix.karaf.deployer</groupId>
+        <artifactId>deployer</artifactId>
+        <version>1.3.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>org.apache.felix.karaf.deployer</groupId>
+    <artifactId>org.apache.felix.karaf.deployer.war</artifactId>
+    <packaging>bundle</packaging>
+    <version>1.3.0-SNAPSHOT</version>
+    <name>Apache Felix Karaf :: WAR Deployer</name>
+
+    <description>This deployer transforms a plain war file to a deployable bundle</description>
+
+    <properties>
+        <appendedResourcesDirectory>${basedir}/../../etc/appended-resources</appendedResourcesDirectory>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>commons-logging</groupId>
+            <artifactId>commons-logging</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.fileinstall</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.servicemix.bundles</groupId>
+            <artifactId>org.apache.servicemix.bundles.junit</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <configuration>
+                    <instructions>
+                        <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+                        <Export-Package>${pom.artifactId}*;version=${project.version}</Export-Package>
+                        <Import-Package>!${pom.artifactId}*,*</Import-Package>
+                        <Private-Package>org.apache.felix.karaf.deployer.war</Private-Package>
+                        <_versionpolicy>${bnd.version.policy}</_versionpolicy>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/karaf/deployer/war/src/main/java/org/apache/felix/karaf/deployer/war/WarDeploymentListener.java b/karaf/deployer/war/src/main/java/org/apache/felix/karaf/deployer/war/WarDeploymentListener.java
new file mode 100644
index 0000000..855c49c
--- /dev/null
+++ b/karaf/deployer/war/src/main/java/org/apache/felix/karaf/deployer/war/WarDeploymentListener.java
@@ -0,0 +1,67 @@
+/**
+ *
+ * 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.felix.karaf.deployer.war;
+
+import java.io.File;
+import java.net.URL;
+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.felix.fileinstall.ArtifactUrlTransformer;
+
+/**
+ * A deployment listener that listens for war deployements.
+ */
+public class WarDeploymentListener implements ArtifactUrlTransformer {
+
+    private static final Log LOGGER = LogFactory.getLog(WarDeploymentListener.class);
+
+    public boolean canHandle(File artifact) {
+        try {
+            JarFile jar = new JarFile(artifact);
+            JarEntry entry = jar.getJarEntry("WEB-INF/web.xml");
+            // Only handle WAR 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 URL transform(URL artifact) throws Exception {
+        try {
+            return new URL("war", null, artifact.toString());
+        } catch (Exception e) {
+            LOGGER.error("Unable to build war bundle", e);
+            return null;
+        }
+    }
+
+}
diff --git a/karaf/deployer/war/src/main/resources/OSGI-INF/blueprint/war-deployer.xml b/karaf/deployer/war/src/main/resources/OSGI-INF/blueprint/war-deployer.xml
new file mode 100644
index 0000000..b1a2766
--- /dev/null
+++ b/karaf/deployer/war/src/main/resources/OSGI-INF/blueprint/war-deployer.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <bean id="warDeploymentListener" class="org.apache.felix.karaf.deployer.war.WarDeploymentListener"/>
+
+    <reference id="warUrlHandlerRef" interface="org.osgi.service.url.URLStreamHandlerService" filter="url.handler.protocol=war" />
+
+    <service ref="warDeploymentListener" auto-export="interfaces" depends-on="warDeploymentListener" />
+
+</blueprint>
diff --git a/karaf/pom.xml b/karaf/pom.xml
index 808b699..8c65288 100644
--- a/karaf/pom.xml
+++ b/karaf/pom.xml
@@ -186,6 +186,11 @@
                 <version>${pom.version}</version>
             </dependency>
             <dependency>
+                <groupId>org.apache.felix.karaf.deployer</groupId>
+                <artifactId>org.apache.felix.karaf.deployer.war</artifactId>
+                <version>${pom.version}</version>
+            </dependency>
+            <dependency>
                 <groupId>org.apache.felix.karaf</groupId>
                 <artifactId>org.apache.felix.karaf.management</artifactId>
                 <version>${pom.version}</version>
@@ -497,6 +502,11 @@
                 <version>${junit.version}</version>
             </dependency>
             <dependency>
+                <groupId>org.apache.servicemix.bundles</groupId>
+                <artifactId>org.apache.servicemix.bundles.jetty-bundle</artifactId>
+                <version>${jetty.bundle.version}</version>
+            </dependency>
+            <dependency>
                 <groupId>log4j</groupId>
                 <artifactId>log4j</artifactId>
                 <version>${log4j.version}</version>
@@ -583,6 +593,11 @@
                 <version>${pax.url.version}</version>
             </dependency>
             <dependency>
+                <groupId>org.ops4j.pax.url</groupId>
+                <artifactId>pax-url-war</artifactId>
+                <version>${pax.url.version}</version>
+            </dependency>
+            <dependency>
                 <groupId>org.apache.geronimo.specs</groupId>
                 <artifactId>geronimo-servlet_2.5_spec</artifactId>
                 <version>${geronimo.servlet.version}</version>
@@ -658,6 +673,41 @@
                 <artifactId>pax-runner-no-jcl</artifactId>
                 <version>${pax.runner.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.ops4j.pax.web</groupId>
+                <artifactId>pax-web-api</artifactId>
+                <version>${pax.web.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.ops4j.pax.web</groupId>
+                <artifactId>pax-web-spi</artifactId>
+                <version>${pax.web.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.ops4j.pax.web</groupId>
+                <artifactId>pax-web-runtime</artifactId>
+                <version>${pax.web.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.ops4j.pax.web</groupId>
+                <artifactId>pax-web-jetty</artifactId>
+                <version>${pax.web.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.ops4j.pax.web</groupId>
+                <artifactId>pax-web-jsp</artifactId>
+                <version>${pax.web.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.ops4j.pax.web</groupId>
+                <artifactId>pax-web-extender-war</artifactId>
+                <version>${pax.web.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.ops4j.pax.web</groupId>
+                <artifactId>pax-web-extender-whiteboard</artifactId>
+                <version>${pax.web.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>