001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: DerbyControl.java 11775 2008-01-31 07:48:00Z lzheng $
023: */
024:
025: package com.bostechcorp.cbesb.common.util;
026:
027: import java.io.File;
028: import java.io.FileOutputStream;
029: import java.io.FilenameFilter;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.util.Properties;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: public class DerbyControl extends Thread {
039: private static boolean isRunning = false;
040: private static DerbyControl runner;
041: private String host;
042: private String port;
043: private String serverRunDirectory;
044: private String rootDir;
045: private static Log log = LogFactory.getLog(DerbyControl.class);
046:
047: private Process serverProcess;
048: private StopServerOnShutdown shutdownHook;
049:
050: private DerbyControl() {
051: super ();
052: shutdownHook = new StopServerOnShutdown();
053: Runtime.getRuntime().addShutdownHook(shutdownHook);
054: }
055:
056: public void finalize() {
057: if (shutdownHook != null)
058: Runtime.getRuntime().removeShutdownHook(shutdownHook);
059: }
060:
061: private static final String PROPERTY_FILE = "derbyserver.properties";
062: private static final String SERVER_CLASS = "org.apache.derby.drda.NetworkServerControl";
063:
064: private static void init(String rootDir) throws IOException {
065: Properties dbprops = new Properties();
066: runner = new DerbyControl();
067: runner.rootDir = rootDir;
068: dbprops.load(RuntimeClassLoader.getInputStream(PROPERTY_FILE));
069: runner.host = dbprops.getProperty("host");
070: runner.port = dbprops.getProperty("port");
071: runner.serverRunDirectory = dbprops.getProperty("directory");
072: log.info("path:" + runner.makeClasspath());
073: }
074:
075: public static void startServer(String rootDir) {
076: try {
077: if (isRunning) {
078: // The server may be shutting down from a stop call so give it some time;
079: Thread.sleep(5000);
080: }
081: if (isRunning)
082: return;
083:
084: // create a new runner since we cant restart a thread after it terminates
085: // String rootDir = runner.rootDir;
086: DerbyControl.init(rootDir);
087: runner.start();
088: } catch (Exception e) {
089: ErrorUtil.printError("\n\n\nerror starting Derby server: ",
090: e);
091: }
092: }
093:
094: public static void stopServer() {
095: if (!isRunning)
096: return;
097: runner.serverProcess.destroy();
098: synchronized (runner.serverProcess) {
099: runner.serverProcess.notify();
100: }
101:
102: }
103:
104: private class ServerLog implements Runnable {
105: private Log log;
106: private InputStream is;
107: private OutputStream os;
108: private Thread runner;
109: private boolean isRunLog = false;
110:
111: protected ServerLog(Log log, InputStream is, OutputStream os) {
112: this .os = os;
113: this .log = log;
114: this .is = is;
115: isRunLog = true;
116: runner = new Thread(this );
117: runner.start();
118: }
119:
120: public void run() {
121: while (isRunLog) {
122: byte[] inbytes = new byte[100];
123: try {
124: int bytes = is.read(inbytes);
125: if (bytes > 0) {
126: os.write(inbytes, 0, bytes);
127: } else
128: break;
129: } catch (Exception e) {
130: if (!(e instanceof IOException)) {
131: ErrorUtil
132: .printError(
133: "\n\n\nerror logging database server output:",
134: e);
135: }
136: }
137: }
138: // log.info("ServerLog stopped");
139: }
140:
141: public void stop() {
142: isRunLog = false;
143: }
144: }
145:
146: private static class DerbyJarFilter implements FilenameFilter {
147: public boolean accept(File dir, String name) {
148: return name.endsWith(".jar") && name.contains("derby");
149: }
150: }
151:
152: private String makeClasspath() {
153: File rootDirFile = new File(rootDir);
154: File[] derbyJars = rootDirFile.listFiles(new DerbyJarFilter());
155: String result = "";
156: for (int i = 0; i < derbyJars.length; i++)
157: result = result.concat(((i > 0) ? File.pathSeparator : "")
158: + derbyJars[i].getAbsolutePath());
159:
160: //log.info("class path: " + result);
161: return result;
162: }
163:
164: public void run() {
165: try {
166: String command = "java -cp " + makeClasspath() + " "
167: + SERVER_CLASS + " start -h " + runner.host
168: + " -p " + runner.port;
169: File workingDir = new File(runner.serverRunDirectory);
170: Runtime rt = Runtime.getRuntime();
171: FileOutputStream os = new FileOutputStream(new File(
172: workingDir, "derbyserver.log"));
173: log.info("\n\n\nstarting derby server");
174: log.info(" working dir=" + workingDir);
175: runner.serverProcess = rt.exec(command, null, workingDir);
176: isRunning = true;
177: ServerLog ol = new ServerLog(log, runner.serverProcess
178: .getInputStream(), os);
179: ServerLog el = new ServerLog(log, runner.serverProcess
180: .getErrorStream(), os);
181: runner.serverProcess.waitFor();
182: isRunning = false;
183: log.info("\n\n\nderby server exiting:");
184: ol.stop();
185: el.stop();
186:
187: } catch (Exception e) {
188: ErrorUtil.printError("\n\n\nerror starting derby server: ",
189: e);
190: }
191: }
192:
193: public class StopServerOnShutdown extends Thread {
194: public void run() {
195: log.info("In Shutdown hook, derby server exiting:");
196: if (isRunning) {
197: runner.serverProcess.destroy();
198: synchronized (runner.serverProcess) {
199: runner.serverProcess.notify();
200: }
201:
202: }
203: }
204: }
205:
206: static public void main(String[] args) {
207:
208: //DerbyControl.init(EsbPathHelper.getCbesbHomeDir()+"/db/");
209: DerbyControl.startServer(EsbPathHelper.getCbesbHomeDir()
210: + "/db/");
211:
212: }
213: }
|