Home » geronimo-2.2-source-release » org.apache.geronimo.console.databasemanager.wizard » [javadoc | source]

    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;
   20   import java.io.File;
   21   import java.io.FileOutputStream;
   22   import java.io.IOException;
   23   import java.io.InputStream;
   24   import java.io.OutputStream;
   25   import java.io.Serializable;
   26   import java.net.MalformedURLException;
   27   import java.net.URL;
   28   import java.net.URLConnection;
   29   import java.util.ArrayList;
   30   import java.util.Collections;
   31   import java.util.HashSet;
   32   import java.util.Iterator;
   33   import java.util.List;
   34   import java.util.Properties;
   35   import java.util.Random;
   36   import java.util.Set;
   37   import java.util.jar.JarEntry;
   38   import java.util.jar.JarFile;
   39   import org.slf4j.Logger;
   40   import org.slf4j.LoggerFactory;
   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: 653782 $ $Date: 2008-05-06 07:10:14 -0700 (Tue, 06 May 2008) $
   50    */
   51   public class DriverDownloader {
   52       private static final Logger log = LoggerFactory.getLogger(DriverDownloader.class);
   53       Random random;
   54   
   55       public Properties readDriverFile(URL url) {
   56           try {
   57               InputStream in = url.openStream();
   58               Properties props = new Properties();
   59               props.load(in);
   60               in.close();
   61               return props;
   62           } catch (IOException e) {
   63               log.error("Unable to download driver properties", e);
   64               return null;
   65           }
   66       }
   67   
   68       public DriverInfo[] loadDriverInfo(URL driverInfoFile) {
   69           List list = new ArrayList();
   70           Properties props = readDriverFile(driverInfoFile);
   71           if(props == null) {
   72               return new DriverInfo[0];
   73           }
   74           Set drivers = new HashSet();
   75           for (Iterator it = props.keySet().iterator(); it.hasNext();) {
   76               String key = (String) 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 urls = new ArrayList();
   86           for (Iterator it = drivers.iterator(); it.hasNext();) {
   87               String driver = (String) it.next();
   88               String name = props.getProperty("driver."+driver+".name");
   89               String repository = props.getProperty("driver."+driver+".repository");
   90               String unzip = props.getProperty("driver."+driver+".unzip");
   91               urls.clear();
   92               int index = 1;
   93               while(true) {
   94                   String url = props.getProperty("driver."+driver+".url."+index);
   95                   if(url != null) {
   96                       ++index;
   97                       try {
   98                           urls.add(new URL(url));
   99                       } catch (MalformedURLException 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[]) urls.toArray(new URL[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 {
  121           int urlIndex = 0;
  122           if (driver.urls.length > 1) {
  123               if (random == null) {
  124                   random = new Random();
  125               }
  126               urlIndex = random.nextInt(driver.urls.length);
  127           }
  128           URL url = driver.urls[urlIndex];
  129           InputStream in;
  130           String 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 uc = url.openConnection();
  137               int filesize = uc.getContentLength();
  138               InputStream net = uc.getInputStream();
  139               JarFile jar = null;
  140               File download = null;
  141               try {
  142                   download = File.createTempFile("geronimo-driver-download", ".zip");
  143                   OutputStream out = new BufferedOutputStream(new FileOutputStream(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(download);
  166                   JarEntry 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 e) {
  177                       log.error("Unable to close JAR file", e);
  178                   }
  179                   if (download != null) {
  180                       download.delete();
  181                   }
  182               }
  183           } else {
  184               URLConnection 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, Serializable {
  191           private final static long serialVersionUID = -1202452382992975449L;
  192           
  193           private String name;
  194           private String repositoryURI;
  195           private String unzipPath;
  196           private URL[] urls;
  197   
  198           public DriverInfo(String name, String repositoryURI) {
  199               this.name = name;
  200               this.repositoryURI = repositoryURI;
  201           }
  202   
  203           public String getName() {
  204               return name;
  205           }
  206   
  207           public void setName(String name) {
  208               this.name = name;
  209           }
  210   
  211           public String getRepositoryURI() {
  212               return repositoryURI;
  213           }
  214   
  215           public void setRepositoryURI(String repositoryURI) {
  216               this.repositoryURI = repositoryURI;
  217           }
  218   
  219           public String getUnzipPath() {
  220               return unzipPath;
  221           }
  222   
  223           public void setUnzipPath(String unzipPath) {
  224               this.unzipPath = unzipPath;
  225           }
  226   
  227           public URL[] getUrls() {
  228               return urls;
  229           }
  230   
  231           public void setUrls(URL[] urls) {
  232               this.urls = urls;
  233           }
  234   
  235           public int compareTo(Object o) {
  236               return name.compareTo(((DriverInfo)o).name);
  237           }
  238       }
  239   }

Home » geronimo-2.2-source-release » org.apache.geronimo.console.databasemanager.wizard » [javadoc | source]