001: /*
002:
003: Derby - Class org.apache.derby.iapi.jdbc.DRDAServerStarter
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
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: */
021:
022: package org.apache.derby.iapi.jdbc;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025: import org.apache.derby.iapi.services.monitor.Monitor;
026: import org.apache.derby.iapi.services.monitor.ModuleControl;
027: import org.apache.derby.iapi.reference.MessageId;
028: import org.apache.derby.iapi.reference.Property;
029: import java.io.PrintWriter;
030: import java.lang.Runnable;
031: import java.lang.Thread;
032: import java.lang.reflect.Constructor;
033: import java.lang.reflect.Method;
034: import java.lang.reflect.InvocationTargetException;
035: import java.net.InetAddress;
036: import java.security.AccessController;
037: import java.security.PrivilegedActionException;
038: import java.security.PrivilegedExceptionAction;
039:
040: public final class DRDAServerStarter implements ModuleControl, Runnable {
041:
042: private Object server;
043: private Method serverStartMethod;
044: private Method serverShutdownMethod;
045: private boolean loadSysIBM;
046: private Thread serverThread;
047: private static final String serverClassName = "org.apache.derby.impl.drda.NetworkServerControlImpl";
048: private Class serverClass;
049:
050: private InetAddress listenAddress = null;
051: private int portNumber = -1;
052: private PrintWriter consoleWriter = null;
053:
054: /**
055: * Try to start the DRDA server. Log an error in error log and continue if it cannot be started.
056: */
057: // public static void start()
058: // {
059:
060: public void setStartInfo(InetAddress listenAddress, int portNumber,
061: PrintWriter consoleWriter) {
062: this .listenAddress = listenAddress;
063: this .portNumber = portNumber;
064:
065: // wrap the user-set consoleWriter with autoflush to true.
066: // this will ensure that messages to console will be
067: // written out to the consoleWriter on a println.
068: // DERBY-1466
069: if (consoleWriter != null)
070: this .consoleWriter = new PrintWriter(consoleWriter, true);
071: else
072: this .consoleWriter = consoleWriter;
073: }
074:
075: public void boot(boolean create, java.util.Properties properties) {
076: if (server != null) {
077: if (SanityManager.DEBUG)
078: SanityManager
079: .THROWASSERT("Network server starter module booted twice.");
080: return;
081: }
082: // Load the server class indirectly so that Cloudscape does not require the network code
083: try {
084: serverClass = Class.forName(serverClassName);
085: } catch (ClassNotFoundException cnfe) {
086: Monitor.logTextMessage(
087: MessageId.CONN_NETWORK_SERVER_CLASS_FIND,
088: serverClassName);
089: return;
090: } catch (java.lang.Error e) {
091: Monitor.logTextMessage(
092: MessageId.CONN_NETWORK_SERVER_CLASS_LOAD,
093: serverClassName, e.getMessage());
094: return;
095: }
096: try {
097: Constructor serverConstructor;
098: try {
099: serverConstructor = (Constructor) AccessController
100: .doPrivileged(new PrivilegedExceptionAction() {
101: public Object run()
102: throws NoSuchMethodException,
103: SecurityException {
104: if (listenAddress == null)
105: return serverClass
106: .getConstructor(null);
107: else
108: return serverClass
109: .getConstructor(new Class[] {
110: java.net.InetAddress.class,
111: Integer.TYPE });
112: }
113: });
114: serverStartMethod = (Method) AccessController
115: .doPrivileged(new PrivilegedExceptionAction() {
116: public Object run()
117: throws NoSuchMethodException,
118: SecurityException {
119: return serverClass
120: .getMethod(
121: "blockingStart",
122: new Class[] { java.io.PrintWriter.class });
123: }
124: });
125:
126: serverShutdownMethod = (Method) AccessController
127: .doPrivileged(new PrivilegedExceptionAction() {
128: public Object run()
129: throws NoSuchMethodException,
130: SecurityException {
131: return serverClass.getMethod(
132: "directShutdown", null);
133: }
134: });
135: } catch (PrivilegedActionException e) {
136: Exception e1 = e.getException();
137: Monitor.logTextMessage(
138: MessageId.CONN_NETWORK_SERVER_START_EXCEPTION,
139: e1.getMessage());
140: e.printStackTrace(Monitor.getStream().getPrintWriter());
141: return;
142:
143: }
144: if (listenAddress == null)
145: server = serverConstructor.newInstance(null);
146: else
147: server = serverConstructor.newInstance(new Object[] {
148: listenAddress, new Integer(portNumber) });
149:
150: serverThread = Monitor.getMonitor().getDaemonThread(this ,
151: "NetworkServerStarter", false);
152: serverThread.start();
153: } catch (Exception e) {
154: Monitor.logTextMessage(
155: MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e
156: .getMessage());
157: server = null;
158: e.printStackTrace(Monitor.getStream().getPrintWriter());
159: }
160: } // end of boot
161:
162: public void run() {
163: try {
164: serverStartMethod.invoke(server,
165: new Object[] { consoleWriter });
166: } catch (InvocationTargetException ite) {
167: Monitor.logTextMessage(
168: MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, ite
169: .getTargetException().getMessage());
170: ite.printStackTrace(Monitor.getStream().getPrintWriter());
171:
172: server = null;
173: } catch (Exception e) {
174: Monitor.logTextMessage(
175: MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e
176: .getMessage());
177: server = null;
178: e.printStackTrace(Monitor.getStream().getPrintWriter());
179: }
180: }
181:
182: public void stop() {
183: try {
184: if (serverThread != null && serverThread.isAlive()) {
185: serverShutdownMethod.invoke(server, null);
186: serverThread.interrupt();
187: serverThread = null;
188: }
189:
190: } catch (InvocationTargetException ite) {
191: Monitor.logTextMessage(
192: MessageId.CONN_NETWORK_SERVER_SHUTDOWN_EXCEPTION,
193: ite.getTargetException().getMessage());
194: ite.printStackTrace(Monitor.getStream().getPrintWriter());
195:
196: } catch (Exception e) {
197: Monitor.logTextMessage(
198: MessageId.CONN_NETWORK_SERVER_SHUTDOWN_EXCEPTION, e
199: .getMessage());
200: e.printStackTrace(Monitor.getStream().getPrintWriter());
201: }
202:
203: serverThread = null;
204: server = null;
205: serverClass = null;
206: serverStartMethod = null;
207: serverShutdownMethod = null;
208: listenAddress = null;
209: portNumber = -1;
210: consoleWriter = null;
211:
212: } // end of stop
213: }
|