1 20 package org.apache.cactus.integration.ant.container.resin; 21 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.Field ; 24 import java.lang.reflect.Method ; 25 import java.util.ArrayList ; 26 27 import org.apache.cactus.integration.ant.container.AbstractServerRun; 28 29 35 public class ResinRun extends AbstractServerRun 36 { 37 42 private Object resinServer; 43 44 47 public ResinRun(String [] theArgs) 48 { 49 super(theArgs); 50 } 51 52 57 public static void main(String [] theArgs) 58 { 59 ResinRun resin = new ResinRun(theArgs); 60 61 resin.doRun(); 62 } 63 64 70 protected final Thread doStartServer(String [] theArgs) 71 { 72 Thread runningThread = this; 73 74 try 75 { 76 if (isResinVersion("2.0")) 77 { 78 startResin20x(theArgs); 79 } 80 else if (isResinVersion("2.1")) 81 { 82 startResin21x(theArgs); 83 } 84 else if (isResinVersion("3")) 85 { 86 runningThread = startResin3x(theArgs); 87 } 88 else 89 { 90 throw new RuntimeException ("Unsupported Resin version [" 91 + getResinVersion() + "]"); 92 } 93 } 94 catch (Exception e) 95 { 96 e.printStackTrace(); 97 throw new RuntimeException ("Failed to start Resin server"); 98 } 99 100 return runningThread; 101 } 102 103 109 private void startResin20x(String [] theArgs) throws Exception 110 { 111 Class resinClass = 112 Class.forName("com.caucho.server.http.ResinServer"); 113 Constructor constructor = resinClass.getConstructor( 114 new Class [] {theArgs.getClass(), boolean.class}); 115 116 this.resinServer = constructor.newInstance( 117 new Object [] {theArgs, Boolean.TRUE}); 118 119 Method initMethod = this.resinServer.getClass().getMethod("init", 120 new Class [] {boolean.class}); 121 122 initMethod.invoke(this.resinServer, new Object [] {Boolean.TRUE}); 123 } 124 125 131 private void startResin21x(String [] theArgs) throws Exception 132 { 133 Class resinClass = 134 Class.forName("com.caucho.server.http.ResinServer"); 135 Constructor constructor = resinClass.getConstructor( 136 new Class [] {theArgs.getClass(), boolean.class}); 137 138 this.resinServer = constructor.newInstance( 139 new Object [] {theArgs, Boolean.TRUE}); 140 141 Method initMethod = this.resinServer.getClass().getMethod("init", 142 new Class [] {ArrayList .class}); 143 144 initMethod.invoke(this.resinServer, new Object [] {null}); 145 } 146 147 154 private Thread startResin3x(final String [] theArgs) throws Exception 155 { 156 Thread startThread = new Thread () 161 { 162 public void run() 163 { 164 try 165 { 166 Class resinClass = 167 Class.forName("com.caucho.server.http.ResinServer"); 168 169 Method mainMethod = resinClass.getMethod("main", 170 new Class [] {String [].class}); 171 172 mainMethod.invoke(null, new Object [] {theArgs}); 173 } 174 catch (Exception e) 175 { 176 e.printStackTrace(); 177 throw new RuntimeException ( 178 "Failed to start Resin 3.x. Error = [" 179 + e.getMessage() + "]"); 180 } 181 } 182 }; 183 startThread.start(); 184 185 return startThread; 186 } 187 188 194 protected final void doStopServer(String [] theArgs, 195 Thread theRunningServerThread) 196 { 197 try 198 { 199 if (isResinVersion("2.0")) 200 { 201 stopResin20x(theArgs); 202 } 203 else if (isResinVersion("2.1")) 204 { 205 stopResin20x(theArgs); 206 } 207 else if (isResinVersion("3")) 208 { 209 stopResin3x(theArgs, theRunningServerThread); 210 } 211 else 212 { 213 throw new RuntimeException ("Unsupported Resin version [" 214 + getResinVersion() + "]"); 215 } 216 } 217 catch (Exception e) 218 { 219 e.printStackTrace(); 220 throw new RuntimeException ( 221 "Failed to stop the running Resin server"); 222 } 223 } 224 225 231 private void stopResin20x(String [] theArgs) throws Exception 232 { 233 Method closeMethod = this.resinServer.getClass().getMethod( 234 "close", null); 235 236 closeMethod.invoke(this.resinServer, null); 237 } 238 239 246 private void stopResin3x(String [] theArgs, 247 Thread theRunningServerThread) throws Exception 248 { 249 theRunningServerThread.stop(); 253 } 254 255 258 private String getResinVersion() 259 { 260 String version; 261 262 try 263 { 264 Class versionClass = Class.forName("com.caucho.Version"); 265 Field versionField = versionClass.getField("VERSION"); 266 version = (String ) versionField.get(null); 267 } 268 catch (Exception e) 269 { 270 throw new RuntimeException ("Cannot get Resin version. Error = [" 271 + e.getMessage() + "]"); 272 } 273 274 return version; 275 } 276 277 281 private boolean isResinVersion(String theVersionPrefix) 282 { 283 return getResinVersion().startsWith(theVersionPrefix); 284 } 285 } 286 | Popular Tags |