KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > databasemanager > wizard > DriverDownloader


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.console.databasemanager.wizard;
18
19 import java.io.BufferedOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLConnection JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Random JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.jar.JarEntry JavaDoc;
38 import java.util.jar.JarFile JavaDoc;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.geronimo.kernel.repository.Artifact;
42 import org.apache.geronimo.kernel.repository.FileWriteMonitor;
43 import org.apache.geronimo.kernel.repository.WriteableRepository;
44
45 /**
46  * A utility that handles listing and downloading available JDBC driver JARs.
47  * It can handle straight JARs and also JARs in ZIP files.
48  *
49  * @version $Rev: 476061 $ $Date: 2006-11-17 01:36:50 -0500 (Fri, 17 Nov 2006) $
50  */

51 public class DriverDownloader {
52     private final static Log log = LogFactory.getLog(DriverDownloader.class);
53     Random JavaDoc random;
54
55     public Properties JavaDoc readDriverFile(URL JavaDoc url) {
56         try {
57             InputStream JavaDoc in = url.openStream();
58             Properties JavaDoc props = new Properties JavaDoc();
59             props.load(in);
60             in.close();
61             return props;
62         } catch (IOException JavaDoc e) {
63             log.error("Unable to download driver properties", e);
64             return null;
65         }
66     }
67
68     public DriverInfo[] loadDriverInfo(URL JavaDoc driverInfoFile) {
69         List JavaDoc list = new ArrayList JavaDoc();
70         Properties JavaDoc props = readDriverFile(driverInfoFile);
71         if(props == null) {
72             return new DriverInfo[0];
73         }
74         Set JavaDoc drivers = new HashSet JavaDoc();
75         for (Iterator JavaDoc it = props.keySet().iterator(); it.hasNext();) {
76             String JavaDoc key = (String JavaDoc) it.next();
77             if(!key.startsWith("driver.")) {
78                 continue;
79             }
80             int pos = key.indexOf('.', 7);
81             if(pos > -1) {
82                 drivers.add(key.substring(7, pos));
83             }
84         }
85         List JavaDoc urls = new ArrayList JavaDoc();
86         for (Iterator JavaDoc it = drivers.iterator(); it.hasNext();) {
87             String JavaDoc driver = (String JavaDoc) it.next();
88             String JavaDoc name = props.getProperty("driver."+driver+".name");
89             String JavaDoc repository = props.getProperty("driver."+driver+".repository");
90             String JavaDoc unzip = props.getProperty("driver."+driver+".unzip");
91             urls.clear();
92             int index = 1;
93             while(true) {
94                 String JavaDoc url = props.getProperty("driver."+driver+".url."+index);
95                 if(url != null) {
96                     ++index;
97                     try {
98                         urls.add(new URL JavaDoc(url));
99                     } catch (MalformedURLException JavaDoc e) {
100                         log.error("Unable to process URL from driver list", e);
101                     }
102                 } else {
103                     break;
104                 }
105             }
106             if(name != null && repository != null && urls.size() > 0) {
107                 DriverInfo info = new DriverInfo(name, repository);
108                 info.setUnzipPath(unzip);
109                 info.setUrls((URL JavaDoc[]) urls.toArray(new URL JavaDoc[urls.size()]));
110                 list.add(info);
111             }
112         }
113         Collections.sort(list);
114         return (DriverInfo[]) list.toArray(new DriverInfo[list.size()]);
115     }
116
117     /**
118      * Downloads a driver and loads it into the local repository.
119      */

120     public void loadDriver(WriteableRepository repo, DriverInfo driver, FileWriteMonitor monitor) throws IOException JavaDoc {
121         int urlIndex = 0;
122         if (driver.urls.length > 1) {
123             if (random == null) {
124                 random = new Random JavaDoc();
125             }
126             urlIndex = random.nextInt(driver.urls.length);
127         }
128         URL JavaDoc url = driver.urls[urlIndex];
129         InputStream JavaDoc in;
130         String JavaDoc uri = driver.getRepositoryURI();
131         if (driver.unzipPath != null) {
132             byte[] buf = new byte[1024];
133             int size;
134             int total = 0;
135             int threshold = 10240;
136             URLConnection JavaDoc uc = url.openConnection();
137             int filesize = uc.getContentLength();
138             InputStream JavaDoc net = uc.getInputStream();
139             JarFile JavaDoc jar = null;
140             File JavaDoc download = null;
141             try {
142                 download = File.createTempFile("geronimo-driver-download", ".zip");
143                 OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(download));
144                 if (monitor != null) {
145                     monitor.writeStarted("Download driver archive to " + download, filesize);
146                 }
147                 try {
148                     while ((size = net.read(buf)) > -1) {
149                         out.write(buf, 0, size);
150                         if (monitor != null) {
151                             total += size;
152                             if (total > threshold) {
153                                 monitor.writeProgress(total);
154                                 threshold += 10240;
155                             }
156                         }
157                     }
158                     out.flush();
159                     out.close();
160                 } finally {
161                     if (monitor != null) {
162                         monitor.writeComplete(total);
163                     }
164                 }
165                 jar = new JarFile JavaDoc(download);
166                 JarEntry JavaDoc entry = jar.getJarEntry(driver.unzipPath);
167                 if (entry == null) {
168                     log.error("Cannot extract driver JAR " + driver.unzipPath + " from download file " + url);
169                 } else {
170                     in = jar.getInputStream(entry);
171                     repo.copyToRepository(in, (int)entry.getSize(), Artifact.create(uri), monitor);
172                 }
173             } finally {
174                 if (jar != null) try {
175                     jar.close();
176                 } catch (IOException JavaDoc e) {
177                     log.error("Unable to close JAR file", e);
178                 }
179                 if (download != null) {
180                     download.delete();
181                 }
182             }
183         } else {
184             URLConnection JavaDoc con = url.openConnection();
185             in = con.getInputStream();
186             repo.copyToRepository(in, con.getContentLength(), Artifact.create(uri), monitor);
187         }
188     }
189
190     public static class DriverInfo implements Comparable JavaDoc, Serializable JavaDoc {
191         private final static long serialVersionUID = -1202452382992975449L;
192         
193         private String JavaDoc name;
194         private String JavaDoc repositoryURI;
195         private String JavaDoc unzipPath;
196         private URL JavaDoc[] urls;
197
198         public DriverInfo(String JavaDoc name, String JavaDoc repositoryURI) {
199             this.name = name;
200             this.repositoryURI = repositoryURI;
201         }
202
203         public String JavaDoc getName() {
204             return name;
205         }
206
207         public void setName(String JavaDoc name) {
208             this.name = name;
209         }
210
211         public String JavaDoc getRepositoryURI() {
212             return repositoryURI;
213         }
214
215         public void setRepositoryURI(String JavaDoc repositoryURI) {
216             this.repositoryURI = repositoryURI;
217         }
218
219         public String JavaDoc getUnzipPath() {
220             return unzipPath;
221         }
222
223         public void setUnzipPath(String JavaDoc unzipPath) {
224             this.unzipPath = unzipPath;
225         }
226
227         public URL JavaDoc[] getUrls() {
228             return urls;
229         }
230
231         public void setUrls(URL JavaDoc[] urls) {
232             this.urls = urls;
233         }
234
235         public int compareTo(Object JavaDoc o) {
236             return name.compareTo(((DriverInfo)o).name);
237         }
238     }
239 }
240
Popular Tags