moving felix plugin to trunk

git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@432307 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.daemon/src/main/java/org/apache/felix/daemon/Service.java b/org.apache.felix.daemon/src/main/java/org/apache/felix/daemon/Service.java
index decc4ca..a7c037a 100644
--- a/org.apache.felix.daemon/src/main/java/org/apache/felix/daemon/Service.java
+++ b/org.apache.felix.daemon/src/main/java/org/apache/felix/daemon/Service.java
@@ -57,6 +57,12 @@
     /** the felix installation layout */
     private FelixLayout layout;
 
+    
+    public Felix getFelixInstance()
+    {
+        return instance;
+    }
+    
 
     public void init( InstallationLayout suppliedLayout, String[] args ) throws Exception
     {
diff --git a/tools/maven2/maven-felix-plugin/pom.xml b/tools/maven2/maven-felix-plugin/pom.xml
new file mode 100644
index 0000000..6110d5d
--- /dev/null
+++ b/tools/maven2/maven-felix-plugin/pom.xml
@@ -0,0 +1,46 @@
+<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">
+  <parent>
+    <artifactId>felix</artifactId>
+    <groupId>org.apache.felix</groupId>
+    <version>0.8.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.felix.plugins</groupId>
+  <artifactId>maven-felix-plugin</artifactId>
+  <packaging>maven-plugin</packaging>
+  <name>Maven Felix Plugin</name>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-project</artifactId>
+      <version>2.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+      <version>2.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>1.0.5</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.framework</artifactId>
+      <version>0.8.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>0.8.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/tools/maven2/maven-felix-plugin/src/main/java/org/apache/felix/tools/maven/felix/plugin/FelixRunMojo.java b/tools/maven2/maven-felix-plugin/src/main/java/org/apache/felix/tools/maven/felix/plugin/FelixRunMojo.java
new file mode 100644
index 0000000..538f1ff
--- /dev/null
+++ b/tools/maven2/maven-felix-plugin/src/main/java/org/apache/felix/tools/maven/felix/plugin/FelixRunMojo.java
@@ -0,0 +1,199 @@
+package org.apache.felix.tools.maven.felix.plugin;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.felix.framework.Felix;
+import org.apache.felix.framework.util.MutablePropertyResolver;
+import org.apache.felix.framework.util.MutablePropertyResolverImpl;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.FileUtils;
+
+
+/**
+ * Starts up Felix, installs this module's bundle artifact with dependent 
+ * bundles, and starts them.
+ * 
+ * @goal run
+ * @phase integration-test
+ */
+public class FelixRunMojo extends AbstractMojo
+{
+    /**
+     * The name of the felix cache profile.
+     * 
+     * @parameter expression=”${run.felix.cache.profile}” default-value="test"
+     * @description the felix.cache.profile property value
+     */
+    private String felixCacheProfile;
+    
+    /**
+     * The location of the felix bundle cache directory.
+     * 
+     * @parameter expression=”${run.felix.cache.dir}” default-value="${basedir}/target/.felix"
+     * @description the felix.cache.dir property value
+     */
+    private File felixCacheDir;
+    
+    /**
+     * The location of the test bundle.
+     * 
+     * @parameter expression="${run.felix.test.bundle}" 
+     *            default-value="${basedir}/target/${project.artifactId}-${project.version}.jar"
+     */
+    private File felixTestBundle;
+
+    /**
+     * @parameter default-value="${project}"
+     */
+    private MavenProject project;
+
+    /**
+     * @parameter
+     */
+    private List exclusions;
+    
+    /**
+     * The felix container used to run the integration tests.
+     */
+    private Felix felixContainer = new Felix();
+
+
+    public void execute() throws MojoExecutionException, MojoFailureException
+    {
+        // -------------------------------------------------------------------
+        // Clean out the old cache directory if it exists
+        // -------------------------------------------------------------------
+        
+        if ( felixCacheDir.exists() )
+        {
+            try
+            {
+                FileUtils.forceDelete( felixCacheDir );
+            }
+            catch ( IOException e )
+            {
+                throw new MojoFailureException( "failed to delete old Felix cache directory: " 
+                    + felixCacheDir.getAbsolutePath() );
+            }
+        }
+
+        // -------------------------------------------------------------------
+        // Build the properties we will stuff into the resolver
+        // -------------------------------------------------------------------
+
+        Properties props = new Properties();
+        props.put( "felix.cache.dir", felixCacheDir.getAbsolutePath() );
+        props.put( "felix.cache.profile", felixCacheProfile );
+        props.put( "felix.embedded.execution", "true" );
+        props.put( "org.osgi.framework.system.packages", 
+            "org.osgi.framework; version=1.3.0, " +
+            "org.osgi.service.packageadmin; version=1.2.0, " +
+            "org.osgi.service.startlevel; version=1.0.0, " +
+            "org.osgi.service.url; version=1.0.0, " +
+            "${jre-${java.specification.version}}" );
+        try
+        {
+            props.put( "felix.auto.start.1", getAutoStart() );
+        }
+        catch ( MalformedURLException e )
+        {
+            e.printStackTrace();
+        }
+        
+        // -------------------------------------------------------------------
+        // Start up Felix with resolver and shut it down
+        // -------------------------------------------------------------------
+
+        MutablePropertyResolver resolver = new MutablePropertyResolverImpl(props);
+        felixContainer.start( resolver, new ArrayList() );
+        getLog().info( "-=============================-" );
+        getLog().info( "| Felix: successfully started |" );
+        getLog().info( "-=============================-" );
+        felixContainer.shutdown();
+        getLog().info( "-==============================-" );
+        getLog().info( "| Felix: successfully shutdown |" );
+        getLog().info( "-==============================-" );
+    }
+    
+
+    public String getAutoStart() throws MalformedURLException
+    {
+        List included = new ArrayList();
+        List excluded = new ArrayList();
+        if ( exclusions == null )
+        {
+            exclusions = Collections.EMPTY_LIST;
+        }
+        
+        StringBuffer buf = new StringBuffer();
+        StringBuffer keyBuf = new StringBuffer();
+        for ( Iterator ii = project.getDependencyArtifacts().iterator(); ii.hasNext(); /**/) 
+        {
+            Artifact dep = ( Artifact ) ii.next();
+            keyBuf.setLength( 0 );
+            keyBuf.append( dep.getGroupId() ).append( ":" ).append( dep.getArtifactId() );
+            String depKey = keyBuf.toString();
+            
+            // -------------------------------------------------------------------
+            // Add only provided dependent artifacts that have not been excluded
+            // -------------------------------------------------------------------
+            
+            if ( dep.getScope().equalsIgnoreCase( "provided" ) )
+            {
+                if ( dep.getArtifactId().equalsIgnoreCase( "org.osgi.core" ) ||
+                     exclusions.contains( depKey ) )
+                {
+                    excluded.add( depKey );
+                    continue;
+                }
+                
+                included.add( depKey );
+                buf.append( dep.getFile().toURL() );
+                buf.append( " " );
+            }
+        }
+        
+        keyBuf.setLength( 0 );
+        keyBuf.append( project.getGroupId() ).append( ":" ).append( project.getArtifactId() );
+        String depKey = keyBuf.toString();
+        included.add( depKey );
+        buf.append( felixTestBundle.toURL() );
+        
+        // -------------------------------------------------------------------
+        // Report what was included and what was excluded
+        // -------------------------------------------------------------------
+        
+        getLog().info( "" );
+        getLog().info( "\t\tJars/Bundles Included for Autostart" ); 
+        getLog().info( "\t\t-----------------------------------" );
+        getLog().info( "" );
+        for ( Iterator ii = included.iterator(); ii.hasNext(); /**/ )
+        {
+            getLog().info( "\t\t" + ( String ) ii.next() );
+        }
+        getLog().info( "" );
+        getLog().info( "" );
+        getLog().info( "\t\tJars/Bundles Excluded from Autostart" );
+        getLog().info( "\t\t------------------------------------" );
+        getLog().info( "" );
+        for ( Iterator ii = excluded.iterator(); ii.hasNext(); /**/ )
+        {
+            getLog().info( "\t\t" + ( String ) ii.next() );
+        }
+        getLog().info( "" );
+        getLog().info( "" );
+        return buf.toString();
+    }
+}