View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.geronimo.ews.ws4j2ee.utils;
17  
18  import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.util.ArrayList;
29  import java.util.Enumeration;
30  import java.util.jar.JarFile;
31  import java.util.zip.ZipEntry;
32  import java.util.zip.ZipFile;
33  
34  /***
35   * @author Srinath Perera (hemapani@opensource.lk)
36   */
37  public class FileUtils {
38      protected static Log log =
39              LogFactory.getLog(FileUtils.class.getName());
40              
41      public static void copyFile(File in, File out) throws GenerationFault {
42          try {
43              FileInputStream ins = new FileInputStream(in);
44              FileOutputStream outs = new FileOutputStream(out);
45              copyFile(ins, outs);
46              ins.close();
47              outs.close();
48          } catch (Exception e) {
49              throw GenerationFault.createGenerationFault(e);
50          }
51      }
52      
53                  
54      public static void copyFile(InputStream in, OutputStream out) throws GenerationFault {
55          try {
56              byte[] buf = new byte[1024];
57              int val = in.read(buf);
58              while (val > 0) {
59                  out.write(buf, 0, val);
60                  val = in.read(buf);
61              }
62              out.close();
63              in.close();
64          } catch (Exception e) {
65              log.error(e);
66              throw GenerationFault.createGenerationFault(e);
67          }
68      }
69  
70      public static int configCount = 0;
71      private JarFile ajar;
72      private static byte[] data = new byte[10 * 1024];
73      private String dir;
74  
75      public static ArrayList uncompressWar(File outDir, File WarFile) throws IOException {
76          ArrayList urls = new ArrayList();
77          ZipFile zippedWar = new ZipFile(WarFile);
78          Enumeration enties = zippedWar.entries();
79          File classesDir = new File(outDir, "WEB-INF/classes");
80          classesDir.mkdirs();
81          urls.add(classesDir);
82          while (enties.hasMoreElements()) {
83              ZipEntry entry = (ZipEntry) enties.nextElement();
84              if (entry.isDirectory()) {
85              } else {
86                  if (entry.getName().startsWith("WEB-INF/classes")) {
87                      File out = new File(outDir, entry.getName());
88                      out.getParentFile().mkdirs();
89                      uncompressFile(zippedWar.getInputStream(entry), out);
90                  }
91                  if (entry.getName().startsWith("WEB-INF/lib")) {
92                      File outJar = new File(outDir, entry.getName());
93                      uncompressFile(zippedWar.getInputStream(entry), outJar);
94                      urls.add(outJar);
95                  }
96              }
97          }
98          return urls;
99      }
100 
101     public static void uncompressFile(InputStream is, File outFile) throws IOException {
102         outFile.getParentFile().mkdirs();
103         OutputStream os = new FileOutputStream(outFile);
104         int val = 1;
105         while (true) {
106             val = is.read(data, 0, 1024);
107             if (val < 0 || val == 0)
108                 break;
109             os.write(data, 0, val);
110         }
111         is.close();
112         os.close();
113     }
114 
115 }