geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject cvs commit: incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/kernel/config LocalConfigStore.java
Date Fri, 23 Jan 2004 16:31:22 GMT
jboynes     2004/01/23 08:31:22

  Modified:    modules/kernel/src/java/org/apache/geronimo/kernel/config
                        LocalConfigStore.java
  Log:
  clean up install method
  
  Revision  Changes    Path
  1.5       +62 -40    incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/kernel/config/LocalConfigStore.java
  
  Index: LocalConfigStore.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/kernel/src/java/org/apache/geronimo/kernel/config/LocalConfigStore.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- LocalConfigStore.java	22 Jan 2004 02:46:27 -0000	1.4
  +++ LocalConfigStore.java	23 Jan 2004 16:31:21 -0000	1.5
  @@ -63,6 +63,8 @@
   import java.io.IOException;
   import java.io.ObjectInputStream;
   import java.io.OutputStream;
  +import java.io.BufferedOutputStream;
  +import java.io.InputStream;
   import java.net.MalformedURLException;
   import java.net.URI;
   import java.net.URL;
  @@ -147,7 +149,21 @@
       private void saveIndex() throws IOException {
           File indexFile = new File(root, INDEX_NAME);
           File tmpFile = File.createTempFile("index", ".tmp", root);
  -        tmpFile.renameTo(indexFile);
  +        FileOutputStream fos = new FileOutputStream(tmpFile);
  +        try {
  +            BufferedOutputStream os = new BufferedOutputStream(fos);
  +            index.store(os, null);
  +            os.close();
  +            fos = null;
  +            indexFile.delete();
  +            tmpFile.renameTo(indexFile);
  +        } catch (IOException e) {
  +            if (fos != null) {
  +                fos.close();
  +            }
  +            tmpFile.delete();
  +            throw e;
  +        }
       }
   
       public void install(URL source) throws IOException, InvalidConfigException {
  @@ -157,46 +173,23 @@
           }
           File bundleRoot = new File(root, newId);
           bundleRoot.mkdir();
  -        ZipInputStream zis = new ZipInputStream(source.openStream());
  +        InputStream is = source.openStream();
           try {
  -            ZipEntry entry;
  -            byte[] buffer = new byte[4096];
  -            while ((entry = zis.getNextEntry()) != null) {
  -                File out = new File(bundleRoot, entry.getName());
  -                if (entry.isDirectory()) {
  -                    out.mkdirs();
  -                } else {
  -                    out.getParentFile().mkdirs();
  -                    OutputStream os = new FileOutputStream(out);
  -                    try {
  -                        int count;
  -                        while ((count = zis.read(buffer)) > 0) {
  -                            os.write(buffer, 0, count);
  -                        }
  -                    } finally {
  -                        os.close();
  -                    }
  -                    zis.closeEntry();
  -                }
  -            }
  -            try {
  -                GBeanMBean config = loadConfig(bundleRoot);
  -                index.setProperty(config.getAttribute("ID").toString(), newId);
  -            } catch (Exception e) {
  -                throw new InvalidConfigException("Unable to get ID from downloaded configuration",
e);
  -            }
  -            synchronized (this) {
  -                saveIndex();
  -            }
  -        } catch (IOException e) {
  -            delete(bundleRoot);
  -            throw e;
  -        } catch (InvalidConfigException e) {
  -            delete(bundleRoot);
  -            throw e;
  +            unpack(bundleRoot, is);
           } finally {
  -            zis.close();
  +            is.close();
  +        }
  +        try {
  +            GBeanMBean config = loadConfig(bundleRoot);
  +            index.setProperty(config.getAttribute("ID").toString(), newId);
  +        } catch (Exception e) {
  +            delete(bundleRoot);
  +            throw new InvalidConfigException("Unable to get ID from downloaded configuration",
e);
           }
  +        synchronized (this) {
  +            saveIndex();
  +        }
  +
       }
   
       public synchronized GBeanMBean getConfig(URI configID) throws NoSuchConfigException,
IOException, InvalidConfigException {
  @@ -245,7 +238,36 @@
           }
       }
   
  -    private void delete(File root) throws IOException {
  +    public static void unpack(File to, InputStream from) throws IOException {
  +        ZipInputStream zis = new ZipInputStream(from);
  +        try {
  +            ZipEntry entry;
  +            byte[] buffer = new byte[4096];
  +            while ((entry = zis.getNextEntry()) != null) {
  +                File out = new File(to, entry.getName());
  +                if (entry.isDirectory()) {
  +                    out.mkdirs();
  +                } else {
  +                    out.getParentFile().mkdirs();
  +                    OutputStream os = new FileOutputStream(out);
  +                    try {
  +                        int count;
  +                        while ((count = zis.read(buffer)) > 0) {
  +                            os.write(buffer, 0, count);
  +                        }
  +                    } finally {
  +                        os.close();
  +                    }
  +                    zis.closeEntry();
  +                }
  +            }
  +        } catch (IOException e) {
  +            delete(to);
  +            throw e;
  +        }
  +    }
  +
  +    private static void delete(File root) throws IOException {
           File[] files = root.listFiles();
           for (int i = 0; i < files.length; i++) {
               File file = files[i];
  
  
  

Mime
View raw message