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.IOException;
   21   import java.io.InputStream;
   22   import java.util.Collection;
   23   import java.util.Collections;
   24   import java.util.Enumeration;
   25   import java.util.Iterator;
   26   import java.util.LinkedList;
   27   import java.util.jar.JarEntry;
   28   import java.util.jar.JarFile;
   29   import java.util.jar.Manifest;
   30   import java.util.zip.ZipEntry;
   31   
   32   /**
   33    * @version $Rev: 726699 $ $Date: 2008-12-15 06:30:36 -0800 (Mon, 15 Dec 2008) $
   34    */
   35   public class NestedJarFile extends JarFile {
   36       private JarFile baseJar;
   37       private String basePath;
   38       private boolean isClosed = false;
   39       private boolean manifestLoaded = false;
   40       private Manifest manifest;
   41       private File tempFile;
   42   
   43       public NestedJarFile(JarFile jarFile, String path) throws IOException {
   44           super(DeploymentUtil.DUMMY_JAR_FILE);
   45   
   46           // verify that the jar actually contains that path
   47           JarEntry targetEntry = jarFile.getJarEntry(path + "/");
   48           if (targetEntry == null) {
   49               targetEntry = jarFile.getJarEntry(path);
   50               if (targetEntry == null) {
   51                   throw new IOException("Jar entry does not exist: jarFile=" + jarFile.getName() + ", path=" + path);
   52               }
   53           }
   54   
   55           if (targetEntry.isDirectory()) {
   56           	if(targetEntry instanceof UnpackedJarEntry) {
   57           		//unpacked nested module inside unpacked ear
   58           		File targetFile = ((UnpackedJarEntry) targetEntry).getFile();
   59           		baseJar = new UnpackedJarFile(targetFile);
   60                   basePath = "";
   61           	} else {
   62           		baseJar = jarFile;
   63           		if (!path.endsWith("/")) {
   64                       path += "/";
   65                   }
   66                   basePath = path;
   67           	}
   68           } else {
   69               if (targetEntry instanceof UnpackedJarEntry) {
   70                   // for unpacked jars we don't need to copy the jar file
   71                   // out to a temp directory, since it is already available
   72                   // as a raw file
   73                   File targetFile = ((UnpackedJarEntry) targetEntry).getFile();
   74                   baseJar = new JarFile(targetFile);
   75                   basePath = "";
   76               } else {
   77                   tempFile = DeploymentUtil.toFile(jarFile, targetEntry.getName());
   78                   baseJar = new JarFile(tempFile);
   79                   basePath = "";
   80               }
   81           }
   82       }
   83   
   84       public boolean isUnpacked() {
   85           if (isClosed) {
   86               throw new IllegalStateException("NestedJarFile is closed");
   87           }
   88   
   89           return ( basePath.length() > 0 ) ||
   90                  ( ( baseJar != null ) && ( baseJar instanceof UnpackedJarFile ) );
   91       }
   92   
   93       public boolean isPacked() {
   94           if (isClosed) {
   95               throw new IllegalStateException("NestedJarFile is closed");
   96           }
   97   
   98           return ( basePath.length() == 0 ) &&
   99                  ( ( baseJar == null ) || !( baseJar instanceof UnpackedJarFile ) );
  100       }
  101   
  102       public JarFile getBaseJar() {
  103           if (isClosed) {
  104               throw new IllegalStateException("NestedJarFile is closed");
  105           }
  106           return baseJar;
  107       }
  108   
  109       public String getBasePath() {
  110           if (isClosed) {
  111               throw new IllegalStateException("NestedJarFile is closed");
  112           }
  113           return basePath;
  114       }
  115   
  116       public Manifest getManifest() throws IOException {
  117           if (isClosed) {
  118               throw new IllegalStateException("NestedJarFile is closed");
  119           }
  120   
  121           if (!manifestLoaded) {
  122               JarEntry manifestEntry = getBaseEntry("META-INF/MANIFEST.MF");
  123   
  124               if (manifestEntry != null && !manifestEntry.isDirectory()) {
  125                   InputStream in = null;
  126                   try {
  127                       in = baseJar.getInputStream(manifestEntry);
  128                       manifest = new Manifest(in);
  129                   } finally {
  130                       if (in != null) {
  131                           try {
  132                               in.close();
  133                           } catch (IOException e) {
  134                               // ignore
  135                           }
  136                       }
  137                   }
  138               }
  139               manifestLoaded = true;
  140           }
  141           return manifest;
  142       }
  143   
  144       public NestedJarEntry getNestedJarEntry(String name) {
  145           if (isClosed) {
  146               throw new IllegalStateException("NestedJarFile is closed");
  147           }
  148   
  149           JarEntry baseEntry = getBaseEntry(name);
  150           if (baseEntry == null) {
  151               return null;
  152           }
  153           return new NestedJarEntry(name, baseEntry, getManifestSafe());
  154       }
  155   
  156       public JarEntry getJarEntry(String name) {
  157           if (isClosed) {
  158               throw new IllegalStateException("NestedJarFile is closed");
  159           }
  160   
  161           return getNestedJarEntry(name);
  162       }
  163   
  164       public ZipEntry getEntry(String name) {
  165           if (isClosed) {
  166               throw new IllegalStateException("NestedJarFile is closed");
  167           }
  168   
  169           return getNestedJarEntry(name);
  170       }
  171   
  172       public Enumeration entries() {
  173           if (isClosed) {
  174               throw new IllegalStateException("NestedJarFile is closed");
  175           }
  176   
  177           Collection baseEntries = Collections.list(baseJar.entries());
  178           Collection entries = new LinkedList();
  179           for (Iterator iterator = baseEntries.iterator(); iterator.hasNext();) {
  180               JarEntry baseEntry = (JarEntry) iterator.next();
  181               String path = baseEntry.getName();
  182               if (path.startsWith(basePath)) {
  183                   entries.add(new NestedJarEntry(path.substring(basePath.length()), baseEntry, getManifestSafe()));
  184               }
  185           }
  186           return Collections.enumeration(entries);
  187       }
  188   
  189       public InputStream getInputStream(ZipEntry zipEntry) throws IOException {
  190           if (isClosed) {
  191               throw new IllegalStateException("NestedJarFile is closed");
  192           }
  193   
  194           JarEntry baseEntry;
  195           if (zipEntry instanceof NestedJarEntry) {
  196               baseEntry = ((NestedJarEntry)zipEntry).getBaseEntry();
  197           } else {
  198               baseEntry = getBaseEntry(zipEntry.getName());
  199           }
  200   
  201           if (baseEntry == null) {
  202               throw new IOException("Entry not found: name=" + zipEntry.getName());
  203           } else if (baseEntry.isDirectory()) {
  204               return new DeploymentUtil.EmptyInputStream();
  205           }
  206           return baseJar.getInputStream(baseEntry);
  207       }
  208   
  209       public String getName() {
  210           return baseJar.getName();
  211       }
  212   
  213       /**
  214        * Always returns -1.
  215        * @return -1
  216        */
  217       public int size() {
  218           if (isClosed) {
  219               throw new IllegalStateException("NestedJarFile is closed");
  220           }
  221           return -1;
  222       }
  223   
  224       public void close() throws IOException {
  225           if (isClosed) {
  226               return;
  227           }
  228   
  229           try {
  230               try {
  231                   super.close();
  232               } catch(IOException ignored) {
  233               }
  234               if (baseJar != null && basePath.length() == 0) {
  235                   // baseJar is created by us.  We should be closing it too.
  236                   baseJar.close();
  237               }
  238           } finally {
  239               isClosed = true;
  240               baseJar = null;
  241               basePath = null;
  242               manifestLoaded = false;
  243               manifest = null;
  244               if (tempFile != null) {
  245                   tempFile.delete();
  246                   tempFile = null;
  247               }
  248           }
  249       }
  250   
  251       protected void finalize() throws IOException {
  252           close();
  253       }
  254   
  255       private JarEntry getBaseEntry(String name) {
  256           return baseJar.getJarEntry(basePath + name);
  257       }
  258   
  259       private Manifest getManifestSafe() {
  260           Manifest manifest = null;
  261           try {
  262               manifest = getManifest();
  263           } catch (IOException e) {
  264               // ignore
  265           }
  266           return manifest;
  267       }
  268   
  269   }

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