001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JarURLHelper.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * JarURLHelper.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on May 18, 2005, 12:54 PM
037: */package com.sun.jbi.management.internal.support;
038:
039: import com.sun.jbi.StringTranslator;
040: import com.sun.jbi.management.LocalStringKeys;
041:
042: import java.net.URL;
043: import java.net.JarURLConnection;
044:
045: import java.util.zip.ZipFile;
046:
047: /**
048: * Helper class for Jar URLs.
049: *
050: * @author Sun Microsystems, Inc.
051: */
052: public class JarURLHelper {
053: /**
054: * URL Protocol : File.
055: */
056: public static final String FILE = "file";
057:
058: /**
059: * URL Protocol : HTTP.
060: */
061: public static final String HTTP = "http";
062:
063: /**
064: * URL Protocol : jar.
065: */
066: public static final String JAR = "jar";
067:
068: /**
069: * Jar Separator.
070: */
071: public static final String JAR_SEPARATOR = "!/";
072:
073: /**
074: * Get the ZipFile from the fileURL.
075: *
076: * @param fileURL is the File URL.
077: * @param translator is the StringTranslator.
078: * @throws java.io.IOException on IO related errors.
079: * @return the ZipFile for the URL.
080: */
081: public static ZipFile getZipFileFromURL(URL fileURL,
082: StringTranslator translator) throws java.io.IOException {
083: if (FILE.equals(fileURL.getProtocol())) {
084: return new ZipFile(fileURL.getPath());
085: } else if (JAR.equals(fileURL.getProtocol())) {
086: JarURLConnection conxn = (JarURLConnection) fileURL
087: .openConnection();
088: conxn.setUseCaches(false);
089: return conxn.getJarFile();
090: } else {
091: String errMsg = translator.getString(
092: LocalStringKeys.UNSUPPORTED_URL_PROTOCOL, fileURL
093: .getProtocol());
094:
095: throw new java.io.IOException(errMsg);
096: }
097: }
098:
099: /**
100: * Convert the URL to a JAR URL, for example if the File URL is
101: * file://deploy/SA.zip it will be converted to jar:file://deploy/SA.zip!/ .
102: *
103: * @param fileURL is the original JarFile URL.
104: * @param translator is the StringTranslator
105: * @return a jar URL created from the fileURL.
106: * @throws java.io.IOException if the URL cannot be converted to a JAR URL
107: */
108: public static URL convertToJarURL(URL fileURL,
109: StringTranslator translator) throws java.io.IOException {
110: try {
111: if (JAR.equalsIgnoreCase(fileURL.getProtocol())) {
112: return fileURL;
113: }
114: if (FILE.equalsIgnoreCase(fileURL.getProtocol())
115: || HTTP.equalsIgnoreCase(fileURL.getProtocol())) {
116: return new URL(JAR + ":" + fileURL.toString()
117: + JAR_SEPARATOR);
118: } else {
119: String errMsg = translator.getString(
120: LocalStringKeys.UNSUPPORTED_URL_PROTOCOL,
121: fileURL.getProtocol());
122:
123: throw new java.io.IOException(errMsg);
124: }
125: } catch (Exception ex) {
126: throw new java.io.IOException(ex.getMessage());
127: }
128: }
129:
130: }
|