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: */package org.apache.openejb.util;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.FileNotFoundException;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.util.jar.JarEntry;
024: import java.util.jar.JarFile;
025: import java.util.jar.JarInputStream;
026: import java.util.jar.JarOutputStream;
027:
028: import org.apache.openejb.OpenEJBException;
029:
030: /**
031: * @version $Rev: 546178 $ $Date: 2007-06-11 08:58:12 -0700 $
032: */
033: public class JarUtils {
034:
035: private static Messages messages = new Messages(
036: "org.apache.openejb.util.resources");
037:
038: public static void addFileToJar(String jarFile, String file)
039: throws OpenEJBException {
040: try {
041: JarInputStream jis = new JarInputStream(
042: new FileInputStream(jarFile));
043: File tempJar = File.createTempFile("temp", "jar");
044: JarOutputStream jos = new JarOutputStream(
045: new FileOutputStream(tempJar));
046: JarEntry nextJarEntry = null;
047: while ((nextJarEntry = jis.getNextJarEntry()) != null) {
048: jos.putNextEntry(nextJarEntry);
049: }
050: jis.close();
051: jos.putNextEntry(new JarEntry(file));
052: FileInputStream fis = new FileInputStream(file);
053: for (int c = fis.read(); c != -1; c = fis.read()) {
054: jos.write(c);
055: }
056: fis.close();
057: jos.close();
058:
059: File oldJar = new File(jarFile);
060: oldJar.delete();
061: tempJar.renameTo(oldJar);
062: } catch (FileNotFoundException e) {
063: throw new OpenEJBException(messages.format("file.0003",
064: file, jarFile, e.getMessage()), e);
065: } catch (IOException e) {
066: throw new OpenEJBException(messages.format("file.0003",
067: file, jarFile, e.getMessage()), e);
068: }
069:
070: }
071:
072: @SuppressWarnings("unchecked")
073: public static JarFile getJarFile(String jarFile)
074: throws OpenEJBException {
075: /*[1.1] Get the jar ***************/
076: JarFile jar = null;
077: try {
078: File file = new File(jarFile);
079: jar = new JarFile(file);
080: } catch (FileNotFoundException e) {
081: throw new OpenEJBException(messages.format("file.0001",
082: jarFile, e.getLocalizedMessage()), e);
083: } catch (IOException e) {
084: throw new OpenEJBException(messages.format("file.0002",
085: jarFile, e.getLocalizedMessage()), e);
086: }
087: return jar;
088: }
089:
090: @SuppressWarnings("unchecked")
091: public static ClassLoader getContextClassLoader() {
092: return (ClassLoader) java.security.AccessController
093: .doPrivileged(new java.security.PrivilegedAction() {
094: public Object run() {
095: return Thread.currentThread()
096: .getContextClassLoader();
097: }
098: });
099: }
100: }
|