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 java.io.File;
   22   import java.io.IOException;
   23   import java.util.jar.JarFile;
   24   import java.util.jar.Manifest;
   25   
   26   import org.osgi.framework.Constants;
   27   import org.osgi.framework.Version;
   28   
   29   /** BundleVersionInfo based on a bundle jar file */
   30   public class FileBundleVersionInfo extends BundleVersionInfo<File> {
   31   
   32       private final String symbolicName;
   33       private final Version version;
   34       private final boolean isSnapshot;
   35       private final long lastModified;
   36       private final File source;
   37   
   38       public FileBundleVersionInfo(File bundle) throws IOException {
   39           source = bundle;
   40           final JarFile f = new JarFile(bundle);
   41           try {
   42               final Manifest m = f.getManifest();
   43               if(m == null) {
   44                   symbolicName = null;
   45                   version = null;
   46                   isSnapshot = false;
   47                   lastModified = BND_LAST_MODIFIED_MISSING;
   48               } else {
   49                   symbolicName = m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
   50                   final String v = m.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
   51                   version = v == null ? null : new Version(v);
   52                   isSnapshot = v != null && v.contains(SNAPSHOT_MARKER);
   53                   final String last = m.getMainAttributes().getValue(BND_LAST_MODIFIED);
   54                   long lastMod = BND_LAST_MODIFIED_MISSING;
   55                   if(last != null) {
   56                       try {
   57                           lastMod = Long.parseLong(last);
   58                       } catch(NumberFormatException ignore) {
   59                       }
   60                   }
   61                   lastModified = lastMod;
   62               }
   63           } finally {
   64               f.close();
   65           }
   66       }
   67   
   68       @Override
   69       public String toString() {
   70           return getClass().getSimpleName() + " " + source.getAbsolutePath();
   71       }
   72   
   73       public boolean isBundle() {
   74           return symbolicName != null;
   75       }
   76   
   77       public long getBundleLastModified() {
   78           return lastModified;
   79       }
   80   
   81       public String getBundleSymbolicName() {
   82           return symbolicName;
   83       }
   84   
   85       public File getSource() {
   86           return source;
   87       }
   88   
   89       public Version getVersion() {
   90           return version;
   91       }
   92   
   93       public boolean isSnapshot() {
   94           return isSnapshot;
   95       }
   96   }

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