Save This Page
Home » org.apache.sling.commons.osgi-2.0.6-source-release » org.apache.sling.commons.osgi.bundleversion » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   package org.apache.sling.commons.osgi.bundleversion;
   20   
   21   import org.osgi.framework.Version;
   22   
   23   /** Provides bundle version information, which can be
   24    *  extracted from bundle files or Bundle objects.
   25    */
   26   public abstract class BundleVersionInfo<T> implements Comparable<BundleVersionInfo<?>> {
   27       
   28       private static final int A_GREATER = 1; 
   29       private static final int B_GREATER = -1;
   30       private static final int EQUAL = 0;
   31       
   32       /** Marker used by Maven to identify snapshots */
   33       public static final String SNAPSHOT_MARKER = "SNAPSHOT";
   34       
   35       /** Name of the BND attribute that provides the bundle's last modified timestamp */
   36       public static final String BND_LAST_MODIFIED = "Bnd-LastModified";
   37       
   38       /** Value for {@link #getBundleLastModified} if corresponding header
   39        *  is not present
   40        */
   41       public static final long BND_LAST_MODIFIED_MISSING = -1L;
   42       
   43       /** Return the source of information: underlying File or Bundle */
   44       public abstract T getSource();
   45       
   46       /** True if the provided data is a valid bundle */
   47       public abstract boolean isBundle();
   48       
   49       /** Return the bundle symbolic name, null if not available */
   50       public abstract String getBundleSymbolicName();
   51       
   52       /** Return the bundle version, null if not available */
   53       public abstract Version getVersion();
   54       
   55       /** True if the bundle version indicates a snapshot */
   56       public abstract boolean isSnapshot();
   57       
   58       /** Return the bundle last modification time, based on the BND_LAST_MODIFIED 
   59        *  manifest header, if available. This is *not* the Bundle.getLastModified()
   60        *  value, which refers to actions in the OSGi framework.
   61        *  @return BND_LAST_MODIFIED_MISSING if header not supplied */
   62       public abstract long getBundleLastModified();
   63       
   64       
   65       /** Compare based on bundle version info, and for snapshots
   66        *  based on {@link #getBundleLastModified}
   67        */
   68       public int compareTo(BundleVersionInfo<?> other) {
   69           // Handle null values
   70           if(other == null) {
   71               throw new IllegalArgumentException("b is null, cannot compare");
   72           }
   73           
   74           // Handle non-bundles: we don't want them!
   75           if(!isBundle()) {
   76               throw new IllegalArgumentException("Not a bundle, cannot compare: " + this);
   77           }
   78           if(!other.isBundle()) {
   79               throw new IllegalArgumentException("Not a bundle, cannot compare:" + other);
   80           }
   81           
   82           // First compare symbolic names
   83           int result = getBundleSymbolicName().compareTo(other.getBundleSymbolicName());
   84           
   85           // Then compare versions
   86           if(result == EQUAL) {
   87               final Version va = getVersion();
   88               final Version vb = other.getVersion();
   89               if(va == null && vb == null) {
   90                   // result = EQUAL
   91               } else if(vb == null) {
   92                   result = A_GREATER;
   93               } else if(va == null) {
   94                   result = B_GREATER;
   95               } else {
   96                   result = va.compareTo(vb);
   97               }
   98           }
   99           
  100           // Then, if snapshots, compare modification times, more recent comes first
  101           if(result == EQUAL && isSnapshot()) {
  102               final long ma = getBundleLastModified();
  103               final long mb = other.getBundleLastModified();
  104               if(ma > mb) {
  105                   result = A_GREATER;
  106               } else if(mb > ma) {
  107                   result = B_GREATER;
  108               }
  109           }
  110           
  111           return result;
  112       }
  113   
  114   }

Save This Page
Home » org.apache.sling.commons.osgi-2.0.6-source-release » org.apache.sling.commons.osgi.bundleversion » [javadoc | source]