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:
020: package org.apache.axis2.deployment;
021:
022: import org.apache.axis2.deployment.repository.util.DeploymentFileData;
023: import org.apache.axis2.deployment.repository.util.WSInfo;
024: import org.apache.axis2.deployment.repository.util.WSInfoList;
025: import org.apache.axis2.deployment.util.Utils;
026: import org.apache.axis2.util.Loader;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import java.io.File;
031: import java.io.UnsupportedEncodingException;
032: import java.net.*;
033: import java.util.ArrayList;
034: import java.util.Enumeration;
035: import java.util.HashMap;
036: import java.util.Iterator;
037:
038: public class RepositoryListener implements DeploymentConstants {
039: protected static final Log log = LogFactory
040: .getLog(RepositoryListener.class);
041:
042: protected DeploymentEngine deploymentEngine;
043: private HashMap directoryToExtensionMappingMap;
044:
045: /** Reference to a WSInfoList */
046: protected WSInfoList wsInfoList;
047:
048: /**
049: * This constructor takes two arguments, a folder name and a reference to Deployment Engine
050: * First, it initializes the system, by loading all the modules in the /modules directory and
051: * then creates a WSInfoList to store information about available modules and services.
052: *
053: * @param deploymentEngine reference to engine registry for updates
054: * @param isClasspath true if this RepositoryListener should scan the classpath for Modules
055: */
056: public RepositoryListener(DeploymentEngine deploymentEngine,
057: boolean isClasspath) {
058: this .deploymentEngine = deploymentEngine;
059: wsInfoList = new WSInfoList(deploymentEngine);
060: init2(isClasspath);
061: }
062:
063: public void init2(boolean isClasspath) {
064: if (!isClasspath) {
065: init();
066: }
067: loadClassPathModules();
068: }
069:
070: /** Finds a list of modules in the folder and adds to wsInfoList. */
071: public void checkModules() {
072: File root = deploymentEngine.getModulesDir();
073: File[] files = root.listFiles();
074:
075: if (files != null) {
076: for (int i = 0; i < files.length; i++) {
077: File file = files[i];
078: if (isSourceControlDir(file)) {
079: continue;
080: }
081: if (!file.isDirectory()) {
082: if (DeploymentFileData.isModuleArchiveFile(file
083: .getName())) {
084: addFileToDeploy(file, deploymentEngine
085: .getModuleDeployer(),
086: WSInfo.TYPE_MODULE);
087: }
088: } else {
089: if (!"lib".equalsIgnoreCase(file.getName())) {
090: addFileToDeploy(file, deploymentEngine
091: .getModuleDeployer(),
092: WSInfo.TYPE_MODULE);
093: }
094: }
095: }
096: }
097: }
098:
099: protected boolean isSourceControlDir(File file) {
100: if (file.isDirectory()) {
101: String name = file.getName();
102: if (name.equalsIgnoreCase("CVS")
103: || name.equalsIgnoreCase(".svn")) {
104: return true;
105: }
106: }
107: return false;
108: }
109:
110: protected void loadClassPathModules() {
111: ModuleDeployer deployer = deploymentEngine.getModuleDeployer();
112:
113: // Find Modules on the class path (i.e. if classpath includes "addressing.mar" then
114: // addressing will be available for engaging)
115:
116: ClassLoader loader = Thread.currentThread()
117: .getContextClassLoader();
118: try {
119: Enumeration moduleURLs = loader
120: .getResources("META-INF/module.xml");
121: while (moduleURLs.hasMoreElements()) {
122: try {
123: URL url = (URL) moduleURLs.nextElement();
124: String fileName = url.toString();
125: if (fileName.startsWith("jar")) {
126: url = ((java.net.JarURLConnection) url
127: .openConnection()).getJarFileURL();
128: fileName = url.toString();
129: File f = new File(new URI(fileName));
130: addFileToDeploy(f, deployer, WSInfo.TYPE_MODULE);
131: } else if (fileName.startsWith("file")) {
132: fileName = fileName.substring(0, fileName
133: .lastIndexOf("/META-INF/module.xml"));
134: File f = new File(new URI(fileName));
135: addFileToDeploy(f, deployer, WSInfo.TYPE_MODULE);
136: }
137:
138: } catch (URISyntaxException e) {
139: log.info(e);
140: }
141: }
142: } catch (Exception e) {
143: // Oh well, log the problem
144: log.debug(e);
145: }
146:
147: String classPath = getLocation();
148:
149: if (classPath == null)
150: return;
151:
152: int lstindex = classPath.lastIndexOf(File.separatorChar);
153: if (lstindex > 0) {
154: classPath = classPath.substring(0, lstindex);
155: } else {
156: classPath = ".";
157: }
158: File root = new File(classPath);
159: File[] files = root.listFiles();
160: if (files != null) {
161: for (int i = 0; i < files.length; i++) {
162: File file = files[i];
163: if (!file.isDirectory()) {
164: if (DeploymentFileData.isModuleArchiveFile(file
165: .getName())) {
166: //adding modules in the class path
167: addFileToDeploy(file, deployer,
168: WSInfo.TYPE_MODULE);
169: }
170: }
171: }
172: }
173:
174: ClassLoader cl = deploymentEngine.getAxisConfig()
175: .getModuleClassLoader();
176: while (cl != null) {
177: if (cl instanceof URLClassLoader) {
178: URL[] urls = ((URLClassLoader) cl).getURLs();
179: for (int i = 0; (urls != null) && i < urls.length; i++) {
180: String path = urls[i].getPath();
181: //If it is a drive letter, adjust accordingly.
182: if (path.length() >= 3 && path.charAt(0) == '/'
183: && path.charAt(2) == ':') {
184: path = path.substring(1);
185: }
186: try {
187: path = URLDecoder.decode(path,
188: Utils.defaultEncoding);
189: } catch (UnsupportedEncodingException e) {
190: // Log this?
191: }
192: File file = new File(path.replace('/',
193: File.separatorChar).replace('|', ':'));
194: if (file.isFile()) {
195: if (DeploymentFileData.isModuleArchiveFile(file
196: .getName())) {
197: //adding modules in the class path
198: addFileToDeploy(file, deployer,
199: WSInfo.TYPE_MODULE);
200: }
201: }
202: }
203: }
204: cl = cl.getParent();
205: }
206:
207: deploymentEngine.doDeploy();
208: }
209:
210: /**
211: * To get the location of the Axis2.jar from that I can drive the location of class path
212: *
213: * @return String (location of the axis2 jar)
214: */
215: protected String getLocation() {
216: try {
217: Class clazz = Loader
218: .loadClass("org.apache.axis2.engine.AxisEngine");
219: java.net.URL url = clazz.getProtectionDomain()
220: .getCodeSource().getLocation();
221: String location = url.toString();
222: if (location.startsWith("jar")) {
223: url = ((java.net.JarURLConnection) url.openConnection())
224: .getJarFileURL();
225: location = url.toString();
226: }
227: if (location.startsWith("file")) {
228: File file = Utils.toFile(url);
229: return file.getAbsolutePath();
230: } else {
231: return url.toString();
232: }
233: } catch (Throwable t) {
234: return null;
235: }
236: }
237:
238: /** Finds a list of services in the folder and adds to wsInfoList. */
239: public void checkServices() {
240: findServicesInDirectory();
241: loadOtherDirectories();
242: update();
243: }
244:
245: /**
246: * First initializes the WSInfoList, then calls checkModule to load all the modules and calls
247: * update() to update the Deployment engine and engine registry.
248: */
249: public void init() {
250: wsInfoList.init();
251: checkModules();
252: directoryToExtensionMappingMap = deploymentEngine
253: .getDirectoryToExtensionMappingMap();
254: deploymentEngine.doDeploy();
255: }
256:
257: //This will load the files from the directories
258: // specified by axis2.xml (As <deployer>)
259: private void loadOtherDirectories() {
260: if (directoryToExtensionMappingMap.size() > 0) {
261: Iterator keys = directoryToExtensionMappingMap.keySet()
262: .iterator();
263: while (keys.hasNext()) {
264: String s = (String) keys.next();
265: ArrayList list = (ArrayList) directoryToExtensionMappingMap
266: .get(s);
267: for (int i = 0; i < list.size(); i++) {
268: String extension = (String) list.get(i);
269: findFileForGivenDirectory(s, extension);
270: }
271:
272: }
273: }
274: }
275:
276: private void findFileForGivenDirectory(String dir, String extension) {
277: try {
278: File directory = new File(deploymentEngine
279: .getRepositoryDir(), dir);
280: if (directory.exists()) {
281: File[] files = directory.listFiles();
282: if (files != null && files.length > 0) {
283: for (int i = 0; i < files.length; i++) {
284: File file = files[i];
285: if (isSourceControlDir(file)) {
286: continue;
287: }
288: if (!file.isDirectory()
289: && extension.equals(DeploymentFileData
290: .getFileExtension(file
291: .getName()))) {
292: addFileToDeploy(
293: file,
294: deploymentEngine
295: .getDeployerForExtension(extension),
296: WSInfo.TYPE_CUSTOM);
297: }
298: }
299: }
300: }
301: } catch (Exception e) {
302: //need to log the exception
303: }
304: }
305:
306: /** Searches a given folder for jar files and adds them to a list in the WSInfolist class. */
307: protected void findServicesInDirectory() {
308: File root = deploymentEngine.getServicesDir();
309: File[] files = root.listFiles();
310:
311: if (files != null && files.length > 0) {
312: for (int i = 0; i < files.length; i++) {
313: File file = files[i];
314: if (isSourceControlDir(file)) {
315: continue;
316: }
317: if (!file.isDirectory()) {
318: if (DeploymentFileData.isServiceArchiveFile(file
319: .getName())) {
320: addFileToDeploy(file, deploymentEngine
321: .getServiceDeployer(),
322: WSInfo.TYPE_SERVICE);
323: } else {
324: String ext = DeploymentFileData
325: .getFileExtension(file.getName());
326: Deployer deployer = deploymentEngine
327: .getDeployerForExtension(ext);
328: // If we found a deployer for this type of file, use it. Otherwise
329: // ignore the file.
330: if (deployer != null) {
331: addFileToDeploy(file, deployer,
332: WSInfo.TYPE_SERVICE);
333: }
334: }
335: } else {
336: if (!"lib".equalsIgnoreCase(file.getName())) {
337: addFileToDeploy(file, deploymentEngine
338: .getServiceDeployer(),
339: WSInfo.TYPE_SERVICE);
340: }
341: }
342: }
343: }
344: }
345:
346: /** Method invoked from the scheduler to start the listener. */
347: public void startListener() {
348: checkServices();
349: // update();
350: }
351:
352: /** Updates WSInfoList object. */
353: public void update() {
354: wsInfoList.update();
355: }
356:
357: public void updateRemote() throws Exception {
358: findServicesInDirectory();
359: update();
360: }
361:
362: public void addFileToDeploy(File file, Deployer deployer, int type) {
363: wsInfoList.addWSInfoItem(file, deployer, type);
364: }
365: }
|