KickJava   Java API By Example, From Geeks To Geeks.

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


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.DeploymentConstants;
20 import org.apache.axis2.deployment.DeploymentEngine;
21 import org.apache.axis2.deployment.DeploymentException;
22 import org.apache.axis2.deployment.DeploymentParser;
23 import org.apache.axis2.description.AxisDescWSDLComponentFactory;
24 import org.apache.axis2.description.ModuleDescription;
25 import org.apache.axis2.description.ServiceDescription;
26 import org.apache.axis2.wsdl.WSDLVersionWrapper;
27 import org.apache.axis2.wsdl.builder.WOMBuilderFactory;
28 import org.apache.axis2.wsdl.builder.WOMBuilder;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.wsdl.WSDLDescription;
32
33 import java.io.*;
34 import java.util.Iterator JavaDoc;
35 import java.util.zip.ZipEntry JavaDoc;
36 import java.util.zip.ZipInputStream JavaDoc;
37 import java.util.zip.ZipOutputStream JavaDoc;
38
39 public class ArchiveReader implements DeploymentConstants {
40
41     private Log log = LogFactory.getLog(getClass());
42
43 // public ServiceDescription createService(String filename) throws DeploymentException {
44
// String strArchive = filename;
45
// ZipInputStream zin;
46
// boolean foundwsdl = false;
47
// ServiceDescription service = null;
48
// Definition difinition = null;
49
// try {
50
// zin = new ZipInputStream(new FileInputStream(strArchive));
51
// ZipEntry entry;
52
// while ((entry = zin.getNextEntry()) != null) {
53
// if (entry.getName().equals(SERVICEWSDL)) {
54
// WSDLVersionWrapper wsdlVersionWrapper = WOMBuilderFactory.getBuilder(
55
// WOMBuilderFactory.WSDL11).build(zin, new AxisDescWSDLComponentFactory());
56
// WSDLDescription womDescription = wsdlVersionWrapper.getDescription();
57
// Iterator iterator = womDescription.getServices().keySet().iterator();
58
// if(iterator.hasNext()){
59
// service = (ServiceDescription)iterator.next();
60
// }
61
// difinition = wsdlVersionWrapper.getDefinition();
62
// foundwsdl = true;
63
// break;
64
// }
65
// }
66
// zin.close();
67
// if (!foundwsdl) {
68
// service = new ServiceDescription();
69
// log.info("WSDL file not found for the service : " + filename);
70
// }
71
// service.setWSDLDefinition(difinition);
72
// } catch (Exception e) {
73
// throw new DeploymentException(e);
74
// }
75
// return service;
76
// }
77

78     public ServiceDescription createService(ArchiveFileData file) throws DeploymentException {
79         ServiceDescription service = null;
80         InputStream JavaDoc in= file.getClassLoader().getResourceAsStream(SERVICEWSDL);
81         boolean foundservice = false;
82         try {
83             if(in!= null){
84                 WOMBuilder builder = WOMBuilderFactory.getBuilder(WOMBuilderFactory.WSDL11);
85                 WSDLVersionWrapper wsdlVersionWrapper = builder.build(in, new AxisDescWSDLComponentFactory());
86                 WSDLDescription womDescription = wsdlVersionWrapper.getDescription();
87                 Iterator JavaDoc iterator = womDescription.getServices().keySet().iterator();
88                 if(iterator.hasNext()){
89                     foundservice = true;
90                     service = (ServiceDescription)womDescription.getServices().get(iterator.next());
91                 }
92                 if(!foundservice){
93                     service = new ServiceDescription();
94                 }
95                 service.setWSDLDefinition(wsdlVersionWrapper.getDefinition());
96                 in.close();
97             } else {
98                 service = new ServiceDescription();
99                 log.info("WSDL file not found for the service : " + file.getName());
100             }
101         } catch (Exception JavaDoc e) {
102             throw new DeploymentException(e);
103         }
104
105         return service;
106     }
107
108     /**
109      * This method will readServiceArchive the given jar or aar.
110      * it take two arguments filename and refereance to DeployEngine
111      *
112      * @param filename
113      * @param engine
114      */

115
116     public void readServiceArchive(String JavaDoc filename, DeploymentEngine engine, ServiceDescription service) throws DeploymentException {
117         // get attribute values
118
boolean foundServiceXML = false;
119         String JavaDoc strArchive = filename;
120         ZipInputStream JavaDoc zin;
121         try {
122             zin = new ZipInputStream JavaDoc(new FileInputStream(strArchive));
123             ZipEntry JavaDoc entry;
124             while ((entry = zin.getNextEntry()) != null) {
125                 if (entry.getName().equals(SERVICEXML)) {
126                     foundServiceXML = true;
127                     DeploymentParser schme = new DeploymentParser(zin, engine);
128                     schme.parseServiceXML(service);
129                     break;
130                 }
131             }
132             zin.close();
133             if (!foundServiceXML) {
134                 throw new DeploymentException("service.xml not found");
135             }
136         } catch (Exception JavaDoc e) {
137             throw new DeploymentException(e);
138         }
139     }
140
141     public void readModuleArchive(String JavaDoc filename, DeploymentEngine engine, ModuleDescription module) throws DeploymentException {
142         // get attribute values
143
boolean foundmoduleXML = false;
144         String JavaDoc strArchive = filename;
145         ZipInputStream JavaDoc zin = null;
146         try {
147             zin = new ZipInputStream JavaDoc(new FileInputStream(strArchive));
148             ZipEntry JavaDoc entry;
149             while ((entry = zin.getNextEntry()) != null) {
150                 if (entry.getName().equals(MODULEXML)) {
151                     foundmoduleXML = true;
152                     DeploymentParser schme = new DeploymentParser(zin, engine);
153                     schme.procesModuleXML(module);
154                     break;
155                 }
156             }
157             // zin.closeEntry();
158
zin.close();
159             if (!foundmoduleXML) {
160                 throw new DeploymentException("module.xml not found for the module : " + strArchive);
161             }
162         } catch (Exception JavaDoc e) {
163             throw new DeploymentException(e.getMessage());
164         }
165     }
166
167     /**
168      * This method first check whether the given module is there in the user home dirctory if so return
169      * that , else try to read the given module form classpath (from resources ) if found first get the module.mar
170      * file from the resourceStream and write that into user home/axis2home/nodule directory
171      * @param moduleName
172      * @return
173      * @throws DeploymentException
174      */

175     public File creatModuleArchivefromResource(String JavaDoc moduleName) throws DeploymentException {
176         File modulearchiveFile = null;
177         File modules = null;
178         try {
179             int BUFFER = 2048;
180             if(DeploymentEngine.axis2repository == null ){
181                 String JavaDoc userHome = System.getProperty("user.home");
182                 File userHomedir = new File(userHome);
183                 File repository = new File(userHomedir, ".axis2home");
184                 if (!repository.exists()) {
185                     repository.mkdirs();
186                     modules = new File(repository, "modules");
187                     modules.mkdirs();
188                 }
189             } else {
190                 modules = new File(DeploymentEngine.axis2repository , "modules");
191                 if(!modules.exists()){
192                     modules = new File(DeploymentEngine.axis2repository, "modules");
193                     modules.mkdirs();
194                 }
195             }
196             String JavaDoc modulearchiveName =moduleName + ".mar";
197             modulearchiveFile = new File(modules,modulearchiveName);
198             if (modulearchiveFile.exists()) {
199                 return modulearchiveFile;
200             } else {
201                 modulearchiveFile.createNewFile();
202             }
203             FileOutputStream dest = new
204                     FileOutputStream(modulearchiveFile);
205             ZipOutputStream JavaDoc out = new ZipOutputStream JavaDoc(new
206                     BufferedOutputStream(dest));
207             byte data[] = new byte[BUFFER];
208
209             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
210             InputStream JavaDoc in = cl.getResourceAsStream("modules/" + moduleName + ".mar");
211             if(in == null ){
212                 in = cl.getResourceAsStream("modules/" + moduleName + ".jar");
213             }
214             if(in == null){
215                 throw new DeploymentException( moduleName + " module is not found");
216             }
217             ZipInputStream JavaDoc zin = null;
218             zin = new ZipInputStream JavaDoc(in);
219             ZipEntry JavaDoc entry;
220             while ((entry = zin.getNextEntry()) != null) {
221                 ZipEntry JavaDoc zip = new ZipEntry JavaDoc(entry);
222                 out.putNextEntry(zip);
223                 int count;
224                 while ((count = zin.read(data, 0, BUFFER)) != -1) {
225                     out.write(data, 0, count);
226                 }
227             }
228             out.close();
229             zin.close();
230         } catch (Exception JavaDoc e) {
231             throw new DeploymentException(e.getMessage());
232         }
233         return modulearchiveFile;
234     }
235
236 }
237
238
239
240
241
242
243
244
245
Popular Tags