1 23 package com.sun.enterprise.tools.deployment.main; 24 25 import java.io.*; 26 import java.net.*; 27 import com.sun.enterprise.tools.deployment.backend.JarInstaller; 28 import com.sun.enterprise.tools.deployment.backend.DeploymentSessionImpl; 29 import com.sun.enterprise.tools.deployment.backend.DeploymentSession; 30 import java.rmi.RemoteException ; 32 import java.util.*; 33 import java.util.zip.*; 34 import java.util.jar.*; 35 import com.sun.ejb.sqlgen.DBInfo; 36 import com.sun.ejb.sqlgen.SQLGenerator; 37 import com.sun.enterprise.tools.packager.ComponentPackager; 38 import com.sun.enterprise.util.LocalStringManagerImpl; 39 import com.sun.enterprise.util.NotificationEvent; 40 import com.sun.enterprise.util.FileUtil; 41 42 import com.sun.enterprise.deployment.xml.*; 43 import com.sun.enterprise.deployment.*; 44 45 48 49 public class DeployTool { 50 private ApplicationManager applicationManager; 51 private StandAloneManager standaloneManager; 52 private ServerManager serverManager; 53 private ComponentPackager componentPackager; 54 private File workingDirectory; 55 private File toolHomeDirectory; 56 private static LocalStringManagerImpl localStrings = 57 new LocalStringManagerImpl(DeployTool.class); 58 public static final String HOME_DIR = ".assemblytool"; 60 private static DeployTool deployToolInstance = null; 61 62 63 public static final String UI_STARTUP_CLASS = "com.sun.enterprise.tools.deployment.ui.DT"; protected UIFactory uiFactory = null; 65 66 private String userdir = null; 67 70 71 public DeployTool(boolean hasUI, String dir){ 73 userdir = dir; 74 _deploytool(hasUI); 75 } 76 77 78 public DeployTool(boolean hasUI) { 79 _deploytool(hasUI); 80 } 81 82 private void _deploytool(boolean hasUI){ 83 84 deployToolInstance = this; 85 86 93 this.uiFactory = createUIFactory(hasUI); 94 95 if (this.uiFactory != null){ 96 97 File cfgDir = null; 101 if (userdir == null){ 102 cfgDir = this.uiFactory.getConfigDirectory(); 103 }else{ 104 cfgDir = new File(userdir); 105 cfgDir.mkdirs(); 106 } 107 File tmpDir = this.uiFactory.getTempDirectory(); 108 System.out.println("user directory = " + cfgDir); System.out.println("temp directory = " + tmpDir); this.applicationManager = new ApplicationManager(cfgDir, tmpDir); 111 this.standaloneManager = new StandAloneManager(cfgDir, tmpDir); 112 this.serverManager = new ServerManager(cfgDir); 113 this.uiFactory.startUI(); 114 115 } else { 116 117 118 File toolDir = this.getToolHomeDirectory(); 119 File tempDir = this.getWorkingDirectory(); 120 System.out.println("user directory = " + toolDir); System.out.println("temp directory = " + tempDir); this.applicationManager = new ApplicationManager(toolDir, tempDir); 123 this.standaloneManager = new StandAloneManager(toolDir, tempDir); 124 this.serverManager = new ServerManager(toolDir); 125 126 } 127 128 } 129 130 public static DeployTool getDeployToolInstance() { 131 return deployToolInstance; 134 } 135 136 137 protected static UIFactory createUIFactory(boolean hasUI) { 138 139 if (hasUI) { 140 141 try { 142 143 144 Class uiFactoryClass = Class.forName(UI_STARTUP_CLASS); 145 if (!UIFactory.class.isAssignableFrom(uiFactoryClass)) { 146 throw new ClassCastException ("Class does not implement UIFactory"); } 148 149 150 return (UIFactory)uiFactoryClass.newInstance(); 151 152 } catch (Throwable t) { 153 154 System.out.println(localStrings.getLocalString( 155 "enterprise.tools.deployment.main.error_creating_ui", 156 "Unable to create UI: " + t)); 157 158 } 159 160 } 161 162 return null; 163 164 } 165 166 169 public Application deployStandaloneModule(File moduleFile) 170 throws Exception { 171 172 File tmpDir = FileUtil.getTempDirectory(); 174 String appName = "app." + moduleFile.getName().replace('.', '_'); File appFile = new File(tmpDir, appName + ".ear"); int i = 0; 177 while (appFile.exists()) { 178 i++; 179 appFile = new File(tmpDir, appName + "_" + i + ".ear"); } 181 if (i > 0) { 183 appName = appName + "_" + i; } 185 appFile.deleteOnExit(); 186 Application app = new Application(appName, appFile); 187 if (EjbBundleArchivist.isEjbBundle(moduleFile)) { 188 app.addEjbJarFile(moduleFile); 189 } else if (WebBundleArchivist.isWebBundle(moduleFile)) { 190 app.addWebJarFile(moduleFile); 191 } else if (ApplicationClientArchivist.isApplicationClientJar(moduleFile)) { 192 app.addAppClientJarFile(moduleFile); 193 } else { 194 throw new IllegalArgumentException ("Unsupported module type: " + moduleFile); 196 } 197 return app; 198 } 199 200 public void deploy(Application application, String serverName, DeploymentSession deploymentSession, File clientCodeFile) 202 throws Exception 203 { 204 deploy(application.getName(), application.getApplicationArchivist().getApplicationFile(), serverName, deploymentSession, clientCodeFile); 205 } 206 207 public void deploy(String applicationName, File appArchiveFile, 208 String serverName, DeploymentSession deploymentSession, File clientCodeFile) 209 throws Exception { 210 211 System.out.println(localStrings.getLocalString( 212 "enterprise.tools.deployment.main.deployapplicationfileonserversaveasclientjar", 213 "Deploy the application in {0} on the server {1} saving the client jar as {2}", 214 new Object [] { appArchiveFile, serverName, clientCodeFile })); 215 Object [] msg = { applicationName, serverName }; 216 String clientCode = null; 218 Log.print(this,localStrings.getLocalString( 219 "enterprise.tools.deployment.main.deploytool.deploy_command", 220 "Deploy {0} on {1}", msg)); 221 JarInstaller backend = this.getServerManager().getServerForName(serverName); 222 DeploymentSession deploymentSessionToUse = null; 223 if (deploymentSession == null) { 224 deploymentSessionToUse = this.getServerManager().createDeploymentSession(serverName); 225 } else { 226 deploymentSessionToUse = deploymentSession; 227 } 228 229 FileInputStream fis = new FileInputStream(appArchiveFile); 230 DataInputStream dis = new DataInputStream(fis); 231 byte[] jarData = new byte[(int) appArchiveFile.length()]; 232 dis.readFully(jarData); 233 dis.close(); 234 fis.close(); 235 clientCode = backend.deployApplication(jarData, applicationName, deploymentSessionToUse); 236 Log.print(this, localStrings.getLocalString( 237 "enterprise.tools.deployment.main.clientcodeat", 238 "client code at {0}", new Object [] {clientCode})); 239 if (clientCode != null && clientCodeFile != null) { 240 writeClientJarToFile(clientCode, clientCodeFile); 241 deploymentSessionToUse.notification(new NotificationEvent(this, DeploymentSession.CLIENT_CODE_RETURNED, this)); 242 deploymentSessionToUse.setStatusMessage(localStrings.getLocalString( 243 "enterprise.tools.deployment.main.clientcodefordeployedapplicationsavedtofile", 244 "Client code for the deployed application {0} saved to {1}", new Object [] {applicationName, clientCodeFile})); 245 } 246 } 247 248 private void writeClientJarToFile(String clientCode, 249 File clientCodeFile ) throws IOException { 250 URL u = new URL(clientCode); 251 HttpURLConnection http = (HttpURLConnection) u.openConnection(); 252 int code = http.getResponseCode(); 253 if(code != 200) { 254 System.out.println(localStrings.getLocalString( 255 "enterprise.tools.deployment.main.cannotdownloadURL", 256 "Cannot download URL {0}", new Object [] {clientCode})); 257 System.out.println(localStrings.getLocalString( 258 "enterprise.tools.deployment.main.status", 259 "Status: {0}", new Object [] {new Integer (code)})); 260 throw new IOException(localStrings.getLocalString( 261 "enterprise.tools.deployment.main.cannotdownloadURL", 262 "Cannot download URL {0}", new Object [] {clientCode})); 263 } 264 BufferedInputStream is = new BufferedInputStream(http.getInputStream()); 265 FileOutputStream fos = new FileOutputStream(clientCodeFile); 266 int len = 0; 267 int contentLength = http.getContentLength(); 268 byte[] buf = new byte[contentLength+1]; 270 while((len = is.read(buf)) != -1) 271 fos.write(buf, 0, len); 272 } 273 274 private void saveAsBytes(byte[] data, File file) throws IOException { 275 if (data == null) { 276 throw new IOException(localStrings.getLocalString( 277 "enterprise.tools.deployment.main.nulldataforclientcodefile", 278 "null data for client code file")); 279 } 280 FileOutputStream fileStream = new FileOutputStream(file); 281 fileStream.write(data, 0, data.length); 282 fileStream.close(); 283 } 284 285 288 public void setRuntimeDeploymentInfo(Application application, File runtimeDeploymentInfo) throws Exception { 289 FileInputStream fis = new FileInputStream(runtimeDeploymentInfo); 291 RuntimeDescriptorNode node = (RuntimeDescriptorNode) RuntimeDescriptorNode.readRuntimeDescriptorNodes(fis).elementAt(0); 292 node.updateRuntimeInformation(application); 293 this.getApplicationManager().saveApplication(application); 294 295 Object [] msg = {application.getName()}; 296 Log.print(this, localStrings.getLocalString( "enterprise.tools.deployment.main.deploytool.setruntime_command", "Done setting runtime deployment information on {0} to: {1}", msg)); 297 } 298 299 303 public void doGenerateSQL(String applicationFilename, String serverName, boolean overWrite) throws Exception 304 { 305 DBInfo dbInfo = this.getServerManager().getDBInfo(serverName); 306 Application application = ApplicationArchivist.openAT( new File(applicationFilename)); 308 309 Iterator itr = application.getEjbBundleDescriptors().iterator(); 310 while ( itr.hasNext() ) { 311 EjbBundleDescriptor ebd = (EjbBundleDescriptor)itr.next(); 312 313 SQLGenerator.generateSQL(ebd, ebd.getCMPResourceReference(), 314 overWrite, dbInfo); 315 } 316 317 application.getApplicationArchivist().save(application.getApplicationArchivist().getApplicationFile(), true); 318 System.out.println(localStrings.getLocalString( 319 "enterprise.tools.deployment.main.donegeneratingSQL", 320 "Done generating SQL")); 321 } 322 323 324 public ComponentPackager getComponentPackager() { 325 if (this.componentPackager == null) { 326 this.componentPackager = new ComponentPackager(); 327 } 328 return this.componentPackager; 329 } 330 331 332 public ApplicationManager getApplicationManager() { 333 return applicationManager; 334 } 335 336 337 public StandAloneManager getStandAloneManager() { 338 return standaloneManager; 339 } 340 341 342 public ServerManager getServerManager() { 343 return serverManager; 344 } 345 346 349 350 public File getWorkingDirectory() { 351 358 String home = System.getProperty("user.home"); 359 if (home == null) { 360 home = ""; } 362 File tmp = new File(home, "tmp"); tmp.mkdirs(); 364 return tmp; 365 } 366 367 369 public File getToolHomeDirectory() { 370 371 372 373 376 382 File homedir; 383 if (userdir != null){ 384 homedir = new File(userdir); 385 }else{ 386 String home = System.getProperty("user.home"); 387 if (home == null) { 388 home = ""; } 390 homedir= new File(home, HOME_DIR); 391 } 392 homedir.mkdirs(); 393 return homedir; 394 } 396 397 398 public String toString() { 399 return "Deploy Tool"; } 401 402 404 static public void main(String [] args) { 405 try { 406 DeployTool tool = new DeployTool(false); 407 Application app = tool.deployStandaloneModule 408 (new File("/home/tcng/Test/ejb.jar")); System.err.println(app.toString()); 410 app = tool.deployStandaloneModule 411 (new File("/home/tcng/Test/app.jar")); System.err.println(app.toString()); 413 app = tool.deployStandaloneModule 414 (new File("/home/tcng/Test/web.war")); System.err.println(app.toString()); 416 417 } catch (Exception ex) { 418 ex.printStackTrace(); 419 } 420 } 421 422 } 423 | Popular Tags |