01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.pluto.util.install.file;
18:
19: import java.io.File;
20: import java.io.FileWriter;
21: import java.io.FilenameFilter;
22: import java.io.IOException;
23: import java.io.PrintWriter;
24:
25: import org.apache.commons.logging.Log;
26: import org.apache.commons.logging.LogFactory;
27:
28: /**
29: * TODO JavaDoc
30: *
31: * @version 1.0
32: * @since Dec 11, 2005
33: */
34: public class TomcatCrossContextGenerator {
35: private static final String PLUTO_TEMP_DIR = "PlutoDomain";
36: private static Log LOG = LogFactory
37: .getLog(TomcatCrossContextGenerator.class);
38:
39: public static void main(String[] args) throws IOException {
40:
41: File tomcatHome = new File(args[0]);
42: File webapps = new File(tomcatHome, "webapps");
43: File conf = new File(tomcatHome, "conf/Catalina/localhost");
44:
45: File[] files = webapps.listFiles(new PortletFileNameFilter());
46: for (int i = 0; i < files.length; i++) {
47: String fileName = files[i].getName();
48: String contextName = fileName.substring(0, fileName
49: .indexOf(".war"));
50:
51: createContextFile(conf, fileName, contextName);
52: }
53: }
54:
55: /**
56: * Creates a tomcat-specific context deployment descriptor
57: * and deploys it.
58: *
59: * @param confDir Tomcat conf directory
60: * @param fileName File name of the war
61: * @param contextName The root name of the context file
62: * @throws IOException If there is a problem
63: */
64: public static void createContextFile(File confDir, String fileName,
65: String contextName) throws IOException {
66: PrintWriter out = null;
67: try {
68: StringBuffer contents = new StringBuffer();
69: contents.append("<Context ").append("path=\"").append(
70: contextName).append("\" ").append("docBase=\"../")
71: .append(PLUTO_TEMP_DIR).append("/")
72: .append(fileName).append("\" ").append(
73: "crossContext=\"true\">").append(
74: "</Context>");
75: File confFile = new File(confDir, contextName + ".xml");
76: if (LOG.isInfoEnabled()) {
77: LOG.info("Writing file: " + confFile.getAbsolutePath());
78: }
79: out = new PrintWriter(new FileWriter(confFile));
80: out.println(contents.toString());
81: } finally {
82: if (out != null) {
83: out.flush();
84: out.close();
85: }
86: }
87: }
88:
89: public static class PortletFileNameFilter implements FilenameFilter {
90: public boolean accept(File dir, String name) {
91: if (name.startsWith("portlet") && name.endsWith(".war")) {
92: return true;
93: }
94: return false;
95: }
96: }
97:
98: }
|