KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > RiTestServer


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: RiTestServer.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test;
46
47 import java.io.DataInputStream JavaDoc;
48 import java.io.File JavaDoc;
49 import java.net.URL JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 import javax.naming.Context JavaDoc;
53
54 /**
55  * The Client test suite needs the following environment variables
56  * to be set before it can be run.
57  *
58  * <code>test.home</code>
59  * <code>server.classpath</code>
60  *
61  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
62  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
63  */

64 public class RiTestServer implements TestServer {
65
66     protected Process JavaDoc server;
67     protected boolean startServerProcess;
68     protected String JavaDoc configFile;
69     protected String JavaDoc serverClassName = " org.openejb.ri.server.Server ";
70     protected String JavaDoc classPath;
71     protected DataInputStream JavaDoc in;
72     protected DataInputStream JavaDoc err;
73     protected String JavaDoc testHomePath;
74     protected File JavaDoc testHome;
75
76     /**
77      * The environment variable <code>test.home</code> sould be set
78      * to the base directory where the test suite is located.
79      */

80     public static final String JavaDoc TEST_HOME = "test.home";
81     public static final String JavaDoc SERVER_CLASSPATH = "server.classpath";
82     public static final String JavaDoc SERVER_CONFIG = "test.server.config";
83     public static final String JavaDoc START_SERVER_PROCESS = "test.start.server.process";
84     public static final String JavaDoc BAD_ENVIRONMENT_ERROR = "The following environment variables must be set before running the test suite:\n";
85     
86     
87     static{
88         System.setProperty("noBanner", "true");
89     }
90         
91     public RiTestServer(){}
92
93     public void init(Properties JavaDoc props){
94         try{
95             /* [DMB] Temporary fix */
96             try{
97                 System.setSecurityManager(new TestSecurityManager());
98             } catch (Exception JavaDoc e){
99                 e.printStackTrace();
100             }
101             /* [DMB] Temporary fix */
102             
103             String JavaDoc tmp = props.getProperty(START_SERVER_PROCESS, "true").trim();
104             startServerProcess = "true".equalsIgnoreCase(tmp);
105                         
106             /* If we will not be starting process for the
107              * server than we don't need to read in the other
108              * properties
109              */

110             if (!startServerProcess) return;
111
112             testHomePath = props.getProperty(TEST_HOME);
113             classPath = props.getProperty(SERVER_CLASSPATH);
114             configFile = props.getProperty(SERVER_CONFIG);
115
116             checkEnvironment();
117             
118             testHome = new File JavaDoc(testHomePath);
119             testHome = testHome.getAbsoluteFile();
120             testHomePath = testHome.getAbsolutePath();
121
122             prepareServerClasspath();
123         }catch (Exception JavaDoc e){
124             e.printStackTrace ();
125             System.exit(-1);
126         }
127     }
128
129     public void destroy(){
130
131     }
132
133     /**
134      * Starts and Ri Server with the configuration file from
135      * the properties used to create this RiTestServer.
136      *
137      * @param confFileName
138      */

139     public void start(){
140         
141         if (!startServerProcess) return;
142         
143         String JavaDoc command = "java -classpath "+classPath+" "+serverClassName +" "+configFile;
144         try{
145             server = Runtime.getRuntime().exec( command );
146             in = new DataInputStream JavaDoc( server.getInputStream());
147             err = new DataInputStream JavaDoc( server.getErrorStream());
148             while(true){
149                 try{
150                     String JavaDoc line = in.readLine();
151                         System.out.println(line);
152                     if (line == null || "[RI Server] Ready!".equals(line)) break;
153                     
154                 } catch (Exception JavaDoc e){ break; }
155             }
156
157             Thread JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc(){
158                 public void run(){
159                     while(true){
160                         try{
161                             String JavaDoc line = in.readLine();
162                             if ( line == null ) break;
163                                 System.out.println(line);
164                         } catch (Exception JavaDoc e){ break; }
165                     }
166                 
167                 }
168             });
169             t.start();
170             Thread JavaDoc t2 = new Thread JavaDoc(new Runnable JavaDoc(){
171                 public void run(){
172                     while(true){
173                         try{
174                             String JavaDoc line = err.readLine();
175                             if ( line == null ) break;
176 // System.out.println(line);
177
} catch (Exception JavaDoc e){ break; }
178                     }
179                 
180                 }
181             });
182             t2.start();
183         } catch (Exception JavaDoc e){
184             e.printStackTrace();
185         }
186     }
187
188     public void stop(){
189         if (!startServerProcess) return;
190         
191         if (server != null) server.destroy();
192         server = null;
193         try{
194             in.close();
195             err.close();
196         } catch (Exception JavaDoc e){}
197     }
198     
199     public Properties JavaDoc getContextEnvironment(){
200         Properties JavaDoc properties = new Properties JavaDoc();
201         properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.ri.server.RiInitCtxFactory");
202
203         try{
204         properties.put(Context.PROVIDER_URL, new URL JavaDoc("http","127.0.0.1",1098,""));
205         } catch (Exception JavaDoc e){}
206         
207         properties.put(Context.SECURITY_PRINCIPAL, "STATEFUL_TEST_CLIENT");
208         properties.put(Context.SECURITY_CREDENTIALS, "STATEFUL_TEST_CLIENT");
209
210         return properties;
211     }
212
213     //==========================================
214
// Methods supporting this implementation
215
// of the TestServer interface
216
//
217
private String JavaDoc getConfFilePath(String JavaDoc confFileName){
218         String JavaDoc str = getConfFile(confFileName).getAbsolutePath();
219         return str;
220     }
221
222     private File JavaDoc getConfFile(String JavaDoc confFileName){
223         return new File JavaDoc(testHome, confFileName);
224     }
225     
226     private void checkEnvironment(){
227         
228         if ( testHomePath == null || classPath == null || configFile == null ) {
229             String JavaDoc error = BAD_ENVIRONMENT_ERROR;
230             error += ( testHomePath == null )? TEST_HOME +"\n" : "";
231             error += ( classPath == null )? SERVER_CLASSPATH+"\n" : "";
232             error += ( configFile == null )? SERVER_CONFIG +"\n" : "";
233             throw new RuntimeException JavaDoc(error);
234         }
235     }
236
237     private void prepareServerClasspath(){
238         char PS = File.pathSeparatorChar;
239         char FS = File.separatorChar;
240
241         String JavaDoc javaTools = System.getProperty("java.home")+FS+"lib"+FS+"tools.jar";
242         classPath = classPath.replace('/', FS);
243         classPath = classPath.replace(':', PS);
244         classPath+= PS + javaTools;
245     }
246     //
247
// Methods supporting this implementation
248
// of the TestServer interface
249
//==========================================
250

251 }
252
Popular Tags