directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r1074785 - in /directory/shared/branches/akarasulu/util/src: main/java/org/apache/directory/shared/util/OsgiUtils.java test/java/org/apache/directory/shared/util/OsgiUtilsTest.java
Date Sat, 26 Feb 2011 03:27:40 GMT
Author: akarasulu
Date: Sat Feb 26 03:27:40 2011
New Revision: 1074785

URL: http://svn.apache.org/viewvc?rev=1074785&view=rev
Log:
adding some utilities to deal with exported bundles

Added:
    directory/shared/branches/akarasulu/util/src/main/java/org/apache/directory/shared/util/OsgiUtils.java
    directory/shared/branches/akarasulu/util/src/test/java/org/apache/directory/shared/util/OsgiUtilsTest.java

Added: directory/shared/branches/akarasulu/util/src/main/java/org/apache/directory/shared/util/OsgiUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/util/src/main/java/org/apache/directory/shared/util/OsgiUtils.java?rev=1074785&view=auto
==============================================================================
--- directory/shared/branches/akarasulu/util/src/main/java/org/apache/directory/shared/util/OsgiUtils.java
(added)
+++ directory/shared/branches/akarasulu/util/src/main/java/org/apache/directory/shared/util/OsgiUtils.java
Sat Feb 26 03:27:40 2011
@@ -0,0 +1,201 @@
+/*
+ *   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.directory.shared.util;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Utilities for OSGi environments and embedding OSGi containers.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class OsgiUtils
+{
+    /** A logger */
+    private static final Logger LOG = LoggerFactory.getLogger( OsgiUtils.class );
+    
+    
+    /**
+     * All the packages that are exported from all bundles found on the system 
+     * classpath. The provided filter if not null is used to prune classpath 
+     * elements. Any uses terms found are stripped from the bundles.
+     *
+     * @return All the exported packages of all bundles on the classpath.
+     */
+    public static Set<String> getAllBundleExports( FileFilter filter )
+    {
+        Set<String> pkgs = new HashSet<String>();
+        Set<File> candidates = getClasspathCandidates( filter );
+        
+        for ( File candidate : candidates )
+        {
+            String exports = getBundleExports( candidate );
+            
+            if ( exports == null )
+            {
+                LOG.debug( "No export found for candidate: {}", candidate );
+                continue;
+            }
+            
+            LOG.debug( "Processing exports for candidate: {}\n\n{}\n", candidate, exports
);
+            splitIntoPackages( exports, pkgs );
+        }
+        
+        return pkgs;
+    }
+
+    
+    /**
+     * Splits an Package-Export OSGi Manifest Attribute value into packages 
+     * while stripping away the key/value properties.
+     *
+     * @param exports The Package-Export OSGi Manifest Attribute value.
+     * @return The set of exported packages without properties.
+     */
+    public static Set<String> splitIntoPackages( String exports, Set<String>
pkgs )
+    {
+        if ( pkgs == null )
+        {
+            pkgs = new HashSet<String>();
+        }
+        
+        int index = 0;
+        boolean inPkg = true;
+        boolean inProps = false;
+        StringBuilder pkg = new StringBuilder();
+        
+        while ( index < exports.length() )
+        {
+            if ( inPkg && exports.charAt( index ) != ';' )
+            {
+                pkg.append( exports.charAt( index ) );
+                index++;
+            }
+            else if ( inPkg && exports.charAt( index ) == ';' )
+            {
+                inPkg = false;
+                inProps = true;
+                
+                pkgs.add( pkg.toString() );
+                LOG.debug( "Added package: {}", pkg.toString() );
+                pkg.setLength( 0 );
+                
+                index += 8;
+            }
+            else if ( inProps && exports.charAt( index ) == '"' 
+                && index + 1 < exports.length()
+                && exports.charAt( index + 1 ) == ',' )
+            {
+                inPkg = true;
+                inProps = false;
+                index += 2;
+            }
+            else if ( inProps )
+            {
+                index++;
+            }
+            else
+            {
+                LOG.error( "Unexpected parser condition throwing IllegalStateException."
);
+                throw new IllegalStateException( "Should never get here!" );
+            }
+        }
+        
+        return pkgs;
+    }
+    
+    
+    public static Set<File> getClasspathCandidates( FileFilter filter )
+    {
+        Set<File> candidates = new HashSet<File>();
+        String[] cpElements = System.getProperty( "java.class.path" ).split( ":" );
+        
+        for ( String element : cpElements )
+        {
+            File candidate = new File( element );
+            
+            if ( candidate.isFile() )
+            {
+                if ( filter != null && filter.accept( candidate ) )
+                {
+                    candidates.add( candidate );
+                    LOG.info( "Accepted candidate with filter: {}", candidate.toString()
);
+                }
+                else if ( filter == null && candidate.getName().endsWith( ".jar"
) )
+                {
+                    candidates.add( candidate );
+                    LOG.info( "Accepted candidate without filter: {}", candidate.toString()
);
+                }
+                else
+                {
+                    LOG.info( "Rejecting candidate: {}", candidate.toString() );
+                }
+            }
+        }
+        
+        return candidates;
+    }
+
+    
+    /**
+     * Gets the attribute value for the Export-Bundle OSGi Manifest Attribute.
+     * 
+     * @param bundle The absolute path to a file bundle.
+     * @return The value as it appears in the Manifest, as a comma delimited 
+     * list of packages with possible "uses" phrases appended to each package 
+     * or null if the attribute does not exist.
+     */
+    public static String getBundleExports( File bundle )
+    {   
+        JarFile jar;
+        try
+        {
+            jar = new JarFile( bundle );
+            Manifest manifest = jar.getManifest();
+            
+            Attributes attrs = manifest.getMainAttributes();
+            for ( Object key : attrs.keySet() )
+            {
+                if ( key.toString().equals( "Export-Package" ) )
+                {
+                    return attrs.get( key ).toString();
+                }
+            }
+            
+            return null;
+        }
+        catch ( IOException e )
+        {
+            LOG.error( "Failed to open jar file or manifest.", e );
+            throw new RuntimeException( "Failed to open jar file or manifest.", e );
+        }
+    }
+}

