1 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 ; 30 import java.lang.Runnable ; 31 import java.lang.Thread ; 32 import java.lang.reflect.Constructor ; 33 import java.lang.reflect.Method ; 34 import java.lang.reflect.InvocationTargetException ; 35 import java.net.InetAddress ; 36 import java.security.AccessController ; 37 import java.security.PrivilegedActionException ; 38 import java.security.PrivilegedExceptionAction ; 39 40 public final class DRDAServerStarter implements ModuleControl, Runnable 41 { 42 43 private Object server; 44 private Method serverStartMethod; 45 private Method serverShutdownMethod; 46 private boolean loadSysIBM; 47 private Thread serverThread; 48 private static final String serverClassName = "org.apache.derby.impl.drda.NetworkServerControlImpl"; 49 private Class serverClass; 50 51 private InetAddress listenAddress =null; 52 private int portNumber = -1; 53 private PrintWriter consoleWriter = null; 54 55 58 61 62 public void setStartInfo(InetAddress listenAddress, int portNumber, PrintWriter 63 consoleWriter) 64 { 65 this.listenAddress = listenAddress; 66 this.portNumber = portNumber; 67 68 if (consoleWriter != null) 73 this.consoleWriter = new PrintWriter (consoleWriter,true); 74 else 75 this.consoleWriter = consoleWriter; 76 } 77 78 79 80 public void boot(boolean create, 81 java.util.Properties properties) 82 { 83 if( server != null) 84 { 85 if (SanityManager.DEBUG) 86 SanityManager.THROWASSERT( "Network server starter module booted twice."); 87 return; 88 } 89 try 91 { 92 serverClass = Class.forName( serverClassName); 93 } 94 catch( ClassNotFoundException cnfe) 95 { 96 Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_CLASS_FIND, serverClassName); 97 return; 98 } 99 catch( java.lang.Error e) 100 { 101 Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_CLASS_LOAD, 102 serverClassName, 103 e.getMessage()); 104 return; 105 } 106 try 107 { 108 Constructor serverConstructor; 109 try 110 { 111 serverConstructor = (Constructor ) AccessController.doPrivileged( 112 new PrivilegedExceptionAction () { 113 public Object run() throws NoSuchMethodException , SecurityException 114 { 115 if (listenAddress == null) 116 return serverClass.getConstructor(null); 117 else 118 return 119 serverClass.getConstructor(new 120 Class [] {java.net.InetAddress .class, 121 Integer.TYPE});} 122 } 123 ); 124 serverStartMethod = (Method ) AccessController.doPrivileged( 125 new PrivilegedExceptionAction () { 126 public Object run() throws NoSuchMethodException , SecurityException 127 { return serverClass.getMethod( "blockingStart", new Class [] { java.io.PrintWriter .class});} 128 } 129 ); 130 131 serverShutdownMethod = (Method ) AccessController.doPrivileged( 132 new PrivilegedExceptionAction () { 133 public Object run() throws NoSuchMethodException , SecurityException 134 { return serverClass.getMethod( "directShutdown", null);} 135 } 136 ); 137 } 138 catch( PrivilegedActionException e) 139 { 140 Exception 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 [] 151 {listenAddress, new Integer (portNumber)}); 152 153 serverThread = Monitor.getMonitor().getDaemonThread( this, "NetworkServerStarter", false); 154 serverThread.start(); 155 } 156 catch( Exception e) 157 { 158 Monitor.logTextMessage( MessageId.CONN_NETWORK_SERVER_START_EXCEPTION, e.getMessage()); 159 server = null; 160 e.printStackTrace(Monitor.getStream().getPrintWriter()); 161 } 162 } 164 public void run() 165 { 166 try 167 { 168 serverStartMethod.invoke( server, 169 new Object [] {consoleWriter }); 170 } 171 catch( InvocationTargetException 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 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 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 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 } } 223 | Popular Tags |