Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.util » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.deployment.util;
   18   
   19   import java.io.File;
   20   import java.io.FileInputStream;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.net.URI;
   24   import java.util.Collection;
   25   import java.util.Collections;
   26   import java.util.Enumeration;
   27   import java.util.Iterator;
   28   import java.util.LinkedList;
   29   import java.util.jar.JarEntry;
   30   import java.util.jar.JarFile;
   31   import java.util.jar.Manifest;
   32   import java.util.zip.ZipEntry;
   33   
   34   /**
   35    * @version $Rev: 726699 $ $Date: 2008-12-15 06:30:36 -0800 (Mon, 15 Dec 2008) $
   36    */
   37   public class UnpackedJarFile extends JarFile {
   38       private final File baseDir;
   39       private boolean manifestLoaded = false;
   40       private Manifest manifest;
   41   
   42       public UnpackedJarFile(File baseDir) throws IOException {
   43           super(DeploymentUtil.DUMMY_JAR_FILE);
   44           this.baseDir = baseDir;
   45           if (!baseDir.isDirectory()) {
   46               throw new IOException("File must be a directory: file=" + baseDir.getAbsolutePath());
   47           }
   48       }
   49   
   50       public File getBaseDir() {
   51           return baseDir;
   52       }
   53   
   54       public Manifest getManifest() throws IOException {
   55           if (!manifestLoaded) {
   56               File manifestFile = getFile("META-INF/MANIFEST.MF");
   57   
   58               if (manifestFile != null && manifestFile.isFile()) {
   59                   FileInputStream in = null;
   60                   try {
   61                       in = new FileInputStream(manifestFile);
   62                       manifest = new Manifest(in);
   63                   } finally {
   64                       if (in != null) {
   65                           try {
   66                               in.close();
   67                           } catch (IOException e) {
   68                               // ignore
   69                           }
   70                       }
   71                   }
   72               }
   73               manifestLoaded = true;
   74           }
   75           return manifest;
   76       }
   77   
   78       public UnpackedJarEntry getUnpackedJarEntry(String name) {
   79           File file = getFile(name);
   80           if (file == null) {
   81               return null;
   82           }
   83           return new UnpackedJarEntry(name, file, getManifestSafe());
   84       }
   85   
   86       public JarEntry getJarEntry(String name) {
   87           return getUnpackedJarEntry(name);
   88       }
   89   
   90       public ZipEntry getEntry(String name) {
   91           return getUnpackedJarEntry(name);
   92       }
   93   
   94       public Enumeration entries() {
   95           Collection files = DeploymentUtil.listRecursiveFiles(baseDir);
   96   
   97           Manifest manifest = getManifestSafe();
   98           LinkedList entries = new LinkedList();
   99           URI baseURI = baseDir.getAbsoluteFile().toURI();
  100           for (Iterator iterator = files.iterator(); iterator.hasNext();) {
  101               File entryFile = ((File) iterator.next()).getAbsoluteFile();
  102               URI entryURI = entryFile.toURI();
  103               URI relativeURI = baseURI.relativize(entryURI);
  104               entries.add(new UnpackedJarEntry(relativeURI.getPath(), entryFile, manifest));
  105           }
  106           return Collections.enumeration(entries);
  107       }
  108   
  109       public InputStream getInputStream(ZipEntry zipEntry) throws IOException {
  110           File file;
  111           if (zipEntry instanceof UnpackedJarEntry) {
  112               file = ((UnpackedJarEntry)zipEntry).getFile();
  113           } else {
  114               file = getFile(zipEntry.getName());
  115           }
  116   
  117           if (file == null) {
  118               throw new IOException("Entry not found: name=" + zipEntry.getName());
  119           } else if (file.isDirectory()) {
  120               return new DeploymentUtil.EmptyInputStream();
  121           }
  122           return new FileInputStream(file);
  123       }
  124   
  125       public String getName() {
  126           return baseDir.getAbsolutePath();
  127       }
  128   
  129       /**
  130        * Always returns -1.
  131        * @return -1
  132        */
  133       public int size() {
  134           return -1;
  135       }
  136   
  137       public void close() throws IOException {
  138           try {
  139               super.close();
  140           } catch(IOException ignored) {
  141           }
  142       }
  143   
  144       protected void finalize() throws IOException {
  145       }
  146   
  147       public File getFile(String name) {
  148           File file = new File(baseDir, name);
  149           if (!file.exists()) {
  150               return null;
  151           }
  152           return file;
  153       }
  154   
  155       private Manifest getManifestSafe() {
  156           Manifest manifest = null;
  157           try {
  158               manifest = getManifest();
  159           } catch (IOException e) {
  160               // ignore
  161           }
  162           return manifest;
  163       }
  164   }

Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.util » [javadoc | source]