KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > deployment > repository > util > ArchiveFileData


1 /*
2 * Copyright 2004,2005 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
17 package org.apache.axis2.deployment.repository.util;
18
19 import org.apache.axis2.deployment.DeploymentEngine;
20 import org.apache.axis2.deployment.DeploymentException;
21 import org.apache.axis2.engine.AxisFault;
22
23 import javax.xml.namespace.QName JavaDoc;
24 import java.io.*;
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipInputStream JavaDoc;
30 import java.util.zip.ZipOutputStream JavaDoc;
31
32 /**
33  * ArchiveFileData = Hot Deployment File Item , to store infromation of the module or servise
34  * item to be deploy
35  */

36 public class ArchiveFileData {
37
38     private ClassLoader JavaDoc classLoader;
39     private File file = null;
40     private int type;
41     private String JavaDoc messgeReceiver;
42     private String JavaDoc moduleClass;
43     private String JavaDoc name;
44
45     private ArrayList JavaDoc modules = new ArrayList JavaDoc();
46
47
48     public ArchiveFileData(int type, String JavaDoc name) {
49         this.type = type;
50         this.name = name;
51     }
52
53     public String JavaDoc getMessgeReceiver() {
54         return messgeReceiver;
55     }
56
57     public void setMessgeReceiver(String JavaDoc messgeReceiver) {
58         this.messgeReceiver = messgeReceiver;
59     }
60
61     public ArchiveFileData(File file, int type) {
62         this.file = file;
63         this.type = type;
64     }
65
66     public String JavaDoc getName() {
67         return file.getName();
68     }
69
70     public String JavaDoc getServiceName() {
71         if (file != null) {
72             return file.getName();
73         } else
74             return name;
75     }
76
77     public String JavaDoc getAbsolutePath() {
78         return file.getAbsolutePath();
79     }
80
81     public int getType() {
82         return type;
83     }
84
85     public File getFile() {
86         return file;
87     }
88
89     public ClassLoader JavaDoc getClassLoader() {
90         return classLoader;
91     }
92
93     public void setClassLoader(ClassLoader JavaDoc classLoader) {
94         this.classLoader = classLoader;
95     }
96
97     public String JavaDoc getModuleClass() {
98         return moduleClass;
99     }
100
101     public void setModuleClass(String JavaDoc moduleClass) {
102         this.moduleClass = moduleClass;
103     }
104
105     public void setClassLoader() throws AxisFault {
106         ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
107         ArrayList JavaDoc URLs = new ArrayList JavaDoc();
108         boolean tobeRecreated = false;// if there is a jar file inside a jar file then the URLClassLoader
109
// has to be craeted taking that file to the account
110
if (file != null) {
111             URL JavaDoc[] urlsToLoadFrom = new URL JavaDoc[0];
112             try {
113                 if (!file.exists()) {
114                     throw new RuntimeException JavaDoc("file not found !!!!!!!!!!!!!!!");
115                 }
116                 URLs.add(file.toURL());
117                 urlsToLoadFrom = new URL JavaDoc[]{file.toURL()};
118                 classLoader = new URLClassLoader JavaDoc(urlsToLoadFrom, parent);
119             } catch (Exception JavaDoc e) {
120                 throw new AxisFault(e);
121             }
122             try {
123                 ZipInputStream JavaDoc zin = new ZipInputStream JavaDoc(new FileInputStream(file));
124                 ZipEntry JavaDoc entry;
125                 String JavaDoc entryName = "";
126                 int BUFFER = 2048;
127                 while ((entry = zin.getNextEntry()) != null) {
128                     entryName = entry.getName();
129                     if (entryName != null && entryName.startsWith("lib/") && entryName.endsWith(".jar")) {
130                         //extarcting jar file form the orignial jar file and copy it to the axis2 lib
131
File libFile = new File(DeploymentEngine.axis2repository, entryName);
132                         FileOutputStream dest = new FileOutputStream(libFile);
133                         ZipOutputStream JavaDoc out = new ZipOutputStream JavaDoc(new BufferedOutputStream(dest));
134                         byte data[] = new byte[BUFFER];
135                         InputStream JavaDoc in = classLoader.getResourceAsStream(entryName);
136                         ZipInputStream JavaDoc jar_zin = null;
137                         jar_zin = new ZipInputStream JavaDoc(in);
138                         ZipEntry JavaDoc jarentry;
139                         while ((jarentry = jar_zin.getNextEntry()) != null) {
140                             ZipEntry JavaDoc zip = new ZipEntry JavaDoc(jarentry);
141                             out.putNextEntry(zip);
142                             int count;
143                             while ((count = jar_zin.read(data, 0, BUFFER)) != -1) {
144                                 out.write(data, 0, count);
145                             }
146                         }
147                         out.close();
148                         jar_zin.close();
149                         URLs.add(libFile.toURL());
150                         tobeRecreated = true;
151                     }
152                 }
153                 zin.close();
154                 if (tobeRecreated) {
155                     URL JavaDoc[] urlstobeload = new URL JavaDoc[URLs.size()];
156                     for (int i = 0; i < URLs.size(); i++) {
157                         URL JavaDoc url = (URL JavaDoc) URLs.get(i);
158                         urlstobeload[i] = url;
159                     }
160                     //recreating the classLoader
161
classLoader = new URLClassLoader JavaDoc(urlstobeload, parent);
162                 }
163             } catch (IOException e) {
164                 throw new DeploymentException(e);
165             }
166         }
167     }
168
169     public void addModule(QName JavaDoc moduleName) {
170         modules.add(moduleName);
171     }
172
173     public ArrayList JavaDoc getModules() {
174         return modules;
175     }
176 }
177
Popular Tags