KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > NetServer


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.NetServer
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.derbyTesting.functionTests.harness;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Vector JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.Constructor JavaDoc;
33 import java.net.ConnectException JavaDoc;
34 import java.net.Socket JavaDoc;
35 import org.apache.derbyTesting.functionTests.util.TestUtil;
36
37 public class NetServer
38 {
39
40     File JavaDoc homeDir; // The server directory (usually the test directory)
41
String JavaDoc jvmName = "jdk13";
42     String JavaDoc clPath;
43     String JavaDoc javaCmd;
44     String JavaDoc jvmflags;
45     String JavaDoc framework;
46     static String JavaDoc hostName;
47     
48     Object JavaDoc[] frameworkInfo;
49     int port;
50     Process JavaDoc pr;
51     BackgroundStreamSaver outSaver, errSaver;
52     FileOutputStream JavaDoc fosOut, fosErr;
53     private String JavaDoc java;
54     private boolean startServer; // whether test will start it's own server
55

56     // Variables for test connection
57
Object JavaDoc networkServer; // Server needs to be created with reflection
58
Method JavaDoc pingMethod;
59
60     private static String JavaDoc NETWORK_SERVER_CLASS_NAME="org.apache.derby.drda.NetworkServerControl";
61     
62     public static Hashtable JavaDoc 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 JavaDoc();
75     // Hashtable is keyed on framework name and has
76
// an array of the framework prefix, suffix, driver, port and
77
// String[] command arguments to start the server
78
// String[] Command arguments to stop the server
79
String JavaDoc url = "jdbc:derby:net://" + hostName + ":1527/";
80     m.put("DerbyNet", new Object JavaDoc[]
81         {url, //prefix
82
"", // suffix
83
"com.ibm.db2.jcc.DB2Driver", //driver
84
"1527", // port
85
new String JavaDoc[] {NETWORK_SERVER_CLASS_NAME, //start
86
"start"},
87          new String JavaDoc[] {NETWORK_SERVER_CLASS_NAME, //shutdown
88
"shutdown"},
89          null}); //shutdown2
90

91     url = "jdbc:derby://" + hostName + ":1527/";
92     m.put("DerbyNetClient", new Object JavaDoc[]
93         {url, //prefix
94
"", // suffix
95
"org.apache.derby.jdbc.ClientDriver", //driver
96
"1527", // port
97
new String JavaDoc[] {NETWORK_SERVER_CLASS_NAME, //start
98
"start"},
99          new String JavaDoc[] {NETWORK_SERVER_CLASS_NAME, //shutdown
100
"shutdown"},
101          null}); //shutdown2
102

103     url = "jdbc:db2://" + hostName + ":50000/";
104     m.put("DB2jcc", new Object JavaDoc[]
105         {url, //prefix
106
"", //suffix
107
"com.ibm.db2.jcc.DB2Driver", //driver
108
"50000", //port
109
null, //start
110
null,
111          null});
112
113     m.put("DB2app", new Object JavaDoc[]
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 JavaDoc homeDir, String JavaDoc jvmName, String JavaDoc clPath, String JavaDoc
124          javaCmd, String JavaDoc jvmflags, String JavaDoc framework, boolean startServer)
125     throws Exception JavaDoc
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 JavaDoc[]) m.get(framework);
134     
135     this.port = Integer.parseInt((String JavaDoc) frameworkInfo[PORT_POS]);
136     this.startServer = startServer;
137     // System.out.println("framework: " + this.framework + "port: " + this.port);
138

