001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.tool.service.control;
020:
021: import org.apache.axis2.tool.core.ClassFileHandler;
022: import org.apache.axis2.tool.core.FileCopier;
023: import org.apache.axis2.tool.core.JarFileWriter;
024: import org.apache.axis2.tool.core.ServiceFileCreator;
025: import org.apache.axis2.tool.core.ServiceXMLCreater;
026: import org.apache.axis2.tool.service.bean.ClassFileSelectionBean;
027: import org.apache.axis2.tool.service.bean.LibrarySelectionBean;
028: import org.apache.axis2.tool.service.bean.Page2Bean;
029: import org.apache.axis2.tool.service.bean.Page3Bean;
030: import org.apache.axis2.tool.service.bean.WSDLFileLocationBean;
031: import org.apache.axis2.tool.service.bean.WizardBean;
032:
033: import java.io.BufferedWriter;
034: import java.io.File;
035: import java.io.FileWriter;
036: import java.io.IOException;
037: import java.util.ArrayList;
038: import java.util.List;
039:
040: public class Controller {
041:
042: public ArrayList getMethodList(WizardBean bean)
043: throws ProcessException {
044: ArrayList returnList = null;
045: try {
046: returnList = new ClassFileHandler()
047: .getMethodNamesFromClass(bean.getPage2bean()
048: .getAutomaticClassName(), bean
049: .getPage1bean().getFileLocation());
050: } catch (IOException e) {
051: throw new ProcessException(
052: "IO Error, The class file location may be faulty!",
053: e);
054: } catch (ClassNotFoundException e) {
055: throw new ProcessException(
056: " The specified class does not exist!!!");
057: } catch (Exception e) {
058: throw new ProcessException(
059: "Unknown Error! See whether all parameters are available");
060: }
061: return returnList;
062: }
063:
064: public void process(WizardBean bean) throws ProcessException,
065: Exception {
066:
067: ClassFileSelectionBean page1Bean = bean.getPage1bean();
068: WSDLFileLocationBean wsdlBean = bean.getWsdlBean();
069: LibrarySelectionBean libBean = bean.getLibraryBean();
070: Page2Bean page2Bean = bean.getPage2bean();
071: Page3Bean page3Bean = bean.getPage3bean();
072:
073: File serviceFile = null;
074: File wsdlFile = null;
075: File classFileFolder = null;
076: File outputFolder = null;
077: String outputFileName = null;
078: boolean isServiceCreated = false;
079: boolean isWSDLAvailable = false;
080:
081: //see if the class file location is valid
082: classFileFolder = new File(page1Bean.getFileLocation());
083: if (!classFileFolder.exists()) {
084: throw new ProcessException(
085: "Specified Class file location is empty!!");
086: }
087: if (!classFileFolder.isDirectory()) {
088: throw new ProcessException(
089: "The class file location must be a folder!");
090: }
091:
092: //see if the service.xml file is valid
093: if (page2Bean.isManual()) {
094: serviceFile = new File(page2Bean.getManualFileName());
095: if (!serviceFile.exists()) {
096: throw new ProcessException(
097: "Specified Service XML file is missing!");
098: }
099: } else {
100: ArrayList methodList = page2Bean.getSelectedMethodNames();
101: if (methodList.isEmpty()) {
102: throw new ProcessException(
103: "There are no methods selected to generate the service!!");
104: }
105: String currentUserDir = System.getProperty("user.dir");
106: String fileName = "services.xml";
107: ServiceXMLCreater serviceXMLCreater = new ServiceXMLCreater(
108: page2Bean.getServiceName(), page2Bean
109: .getAutomaticClassName(), page2Bean
110: .getSelectedMethodNames());
111: String serviceFileString = serviceXMLCreater.toString();
112: serviceFile = new File(currentUserDir + File.separator
113: + fileName);
114: if (serviceFile.exists()) {
115: serviceFile.delete();
116: }
117: FileWriter serviceXMLFileWriter = new FileWriter(
118: serviceFile, true);
119: BufferedWriter writer = new BufferedWriter(
120: serviceXMLFileWriter);
121: writer.write(serviceFileString);
122: writer.close();
123:
124: // new ServiceFileCreator().createServiceFile(
125: // page2Bean.getServiceName(),
126: // page2Bean.getAutomaticClassName(),
127: // page2Bean.getSelectedMethodNames());//create the file here
128:
129: isServiceCreated = true;
130: }
131:
132: //see if the WSDL file is available
133: if (!wsdlBean.isSkip()) {
134: wsdlFile = new File(wsdlBean.getWSDLFileName());
135: if (!wsdlFile.exists()) {
136: throw new ProcessException(
137: "Specified WSDL file is missing!");
138: } else {
139: isWSDLAvailable = true;
140: }
141: }
142:
143: List fileList = new ArrayList();
144: //check the libs
145: if (libBean != null) {
146: String[] files = libBean.getFileList();
147: File tempFile = null;
148: if (files != null) {
149: for (int i = 0; i < files.length; i++) {
150: tempFile = new File(files[i]);
151: if (!tempFile.exists() || tempFile.isDirectory()) {
152: throw new ProcessException("Invalid libraries");
153: } else {
154: fileList.add(tempFile);
155: }
156: }
157: }
158: }
159:
160: outputFolder = new File(page3Bean.getOutputFolderName());
161: outputFileName = page3Bean.getOutputFileName();
162: if (!outputFileName.toLowerCase().endsWith(".jar")
163: && !outputFileName.toLowerCase().endsWith(".aar")) {
164: outputFileName = outputFileName + ".aar";
165: }
166:
167: File tempFileFolder = null;
168: String xmlFilter = ".xml";
169: String wsdlFilter = ".wsdl";
170:
171: try {
172: //create a temporary directory and copy the files
173: tempFileFolder = new File("Service-copy");
174: if (tempFileFolder.exists()) {
175: deleteDir(tempFileFolder);
176: }
177: tempFileFolder.mkdir();
178:
179: File metaInfFolder = new File(tempFileFolder, "META-INF");
180: metaInfFolder.mkdir();
181:
182: File libFolder = new File(tempFileFolder, "lib");
183: libFolder.mkdir();
184:
185: FileCopier classFilecopier = new FileCopier();
186: //copy the classes
187: classFilecopier.copyFiles(classFileFolder, tempFileFolder,
188: page1Bean.getFilter());
189:
190: //copy the service.xml
191: FileCopier serviceXMLcopier = new FileCopier();
192: serviceXMLcopier.copyFiles(serviceFile, metaInfFolder,
193: xmlFilter);
194:
195: //copy the libs
196: FileCopier libCopier = new FileCopier();
197: for (int i = 0; i < fileList.size(); i++) {
198: libCopier.copyFiles((File) fileList.get(i), libFolder,
199: null);
200: }
201:
202: if (isWSDLAvailable) {
203: new FileCopier().copyFiles(wsdlFile, metaInfFolder,
204: wsdlFilter);
205: }
206: //jar the temp directory. the output folder will be created if missing
207: new JarFileWriter().writeJarFile(outputFolder,
208: outputFileName, tempFileFolder);
209: } catch (Exception e) {
210: throw new ProcessException(e);
211: } finally {
212: deleteDir(tempFileFolder);
213: if (isServiceCreated)
214: serviceFile.delete();
215:
216: }
217:
218: }
219:
220: private boolean deleteDir(File dir) {
221: if (dir.isDirectory()) {
222: String[] children = dir.list();
223: for (int i = 0; i < children.length; i++) {
224: boolean success = deleteDir(new File(dir, children[i]));
225: if (!success) {
226: return false;
227: }
228: }
229: }
230:
231: // The directory is now empty so delete it
232: return dir.delete();
233: }
234: }
|