KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > jdbc > DRDAServerStarter


1 /*
2
3    Derby - Class org.apache.derby.iapi.jdbc.DRDAServerStarter
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.jdbc;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.services.monitor.Monitor;
26 import org.apache.derby.iapi.services.monitor.ModuleControl;
27 import org.apache.derby.iapi.reference.MessageId;
28 import org.apache.derby.iapi.reference.Property;
29 import java.io.PrintWriter JavaDoc;
30 import java.lang.Runnable JavaDoc;
31 import java.lang.Thread JavaDoc;
32 import java.lang.reflect.Constructor JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34 import java.lang.reflect.InvocationTargetException JavaDoc;
35 import java.net.InetAddress JavaDoc;
36 import java.security.AccessController JavaDoc;
37 import java.security.PrivilegedActionException JavaDoc;
38 import java.security.PrivilegedExceptionAction JavaDoc;
39
40 public final class DRDAServerStarter implements ModuleControl, Runnable JavaDoc
41 {
42
43     private Object JavaDoc server;
44     private Method JavaDoc serverStartMethod;
45     private Method JavaDoc serverShutdownMethod;
46     private boolean loadSysIBM;
47     private Thread JavaDoc serverThread;
48     private static final String JavaDoc serverClassName = "org.apache.derby.impl.drda.NetworkServerControlImpl";
49     private Class JavaDoc serverClass;
50     
51     private InetAddress JavaDoc listenAddress =null;
52     private int portNumber = -1;
53     private PrintWriter JavaDoc consoleWriter = null;
54
55     /**
56      * Try to start the DRDA server. Log an error in error log and continue if it cannot be started.
57      */

58 // public static void start()
59
// {
60

61
62     public void setStartInfo(InetAddress JavaDoc listenAddress, int portNumber, PrintWriter JavaDoc
63                              consoleWriter)
64     {
65         this.listenAddress = listenAddress;
66         this.portNumber = portNumber;
67
68         // wrap the user-set consoleWriter with autoflush to true.
69
// this will ensure that messages to console will be
70
// written out to the consoleWriter on a println.
71
// DERBY-1466
72
if (consoleWriter != null)
73             this.consoleWriter = new PrintWriter JavaDoc(consoleWriter,true);
74         else
75             this.consoleWriter = consoleWriter;
76     }
77
78
79
80     public void boot(boolean create,
81                      java.util.Properties JavaDoc properties)
82     {
83         if( server != null)
84         {
85             if (SanityManager.DEBUG)
86                 SanityManager.THROWASSERT( "Network server starter module booted twice.");
87             return;
88         }
89         // Load the server class indirectly so that Cloudscape does not require the network code
90
try
91         {
92             serverClass = Class.forName( serverClassName);
93         }
94         catch( ClassNotFoundException JavaDoc cnfe)
95         {
96             Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_CLASS_FIND, serverClassName);
97             return;
98         }
99         catch( java.lang.Error JavaDoc e)
100         {
101             Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_CLASS_LOAD,
102                                     serverClassName,
103                                     e.getMessage());
104             return;
105         }
106         try
107         {
108             Constructor JavaDoc serverConstructor;
109             try
110             {
111                 serverConstructor = (Constructor JavaDoc) AccessController.doPrivileged(
112                   new PrivilegedExceptionAction JavaDoc() {
113                           public Object JavaDoc run() throws NoSuchMethodException JavaDoc, SecurityException JavaDoc
114                           {
115                               if (listenAddress == null)
116                                   return serverClass.getConstructor(null);
117                               else
118                                   return
119                                       serverClass.getConstructor(new
120                                           Class JavaDoc[] {java.net.InetAddress JavaDoc.class,
121                                                    Integer.TYPE});}
122                       }
123                   );
124                 serverStartMethod = (Method JavaDoc) AccessController.doPrivileged(
125                    new PrivilegedExceptionAction JavaDoc() {
126                            public Object JavaDoc run() throws NoSuchMethodException JavaDoc, SecurityException JavaDoc
127                            { return serverClass.getMethod( "blockingStart", new Class JavaDoc[] { java.io.PrintWriter JavaDoc.class});}
128                        }
129                    );
130                 
131                 serverShutdownMethod = (Method JavaDoc) AccessController.doPrivileged(
132                    new PrivilegedExceptionAction JavaDoc() {
133                            public Object JavaDoc run() throws NoSuchMethodException JavaDoc, SecurityException JavaDoc
134                            { return serverClass.getMethod( "directShutdown", null);}
135                        }
136                    );
137             }
138             catch( PrivilegedActionException JavaDoc e)
139             {
140                 Exception JavaDoc e1 = e.getException();
141                 Monitor.logTextMessage(
142                                        MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e1.getMessage());
143                 e.printStackTrace(Monitor.getStream().getPrintWriter());
144                 return;
145
146             }
147             if (listenAddress == null)
148                 server = serverConstructor.newInstance( null);
149             else
150                 server = serverConstructor.newInstance(new Object JavaDoc[]
151                     {listenAddress, new Integer JavaDoc(portNumber)});
152
153             serverThread = Monitor.getMonitor().getDaemonThread( this, "NetworkServerStarter", false);
154             serverThread.start();
155         }
156         catch( Exception JavaDoc e)
157         {
158             Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e.getMessage());
159             server = null;
160             e.printStackTrace(Monitor.getStream().getPrintWriter());
161         }
162     } // end of boot
163

164     public void run()
165     {
166         try
167         {
168             serverStartMethod.invoke( server,
169                                       new Object JavaDoc[] {consoleWriter });
170         }
171         catch( InvocationTargetException JavaDoc ite)
172         {
173             Monitor.logTextMessage(
174                                    MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, ite.getTargetException().getMessage());
175             ite.printStackTrace(Monitor.getStream().getPrintWriter());
176
177             server = null;
178         }
179         catch( Exception JavaDoc e)
180         {
181             Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e.getMessage());
182             server = null;
183             e.printStackTrace(Monitor.getStream().getPrintWriter());
184         }
185     }
186     
187     public void stop()
188     {
189         try {
190             if( serverThread != null && serverThread.isAlive())
191             {
192                 serverShutdownMethod.invoke( server,
193                                              null);
194                 serverThread.interrupt();
195                 serverThread = null;
196             }
197            
198         }
199         catch( InvocationTargetException JavaDoc ite)
200         {
201             Monitor.logTextMessage(
202                                    MessageId.CONN_NETWORK_SERVER_SHUTDOWN_EXCEPTION, ite.getTargetException().getMessage());
203             ite.printStackTrace(Monitor.getStream().getPrintWriter());
204             
205         }
206         catch( Exception JavaDoc e)
207         {
208             Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_SHUTDOWN_EXCEPTION, e.getMessage());
209             e.printStackTrace(Monitor.getStream().getPrintWriter());
210         }
211             
212         serverThread = null;
213         server = null;
214         serverClass = null;
215         serverStartMethod = null;
216         serverShutdownMethod = null;
217         listenAddress = null;
218         portNumber = -1;
219         consoleWriter = null;
220         
221     } // end of stop
222
}
223
Popular Tags