Added: directory/shared/branches/akarasulu/util/src/test/java/org/apache/directory/shared/util/OsgiUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/util/src/test/java/org/apache/directory/shared/util/OsgiUtilsTest.java?rev=1074785&view=auto
==============================================================================
--- directory/shared/branches/akarasulu/util/src/test/java/org/apache/directory/shared/util/OsgiUtilsTest.java
(added)
+++ directory/shared/branches/akarasulu/util/src/test/java/org/apache/directory/shared/util/OsgiUtilsTest.java
Sat Feb 26 03:27:40 2011
@@ -0,0 +1,158 @@
+/*
+ *   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.directory.shared.util;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.util.Set;
+
+import org.junit.Test;
+
+
+/**
+ * Unit tests for OsgiUtils.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class OsgiUtilsTest
+{
+    private static final FileFilter REJECTION_FILTER = new FileFilter()
+    {
+        public boolean accept( File pathname )
+        {
+            return false;
+        }
+    };
+
+    private static final FileFilter JUNIT_SLF4J_FILTER = new FileFilter()
+    {
+        public boolean accept( File pathname )
+        {
+            return pathname.getAbsolutePath().contains( "junit" ) || 
+                pathname.getAbsolutePath().contains( "slf4j" );
+        }
+    };
+
+    private static final FileFilter ONLY_ONE_FILTER = new FileFilter()
+    {
+        boolean isDone = false;
+        
+        public boolean accept( File pathname )
+        {
+            if ( isDone )
+            {
+                return false;
+            }
+            
+            isDone = true;
+            return true;
+        }
+    };
+
+    
+    @Test
+    public void testSplitIntoPackageVersions()
+    {
+        Set<String> pkgs = OsgiUtils.splitIntoPackages( 
+            "org.ops4j.store.intern;uses:=\"org.ops4j.store,org.ops4j.io,org.apache.commons.logging\";"
+
+            "version=\"1.2.2\",org.ops4j.store;uses:=\"org.ops4j.store.intern\";version=\"1.2.2",

+            null );
+
+        assertTrue( "org.ops4j.store.intern", pkgs.contains( "org.ops4j.store.intern" ) );
+        assertTrue( "org.ops4j.store", pkgs.contains( "org.ops4j.store" ) );
+
+        assertEquals( "Expecting 2 packages", 2, pkgs.size() );
+    }
+    
+    
+    @Test
+    public void testSplitIntoPackages()
+    {
+        Set<String> pkgs = OsgiUtils.splitIntoPackages( 
+            "org.apache.log4j.net;uses:=\"org.apache.log4j,org.apache.log4j.spi," +
+            "javax.naming,org.apache.log4j.helpers,javax.jms,org.apache.log4j.xml," +
+            "javax.mail,javax.mail.internet,org.w3c.dom,javax.jmdns\"," +
+            "org.apache.log4j.jmx;uses:=\"org.apache.log4j,javax.management," +
+            "com.sun.jdmk.comm,org.apache.log4j.helpers,org.apache.log4j.spi\"," +
+            "org.apache.log4j.jdbc;uses:=\"org.apache.log4j,org.apache.log4j.spi\"," +
+            "org.apache.log4j.config;uses:=\"org.apache.log4j.helpers,org.apache.log4j,"
+
+            "org.apache.log4j.spi\",org.apache.log4j.helpers;uses:=\"org.apache.log4j," +
+            "org.apache.log4j.spi,org.apache.log4j.pattern\",org.apache.log4j;uses:=\"" +
+            "org.apache.log4j.spi,org.apache.log4j.helpers,org.apache.log4j.pattern," +
+            "org.apache.log4j.or,org.apache.log4j.config\",org.apache.log4j.or.jms;" +
+            "uses:=\"org.apache.log4j.helpers,javax.jms,org.apache.log4j.or\"," +
+            "org.apache.log4j.nt;uses:=\"org.apache.log4j.helpers,org.apache.log4j," +
+            "org.apache.log4j.spi\",org.apache.log4j.or.sax;uses:=\"org.apache.log4j.or,"
+
+            "org.xml.sax\",org.apache.log4j.pattern;uses:=\"org.apache.log4j.helpers," +
+            "org.apache.log4j.spi,org.apache.log4j,org.apache.log4j.or\"," +
+            "org.apache.log4j.spi;uses:=\"org.apache.log4j,org.apache.log4j.helpers," +
+            "com.ibm.uvm.tools,org.apache.log4j.or\",org.apache.log4j.or;uses:=\"" +
+            "org.apache.log4j.helpers,org.apache.log4j.spi,org.apache.log4j\"," +
+            "org.apache.log4j.xml;uses:=\"javax.xml.parsers,org.w3c.dom,org.xml.sax," +
+            "org.apache.log4j.config,org.apache.log4j.helpers,org.apache.log4j," +
+            "org.apache.log4j.spi,org.apache.log4j.or\",org.apache.log4j.varia;uses:=\""
+
+            "org.apache.log4j.spi,org.apache.log4j,org.apache.log4j.helpers\"", null );
+        
+        assertTrue( "org.apache.log4j.net", pkgs.contains( "org.apache.log4j.net" ) );
+        assertTrue( "org.apache.log4j.jmx", pkgs.contains( "org.apache.log4j.jmx" ) );
+        assertTrue( "org.apache.log4j.jdbc", pkgs.contains( "org.apache.log4j.jdbc" ) );
+        assertTrue( "org.apache.log4j.config", pkgs.contains( "org.apache.log4j.config" )
);
+        assertTrue( "org.apache.log4j.helpers", pkgs.contains( "org.apache.log4j.helpers"
) );
+        assertTrue( "org.apache.log4j", pkgs.contains( "org.apache.log4j" ) );
+        assertTrue( "org.apache.log4j.or", pkgs.contains( "org.apache.log4j.or" ) );
+        assertTrue( "org.apache.log4j.or.jms", pkgs.contains( "org.apache.log4j.or.jms" )
);
+        assertTrue( "org.apache.log4j.or.sax", pkgs.contains( "org.apache.log4j.or.sax" )
);
+        assertTrue( "org.apache.log4j.nt", pkgs.contains( "org.apache.log4j.nt" ) );
+        assertTrue( "org.apache.log4j.spi", pkgs.contains( "org.apache.log4j.spi" ) );
+        assertTrue( "org.apache.log4j.pattern", pkgs.contains( "org.apache.log4j.pattern"
) );
+        assertTrue( "org.apache.log4j.xml", pkgs.contains( "org.apache.log4j.xml" ) );
+        assertTrue( "org.apache.log4j.varia", pkgs.contains( "org.apache.log4j.varia" ) );
+
+        assertEquals( "Expecting 14 packages", 14, pkgs.size() );
+    }
+    
+
+    @Test
+    public void testGetClasspathCandidates()
+    {
+        Set<File> candidates = OsgiUtils.getClasspathCandidates( REJECTION_FILTER );
+        assertEquals( "Should have no results with REJECTION_FILTER", 0, candidates.size()
);
+
+        candidates = OsgiUtils.getClasspathCandidates( ONLY_ONE_FILTER );
+        assertEquals( "Should have one result with ONLY_ONE_FILTER", 1, candidates.size()
);
+
+        candidates = OsgiUtils.getClasspathCandidates( JUNIT_SLF4J_FILTER );
+        assertTrue( "Should have at least 4 results with JUNIT_SLF4J_FILTER", candidates.size()
>= 4 );
+        
+        candidates = OsgiUtils.getClasspathCandidates( null );
+        assertTrue( "Should have at least 4 results with no filter", candidates.size() >=
4 );
+    }
+    
+    
+    @Test
+    public void testGetAllBundleExports()
+    {
+        OsgiUtils.getAllBundleExports( null );
+    }
+}



Mime
View raw message