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   
   18   package org.apache.geronimo.deployment.util;
   19   
   20   import java.io.File;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.net.JarURLConnection;
   24   import java.net.URL;
   25   import java.net.URLConnection;
   26   import java.util.jar.JarFile;
   27   import java.util.zip.ZipException;
   28   
   29   /**
   30    * The URLType class assigns type to resources, i.e. files or URLs.
   31    * <p>
   32    * The following types are available:
   33    * <ul>
   34    *  <li><b>UNPACKED_ARCHIVE</b> - directory with META-INF/MANIFEST.MF 
   35    *  <li><b>PACKED_ARCHIVE</b> - file with META-INF/MANIFEST.MF
   36    *  <li><b>COLLECTION</b> - directory with no META-INF/MANIFEST.MF
   37    *  <li><b>RESOURCE</b> -  none of the above
   38    * </ul>
   39    *
   40    * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
   41    */
   42   public class URLType {
   43       public static final String MANIFEST_LOCATION = "META-INF/MANIFEST.MF";
   44   
   45       public static final URLType RESOURCE = new URLType("RESOURCE");
   46       public static final URLType COLLECTION = new URLType("COLLECTION");
   47       public static final URLType PACKED_ARCHIVE = new URLType("PACKED_ARCHIVE");
   48       public static final URLType UNPACKED_ARCHIVE = new URLType("UNPACKED_ARCHIVE");
   49   
   50       public static URLType getType(File file) throws IOException {
   51           if (file.isDirectory()) {
   52               // file is a directory - see if it has a manifest
   53               // we check for an actual manifest file to keep things consistent with a packed archive
   54               if (new File(file, MANIFEST_LOCATION).exists()) {
   55                   return UNPACKED_ARCHIVE;
   56               } else {
   57                   return COLLECTION;
   58               }
   59           } else {
   60               // we have a regular file - see if it contains a manifest
   61               try {
   62                   JarFile jar = null;
   63                   try {
   64                       jar = new JarFile(file);
   65                       jar.getManifest();
   66                   } finally {
   67                       if (jar != null) {
   68                           jar.close();
   69                       }
   70                   }
   71                   return PACKED_ARCHIVE;
   72               } catch (ZipException e) {
   73                   return RESOURCE;
   74               }
   75           }
   76       }
   77   
   78       /**
   79        * Returns the type of url
   80        * 
   81        * @param url 
   82        * @return type of the url
   83        * @throws IOException whenever there're problems with accessing portion of the url
   84        */
   85       public static URLType getType(URL url) throws IOException {
   86           if (url.toString().endsWith("/")) {
   87               URL metaInfURL = new URL(url, MANIFEST_LOCATION);
   88               URLConnection urlConnection = metaInfURL.openConnection();
   89               urlConnection.connect();
   90               try {
   91                   InputStream is = urlConnection.getInputStream();
   92                   is.close();
   93                   return UNPACKED_ARCHIVE;
   94               } catch (IOException e) {
   95                   return COLLECTION;
   96               }
   97           } else {
   98               URL jarURL = new URL("jar:" + url.toString() + "!/");
   99               JarURLConnection jarConnection = (JarURLConnection) jarURL.openConnection();
  100               try {
  101                   jarConnection.getManifest();
  102                   return PACKED_ARCHIVE;
  103               } catch (ZipException e) {
  104                   return RESOURCE;
  105               }
  106           }
  107       }
  108   
  109       private final String desc;
  110   
  111       private URLType(final String desc) {
  112           this.desc = desc;
  113       }
  114   
  115       public boolean equals(Object obj) {
  116           return this == obj;
  117       }
  118   
  119       public String toString() {
  120           return desc;
  121       }
  122   }

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