KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > context > jar > NestedJarHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.virtual.plugins.context.jar;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URISyntaxException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.jar.JarEntry JavaDoc;
34 import java.util.jar.JarFile JavaDoc;
35
36 import org.jboss.virtual.spi.VFSContext;
37 import org.jboss.virtual.spi.VirtualFileHandler;
38
39 /**
40  * Nested Jar Handler.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 1.1 $
45  */

46 public class NestedJarHandler extends AbstractJarHandler
47 {
48    /** serialVersionUID */
49    private static final long serialVersionUID = 1L;
50
51    /** The jar entry */
52    private transient JarEntry JavaDoc entry;
53    
54    /** The temporary file */
55    private transient File JavaDoc temp;
56
57    /**
58     * Create a temporary jar
59     *
60     * @param temp the temporary file
61     * @param parentJar the jar
62     * @param entry the jar entry
63     * @return the jar file
64     * @throws IOException for any error
65     */

66    private static JarFile JavaDoc createTempJar(File JavaDoc temp, JarFile JavaDoc parentJar, JarEntry JavaDoc entry) throws IOException JavaDoc
67    {
68       InputStream JavaDoc inputStream = parentJar.getInputStream(entry);
69       try
70       {
71          FileOutputStream JavaDoc outputStream = new FileOutputStream JavaDoc(temp);
72          try
73          {
74             byte[] buffer = new byte[8096];
75             int read = inputStream.read(buffer);
76             while (read != -1)
77             {
78                outputStream.write(buffer, 0, read);
79                read = inputStream.read(buffer);
80             }
81          }
82          finally
83          {
84             outputStream.close();
85          }
86       }
87       finally
88       {
89          try
90          {
91             inputStream.close();
92          }
93          catch (IOException JavaDoc ignored)
94          {
95          }
96       }
97       
98       return new JarFile JavaDoc(temp);
99    }
100
101    /**
102     * Create a new NestedJarHandler.
103     *
104     * @param context the context
105     * @param parent the parent
106     * @param parentJar the parent jar file
107     * @param entry the jar entry
108     * @param url the url
109     * @throws IOException for an error accessing the file system
110     * @throws IllegalArgumentException for a null context, url or vfsPath
111     */

112    public NestedJarHandler(VFSContext context, VirtualFileHandler parent,
113          JarFile JavaDoc parentJar, JarEntry JavaDoc entry, URL JavaDoc url, String JavaDoc entryName)
114       throws IOException JavaDoc
115    {
116       super(context, parent, url, entryName);
117
118       
119       try
120       {
121          temp = File.createTempFile("nestedjar", null);
122          temp.deleteOnExit();
123
124          initJarFile(createTempJar(temp, parentJar, entry));
125       }
126       catch (IOException JavaDoc original)
127       {
128          // Fix the context of the error message
129
IOException JavaDoc e = new IOException JavaDoc("Error opening jar file: " + url + " reason=" + original.getMessage());
130          e.setStackTrace(original.getStackTrace());
131          throw e;
132       }
133       
134       this.entry = entry;
135    }
136    
137    /**
138     * Get the entry
139     *
140     * @return the file
141     */

142    protected JarEntry JavaDoc getEntry()
143    {
144       checkClosed();
145       return entry;
146    }
147
148    @Override JavaDoc
149    public long getLastModified() throws IOException JavaDoc
150    {
151       return getEntry().getTime();
152    }
153
154    @Override JavaDoc
155    public long getSize() throws IOException JavaDoc
156    {
157       return getEntry().getSize();
158    }
159
160    /**
161     * Overriden to return the raw tmp jar file stream
162     */

163    @Override JavaDoc
164    public InputStream JavaDoc openStream() throws IOException JavaDoc
165    {
166       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(temp);
167       return fis;
168    }
169
170    @Override JavaDoc
171    public URL JavaDoc toURL() throws MalformedURLException JavaDoc, URISyntaxException JavaDoc
172    {
173       return new URL JavaDoc("jar:" + temp.toURL() + "!/");
174    }
175
176    /**
177     * Restore the jar file from the parent jar and entry name
178     *
179     * @param in
180     * @throws IOException
181     * @throws ClassNotFoundException
182     */

183    private void readObject(ObjectInputStream JavaDoc in)
184       throws IOException JavaDoc, ClassNotFoundException JavaDoc
185    {
186       JarFile JavaDoc parentJar = super.getJar();
187       // Initialize the transient values
188
entry = parentJar.getJarEntry(getName());
189       temp = File.createTempFile("nestedjar", null);
190       temp.deleteOnExit();
191       createTempJar(temp, parentJar, entry);
192       // Initial the parent jar entries
193
super.initJarFile(parentJar);
194    }
195
196 }
197
Popular Tags