Author: mrdon
Date: Sat May 24 08:19:34 2008
New Revision: 659837

URL: http://svn.apache.org/viewvc?rev=659837&view=rev
Log:
Adding new automatic package export scanner and tests

Added:
    struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/
    struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/
    struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/
    struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/
    
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/struts2/
    
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/struts2/osgi/
    
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/struts2/osgi/OsgiConfigurationProviderTest.java
Modified:
    struts/sandbox/trunk/struts2-osgi-plugin/plugin/pom.xml
    
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java
    
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-osgi.properties
    
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml

Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/pom.xml?rev=659837&r1=659836&r2=659837&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/plugin/pom.xml (original)
+++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/pom.xml Sat May 24 08:19:34 
2008
@@ -24,27 +24,33 @@
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.main</artifactId>
             <version>1.1.0-SNAPSHOT</version>
-        </dependency>
-
-         <dependency>
-            <groupId>org.apache.felix</groupId>
-            <artifactId>org.apache.felix.fileinstall</artifactId>
-            <version>0.9.0-SNAPSHOT</version>
-
-            <exclusions>
-                <exclusion>
-                    <groupId>org.apache.felix</groupId>
-                    <artifactId>javax.servlet</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
+        </dependency>
+
+         <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.fileinstall</artifactId>
+            <version>0.9.0-SNAPSHOT</version>
+
+            <exclusions>
+                <exclusion>
+                    <groupId>org.apache.felix</groupId>
+                    <artifactId>javax.servlet</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
 
         <dependency>
             <groupId>org.apache.struts</groupId>
             <artifactId>struts2-core</artifactId>
             <version>2.1.3-SNAPSHOT</version>
-        </dependency>
-
+        </dependency>
+
+        <dependency>
+            <groupId>org.twdata.pkgscanner</groupId>
+            <artifactId>package-scanner</artifactId>
+            <version>0.5.1</version>
+        </dependency>
+
         <dependency>
             <groupId>velocity-tools</groupId>
             <artifactId>velocity-tools</artifactId>
@@ -70,4 +76,12 @@
             <version>3.8.1</version>
         </dependency>
    </dependencies>
+
+    <repositories>
+        <repository>
+            <id>pkgscanner-repository</id>
+            <name>pkgscanner Repository for Maven</name>
+            <url>http://pkgscanner.googlecode.com/svn/repo/</url>
+        </repository>
+    </repositories>
 </project>

Modified: 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java?rev=659837&r1=659836&r2=659837&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java
 (original)
+++ 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java
 Sat May 24 08:19:34 2008
@@ -5,25 +5,14 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.security.CodeSource;
 import java.security.ProtectionDomain;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
+import java.util.*;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
 
-import org.apache.felix.fileinstall.FileInstall;
 import org.apache.felix.framework.Felix;
 import org.apache.felix.framework.cache.BundleCache;
 import org.apache.felix.framework.util.FelixConstants;
@@ -42,7 +31,9 @@
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
-import org.osgi.util.tracker.ServiceTracker;
+import org.twdata.pkgscanner.ExportPackage;
+import org.twdata.pkgscanner.PackageScanner;
+import static org.twdata.pkgscanner.PackageScanner.*;
 
 import com.opensymphony.xwork2.ObjectFactory;
 import com.opensymphony.xwork2.config.Configuration;
@@ -145,8 +136,10 @@
         Map<String, String> configMap = new StringMap(false);
 
         configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, 
packages.getProperty("packages")
+                + "," + getScannedPackages(packages)
                 + getSystemPackages(systemProperties));
 
+
         // find bundles
         Set<String> bundlePaths = new 
HashSet<String>(findInPackage("bundles"));
         LOG.info("Loading Struts bundles "+bundlePaths);
@@ -177,7 +170,6 @@
         //other properties
         configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
         configMap.put(FelixConstants.SERVICE_URLHANDLERS_PROP, "false");
-        configMap.put("org.osgi.framework.bootdelegation", "org.apache.*");
         configMap.put(OsgiConfigurationProvider.FELIX_LOG_LEVEL, "4");
         configMap.put(FelixConstants.BUNDLE_CLASSPATH, ".");
 
@@ -216,6 +208,40 @@
         return properties.getProperty(jreVersion);
     }
 
