001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.tomcat.installer;
018:
019: import java.io.File;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.apache.openejb.tomcat.common.TomcatVersion;
024:
025: public class Paths {
026: private final File openejbWarDir;
027: private final List<String> errors = new ArrayList<String>();
028: private File catalinaHomeDir;
029: private File catalinaBaseDir;
030: private File serverXmlFile;
031:
032: public Paths(File openejbWarDir) {
033: this .openejbWarDir = openejbWarDir;
034: }
035:
036: public File getCatalinaHomeDir() {
037: if (catalinaHomeDir == null) {
038: String catalinaHome = System.getProperty("catalina.home");
039: if (catalinaHome != null) {
040: catalinaHomeDir = new File(catalinaHome);
041: }
042: }
043: return catalinaHomeDir;
044: }
045:
046: public void setCatalinaHomeDir(String catalinaHomeDir) {
047: this .catalinaHomeDir = createFile(catalinaHomeDir);
048: }
049:
050: public void setCatalinaHomeDir(File catalinaHomeDir) {
051: this .catalinaHomeDir = catalinaHomeDir;
052: }
053:
054: public File getCatalinaBaseDir() {
055: if (catalinaBaseDir == null) {
056: String catalinaBase = System.getProperty("catalina.base");
057: if (catalinaBase != null) {
058: catalinaBaseDir = new File(catalinaBase);
059: }
060: }
061: return catalinaBaseDir;
062: }
063:
064: public void setCatalinaBaseDir(String catalinaBaseDir) {
065: this .catalinaBaseDir = createFile(catalinaBaseDir);
066: }
067:
068: public void setCatalinaBaseDir(File catalinaBaseDir) {
069: this .catalinaBaseDir = catalinaBaseDir;
070: }
071:
072: public File getServerXmlFile() {
073: if (serverXmlFile == null) {
074: File confdir = getCatalinaConfDir();
075:
076: if (confdir == null)
077: return null;
078:
079: serverXmlFile = new File(confdir, "server.xml");
080: }
081: return serverXmlFile;
082: }
083:
084: public void setServerXmlFile(String serverXmlFile) {
085: this .serverXmlFile = createFile(serverXmlFile);
086: }
087:
088: public void setServerXmlFile(File serverXmlFile) {
089: this .serverXmlFile = serverXmlFile;
090: }
091:
092: public File getCatalinaLibDir() {
093: File catalinaHomeDir = getCatalinaHomeDir();
094:
095: if (catalinaHomeDir == null)
096: return null;
097:
098: if (TomcatVersion.v6.isTheVersion()) {
099: return new File(catalinaHomeDir, "lib");
100: } else {
101: return new File(new File(catalinaHomeDir, "server"), "lib");
102: }
103: }
104:
105: public File getCatalinaConfDir() {
106: File catalinaBaseDir = getCatalinaBaseDir();
107:
108: if (catalinaBaseDir == null)
109: return null;
110:
111: return new File(catalinaBaseDir, "conf");
112: }
113:
114: public File getCatalinaBinDir() {
115: File catalinaHomeDir = getCatalinaHomeDir();
116:
117: if (catalinaHomeDir == null)
118: return null;
119:
120: return new File(catalinaHomeDir, "bin");
121: }
122:
123: public File getCatalinaShFile() {
124: File binDir = getCatalinaBinDir();
125:
126: if (binDir == null)
127: return null;
128:
129: return new File(binDir, "catalina.sh");
130: }
131:
132: public File getCatalinaBatFile() {
133: File binDir = getCatalinaBinDir();
134:
135: if (binDir == null)
136: return null;
137:
138: return new File(binDir, "catalina.bat");
139: }
140:
141: public File getOpenEJBLibDir() {
142: if (openejbWarDir == null)
143: return null;
144:
145: return new File(openejbWarDir, "lib");
146: }
147:
148: public File getOpenEJBTomcatLoaderJar() {
149: return findOpenEJBJar("openejb-tomcat-loader");
150: }
151:
152: public File getOpenEJBJavaagentJar() {
153: return findOpenEJBJar("openejb-javaagent");
154: }
155:
156: public File getOpenEJBCoreJar() {
157: return findOpenEJBJar("openejb-core");
158: }
159:
160: private File findOpenEJBJar(String namePrefix) {
161: File openEJBLibDir = getOpenEJBLibDir();
162: if (openEJBLibDir == null)
163: return null;
164:
165: File openejbLoaderJar = null;
166: for (File file : openEJBLibDir.listFiles()) {
167: if (file.getName().startsWith(namePrefix + "-")
168: && file.getName().endsWith(".jar")) {
169: return file;
170: }
171: }
172:
173: return openejbLoaderJar;
174: }
175:
176: public File getUpdatedAnnotationApiJar() {
177: if (openejbWarDir == null)
178: return null;
179:
180: return new File(openejbWarDir, "tomcat/annotations-api.jar");
181: }
182:
183: public boolean verify() {
184: if (openejbWarDir == null) {
185: addError("OpenEJB war is not unpacked");
186: }
187: if (getCatalinaHomeDir() == null) {
188: addError("Catalina home directory is not defined");
189: }
190: if (getCatalinaBaseDir() == null) {
191: addError("Catalina base directory is not defined");
192: }
193:
194: verifyDirectory("Catalina home", getCatalinaHomeDir());
195: verifyDirectory("Catalina base", getCatalinaBaseDir());
196:
197: // if catalina home or base has errors, just give up
198: if (hasErrors()) {
199: return false;
200: }
201:
202: verifyWritableDirectory("Catalina lib", getCatalinaLibDir());
203: verifyWritableDirectory("Catalina conf", getCatalinaConfDir());
204: verifyDirectory("Catalina bin", getCatalinaBinDir());
205: verifyWritableFile("Catalina server.xml", getServerXmlFile());
206: verifyWritableFile("Catalina catalina.sh", getCatalinaShFile());
207: verifyWritableFile("Catalina catalina.sh", getCatalinaBatFile());
208:
209: verifyDirectory("OpenEJB lib", getOpenEJBLibDir());
210:
211: File openejbLoaderJar = getOpenEJBTomcatLoaderJar();
212: if (openejbLoaderJar == null) {
213: addError("OpenEJB loader jar was not found in the OpenEJB lib dir");
214: }
215: verifyFile("OpenEJB loader jar", openejbLoaderJar);
216:
217: File openejbJavaagentJar = getOpenEJBJavaagentJar();
218: if (openejbJavaagentJar == null) {
219: addError("OpenEJB javaagent jar was not found in the OpenEJB lib dir");
220: }
221: verifyFile("OpenEJB javaagent jar", openejbJavaagentJar);
222:
223: File openejbCoreJar = getOpenEJBCoreJar();
224: if (openejbCoreJar != null) {
225: verifyFile("OpenEJB core jar", openejbCoreJar);
226: }
227:
228: verifyFile("Updated Tomcat annotation-api jar",
229: getUpdatedAnnotationApiJar());
230:
231: return !hasErrors();
232: }
233:
234: public void reset() {
235: errors.clear();
236: }
237:
238: public boolean hasErrors() {
239: return !errors.isEmpty();
240: }
241:
242: public List<String> getErrors() {
243: return errors;
244: }
245:
246: private void addError(String message) {
247: errors.add(message);
248: }
249:
250: private boolean verifyDirectory(String description, File file) {
251: if (file == null) {
252: // ignore... files are built up based on other files, and probles
253: // with the root files will have been logged else where
254: return false;
255: }
256: if (!file.exists()) {
257: addError(description + " directory does not exist");
258: return false;
259: }
260: if (!file.isDirectory()) {
261: addError(description + " directory is not a directory");
262: return false;
263: }
264: if (!file.canRead()) {
265: addError(description + " directory is not readable");
266: return false;
267: }
268: return true;
269: }
270:
271: private void verifyWritableDirectory(String description, File file) {
272: if (verifyDirectory(description, file)) {
273: verifyWritable(description, file);
274: }
275: }
276:
277: private boolean verifyFile(String description, File file) {
278: if (file == null) {
279: // ignore... files are built up based on other files, and probles
280: // with the root files will have been logged else where
281: return false;
282: }
283: if (!file.exists()) {
284: addError(description + " file does not exist");
285: return false;
286: }
287: if (!file.isFile()) {
288: addError(description + " file is not a file");
289: return false;
290: }
291: if (!file.canRead()) {
292: addError(description + " file is not readable");
293: return false;
294: }
295: return true;
296: }
297:
298: private void verifyWritableFile(String description, File file) {
299: if (verifyFile(description, file)) {
300: verifyWritable(description, file);
301: }
302: }
303:
304: private void verifyWritable(String description, File file) {
305: if (file == null) {
306: // ignore... files are built up based on other files, and probles
307: // with the root files will have been logged else where
308: return;
309: }
310: if (!file.canWrite()) {
311: addError(description + " file is not writable");
312: }
313: }
314:
315: private File createFile(String catalinaHomeDir) {
316: if (catalinaHomeDir != null
317: && catalinaHomeDir.trim().length() > 0) {
318: return new File(catalinaHomeDir.trim());
319: }
320: return null;
321: }
322: }
|