geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r891926 - in /geronimo/server/trunk/framework/modules/geronimo-kernel/src: main/java/org/apache/geronimo/kernel/osgi/ test/java/org/apache/geronimo/kernel/osgi/
Date Thu, 17 Dec 2009 21:36:03 GMT
Author: gawor
Date: Thu Dec 17 21:36:02 2009
New Revision: 891926

URL: http://svn.apache.org/viewvc?rev=891926&view=rev
Log:
Improved API for parsing Import/Export-Package headers. VersionRange class taken from Felix

Added:
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java
  (with props)
Modified:
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleDescription.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/HeaderParser.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/BundleDescriptionTest.java
    geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/HeaderParserTest.java

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java?rev=891926&r1=891925&r2=891926&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleClassLoader.java
Thu Dec 17 21:36:02 2009
@@ -135,7 +135,7 @@
         BundleDescription description = new BundleDescription(bundle.getHeaders());
         
         // handle static wire via Import-Package
-        List<BundleDescription.Package> imports = description.getExternalImports();
+        List<BundleDescription.ImportPackage> imports = description.getExternalImports();
         LinkedHashSet<Bundle> wiredBundles = new LinkedHashSet<Bundle>();
         for (BundleDescription.Package packageImport : imports) {
             ExportedPackage[] exports = packageAdmin.getExportedPackages(packageImport.getName());

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleDescription.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleDescription.java?rev=891926&r1=891925&r2=891926&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleDescription.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/BundleDescription.java
Thu Dec 17 21:36:02 2009
@@ -25,6 +25,7 @@
 
 import org.apache.geronimo.kernel.osgi.HeaderParser.HeaderElement;
 import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
 
 /**
  * @version $Rev$, $Date$
@@ -44,12 +45,12 @@
     /**
      * Returns a list of packages that are listed in <i>Import-Package</i> header.
      */
-    public List<Package> getImportPackage() {
+    public List<ImportPackage> getImportPackage() {
         String headerValue = (String) headers.get(Constants.IMPORT_PACKAGE);
-        List<Package> imports = new ArrayList<Package>();
+        List<ImportPackage> imports = new ArrayList<ImportPackage>();
         List<HeaderElement> elements = HeaderParser.parseHeader(headerValue);
         for (HeaderElement element : elements) {
-            Package p = new Package(element.getName(), element.getAttributes(), element.getDirectives());
+            ImportPackage p = new ImportPackage(element.getName(), element.getAttributes(),
element.getDirectives());
             imports.add(p);
         }
         return imports;
@@ -58,15 +59,15 @@
     /**
      * Returns a list of packages that are listed in <i>Export-Package</i> header.
      */
-    public List<Package> getExportPackage() {
+    public List<ExportPackage> getExportPackage() {
         String headerValue = (String) headers.get(Constants.EXPORT_PACKAGE);
-        List<Package> imports = new ArrayList<Package>();
+        List<ExportPackage> exports = new ArrayList<ExportPackage>();
         List<HeaderElement> elements = HeaderParser.parseHeader(headerValue);
         for (HeaderElement element : elements) {
-            Package p = new Package(element.getName(), element.getAttributes(), element.getDirectives());
-            imports.add(p);
+            ExportPackage p = new ExportPackage(element.getName(), element.getAttributes(),
element.getDirectives());
+            exports.add(p);
         }
-        return imports;
+        return exports;
         
     }
     
@@ -74,11 +75,11 @@
      * Returns a list of packages that are listed in <i>Import-Package</i> header
      * and are <b>not</b> listed in <i>Export-Package</i> header.
      */
-    public List<Package> getExternalImports() {
-        List<Package> imports = getImportPackage();
-        List<Package> exports = getExportPackage();
-        List<Package> realImports = new ArrayList<Package>();
-        for (Package p : imports) {
+    public List<ImportPackage> getExternalImports() {
+        List<ImportPackage> imports = getImportPackage();
+        List<ExportPackage> exports = getExportPackage();
+        List<ImportPackage> realImports = new ArrayList<ImportPackage>();
+        for (ImportPackage p : imports) {
             if (!isExported(exports, p)) {
                 realImports.add(p);
             }
@@ -86,7 +87,7 @@
         return realImports;
     }
     
-    private static boolean isExported(List<Package> exports, Package p) {
+    private static boolean isExported(List<ExportPackage> exports, Package p) {
         for (Package export : exports) {
             if (export.getName().equals(p.getName())) {
                 return true;
@@ -119,13 +120,23 @@
         return imports;
     }
     
+    public String getSymbolicName() {
+        return (String) headers.get(Constants.BUNDLE_SYMBOLICNAME);
+    }
+    
+    public Map getHeaders() {
+        return headers;
+    }
+    
     public static class Package {
     
         private String name;
         private Map<String, String> attributes;
         private Map<String, String> directives;
         
-        public Package(String name, Map<String, String> attributes, Map<String,
String> directives) {
+        public Package(String name, 
+                       Map<String, String> attributes, 
+                       Map<String, String> directives) {
             this.name = name;
             this.attributes = attributes;
             this.directives = directives;
@@ -150,6 +161,58 @@
             builder.append(", Directives: ").append(directives);
             return builder.toString();
         }
+        
+        protected VersionRange getVersionRange() {
+            String version = attributes.get(Constants.VERSION_ATTRIBUTE);
+            if (version == null) {
+                version = "0.0.0";
+            }
+            return VersionRange.parse(version);
+        }
+    }
+    
+    public class ExportPackage extends Package {
+
+        private Version version;
+        
+        public ExportPackage(String name,
+                             Map<String, String> attributes,
+                             Map<String, String> directives) {
+            super(name, attributes, directives);
+            version = getVersionRange().getLow();
+        }
+        
+        public Version getVersion() {
+            return version;
+        }
     }
         
+    public class ImportPackage extends Package {
+
+        private boolean optional;
+        private VersionRange versionRange;
+        
+        public ImportPackage(String name,
+                             Map<String, String> attributes,
+                             Map<String, String> directives) {
+            super(name, attributes, directives);
+            
+            String resolution = directives.get(Constants.RESOLUTION_DIRECTIVE);
+            optional = Constants.RESOLUTION_OPTIONAL.equals(resolution);
+            
+            versionRange = super.getVersionRange();
+        }
+        
+        public boolean isOptional() {
+            return optional;
+        }
+        
+        public boolean isMandatory() {
+            return !optional;
+        }
+        
+        public VersionRange getVersionRange() {
+            return versionRange;
+        }
+    }
 }

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/HeaderParser.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/HeaderParser.java?rev=891926&r1=891925&r2=891926&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/HeaderParser.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/HeaderParser.java
Thu Dec 17 21:36:02 2009
@@ -41,7 +41,7 @@
         if (header == null || header.trim().length() == 0) {
             return elements;
         }
-        List<String> clauses = parseDelimitedString(header, ",");
+        List<String> clauses = parseDelimitedString(header, ",", false);
         for (String clause : clauses) {
             String[] tokens = clause.split(";");
             if (tokens.length < 1) {
@@ -70,7 +70,7 @@
         return elements;
     }
 
-    private static List<String> parseDelimitedString(String value, String delim) {
  
+    private static List<String> parseDelimitedString(String value, String delim, boolean
includeQuotes) {   
         if (value == null) {       
             value = "";
         }
@@ -96,11 +96,15 @@
                 list.add(sb.toString().trim());
                 sb.delete(0, sb.length());
                 expecting = (CHAR | DELIMITER | STARTQUOTE);
-            } else if (isQuote && ((expecting & STARTQUOTE) > 0)) {      
     
-                sb.append(c);
+            } else if (isQuote && ((expecting & STARTQUOTE) > 0)) { 
+                if (includeQuotes) {
+                    sb.append(c);
+                }
                 expecting = CHAR | ENDQUOTE;
-            } else if (isQuote && ((expecting & ENDQUOTE) > 0)) {        
  
-                sb.append(c);
+            } else if (isQuote && ((expecting & ENDQUOTE) > 0)) {    
+                if (includeQuotes) {
+                    sb.append(c);
+                }
                 expecting = (CHAR | STARTQUOTE | DELIMITER);
             } else if ((expecting & CHAR) > 0) {            
                 sb.append(c);

Added: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java?rev=891926&view=auto
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java
(added)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java
Thu Dec 17 21:36:02 2009
@@ -0,0 +1,96 @@
+/* 
+ * 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.geronimo.kernel.osgi;
+
+import org.osgi.framework.Version;
+
+public class VersionRange {
+
+    private Version low = null;
+    private boolean isLowInclusive = false;
+    private Version high = null;
+    private boolean isHighInclusive = false;
+    
+    public static final VersionRange infiniteRange = new VersionRange(Version.emptyVersion,
true, null, true);
+
+    public VersionRange(Version low, boolean isLowInclusive, Version high, boolean isHighInclusive)
{
+        this.low = low;
+        this.isLowInclusive = isLowInclusive;
+        this.high = high;
+        this.isHighInclusive = isHighInclusive;
+    }
+
+    public Version getLow() {   
+        return low;
+    }
+
+    public boolean isLowInclusive() {   
+        return isLowInclusive;
+    }
+
+    public Version getHigh() {   
+        return high;
+    }
+
+    public boolean isHighInclusive() {   
+        return isHighInclusive;
+    }
+
+    public boolean isInRange(Version version) {   
+        // We might not have an upper end to the range.
+        if (high == null) {       
+            return (version.compareTo(low) >= 0);
+        } else if (isLowInclusive() && isHighInclusive()) {       
+            return (version.compareTo(low) >= 0) && (version.compareTo(high) <=
0);
+        } else if (isHighInclusive()) {
+            return (version.compareTo(low) > 0) && (version.compareTo(high) <=
0);
+        } else if (isLowInclusive()) {
+            return (version.compareTo(low) >= 0) && (version.compareTo(high) <
0);
+        }
+        return (version.compareTo(low) > 0) && (version.compareTo(high) < 0);
+    }
+
+    public static VersionRange parse(String range) {   
+        // Check if the version is an interval. 
+        if (range.indexOf(',') >= 0) {       
+            String s = range.substring(1, range.length() - 1);
+            String vlo = s.substring(0, s.indexOf(',')).trim();
+            String vhi = s.substring(s.indexOf(',') + 1, s.length()).trim();
+            return new VersionRange (
+                new Version(vlo), (range.charAt(0) == '['),
+                new Version(vhi), (range.charAt(range.length() - 1) == ']'));
+        } else {
+            return new VersionRange(new Version(range), true, null, false);
+        }
+    }
+
+    public String toString() {    
+        if (high != null) {
+            StringBuffer sb = new StringBuffer();
+            sb.append(isLowInclusive ? '[' : '(');
+            sb.append(low.toString());
+            sb.append(',');
+            sb.append(high.toString());
+            sb.append(isHighInclusive ? ']' : ')');
+            return sb.toString();
+        } else {
+            return low.toString();
+        }
+    }
+}

Propchange: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/server/trunk/framework/modules/geronimo-kernel/src/main/java/org/apache/geronimo/kernel/osgi/VersionRange.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/BundleDescriptionTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/BundleDescriptionTest.java?rev=891926&r1=891925&r2=891926&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/BundleDescriptionTest.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/BundleDescriptionTest.java
Thu Dec 17 21:36:02 2009
@@ -23,6 +23,7 @@
 import java.util.Map;
 
 import org.osgi.framework.Constants;
+import org.osgi.framework.Version;
 
 import junit.framework.TestCase;
 
@@ -37,23 +38,24 @@
         
         BundleDescription desc = new BundleDescription(headers);
         
-        List<BundleDescription.Package> imports = desc.getImportPackage();
+        List<BundleDescription.ImportPackage> imports = desc.getImportPackage();
         assertEquals(3, imports.size());
         assertEquals("com.thoughtworks.xstream", imports.get(0).getName());
-        assertEquals("\"1.3\"", imports.get(0).getAttributes().get("version"));
+        assertEquals("1.3", imports.get(0).getAttributes().get("version"));
+        assertEquals(Version.parseVersion("1.3"), imports.get(0).getVersionRange().getLow());
         assertEquals("com.thoughtworks.xstream.converters", imports.get(1).getName());
         assertEquals("org.apache.geronimo.kernel.proxy", imports.get(2).getName());
                 
-        List<BundleDescription.Package> exports = desc.getExportPackage();
+        List<BundleDescription.ExportPackage> exports = desc.getExportPackage();
         assertEquals(2, exports.size());
         assertEquals("org.apache.geronimo.kernel.rmi", exports.get(0).getName());
-        assertEquals("\"javax.rmi.ssl,org.apache.geronimo.gbean,org.slf4j\"", exports.get(0).getDirectives().get("uses"));
+        assertEquals("javax.rmi.ssl,org.apache.geronimo.gbean,org.slf4j", exports.get(0).getDirectives().get("uses"));
         assertEquals("org.apache.geronimo.kernel.proxy", exports.get(1).getName());
         
-        List<BundleDescription.Package> externalImports = desc.getExternalImports();
+        List<BundleDescription.ImportPackage> externalImports = desc.getExternalImports();
         assertEquals(2, externalImports.size());
         assertEquals("com.thoughtworks.xstream", externalImports.get(0).getName());
-        assertEquals("\"1.3\"", externalImports.get(0).getAttributes().get("version"));
+        assertEquals("1.3", externalImports.get(0).getAttributes().get("version"));
         assertEquals("com.thoughtworks.xstream.converters", externalImports.get(1).getName());
       
     }
 }

Modified: geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/HeaderParserTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/HeaderParserTest.java?rev=891926&r1=891925&r2=891926&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/HeaderParserTest.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-kernel/src/test/java/org/apache/geronimo/kernel/osgi/HeaderParserTest.java
Thu Dec 17 21:36:02 2009
@@ -77,7 +77,7 @@
         assertEquals("org.apache.geronimo.kernel.rmi", paths.get(0).getName());
         assertEquals(0, paths.get(0).getAttributes().size());
         assertEquals(1, paths.get(0).getDirectives().size());
-        assertEquals("\"javax.rmi.ssl,org.apache.geronimo.gbean,org.slf4j\"", paths.get(0).getDirective("uses"));
+        assertEquals("javax.rmi.ssl,org.apache.geronimo.gbean,org.slf4j", paths.get(0).getDirective("uses"));
         
         assertEquals("org.apache.geronimo.kernel.proxy", paths.get(1).getName());
         assertEquals(0, paths.get(1).getAttributes().size());
@@ -90,7 +90,7 @@
         
         assertEquals("com.thoughtworks.xstream", paths.get(0).getName());
         assertEquals(1, paths.get(0).getAttributes().size());
-        assertEquals("\"1.3\"", paths.get(0).getAttribute("version"));
+        assertEquals("1.3", paths.get(0).getAttribute("version"));
         assertEquals(0, paths.get(0).getDirectives().size());
         
         assertEquals("com.thoughtworks.xstream.converters", paths.get(1).getName());



Mime
View raw message