001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.deployment.util; 019 020 import java.io.File; 021 import java.io.IOException; 022 import java.io.InputStream; 023 import java.net.JarURLConnection; 024 import java.net.URL; 025 import java.net.URLConnection; 026 import java.util.jar.JarFile; 027 import java.util.zip.ZipException; 028 029 /** 030 * The URLType class assigns type to resources, i.e. files or URLs. 031 * <p> 032 * The following types are available: 033 * <ul> 034 * <li><b>UNPACKED_ARCHIVE</b> - directory with META-INF/MANIFEST.MF 035 * <li><b>PACKED_ARCHIVE</b> - file with META-INF/MANIFEST.MF 036 * <li><b>COLLECTION</b> - directory with no META-INF/MANIFEST.MF 037 * <li><b>RESOURCE</b> - none of the above 038 * </ul> 039 * 040 * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $ 041 */ 042 public class URLType { 043 public static final String MANIFEST_LOCATION = "META-INF/MANIFEST.MF"; 044 045 public static final URLType RESOURCE = new URLType("RESOURCE"); 046 public static final URLType COLLECTION = new URLType("COLLECTION"); 047 public static final URLType PACKED_ARCHIVE = new URLType("PACKED_ARCHIVE"); 048 public static final URLType UNPACKED_ARCHIVE = new URLType("UNPACKED_ARCHIVE"); 049 050 public static URLType getType(File file) throws IOException { 051 if (file.isDirectory()) { 052 // file is a directory - see if it has a manifest 053 // we check for an actual manifest file to keep things consistent with a packed archive 054 if (new File(file, MANIFEST_LOCATION).exists()) { 055 return UNPACKED_ARCHIVE; 056 } else { 057 return COLLECTION; 058 } 059 } else { 060 // we have a regular file - see if it contains a manifest 061 try { 062 JarFile jar = null; 063 try { 064 jar = new JarFile(file); 065 jar.getManifest(); 066 } finally { 067 if (jar != null) { 068 jar.close(); 069 } 070 } 071 return PACKED_ARCHIVE; 072 } catch (ZipException e) { 073 return RESOURCE; 074 } 075 } 076 } 077 078 /** 079 * Returns the type of url 080 * 081 * @param url 082 * @return type of the url 083 * @throws IOException whenever there're problems with accessing portion of the url 084 */ 085 public static URLType getType(URL url) throws IOException { 086 if (url.toString().endsWith("/")) { 087 URL metaInfURL = new URL(url, MANIFEST_LOCATION); 088 URLConnection urlConnection = metaInfURL.openConnection(); 089 urlConnection.connect(); 090 try { 091 InputStream is = urlConnection.getInputStream(); 092 is.close(); 093 return UNPACKED_ARCHIVE; 094 } catch (IOException e) { 095 return COLLECTION; 096 } 097 } else { 098 URL jarURL = new URL("jar:" + url.toString() + "!/"); 099 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 }