KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > JarResource


1 // ========================================================================
2
// $Id: JarResource.java,v 1.19 2005/08/13 00:01:28 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15
package org.mortbay.util;
16
17 import java.io.File JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.JarURLConnection JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.jar.JarEntry JavaDoc;
24 import java.util.jar.JarInputStream JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.mortbay.log.LogFactory;
28
29
30 /* ------------------------------------------------------------ */
31 public class JarResource extends URLResource
32 {
33     private static Log log = LogFactory.getLog(JarResource.class);
34
35     protected transient JarURLConnection JavaDoc _jarConnection;
36     
37     /* -------------------------------------------------------- */
38     JarResource(URL JavaDoc url)
39     {
40         super(url,null);
41     }
42
43     /* ------------------------------------------------------------ */
44     public synchronized void release()
45     {
46         _jarConnection=null;
47         super.release();
48     }
49     
50     /* ------------------------------------------------------------ */
51     protected boolean checkConnection()
52     {
53         super.checkConnection();
54         try
55         {
56             if (_jarConnection!=_connection)
57                 newConnection();
58         }
59         catch(IOException JavaDoc e)
60         {
61             LogSupport.ignore(log,e);
62             _jarConnection=null;
63         }
64         
65         return _jarConnection!=null;
66     }
67
68     /* ------------------------------------------------------------ */
69     protected void newConnection()
70         throws IOException JavaDoc
71     {
72         _jarConnection=(JarURLConnection JavaDoc)_connection;
73     }
74     
75     /* ------------------------------------------------------------ */
76     /**
77      * Returns true if the respresenetd resource exists.
78      */

79     public boolean exists()
80     {
81         if (_urlString.endsWith("!/"))
82             return checkConnection();
83         else
84             return super.exists();
85     }
86
87     /* ------------------------------------------------------------ */
88     public File JavaDoc getFile()
89         throws IOException JavaDoc
90     {
91         return null;
92     }
93     
94     /* ------------------------------------------------------------ */
95     public InputStream JavaDoc getInputStream()
96         throws java.io.IOException JavaDoc
97     {
98         if (!_urlString.endsWith("!/"))
99             return super.getInputStream();
100         
101         URL JavaDoc url = new URL JavaDoc(_urlString.substring(4,_urlString.length()-2));
102         return url.openStream();
103     }
104     
105     /* ------------------------------------------------------------ */
106     public static void extract(Resource resource, File JavaDoc directory, boolean deleteOnExit)
107         throws IOException JavaDoc
108     {
109         if(log.isDebugEnabled())log.debug("Extract "+resource+" to "+directory);
110         JarInputStream JavaDoc jin = new JarInputStream JavaDoc(resource.getInputStream());
111         JarEntry JavaDoc entry=null;
112         while((entry=jin.getNextJarEntry())!=null)
113         {
114             File JavaDoc file=new File JavaDoc(directory,entry.getName());
115             if (entry.isDirectory())
116             {
117                 // Make directory
118
if (!file.exists())
119                     file.mkdirs();
120             }
121             else
122             {
123                 // make directory (some jars don't list dirs)
124
File JavaDoc dir = new File JavaDoc(file.getParent());
125                 if (!dir.exists())
126                     dir.mkdirs();
127
128                 // Make file
129
FileOutputStream JavaDoc fout = null;
130                 try
131                 {
132                     fout = new FileOutputStream JavaDoc(file);
133                     IO.copy(jin,fout);
134                 }
135                 finally
136                 {
137                     IO.close(fout);
138                 }
139
140                 // touch the file.
141
if (entry.getTime()>=0)
142                     file.setLastModified(entry.getTime());
143             }
144             if (deleteOnExit)
145                 file.deleteOnExit();
146         }
147     }
148     
149     /* ------------------------------------------------------------ */
150     public void extract(File JavaDoc directory, boolean deleteOnExit)
151         throws IOException JavaDoc
152     {
153         extract(this,directory,deleteOnExit);
154     }
155 }
156
Popular Tags