1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.jdbc.DRDAServerStarter |
4 | |
5 | Copyright 2003, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.jdbc; |
22 | |
23 | import org.apache.derby.iapi.services.sanity.SanityManager; |
24 | import org.apache.derby.iapi.services.monitor.Monitor; |
25 | import org.apache.derby.iapi.services.monitor.ModuleControl; |
26 | import org.apache.derby.iapi.reference.MessageId; |
27 | import org.apache.derby.iapi.reference.Property; |
28 | import java.io.PrintWriter; |
29 | import java.lang.Runnable; |
30 | import java.lang.Thread; |
31 | import java.lang.reflect.Constructor; |
32 | import java.lang.reflect.Method; |
33 | import java.lang.reflect.InvocationTargetException; |
34 | import java.net.InetAddress; |
35 | import java.security.AccessController; |
36 | import java.security.PrivilegedActionException; |
37 | import java.security.PrivilegedExceptionAction; |
38 | |
39 | public final class DRDAServerStarter implements ModuleControl, Runnable |
40 | { |
41 | |
42 | private Object server; |
43 | private Method serverStartMethod; |
44 | private Method serverShutdownMethod; |
45 | private boolean loadSysIBM; |
46 | private Thread serverThread; |
47 | private static final String serverClassName = "org.apache.derby.impl.drda.NetworkServerControlImpl"; |
48 | private Class serverClass; |
49 | |
50 | private InetAddress listenAddress =null; |
51 | private int portNumber = -1; |
52 | private PrintWriter consoleWriter = null; |
53 | |
54 | /** |
55 | * Try to start the DRDA server. Log an error in error log and continue if it cannot be started. |
56 | */ |
57 | // public static void start() |
58 | // { |
59 | |
60 | |
61 | public void setStartInfo(InetAddress listenAddress, int portNumber, PrintWriter |
62 | consoleWriter) |
63 | { |
64 | this.listenAddress = listenAddress; |
65 | this.portNumber = portNumber; |
66 | this.consoleWriter = consoleWriter; |
67 | } |
68 | |
69 | |
70 | |
71 | public void boot(boolean create, |
72 | java.util.Properties properties) |
73 | { |
74 | if( server != null) |
75 | { |
76 | if (SanityManager.DEBUG) |
77 | SanityManager.THROWASSERT( "Network server starter module booted twice."); |
78 | return; |
79 | } |
80 | // Load the server class indirectly so that Cloudscape does not require the network code |
81 | try |
82 | { |
83 | serverClass = Class.forName( serverClassName); |
84 | } |
85 | catch( ClassNotFoundException cnfe) |
86 | { |
87 | Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_CLASS_FIND, serverClassName); |
88 | return; |
89 | } |
90 | catch( java.lang.Error e) |
91 | { |
92 | Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_CLASS_LOAD, |
93 | serverClassName, |
94 | e.getMessage()); |
95 | return; |
96 | } |
97 | try |
98 | { |
99 | Constructor serverConstructor; |
100 | try |
101 | { |
102 | serverConstructor = (Constructor) AccessController.doPrivileged( |
103 | new PrivilegedExceptionAction() { |
104 | public Object run() throws NoSuchMethodException, SecurityException |
105 | { |
106 | if (listenAddress == null) |
107 | return serverClass.getConstructor(null); |
108 | else |
109 | return |
110 | serverClass.getConstructor(new |
111 | Class[] {java.net.InetAddress.class, |
112 | Integer.TYPE});} |
113 | } |
114 | ); |
115 | serverStartMethod = (Method) AccessController.doPrivileged( |
116 | new PrivilegedExceptionAction() { |
117 | public Object run() throws NoSuchMethodException, SecurityException |
118 | { return serverClass.getMethod( "blockingStart", new Class[] { java.io.PrintWriter.class});} |
119 | } |
120 | ); |
121 | |
122 | serverShutdownMethod = (Method) AccessController.doPrivileged( |
123 | new PrivilegedExceptionAction() { |
124 | public Object run() throws NoSuchMethodException, SecurityException |
125 | { return serverClass.getMethod( "directShutdown", null);} |
126 | } |
127 | ); |
128 | } |
129 | catch( PrivilegedActionException e) |
130 | { |
131 | Exception e1 = e.getException(); |
132 | Monitor.logTextMessage( |
133 | MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e1.getMessage()); |
134 | e.printStackTrace(Monitor.getStream().getPrintWriter()); |
135 | return; |
136 | |
137 | } |
138 | if (listenAddress == null) |
139 | server = serverConstructor.newInstance( null); |
140 | else |
141 | server = serverConstructor.newInstance(new Object[] |
142 | {listenAddress, new Integer(portNumber)}); |
143 | |
144 | serverThread = Monitor.getMonitor().getDaemonThread( this, "NetworkServerStarter", false); |
145 | serverThread.start(); |
146 | } |
147 | catch( Exception e) |
148 | { |
149 | Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e.getMessage()); |
150 | server = null; |
151 | e.printStackTrace(Monitor.getStream().getPrintWriter()); |
152 | } |
153 | } // end of boot |
154 | |
155 | public void run() |
156 | { |
157 | try |
158 | { |
159 | serverStartMethod.invoke( server, |
160 | new Object[] {consoleWriter }); |
161 | } |
162 | catch( InvocationTargetException ite) |
163 | { |
164 | Monitor.logTextMessage( |
165 | MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, ite.getTargetException().getMessage()); |
166 | ite.printStackTrace(Monitor.getStream().getPrintWriter()); |
167 | |
168 | server = null; |
169 | } |
170 | catch( Exception e) |
171 | { |
172 | Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e.getMessage()); |
173 | server = null; |
174 | e.printStackTrace(Monitor.getStream().getPrintWriter()); |
175 | } |
176 | } |
177 | |
178 | public void stop() |
179 | { |
180 | try { |
181 | if( serverThread != null && serverThread.isAlive()) |
182 | { |
183 | serverShutdownMethod.invoke( server, |
184 | null); |
185 | serverThread.interrupt(); |
186 | serverThread = null; |
187 | } |
188 | |
189 | } |
190 | catch( InvocationTargetException ite) |
191 | { |
192 | Monitor.logTextMessage( |
193 | MessageId.CONN_NETWORK_SERVER_SHUTDOWN_EXCEPTION, ite.getTargetException().getMessage()); |
194 | ite.printStackTrace(Monitor.getStream().getPrintWriter()); |
195 | |
196 | } |
197 | catch( Exception e) |
198 | { |
199 | Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_SHUTDOWN_EXCEPTION, e.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 | } |