1 21 22 package org.apache.derbyTesting.functionTests.harness; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.util.Locale ; 28 import java.util.Properties ; 29 import java.util.Vector ; 30 import java.util.Hashtable ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Constructor ; 33 import java.net.ConnectException ; 34 import java.net.Socket ; 35 import org.apache.derbyTesting.functionTests.util.TestUtil; 36 37 public class NetServer 38 { 39 40 File homeDir; String jvmName = "jdk13"; 42 String clPath; 43 String javaCmd; 44 String jvmflags; 45 String framework; 46 static String hostName; 47 48 Object [] frameworkInfo; 49 int port; 50 Process pr; 51 BackgroundStreamSaver outSaver, errSaver; 52 FileOutputStream fosOut, fosErr; 53 private String java; 54 private boolean startServer; 56 Object networkServer; Method pingMethod; 59 60 private static String NETWORK_SERVER_CLASS_NAME="org.apache.derby.drda.NetworkServerControl"; 61 62 public static Hashtable m; 63 public static int PREFIX_POS = 0; 64 public static int SUFFIX_POS = 1; 65 public static int DRIVER_POS = 2; 66 public static int PORT_POS = 3; 67 public static int START_CMD_POS = 4; 68 public static int STOP_CMD1_POS = 5; 69 public static int STOP_CMD2_POS = 6; 70 71 72 static { 73 hostName=TestUtil.getHostName(); 74 m = new Hashtable (); 75 String url = "jdbc:derby:net://" + hostName + ":1527/"; 80 m.put("DerbyNet", new Object [] 81 {url, "", "com.ibm.db2.jcc.DB2Driver", "1527", new String [] {NETWORK_SERVER_CLASS_NAME, "start"}, 87 new String [] {NETWORK_SERVER_CLASS_NAME, "shutdown"}, 89 null}); 91 url = "jdbc:derby://" + hostName + ":1527/"; 92 m.put("DerbyNetClient", new Object [] 93 {url, "", "org.apache.derby.jdbc.ClientDriver", "1527", new String [] {NETWORK_SERVER_CLASS_NAME, "start"}, 99 new String [] {NETWORK_SERVER_CLASS_NAME, "shutdown"}, 101 null}); 103 url = "jdbc:db2://" + hostName + ":50000/"; 104 m.put("DB2jcc", new Object [] 105 {url, "", "com.ibm.db2.jcc.DB2Driver", "50000", null, null, 111 null}); 112 113 m.put("DB2app", new Object [] 114 {"jdbc:db2:", 115 "", 116 "COM.ibm.db2.jdbc.app.DB2Driver", 117 "0", 118 null, 119 null, 120 null}); 121 } 122 123 public NetServer(File homeDir, String jvmName, String clPath, String 124 javaCmd, String jvmflags, String framework, boolean startServer) 125 throws Exception 126 { 127 this.homeDir = homeDir; 128 this.jvmName = jvmName; 129 this.clPath = clPath; 130 this.javaCmd = javaCmd; 131 this.jvmflags = jvmflags; 132 this.framework = framework; 133 frameworkInfo = (Object []) m.get(framework); 134 135 this.port = Integer.parseInt((String ) frameworkInfo[PORT_POS]); 136 this.startServer = startServer; 137 139 } 140 public void start() throws Exception 141 { 142 if (! startServer) 143 { 144 System.out.println("startServer = false. Bypass server startup"); 145 return; 146 } 147 148 (new File (homeDir, framework + "Server")).mkdir(); 150 String [] startcmd = (String []) frameworkInfo[START_CMD_POS]; 151 if (startcmd == null) 153 return; 154 155 String homeDirName = homeDir.getCanonicalPath(); 157 jvm jvm = null; jvm = jvm.getJvm(jvmName); 159 if (jvmName.equals("jview")) 160 jvm.setJavaCmd("jview"); 161 else if (javaCmd != null) 162 jvm.setJavaCmd(javaCmd); 163 164 Vector jvmProps = new Vector (); 165 if ( (clPath != null) && (clPath.length()>0) ) 166 jvm.setClasspath(clPath); 167 168 if ( (jvmflags != null) && (jvmflags.length()>0) ) { 169 jvm.setFlags(jvmflags); 170 } 173 174 jvmProps.addElement("derby.system.home=" + homeDirName); 175 jvm.setD(jvmProps); 176 jvm.setSecurityProps(); 177 Vector vCmd = jvm.getCommandLine(); 181 for (int i = 0; i < startcmd.length; i++) 182 vCmd.addElement(startcmd[i]); 183 184 String serverCmd[] = new String [vCmd.size()]; 185 for (int i = 0; i < vCmd.size(); i++) 186 { 187 serverCmd[i] = (String )vCmd.elementAt(i); 188 System.out.print(serverCmd[i] + " "); 189 } 190 System.out.println(""); 191 pr = Runtime.getRuntime().exec(serverCmd); 193 194 File out = new File (homeDir, framework + ".out"); 196 fosOut = new FileOutputStream (out); 197 outSaver = new BackgroundStreamSaver(pr.getInputStream(), fosOut); 198 File err = new File (homeDir, framework + ".err"); 199 fosErr = new FileOutputStream (err); 200 errSaver = new BackgroundStreamSaver(pr.getErrorStream(), fosErr); 201 202 for (int i = 0 ; i <= 120 ; i++) 203 { 204 if (isDB2Connection(framework)) 206 break; 207 208 try 209 { 210 if (isNetworkServerConnection(framework)) 211 { 212 if (testNetworkServerConnection()) 215 break; 216 } 217 else 218 { 219 Socket s = new Socket (hostName, this.port); 220 s.close(); 221 break; 222 } 223 224 } 225 catch (Exception e) 226 { 227 if (err.length() > 0) { 229 break; 230 } else { 231 Thread.sleep(1000); 235 } 238 239 } 240 } 241 } 242 243 public boolean testNetworkServerConnection() throws Exception 244 { 245 if (! startServer) 246 { 247 System.out.println("startServer = false. Bypass server check"); 248 return true; 249 } 250 251 Object [] testConnectionArg = null; 252 if (networkServer == null) 253 { 254 Constructor serverConstructor; 255 Class serverClass = Class.forName(NETWORK_SERVER_CLASS_NAME); 256 serverConstructor = serverClass.getConstructor(null); 257 networkServer = serverConstructor.newInstance(null); 258 pingMethod = networkServer.getClass().getMethod("ping", 259 null); 260 } 261 pingMethod.invoke(networkServer,null); 262 return true; 263 } 264 265 public void stop() throws Exception 267 { 268 if (! startServer) 269 { 270 return; 271 } 272 273 System.out.println("Attempt to shutdown framework: " 274 + framework); 275 jvm jvm = null; jvm = jvm.getJvm(jvmName); 277 Vector jvmCmd = jvm.getCommandLine(); 278 279 Vector connV = new Vector (); 280 for (int i = 0; i < jvmCmd.size(); i++) 281 { 282 connV.addElement((String )jvmCmd.elementAt(i)); 283 } 284 285 String [] stopcmd1 = (String []) frameworkInfo[STOP_CMD1_POS]; 286 if (stopcmd1 == null) 287 return; 288 289 for (int i = 0; i < stopcmd1.length; i++) 290 connV.addElement(stopcmd1[i]); 291 292 String [] connCmd = new String [connV.size()]; 293 for (int i = 0; i < connV.size(); i++) 294 { 295 connCmd[i] = (String )connV.elementAt(i); 296 } 297 298 299 Vector stopV = new Vector (); 300 for (int i = 0; i < jvmCmd.size(); i++) 301 { 302 stopV.addElement((String )jvmCmd.elementAt(i)); 303 } 304 Process prconn = Runtime.getRuntime().exec(connCmd); 305 TimedProcess tp = new TimedProcess(prconn); 307 tp.waitFor(60); 308 309 String [] stopcmd2 = (String []) frameworkInfo[STOP_CMD2_POS]; 310 if (stopcmd2 != null) 311 { 312 for (int i = 0; i < stopcmd2.length; i++) 313 stopV.addElement(stopcmd2[i]); 314 315 String [] stopCmd = new String [stopV.size()]; 316 for (int i = 0; i < stopV.size(); i++) 317 { 318 stopCmd[i] = (String )stopV.elementAt(i); 319 } 320 321 Process prstop = Runtime.getRuntime().exec(stopCmd); 322 prstop.waitFor(); 323 } 324 325 tp = new TimedProcess(pr); 327 tp.waitFor(60); 329 330 outSaver.finish(); 332 errSaver.finish(); 333 } 334 335 public void printFramworkInfo(String framework) 336 { 337 System.out.println("PREFIX = " + frameworkInfo[PREFIX_POS]); 338 System.out.println("SUFFIX = " + frameworkInfo[SUFFIX_POS]); 339 System.out.println("DRIVER = " + frameworkInfo[DRIVER_POS]); 340 System.out.println("PORT = " + frameworkInfo[PORT_POS]); 341 342 for (int index = START_CMD_POS; index <= STOP_CMD2_POS; index++) 343 { 344 String cmdString = ""; 345 String [] cmdArray = (String []) frameworkInfo[index] ; 346 for (int i = 0; i < cmdArray.length; i++) 347 { 348 cmdString += " " + cmdArray[i]; 349 } 350 if (index == START_CMD_POS) 351 System.out.println("START_CMD = " + cmdString); 352 else 353 System.out.println("STOP_CMD = " + cmdString); 354 355 } 356 } 357 358 public static String getURLPrefix(String fm) 360 { 361 Object [] info = (Object []) m.get(fm); 362 return (String ) info[PREFIX_POS]; 363 } 364 365 public static String getURLSuffix(String fm) 366 { 367 Object [] info = (Object []) m.get(fm); 368 return (String ) info[SUFFIX_POS]; 369 } 370 371 public static String getDriverName(String fm) 372 { 373 Object [] info = (Object []) m.get(fm); 374 if (info != null) 375 return (String ) info[DRIVER_POS]; 376 else 377 return null; 378 } 379 380 public static boolean isDB2Connection(String fm) 381 { 382 return (fm.toUpperCase(Locale.ENGLISH).equals("DB2APP") || 383 fm.toUpperCase(Locale.ENGLISH).equals("DB2JCC")); 384 385 } 386 387 public static boolean isNetworkServerConnection(String fm) 388 { 389 return (fm.toUpperCase(Locale.ENGLISH).startsWith("DERBYNET")); 390 } 391 392 public static boolean isClientConnection(String fm) 393 { 394 return (fm.toUpperCase(Locale.ENGLISH).startsWith("DERBYNET") || 395 fm.toUpperCase(Locale.ENGLISH).equals("DB2JCC")); 396 } 397 398 public static boolean isJCCConnection(String fm) 399 { 400 return fm.toUpperCase(Locale.ENGLISH).equals("DB2JCC") || 401 fm.toUpperCase(Locale.ENGLISH).equals("DERBYNET"); 402 } 403 404 409 410 public static String alterURL(String fm, String url) 411 { 412 String urlPrefix = "jdbc:derby:"; 413 String newURLPrefix = getURLPrefix(fm); 414 String newURLSuffix = getURLSuffix(fm); 415 416 if (newURLPrefix == null) 419 return url; 420 421 if (newURLSuffix == null) 422 newURLSuffix = ""; 423 424 if (url.equals(urlPrefix)) return newURLPrefix; 426 427 int attrOffset = url.indexOf(';'); 430 if (NetServer.isDB2Connection(fm) && 431 attrOffset != -1) 432 url = url.substring(0,attrOffset); 433 434 435 if (url.startsWith(urlPrefix)) 436 { 437 url = newURLPrefix + 439 url.substring(urlPrefix.length()) + 440 newURLSuffix; 441 } 442 else 443 { 444 if (! (url.startsWith("jdbc:"))) 445 { 446 url = newURLPrefix + url + newURLSuffix; 447 } 448 } 449 return url; 450 } 451 452 453 } 454 | Popular Tags |