139     }
140     public void start() throws Exception JavaDoc
141     {
142       if (! startServer)
143       {
144         System.out.println("startServer = false. Bypass server startup");
145         return;
146       }
147
148     // Create the Server directory under the server dir
149
(new File JavaDoc(homeDir, framework + "Server")).mkdir();
150     String JavaDoc[] startcmd = (String JavaDoc[]) frameworkInfo[START_CMD_POS];
151     // if we are just connecting to DB2 we return
152
if (startcmd == null)
153         return;
154     
155         // Build the command to run the WL server
156
String JavaDoc homeDirName = homeDir.getCanonicalPath();
157         jvm jvm = null; // to quiet the compiler
158
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 JavaDoc jvmProps = new Vector JavaDoc();
165         if ( (clPath != null) && (clPath.length()>0) )
166             jvm.setClasspath(clPath);
167
168         if ( (jvmflags != null) && (jvmflags.length()>0) ) {
169             jvm.setFlags(jvmflags);
170             // Set no flags by default (DERBY-1614).
171
// The jvmflags property can be used to set any kind of JVM option.
172
}
173
174         jvmProps.addElement("derby.system.home=" + homeDirName);
175         jvm.setD(jvmProps);
176         jvm.setSecurityProps();
177         // For some platforms (like Mac) the process exec command
178
// must be a string array; so we build this with a Vector
179
// first because some strings (paths) could have spaces
180
Vector JavaDoc vCmd = jvm.getCommandLine();
181     for (int i = 0; i < startcmd.length; i++)
182         vCmd.addElement(startcmd[i]);
183
184     String JavaDoc serverCmd[] = new String JavaDoc[vCmd.size()];
185     for (int i = 0; i < vCmd.size(); i++)
186     {
187         serverCmd[i] = (String JavaDoc)vCmd.elementAt(i);
188         System.out.print(serverCmd[i] + " ");
189     }
190     System.out.println("");
191         // Start a process to run the Server
192
pr = Runtime.getRuntime().exec(serverCmd);
193     
194         // Write the out and err files to the server directory also
195
File JavaDoc out = new File JavaDoc(homeDir, framework + ".out");
196     fosOut = new FileOutputStream JavaDoc(out);
197     outSaver = new BackgroundStreamSaver(pr.getInputStream(), fosOut);
198     File JavaDoc err = new File JavaDoc(homeDir, framework + ".err");
199     fosErr = new FileOutputStream JavaDoc(err);
200     errSaver = new BackgroundStreamSaver(pr.getErrorStream(), fosErr);
201     
202     for (int i = 0 ; i <= 120 ; i++)
203     {
204         // No need to wait for DB2
205
if (isDB2Connection(framework))
206         break;
207      
208         try
209         {
210             if (isNetworkServerConnection(framework))
211             {
212                 // adding a testconnection check
213
// so that the test does not start before the server is up
214
if (testNetworkServerConnection())
215                     break;
216             }
217             else
218             {
219                 Socket JavaDoc s = new Socket JavaDoc(hostName, this.port);
220                 s.close();
221                 break;
222             }
223
224         }
225         catch (Exception JavaDoc e)
226         {
227         // bail out if something has been written to stderr
228
if (err.length() > 0) {
229             break;
230         } else {
231                 // it's probably unnecessary to sleep, since the
232
// connection request generally takes a long time when
233
// the listener hasn't started yet, but what the heck ...
234
Thread.sleep(1000);
235                 // but here we iterate, and after 120 seconds, we stop
236
// waiting to connect.
237
}
238         
239         }
240     }
241     }
242     
243     public boolean testNetworkServerConnection() throws Exception JavaDoc
244     {
245         if (! startServer)
246         {
247             System.out.println("startServer = false. Bypass server check");
248             return true;
249         }
250         
251         Object JavaDoc[] testConnectionArg = null;
252         if (networkServer == null)
253         {
254             Constructor JavaDoc serverConstructor;
255             Class JavaDoc 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     // stop the Server
266
public void stop() throws Exception JavaDoc
267     {
268       if (! startServer)
269       {
270         return;
271       }
272
273     System.out.println("Attempt to shutdown framework: "
274                          + framework);
275     jvm jvm = null; // to quiet the compiler
276
jvm = jvm.getJvm(jvmName);
277     Vector JavaDoc jvmCmd = jvm.getCommandLine();
278     
279     Vector JavaDoc connV = new Vector JavaDoc();
280     for (int i = 0; i < jvmCmd.size(); i++)
281     {
282         connV.addElement((String JavaDoc)jvmCmd.elementAt(i));
283         }
284     
285     String JavaDoc[] stopcmd1 = (String JavaDoc[]) 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 JavaDoc[] connCmd = new String JavaDoc[connV.size()];
293         for (int i = 0; i < connV.size(); i++)
294         {
295             connCmd[i] = (String JavaDoc)connV.elementAt(i);
296         }
297         
298         
299         Vector JavaDoc stopV = new Vector JavaDoc();
300         for (int i = 0; i < jvmCmd.size(); i++)
301         {
302             stopV.addElement((String JavaDoc)jvmCmd.elementAt(i));
303         }
304         Process JavaDoc prconn = Runtime.getRuntime().exec(connCmd);
305         // Give the server sixty seconds to shutdown.
306
TimedProcess tp = new TimedProcess(prconn);
307         tp.waitFor(60);
308         
309         String JavaDoc[] stopcmd2 = (String JavaDoc[]) 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 JavaDoc[] stopCmd = new String JavaDoc[stopV.size()];
316             for (int i = 0; i < stopV.size(); i++)
317             {
318             stopCmd[i] = (String JavaDoc)stopV.elementAt(i);
319             }
320             
321             Process JavaDoc prstop = Runtime.getRuntime().exec(stopCmd);
322             prstop.waitFor();
323         }
324
325         // Try a TimedProcess as Phil did for the WLServer
326
tp = new TimedProcess(pr);
327         // In case the Server didn't shut down, force it to ...
328
tp.waitFor(60);
329         
330         // Finish and close the redirected out and err files
331
outSaver.finish();
332         errSaver.finish();
333     }
334     
335     public void printFramworkInfo(String JavaDoc 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 JavaDoc cmdString = "";
345         String JavaDoc[] cmdArray = (String JavaDoc[]) 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     // Get Framework Info
359
public static String JavaDoc getURLPrefix(String JavaDoc fm)
360         {
361             Object JavaDoc[] info = (Object JavaDoc[]) m.get(fm);
362             return (String JavaDoc) info[PREFIX_POS];
363         }
364     
365     public static String JavaDoc getURLSuffix(String JavaDoc fm)
366     {
367     Object JavaDoc[] info = (Object JavaDoc[]) m.get(fm);
368     return (String JavaDoc) info[SUFFIX_POS];
369     }
370     
371     public static String JavaDoc getDriverName(String JavaDoc fm)
372     {
373     Object JavaDoc[] info = (Object JavaDoc[]) m.get(fm);
374     if (info != null)
375         return (String JavaDoc) info[DRIVER_POS];
376     else
377         return null;
378     }
379     
380     public static boolean isDB2Connection(String JavaDoc 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 JavaDoc fm)
388     {
389         return (fm.toUpperCase(Locale.ENGLISH).startsWith("DERBYNET"));
390     }
391
392     public static boolean isClientConnection(String JavaDoc 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 JavaDoc fm)
399     {
400         return fm.toUpperCase(Locale.ENGLISH).equals("DB2JCC") ||
401             fm.toUpperCase(Locale.ENGLISH).equals("DERBYNET");
402     }
403
404     /**
405      * @param fm framework name. database url from properties file
406      * @return
407      * altered url (i.e. attributes stripped for DB2 and DerbyNet)
408      */

409
410     public static String JavaDoc alterURL(String JavaDoc fm, String JavaDoc url)
411     {
412     String JavaDoc urlPrefix = "jdbc:derby:";
413     String JavaDoc newURLPrefix = getURLPrefix(fm);
414     String JavaDoc newURLSuffix = getURLSuffix(fm);
415     
416     // If we don't have a URL prefix for this framework
417
// just return
418
if (newURLPrefix == null)
419         return url;
420
421     if (newURLSuffix == null)
422         newURLSuffix = "";
423     
424     if (url.equals(urlPrefix)) // Replace embedded
425
return newURLPrefix;
426
427     // If this is a DB2 connection we need to strip
428
// the connection attributes
429
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         // replace jdbc:derby: with our url:
438
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