1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20
21 package org.apache.axis2.deployment.repository.util;
22
23 import org.apache.axis2.AxisFault;
24 import org.apache.axis2.deployment.Deployer;
25 import org.apache.axis2.deployment.DeploymentErrorMsgs;
26 import org.apache.axis2.deployment.DeploymentException;
27 import org.apache.axis2.deployment.util.Utils;
28 import org.apache.axis2.i18n.Messages;
29
30 import java.io.File;
31 import java.net.URL;
32
33 /**
34 * DeploymentFileData represents a "thing to deploy" in Axis2. It consists of a file,
35 * a deployment ClassLoader, and a Deployer.
36 */
37 public class DeploymentFileData {
38 private File file;
39 private ClassLoader classLoader;
40 private Deployer deployer;
41
42 public DeploymentFileData(File file) {
43 this.file = file;
44 }
45
46 public DeploymentFileData(File file, Deployer deployer) {
47 this(file);
48 this.deployer = deployer;
49 }
50
51 public String getAbsolutePath() {
52 return file.getAbsolutePath();
53 }
54
55 public ClassLoader getClassLoader() {
56 return classLoader;
57 }
58
59 public File getFile() {
60 return file;
61 }
62
63 /**
64 * Get the name of the file.
65 *
66 * @return the name of the referenced file
67 */
68 public String getName() {
69 return file.getName(); // No need to check for null due to constructor check
70 }
71
72 /**
73 * Get the name of the file.
74 *
75 * @return the name of the referenced file
76 * @deprecated please use getName() instead - this will disappear after 1.3.
77 */
78 public String getServiceName() {
79 return getName();
80 }
81
82 public static boolean isModuleArchiveFile(String filename) {
83 return (filename.endsWith(".mar"));
84 }
85
86 /**
87 * Checks whether a given file is a jar or an aar file.
88 *
89 * @param filename file to check
90 * @return Returns boolean.
91 */
92 public static boolean isServiceArchiveFile(String filename) {
93 return ((filename.endsWith(".jar")) | (filename.endsWith(".aar")));
94 }
95
96 public static String getFileExtension(String fileName) {
97 int index = fileName.lastIndexOf('.');
98 return fileName.substring(index + 1);
99 }
100
101 public void setClassLoader(ClassLoader classLoader) {
102 this.classLoader = classLoader;
103 }
104
105 public void setClassLoader(boolean isDirectory, ClassLoader parent, File file) throws AxisFault {
106 if (!isDirectory) {
107 if (this.file != null) {
108 URL[] urlsToLoadFrom;
109 try {
110 if (!this.file.exists()) {
111 throw new AxisFault(Messages.getMessage(DeploymentErrorMsgs.FILE_NOT_FOUND,
112 this.file.getAbsolutePath()));
113 }
114 urlsToLoadFrom = new URL[]{this.file.toURL()};
115 classLoader = Utils.createClassLoader(urlsToLoadFrom, parent, true, file);
116 } catch (Exception e) {
117 throw AxisFault.makeFault(e);
118 }
119 }
120 } else {
121 if (this.file != null) {
122 classLoader = Utils.getClassLoader(parent, this.file);
123 }
124 }
125 }
126
127 public Deployer getDeployer() {
128 return deployer;
129 }
130
131 public void setDeployer(Deployer deployer) {
132 this.deployer = deployer;
133 }
134
135 public void deploy() throws DeploymentException {
136 deployer.deploy(this);
137 }
138 }