+    String getScannedPackages(Properties props) {
+        Collection<ExportPackage> exports = new PackageScanner()
+                .select(
+                    jars(
+                         
include(toArray(props.getProperty("scanning.jar.includes"))),
+                         
exclude(toArray(props.getProperty("scanning.jar.excludes")))),
+                    packages(
+                         
include(toArray(props.getProperty("scanning.package.includes"))),
+                         
exclude(toArray(props.getProperty("scanning.package.excludes")))))
+                .scan();
+
+        StringBuilder sb = new StringBuilder();
+        sb.append("Export-Package: ");
+        for (Iterator<ExportPackage> i = exports.iterator(); i.hasNext(); ) {
+            ExportPackage pkg = i.next();
+            sb.append(pkg.getPackageName());
+            if (pkg.getVersion() != null) {
+                sb.append(";version=").append(pkg.getVersion());
+            }
+            if (i.hasNext()) {
+                sb.append(",");
+            }
+        }
+        return sb.toString();
+    }
+
+    String[] toArray(String val) {
+        if (val != null) {
+            return val.split("\\s*,\\s*");
+        } else {
+            return new String[]{};
+        }
+    }
+
     private Properties getProperties(String fileName) {
         URL propertiesURL = 
OsgiConfigurationProvider.class.getClassLoader().getResource(
             fileName);

Modified: 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-osgi.properties
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-osgi.properties?rev=659837&r1=659836&r2=659837&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-osgi.properties
 (original)
+++ 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-osgi.properties
 Sat May 24 08:19:34 2008
@@ -18,10 +18,13 @@
 packages = org.osgi.framework; version=1.4.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, \
-           org.apache.struts2.osgi; version=1.0.0, \
-           org.apache.struts2.dispatcher; version=1.0.0, \
-           com.opensymphony.xwork2.config; version=1.0.0, \
-           com.opensymphony.xwork2.config.entities; version=1.0.0, \
-           com.opensymphony.xwork2.inject; version=1.0.0, \
-           com.opensymphony.xwork2; version=1.0.0
+           org.osgi.service.url; version=1.0.0
+
+scanning.jar.includes = *.jar
+scanning.jar.excludes =
+scanning.package.includes = com.opensymphony.xwork2.*, \
+                            org.apache.struts2.*, \
+                            ognl.*, \
+                            freemarker.*, \
+                            org.apache.velocity.*
+scanning.package.excludes =

Modified: 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml?rev=659837&r1=659836&r2=659837&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml
 (original)
+++ 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml
 Sat May 24 08:19:34 2008
@@ -8,10 +8,10 @@
     <bean type="org.apache.struts2.osgi.BundleAccessor" 
class="org.apache.struts2.osgi.DefaultBundleAccessor" />
     <bean type="org.apache.struts2.osgi.PackageLoader" 
class="org.apache.struts2.osgi.BundlePackageLoader" />
     <bean name="osgi" type="com.opensymphony.xwork2.ObjectFactory" 
class="org.apache.struts2.osgi.DelegatingObjectFactory" />
-    <bean name="osgi" type="com.opensymphony.xwork2.config.PackageProvider" 
class="org.apache.struts2.osgi.OsgiConfigurationProvider" />
+    <bean name="osgi" type="com.opensymphony.xwork2.config.PackageProvider" 
class="org.apache.struts2.osgi.OsgiConfigurationProviderTest" />
     
     <constant name="struts.objectFactory" value="osgi" />
     <constant name="struts.objectFactory.delegate" value="struts" />
-    <constant name="struts.freemarker.manager.classname" 
value="org.apache.struts2.osgi.BundleFreemarkerManager" />
+    <constant name="struts.freemarker.manager.classname" 
value="org.apache.struts2.osgi.BundleFreemarkerManager" />
     <constant name="struts.staticContentLoader" 
value="org.apache.struts2.osgi.loaders.StaticContentBundleResourceLoader" />
 </struts>

Added: 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/struts2/osgi/OsgiConfigurationProviderTest.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/struts2/osgi/OsgiConfigurationProviderTest.java?rev=659837&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/struts2/osgi/OsgiConfigurationProviderTest.java
 (added)
+++ 
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/test/java/org/apache/struts2/osgi/OsgiConfigurationProviderTest.java
 Sat May 24 08:19:34 2008
@@ -0,0 +1,41 @@
+package org.apache.struts2.osgi;
+
+import junit.framework.TestCase;
+
+import java.util.Properties;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: mrdon
+ * Date: 25/05/2008
+ * Time: 1:02:28 AM
+ * To change this template use File | Settings | File Templates.
+ */
+public class OsgiConfigurationProviderTest extends TestCase {
+
+    public void testGetScannedPackages() {
+        OsgiConfigurationProvider prov = new OsgiConfigurationProvider();
+        Properties props = new Properties();
+        props.setProperty("scanning.jar.includes", "*.jar");
+        props.setProperty("scanning.package.includes", 
"com.opensymphony.xwork2.*, org.apache.struts2.*,ognl.*,freemarker.*, 
org.apache.velocity.*");
+        String export = prov.getScannedPackages(props);
+        assertNotNull(export);
+        assertTrue(export.length() > 20);
+
+    }
+    public void testToArray() {
+        OsgiConfigurationProvider prov = new OsgiConfigurationProvider();
+
+        areEqual(new String[]{}, prov.toArray(null));
+        areEqual(new String[]{"foo", "bar"}, prov.toArray("foo,bar"));
+        areEqual(new String[]{"foo", "bar"}, prov.toArray("foo, bar"));
+        areEqual(new String[]{"foo", "bar"}, prov.toArray("foo, \nbar"));
+
+    }
+
+    private void areEqual(String[] expected, String[] actual) {
+        for (int x=0; x<expected.length; x++) {
+            assertEquals(expected[x], actual[x]);
+        }
+    }
+}


Reply via email to