001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/mailarchive/tags/sakai_2-4-1/mailarchive-james/james/src/java/org/sakaiproject/james/JamesServlet.java $
003: * $Id: JamesServlet.java 8186 2006-04-24 01:42:56Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.james;
021:
022: import java.io.BufferedReader;
023: import java.io.BufferedWriter;
024: import java.io.File;
025: import java.io.FileReader;
026: import java.io.FileWriter;
027: import java.io.Writer;
028: import java.util.HashMap;
029:
030: import javax.servlet.ServletConfig;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServlet;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.sakaiproject.component.cover.ServerConfigurationService;
037: import org.sakaiproject.util.StringUtil;
038:
039: /**
040: * <p>
041: * JamesServlet starts James.
042: * </p>
043: */
044: public class JamesServlet extends HttpServlet {
045: /** Our log (commons). */
046: private static Log M_log = LogFactory.getLog(JamesServlet.class);
047:
048: /** config variable and system property for the james / phoenix home. */
049: private final static String PHOENIX_HOME = "phoenix.home";
050:
051: /** The james / phoenix home value. */
052: protected String m_phoenixHome = null;
053:
054: /** The JamesRunner (Thread). */
055: protected JamesRunner m_runner = null;
056:
057: /** James thread */
058: public class JamesRunner extends Thread {
059: /**
060: * construct and start the init activity
061: */
062: public JamesRunner() {
063: start();
064: }
065:
066: /**
067: * Run the James thread.
068: */
069: public void run() {
070: System.setProperty(PHOENIX_HOME, m_phoenixHome);
071:
072: M_log.info("run: starting James service");
073:
074: // start James / Avalon running in this VM.
075: try {
076: // set the phoenix log in our log area (logs in sakai.home)
077: String sakaiHome = System.getProperty("sakai.home");
078: String[] args = new String[2];
079: args[0] = "-l";
080: args[1] = sakaiHome + "logs/phoenix.log";
081:
082: int exitCode = PhoenixLauncherMain.startup(args,
083: new HashMap(), true);
084: M_log.info("run: James service stopped: " + exitCode);
085: } catch (Throwable e) {
086: M_log.warn("run: exception:", e);
087: }
088: }
089: }
090:
091: /**
092: * Access the Servlet's information display.
093: *
094: * @return servlet information.
095: */
096: public String getServletInfo() {
097: return "Sakai James Servlet";
098: }
099:
100: /**
101: * Initialize the servlet.
102: *
103: * @param config
104: * The servlet config.
105: * @throws ServletException
106: */
107: public void init(ServletConfig config) throws ServletException {
108: super .init(config);
109:
110: startJames(config);
111: }
112:
113: /**
114: * Shutdown the servlet.
115: */
116: public void destroy() {
117: M_log.info("destroy()");
118:
119: if (m_runner == null)
120: return;
121:
122: PhoenixLauncherMain.shutdown();
123:
124: m_runner = null;
125:
126: super .destroy();
127: }
128:
129: protected void startJames(ServletConfig config) {
130: // get config info
131: String host = ServerConfigurationService.getServerName();
132: String dns1 = StringUtil.trimToNull(ServerConfigurationService
133: .getString("smtp.dns.1"));
134: String dns2 = StringUtil.trimToNull(ServerConfigurationService
135: .getString("smtp.dns.2"));
136: String smtpPort = StringUtil
137: .trimToNull(ServerConfigurationService
138: .getString("smtp.port"));
139: boolean enabled = ServerConfigurationService.getBoolean(
140: "smtp.enabled", false);
141:
142: M_log.info("init(): host: " + host + " enabled: " + enabled
143: + " dns1: " + dns1 + " dns2: " + dns2 + " smtp.port: "
144: + smtpPort);
145:
146: // if not enabled, don't start james
147: if (!enabled) {
148: return;
149: }
150:
151: // set the home for james / phoenix, as configured
152: String homeRelative = config.getInitParameter(PHOENIX_HOME);
153: if (homeRelative == null) {
154: // or pointing to the webapps root if not configured
155: homeRelative = "";
156: }
157:
158: // expand to real path
159: m_phoenixHome = getServletContext().getRealPath(homeRelative);
160:
161: customizeConfig(host, dns1, dns2, smtpPort);
162:
163: // start the James thread
164: m_runner = new JamesRunner();
165: }
166:
167: protected void customizeConfig(String host, String dns1,
168: String dns2, String smtpPort) {
169: try {
170: // read the config file
171: File f = new File(m_phoenixHome
172: + "/apps/james/SAR-INF/config.xml");
173: BufferedReader input = new BufferedReader(new FileReader(f));
174: StringBuffer contents = new StringBuffer();
175: String line = null;
176: while ((line = input.readLine()) != null) {
177: contents.append(line);
178: contents.append(System.getProperty("line.separator"));
179: }
180: input.close();
181:
182: // modify [HOST], [DNS1], [DNS2] and [SMTP.PORT]
183: int pos = -1;
184: String target = "[HOST]";
185: while ((pos = contents.indexOf(target)) != -1) {
186: contents.replace(pos, pos + target.length(), host);
187: }
188:
189: target = "<server>[DNS1]</server>";
190: pos = contents.indexOf(target);
191: if (pos != -1) {
192: if ((dns1 != null) && (dns1.length() > 0)) {
193: contents.replace(pos, pos + target.length(),
194: "<server>" + dns1 + "</server>");
195: } else {
196: contents.replace(pos, pos + target.length(), "");
197: }
198: }
199:
200: target = "<server>[DNS2]</server>";
201: pos = contents.indexOf(target);
202: if (pos != -1) {
203: if ((dns2 != null) && (dns2.length() > 0)) {
204: contents.replace(pos, pos + target.length(),
205: "<server>" + dns2 + "</server>");
206: } else {
207: contents.replace(pos, pos + target.length(), "");
208: }
209: }
210:
211: target = "<port>[SMTP.PORT]</port>";
212: pos = contents.indexOf(target);
213: if (pos != -1) {
214: if ((smtpPort == null) || (smtpPort.length() == 0)) {
215: smtpPort = "25";
216: }
217: contents.replace(pos, pos + target.length(), "<port>"
218: + smtpPort + "</port>");
219: }
220:
221: // write it back
222: Writer output = null;
223: try {
224: output = new BufferedWriter(new FileWriter(f));
225: output.write(contents.toString());
226: } finally {
227: if (output != null)
228: output.close();
229: }
230: } catch (Exception e) {
231: }
232:
233: try {
234: // read the environment file
235: File f = new File(m_phoenixHome
236: + "/apps/james/SAR-INF/environment.xml");
237: BufferedReader input = new BufferedReader(new FileReader(f));
238: StringBuffer contents = new StringBuffer();
239: String line = null;
240: while ((line = input.readLine()) != null) {
241: contents.append(line);
242: contents.append(System.getProperty("line.separator"));
243: }
244: input.close();
245:
246: // modify [SAKAI.HOME]
247: int pos = -1;
248: String target = "[SAKAI.HOME]";
249: while ((pos = contents.indexOf(target)) != -1) {
250: contents.replace(pos, pos + target.length(), System
251: .getProperty("sakai.home"));
252: }
253:
254: // write it back
255: Writer output = null;
256: try {
257: output = new BufferedWriter(new FileWriter(f));
258: output.write(contents.toString());
259: } finally {
260: if (output != null)
261: output.close();
262: }
263: } catch (Exception e) {
264: }
265: }
266: }
|