KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.JarURLConnection JavaDoc;
27 import java.net.URL JavaDoc;
28
29 import org.jboss.virtual.spi.VFSContext;
30 import org.jboss.virtual.spi.VirtualFileHandler;
31
32 /**
33  * JarHandler.
34  *
35  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
36  * @version $Revision: 1.1 $
37  */

38 public class JarHandler extends AbstractJarHandler
39 {
40    /** serialVersionUID */
41    private static final long serialVersionUID = 1L;
42
43    /**
44     * Create a new JarHandler.
45     *
46     * @param context the context
47     * @param parent the parent
48     * @param url the url
49     * @param name the name
50     * @throws IOException for an error accessing the file system
51     * @throws IllegalArgumentException for a null context, url or vfsPath
52     */

53    public JarHandler(VFSContext context, VirtualFileHandler parent, URL JavaDoc url, String JavaDoc name) throws IOException JavaDoc
54    {
55       super(context, parent, url, name);
56
57       try
58       {
59          JarURLConnection JavaDoc connection = (JarURLConnection JavaDoc) url.openConnection();
60          initJarFile(connection.getJarFile());
61       }
62       catch (IOException JavaDoc original)
63       {
64          // Fix the context of the error message
65
IOException JavaDoc e = new IOException JavaDoc("Error opening jar file: " + url + " reason=" + original.getMessage());
66          e.setStackTrace(original.getStackTrace());
67          throw e;
68       }
69    }
70
71    /**
72     * Override to return the input stream of the underlying url rather than
73     * a JarInputStream for the jar as this is not usable for copying the
74     * jar.
75     */

76    @Override JavaDoc
77    public InputStream JavaDoc openStream() throws IOException JavaDoc
78    {
79       checkClosed();
80       String JavaDoc jarURL = getURL().toString();
81       String JavaDoc underlyingURL = jarURL.substring(4, jarURL.length()-2);
82       URL JavaDoc realURL = new URL JavaDoc(underlyingURL);
83       InputStream JavaDoc is = realURL.openStream();
84       return is;
85    }
86 }
87
Popular Tags