PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/derby-10.9.1.0/db-derby-10.9.1.0-src/java/testing/org/apache/derbyTesting/functionTests/harness/NetServer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 471 lines | 340 code | 68 blank | 63 comment | 54 complexity | ad526e13aea67a00f1d4699ebfc815d2 MD5 | raw file
  1. /*
  2. Derby - Class org.apache.derbyTesting.functionTests.harness.NetServer
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. package org.apache.derbyTesting.functionTests.harness;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileOutputStream;
  20. import java.util.Locale;
  21. import java.util.Properties;
  22. import java.util.Vector;
  23. import java.util.Hashtable;
  24. import java.lang.reflect.Method;
  25. import java.lang.reflect.Constructor;
  26. import java.net.ConnectException;
  27. import java.net.Socket;
  28. import org.apache.derbyTesting.functionTests.util.TestUtil;
  29. public class NetServer
  30. {
  31. File homeDir; // The server directory (usually the test directory)
  32. String jvmName = "jdk13";
  33. String clPath;
  34. String javaCmd;
  35. String jvmflags;
  36. String framework;
  37. String appsRequiredPassword;
  38. static String hostName;
  39. Object[] frameworkInfo;
  40. int port;
  41. Process pr;
  42. BackgroundStreamSaver outSaver, errSaver;
  43. FileOutputStream fosOut, fosErr;
  44. private boolean startServer; // whether test will start it's own server
  45. // Variables for test connection
  46. Object networkServer; // Server needs to be created with reflection
  47. Method pingMethod;
  48. private static String NETWORK_SERVER_CLASS_NAME="org.apache.derby.drda.NetworkServerControl";
  49. public static Hashtable m;
  50. public static int PREFIX_POS = 0;
  51. public static int SUFFIX_POS = 1;
  52. public static int DRIVER_POS = 2;
  53. public static int PORT_POS = 3;
  54. public static int START_CMD_POS = 4;
  55. public static int STOP_CMD1_POS = 5;
  56. public static int STOP_CMD2_POS = 6;
  57. static {
  58. hostName=TestUtil.getHostName();
  59. m = new Hashtable();
  60. // Hashtable is keyed on framework name and has
  61. // an array of the framework prefix, suffix, driver, port and
  62. // String[] command arguments to start the server
  63. // String[] Command arguments to stop the server
  64. String url = "jdbc:derby:net://" + hostName + ":1527/";
  65. m.put("DerbyNet", new Object[]
  66. {url, //prefix
  67. "", // suffix
  68. "com.ibm.db2.jcc.DB2Driver", //driver
  69. "1527", // port
  70. new String[] {NETWORK_SERVER_CLASS_NAME, //start
  71. "start"},
  72. new String[] {NETWORK_SERVER_CLASS_NAME, //shutdown
  73. "shutdown"},
  74. null}); //shutdown2
  75. url = "jdbc:derby://" + hostName + ":1527/";
  76. m.put("DerbyNetClient", new Object[]
  77. {url, //prefix
  78. "", // suffix
  79. "org.apache.derby.jdbc.ClientDriver", //driver
  80. "1527", // port
  81. new String[] {NETWORK_SERVER_CLASS_NAME, //start
  82. "start"},
  83. new String[] {NETWORK_SERVER_CLASS_NAME, //shutdown
  84. "shutdown"},
  85. null}); //shutdown2
  86. url = "jdbc:db2://" + hostName + ":50000/";
  87. m.put("DB2jcc", new Object[]
  88. {url, //prefix
  89. "", //suffix
  90. "com.ibm.db2.jcc.DB2Driver", //driver
  91. "50000", //port
  92. null, //start
  93. null,
  94. null});
  95. m.put("DB2app", new Object[]
  96. {"jdbc:db2:",
  97. "",
  98. "COM.ibm.db2.jdbc.app.DB2Driver",
  99. "0",
  100. null,
  101. null,
  102. null});
  103. }
  104. public NetServer(File homeDir, String jvmName, String clPath,
  105. String javaCmd, String jvmflags, String framework,
  106. boolean startServer, String appsRequiredPassword)
  107. throws Exception
  108. {
  109. this.homeDir = homeDir;
  110. this.jvmName = jvmName;
  111. this.clPath = clPath;
  112. this.javaCmd = javaCmd;
  113. this.jvmflags = jvmflags;
  114. this.framework = framework;
  115. // if authentication is required to shutdown server we need password
  116. // for user APP (the dbo).
  117. this.appsRequiredPassword = appsRequiredPassword;
  118. frameworkInfo = (Object[]) m.get(framework);
  119. this.port = Integer.parseInt((String) frameworkInfo[PORT_POS]);
  120. this.startServer = startServer;
  121. // System.out.println("framework: " + this.framework + "port: " + this.port);
  122. }
  123. public void start() throws Exception
  124. {
  125. if (! startServer)
  126. {
  127. System.out.println("startServer = false. Bypass server startup");
  128. return;
  129. }
  130. // Create the Server directory under the server dir
  131. (new File(homeDir, framework + "Server")).mkdir();
  132. String[] startcmd = (String[]) frameworkInfo[START_CMD_POS];
  133. // if we are just connecting to DB2 we return
  134. if (startcmd == null)
  135. return;
  136. // Build the command to run the WL server
  137. String homeDirName = homeDir.getCanonicalPath();
  138. jvm jvm = null; // to quiet the compiler
  139. jvm = jvm.getJvm(jvmName);
  140. if (jvmName.equals("jview"))
  141. jvm.setJavaCmd("jview");
  142. else if (javaCmd != null)
  143. jvm.setJavaCmd(javaCmd);
  144. Vector jvmProps = new Vector();
  145. if ( (clPath != null) && (clPath.length()>0) )
  146. jvm.setClasspath(clPath);
  147. if ( (jvmflags != null) && (jvmflags.length()>0) ) {
  148. jvm.setFlags(jvmflags);
  149. // Set no flags by default (DERBY-1614).
  150. // The jvmflags property can be used to set any kind of JVM option.
  151. }
  152. jvmProps.addElement("derby.system.home=" + homeDirName);
  153. jvm.setD(jvmProps);
  154. jvm.setSecurityProps();
  155. // For some platforms (like Mac) the process exec command
  156. // must be a string array; so we build this with a Vector
  157. // first because some strings (paths) could have spaces
  158. Vector vCmd = jvm.getCommandLine();
  159. for (int i = 0; i < startcmd.length; i++)
  160. vCmd.addElement(startcmd[i]);
  161. String serverCmd[] = new String[vCmd.size()];
  162. for (int i = 0; i < vCmd.size(); i++)
  163. {
  164. serverCmd[i] = (String)vCmd.elementAt(i);
  165. System.out.print(serverCmd[i] + " ");
  166. }
  167. System.out.println("");
  168. // Start a process to run the Server
  169. pr = Runtime.getRuntime().exec(serverCmd);
  170. // Write the out and err files to the server directory also
  171. File out = new File(homeDir, framework + ".out");
  172. fosOut = new FileOutputStream(out);
  173. outSaver = new BackgroundStreamSaver(pr.getInputStream(), fosOut);
  174. File err = new File(homeDir, framework + ".err");
  175. fosErr = new FileOutputStream(err);
  176. errSaver = new BackgroundStreamSaver(pr.getErrorStream(), fosErr);
  177. for (int i = 0 ; i <= 120 ; i++)
  178. {
  179. // No need to wait for DB2
  180. if (isDB2Connection(framework))
  181. break;
  182. try
  183. {
  184. if (isNetworkServerConnection(framework))
  185. {
  186. // adding a testconnection check
  187. // so that the test does not start before the server is up
  188. if (testNetworkServerConnection())
  189. break;
  190. }
  191. else
  192. {
  193. Socket s = new Socket(hostName, this.port);
  194. s.close();
  195. break;
  196. }
  197. }
  198. catch (Exception e)
  199. {
  200. // bail out if something has been written to stderr
  201. if (err.length() > 0) {
  202. break;
  203. } else {
  204. // it's probably unnecessary to sleep, since the
  205. // connection request generally takes a long time when
  206. // the listener hasn't started yet, but what the heck ...
  207. Thread.sleep(1000);
  208. // but here we iterate, and after 120 seconds, we stop
  209. // waiting to connect.
  210. }
  211. }
  212. }
  213. }
  214. public boolean testNetworkServerConnection() throws Exception
  215. {
  216. if (! startServer)
  217. {
  218. System.out.println("startServer = false. Bypass server check");
  219. return true;
  220. }
  221. Object[] testConnectionArg = null;
  222. if (networkServer == null)
  223. {
  224. Constructor serverConstructor;
  225. Class serverClass = Class.forName(NETWORK_SERVER_CLASS_NAME);
  226. serverConstructor = serverClass.getConstructor(null);
  227. networkServer = serverConstructor.newInstance(null);
  228. pingMethod = networkServer.getClass().getMethod("ping",
  229. null);
  230. }
  231. pingMethod.invoke(networkServer,null);
  232. return true;
  233. }
  234. // stop the Server
  235. public void stop() throws Exception
  236. {
  237. if (! startServer)
  238. {
  239. return;
  240. }
  241. System.out.println("Attempt to shutdown framework: "
  242. + framework);
  243. jvm jvm = null; // to quiet the compiler
  244. jvm = jvm.getJvm(jvmName);
  245. Vector jvmCmd = jvm.getCommandLine();
  246. Vector connV = new Vector();
  247. for (int i = 0; i < jvmCmd.size(); i++)
  248. {
  249. connV.addElement((String)jvmCmd.elementAt(i));
  250. }
  251. String[] stopcmd1 = (String[]) frameworkInfo[STOP_CMD1_POS];
  252. if (stopcmd1 == null)
  253. return;
  254. if (appsRequiredPassword != null) {
  255. String[] modifiedStopCmd = new String[stopcmd1.length + 4];
  256. System.arraycopy(stopcmd1, 0, modifiedStopCmd, 0, stopcmd1.length);
  257. modifiedStopCmd[stopcmd1.length] = "-user";
  258. modifiedStopCmd[stopcmd1.length + 1] = "app";
  259. modifiedStopCmd[stopcmd1.length + 2] = "-password";
  260. modifiedStopCmd[stopcmd1.length + 3] = appsRequiredPassword;
  261. stopcmd1 = modifiedStopCmd;
  262. }
  263. for (int i = 0; i < stopcmd1.length; i++)
  264. connV.addElement(stopcmd1[i]);
  265. String[] connCmd = new String[connV.size()];
  266. for (int i = 0; i < connV.size(); i++)
  267. {
  268. connCmd[i] = (String)connV.elementAt(i);
  269. }
  270. Vector stopV = new Vector();
  271. for (int i = 0; i < jvmCmd.size(); i++)
  272. {
  273. stopV.addElement((String)jvmCmd.elementAt(i));
  274. }
  275. Process prconn = Runtime.getRuntime().exec(connCmd);
  276. // Give the server sixty seconds to shutdown.
  277. TimedProcess tp = new TimedProcess(prconn);
  278. tp.waitFor(60);
  279. String[] stopcmd2 = (String[]) frameworkInfo[STOP_CMD2_POS];
  280. if (stopcmd2 != null)
  281. {
  282. for (int i = 0; i < stopcmd2.length; i++)
  283. stopV.addElement(stopcmd2[i]);
  284. String[] stopCmd = new String[stopV.size()];
  285. for (int i = 0; i < stopV.size(); i++)
  286. {
  287. stopCmd[i] = (String)stopV.elementAt(i);
  288. }
  289. Process prstop = Runtime.getRuntime().exec(stopCmd);
  290. prstop.waitFor();
  291. }
  292. // Try a TimedProcess as Phil did for the WLServer
  293. tp = new TimedProcess(pr);
  294. // In case the Server didn't shut down, force it to ...
  295. tp.waitFor(60);
  296. // Finish and close the redirected out and err files
  297. outSaver.finish();
  298. errSaver.finish();
  299. }
  300. public void printFramworkInfo(String framework)
  301. {
  302. System.out.println("PREFIX = " + frameworkInfo[PREFIX_POS]);
  303. System.out.println("SUFFIX = " + frameworkInfo[SUFFIX_POS]);
  304. System.out.println("DRIVER = " + frameworkInfo[DRIVER_POS]);
  305. System.out.println("PORT = " + frameworkInfo[PORT_POS]);
  306. for (int index = START_CMD_POS; index <= STOP_CMD2_POS; index++)
  307. {
  308. String cmdString = "";
  309. String[] cmdArray = (String[]) frameworkInfo[index] ;
  310. for (int i = 0; i < cmdArray.length; i++)
  311. {
  312. cmdString += " " + cmdArray[i];
  313. }
  314. if (index == START_CMD_POS)
  315. System.out.println("START_CMD = " + cmdString);
  316. else
  317. System.out.println("STOP_CMD = " + cmdString);
  318. }
  319. }
  320. // Get Framework Info
  321. public static String getURLPrefix(String fm)
  322. {
  323. Object[] info = (Object[]) m.get(fm);
  324. return (String) info[PREFIX_POS];
  325. }
  326. public static String getURLSuffix(String fm)
  327. {
  328. Object[] info = (Object[]) m.get(fm);
  329. return (String) info[SUFFIX_POS];
  330. }
  331. public static String getDriverName(String fm)
  332. {
  333. Object[] info = (Object[]) m.get(fm);
  334. if (info != null)
  335. return (String) info[DRIVER_POS];
  336. else
  337. return null;
  338. }
  339. public static boolean isDB2Connection(String fm)
  340. {
  341. return (fm.toUpperCase(Locale.ENGLISH).equals("DB2APP") ||
  342. fm.toUpperCase(Locale.ENGLISH).equals("DB2JCC"));
  343. }
  344. public static boolean isNetworkServerConnection(String fm)
  345. {
  346. return (fm.toUpperCase(Locale.ENGLISH).startsWith("DERBYNET"));
  347. }
  348. public static boolean isClientConnection(String fm)
  349. {
  350. return (fm.toUpperCase(Locale.ENGLISH).startsWith("DERBYNET") ||
  351. fm.toUpperCase(Locale.ENGLISH).equals("DB2JCC"));
  352. }
  353. public static boolean isJCCConnection(String fm)
  354. {
  355. return fm.toUpperCase(Locale.ENGLISH).equals("DB2JCC") ||
  356. fm.toUpperCase(Locale.ENGLISH).equals("DERBYNET");
  357. }
  358. /**
  359. * @param fm framework name. database url from properties file
  360. * @return
  361. * altered url (i.e. attributes stripped for DB2 and DerbyNet)
  362. */
  363. public static String alterURL(String fm, String url)
  364. {
  365. String urlPrefix = "jdbc:derby:";
  366. String newURLPrefix = getURLPrefix(fm);
  367. String newURLSuffix = getURLSuffix(fm);
  368. // If we don't have a URL prefix for this framework
  369. // just return
  370. if (newURLPrefix == null)
  371. return url;
  372. if (newURLSuffix == null)
  373. newURLSuffix = "";
  374. if (url.equals(urlPrefix)) // Replace embedded
  375. return newURLPrefix;
  376. // If this is a DB2 connection we need to strip
  377. // the connection attributes
  378. int attrOffset = url.indexOf(';');
  379. if (NetServer.isDB2Connection(fm) &&
  380. attrOffset != -1)
  381. url = url.substring(0,attrOffset);
  382. if (url.startsWith(urlPrefix))
  383. {
  384. // replace jdbc:derby: with our url:
  385. url = newURLPrefix +
  386. url.substring(urlPrefix.length()) +
  387. newURLSuffix;
  388. }
  389. else
  390. {
  391. if (! (url.startsWith("jdbc:")))
  392. {
  393. url = newURLPrefix + url + newURLSuffix;
  394. }
  395. }
  396. return url;
  397. }
  398. }