KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > jasperreports > util > JarURLStreamHandler


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.api.engine.jasperreports.util;
22
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.net.URLStreamHandler JavaDoc;
28
29 import org.apache.commons.collections.ReferenceMap;
30
31 import com.jaspersoft.jasperserver.api.JSException;
32 import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
33
34 /**
35  * @author Lucian Chirita (lucianc@users.sourceforge.net)
36  * @version $Id: JarURLStreamHandler.java 3090 2006-04-14 16:49:08Z lucian $
37  */

38 public class JarURLStreamHandler extends URLStreamHandler JavaDoc {
39     
40     private static final String JavaDoc PROTOCOL = "jsjar";
41     private static final String JavaDoc URL_PREFIX = PROTOCOL + ":";
42     private static final int URL_PREFIX_LENGHT = URL_PREFIX.length();
43     private static final String JavaDoc SEPARATOR = "!";
44     private static final int SEPARATOR_LENGHT = SEPARATOR.length();
45     
46     private final ReferenceMap urlStreams;
47
48     protected void parseURL(URL JavaDoc u, String JavaDoc spec, int start, int limit) {
49         if (!spec.startsWith(URL_PREFIX)) {
50             throw new JSException("Malformed jar URL \"" + spec + "\"");
51         }
52         int sep = spec.indexOf(SEPARATOR);
53         if (sep < 0) {
54             throw new JSException("Malformed jar URL \"" + spec + "\"");
55         }
56         String JavaDoc jar = spec.substring(URL_PREFIX_LENGHT, sep);
57         String JavaDoc res = spec.substring(sep + SEPARATOR_LENGHT, spec.length());
58         setURL(u, PROTOCOL, null, -1, null, null, jar, null, res);
59     }
60
61     public JarURLStreamHandler() {
62         urlStreams = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD);
63     }
64
65     protected URLConnection JavaDoc openConnection(URL JavaDoc u) throws IOException JavaDoc {
66         JarFileEntry entry = getURLEntry(u);
67         if (entry == null) {
68             throw new JSException("Jar entry not found for URL " + u);
69         }
70         return new JarConnection(u, entry);
71     }
72
73     public URL JavaDoc createURL(JarFileEntry jarEntry) {
74         try {
75             String JavaDoc spec = URL_PREFIX + jarEntry.getJarFile().getName()
76                     + SEPARATOR + jarEntry.getEntry().getName();
77             URL JavaDoc url = new URL JavaDoc(null, spec, this);
78             putURL(url, jarEntry);
79             return url;
80         } catch (MalformedURLException JavaDoc e) {
81             throw new JSExceptionWrapper(e);
82         }
83     }
84     
85     public synchronized void putURL(URL JavaDoc u, JarFileEntry jarEntry) {
86         urlStreams.put(u, jarEntry);
87     }
88
89     protected synchronized JarFileEntry getURLEntry(URL JavaDoc u) {
90         return (JarFileEntry) urlStreams.get(u);
91     }
92
93     protected boolean equals(URL JavaDoc u1, URL JavaDoc u2) {
94         return u1 == u2;
95     }
96
97     protected int hashCode(URL JavaDoc u) {
98         return System.identityHashCode(u);
99     }
100
101     protected String JavaDoc toExternalForm(URL JavaDoc u) {
102         return URL_PREFIX + u.getPath() + SEPARATOR + u.getRef();
103     }
104 }
105
Popular Tags