1 24 package org.objectweb.jonas.server; 25 26 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.net.URLClassLoader ; 33 import java.util.ArrayList ; 34 import java.util.Arrays ; 35 36 import javax.enterprise.deploy.shared.ModuleType ; 37 import javax.management.MBeanException ; 38 import javax.management.Notification ; 39 import javax.management.NotificationFilter ; 40 import javax.management.NotificationListener ; 41 42 import org.apache.commons.modeler.BaseModelMBean; 43 44 import org.objectweb.common.Cmd; 45 46 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc; 47 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException; 48 import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper; 49 50 import org.objectweb.jonas_ejb.genic.wrapper.GenicServiceWrapper; 51 52 import org.objectweb.jonas_lib.files.FileUtils; 53 import org.objectweb.jonas_lib.genclientstub.wrapper.ClientGenStubWrapper; 54 55 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException; 56 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper; 57 58 import org.objectweb.jonas_ws.wsgen.wrapper.WsGenWrapper; 59 60 import org.objectweb.jonas.common.JProp; 61 import org.objectweb.jonas.common.Log; 62 import org.objectweb.jonas.container.EJBService; 63 import org.objectweb.jonas.ear.EarService; 64 import org.objectweb.jonas.ear.EarServiceException; 65 import org.objectweb.jonas.resource.ResourceService; 66 import org.objectweb.jonas.service.ServiceException; 67 import org.objectweb.jonas.service.ServiceManager; 68 import org.objectweb.jonas.web.JWebContainerService; 69 70 import org.objectweb.util.monolog.api.BasicLevel; 71 import org.objectweb.util.monolog.api.Logger; 72 73 80 81 public class J2EEServerMBean extends BaseModelMBean { 82 83 86 private static Logger mgtLogger = Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX); 87 88 92 public J2EEServerMBean() throws MBeanException { 93 super(); 94 } 95 96 99 104 public String [] getDeployedObjects() { 105 ArrayList al = (ArrayList ) ((J2EEServer) (this.resource)).getDeployedObjects(); 106 return (String []) al.toArray(new String [al.size()]); 107 } 108 109 113 public String [] getResources() { 114 ArrayList al = (ArrayList ) ((J2EEServer) (this.resource)).getResources(); 115 return (String []) al.toArray(new String [al.size()]); 116 } 117 118 122 public String [] getJavaVMs() { 123 ArrayList al = (ArrayList ) ((J2EEServer) (this.resource)).getJavaVMs(); 124 return (String []) al.toArray(new String [al.size()]); 125 } 126 127 131 public void sendNotification(Notification pNotification) { 132 ((J2EEServer) (this.resource)).sendNotification(pNotification); 133 } 134 135 142 public void addNotificationListener(NotificationListener pListner, NotificationFilter pFilter, 143 java.lang.Object pHandback) throws java.lang.IllegalArgumentException { 144 ((J2EEServer) (this.resource)).addNotificationListener(pListner, pFilter, pHandback); 145 } 146 147 153 public String deployJar(String fileName) throws Exception { 154 return ((J2EEServer) (this.resource)).deployJar(fileName); 155 } 156 157 162 public void deployWar(String fileName) throws Exception { 163 ((J2EEServer) (this.resource)).deployWar(fileName); 164 } 165 166 173 public String deployEar(String fileName) throws Exception { 174 return ((J2EEServer) (this.resource)).deployEar(fileName); 175 } 176 177 184 public String deployRar(String fileName) throws Exception { 185 return ((J2EEServer) (this.resource)).deployRar(fileName); 186 } 187 188 194 public Boolean isEarDeployed(String fileName) throws Exception { 195 return ((J2EEServer) (this.resource)).isEarDeployed(fileName); 196 } 197 198 204 public Boolean isRarDeployed(String fileName) throws Exception { 205 return ((J2EEServer) (this.resource)).isRarDeployed(fileName); 206 } 207 208 214 public Boolean isJarDeployed(String fileName) throws Exception { 215 return ((J2EEServer) (this.resource)).isJarDeployed(fileName); 216 } 217 218 224 public Boolean isWarDeployed(String fileName) throws Exception { 225 return ((J2EEServer) (this.resource)).isWarDeployed(fileName); 226 } 227 228 233 public void deployLocalFile(String pathname, String [] genicArgs) { 234 try { 235 callGenic(genicArgs, pathname); 236 } catch (Exception e) { 237 mgtLogger.log(BasicLevel.WARN, "Cannot generate classes for this application : '" + pathname + ". " + e.getMessage() + "'."); 238 } 239 } 240 241 256 public String deployFile(Integer typeparam, java.lang.Byte [] bfile, String filename, String [] genicArgs, Boolean moveIntoDeployableDirectory) { 257 String directory = ""; 258 int type = typeparam.intValue(); 259 260 ServiceManager serviceManager = null; 261 try { 262 serviceManager = ServiceManager.getInstance(); 263 } catch (Exception e) { 264 mgtLogger.log(BasicLevel.ERROR, "Cannot get the instance of the Service Manager : '" + e.getMessage() 266 + "'."); 267 } 268 269 boolean isJar = false; 270 if (type == ModuleType.EJB.getValue()) { 271 directory = ((EJBService) serviceManager.getEjbService()).getEjbjarsDirectory(); 272 isJar = true; 273 } else if (type == ModuleType.EAR.getValue()) { 274 directory = ((EarService) serviceManager.getEarService()).getAppsDirectory(); 275 } else if (type == ModuleType.WAR.getValue()) { 276 directory = ((JWebContainerService) serviceManager.getWebContainerService()).getWebappsDirectory(); 277 } else if (type == ModuleType.RAR.getValue()) { 278 directory = ((ResourceService) serviceManager.getResourceService()).getRarsDirectory(); 279 } else if (type == ModuleType.CAR.getValue()) { 280 } 282 File file; 283 if (moveIntoDeployableDirectory.booleanValue()) { 284 file = new File (directory + filename); 285 } else { 286 file = new File (filename); 287 } 288 try { 289 FileOutputStream out = new FileOutputStream (file); 290 byte[] bfileused = new byte[bfile.length]; 291 for (int i = 0; i < bfile.length; i++) { 292 bfileused[i] = bfile[i].byteValue(); 293 } 294 out.write(bfileused); 295 out.close(); 296 if (type == ModuleType.EJB.getValue()) { 297 try { 298 callGenic(genicArgs, file.getAbsolutePath()); 299 } catch (ServiceException e) { 300 mgtLogger.log(BasicLevel.WARN, "deploy File error " + filename + e.getClass().getName() + " " 301 + e.getMessage()); 302 } 303 } else if (type == ModuleType.EAR.getValue()) { 304 unpackAndCompileEar(file, genicArgs); 305 } else if (type == ModuleType.WAR.getValue()) { 306 checkWebAppDeploymentDesc(file); 307 } else { 308 if (mgtLogger.isLoggable(BasicLevel.DEBUG)) { 309 mgtLogger.log(BasicLevel.DEBUG, "Deployment not yet implemented for type '" + type + "."); 310 } 311 } 312 } catch (IOException ioe) { 313 mgtLogger.log(BasicLevel.ERROR, "Cannot dump to outputstream the file '" + file + "' : '" 314 + ioe.getMessage() + "'.", ioe); 315 } 317 318 try { 320 Thread.sleep(1000); 321 } catch (InterruptedException e1) { 322 e1.printStackTrace(); 323 } 324 325 if (!filename.endsWith(".rar")) { 326 String modifiedArchive = null; 327 ClientGenStubWrapper clientWrapper = new ClientGenStubWrapper(); 328 try { 329 modifiedArchive = clientWrapper.callClientGenStubExecute(file.getPath()); 330 } catch (Exception e) { 331 mgtLogger.log(BasicLevel.ERROR, "Cannot launch ClientGenStub on the file '" + file + "' : '" 332 + e.getCause().getMessage() + "'.", e); 333 } 334 335 boolean modified = false; 336 try { 337 modified = clientWrapper.callClientGenStubIsInputModifed(); 338 } catch (Exception e) { 339 mgtLogger.log(BasicLevel.ERROR, "Cannot launch ClientGenStub.isInputModified '" + file + "' : '" 340 + e.getCause().getMessage() + "'.", e); 341 modified = true; 343 } 344 345 if (modified) { 346 File newFile = new File (modifiedArchive); 348 try { 349 FileUtils.copyFile(newFile, file); 350 } catch (Exception e) { 351 mgtLogger.log(BasicLevel.ERROR, "Error when copying file '" + newFile + "'. : " + e.getMessage()); 352 } 353 354 try { 356 Thread.sleep(1000); 357 } catch (InterruptedException e1) { 358 e1.printStackTrace(); 359 } 360 361 WsGenWrapper ww = new WsGenWrapper(); 362 try { 363 modifiedArchive = ww.callWsGenExecute(file.getPath()); 364 } catch (Exception e) { 365 mgtLogger.log(BasicLevel.ERROR, "Cannot launch WsGen on the file '" + file + "' : '" 366 + e.getCause().getMessage() + "'.", e); 367 } 368 369 try { 370 modified = ww.callWsGenIsInputModifed(); 371 } catch (Exception e) { 372 mgtLogger.log(BasicLevel.ERROR, "Cannot launch WsGen on the file '" + file + "' : '" 373 + e.getCause().getMessage() + "'.", e); 374 } 375 376 if (modified) { 377 File newFile2 = new File (modifiedArchive); 379 if (modifiedArchive.endsWith(".ear")) { 381 if (isJar) { 383 String modifiedDirectory = ((EarService) serviceManager.getEarService()).getAppsDirectory(); 386 file = new File (modifiedDirectory + newFile2.getName()); 387 } 388 } 389 try { 390 FileUtils.copyFile(newFile2, file); 391 } catch (Exception e) { 392 mgtLogger.log(BasicLevel.ERROR, "Error when copying file '" + newFile2 + "'. : " + e.getMessage()); 393 } 394 return file.getPath(); 395 } 396 397 return modifiedArchive; 398 } else { 399 return file.getPath(); 401 } 402 } else { 403 return file.getPath(); 404 } 405 } 406 407 408 413 private void callGenic(String [] genicArgs, String jarPath) { 414 String [] args; 415 if (genicArgs != null) { 416 args = new String [genicArgs.length + 1]; 417 for (int i = 0; i < genicArgs.length; i++) { 418 args[i] = genicArgs[i]; 419 } 420 args[genicArgs.length] = jarPath; 421 } else { 422 args = new String [1]; 423 args[0] = jarPath; 424 } 425 if (mgtLogger.isLoggable(BasicLevel.DEBUG)) { 426 mgtLogger.log(BasicLevel.DEBUG, "Calling GenIC with arguments :" + Arrays.asList(args)); 427 } 428 GenicServiceWrapper.callGenic(args); 429 } 430 431 437 private void unpackAndCompileEar(File file, String [] genicArgs) throws ServiceException { 438 URL [] earUrl = new URL [1]; 439 440 try { 441 earUrl[0] = file.toURL(); 442 } catch (MalformedURLException e) { 443 mgtLogger.log(BasicLevel.ERROR, "The url for the file '" + file + "' cannot be built : '" + e.getMessage() 445 + "'."); 446 } 447 448 ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); 451 URLClassLoader loaderCls = new URLClassLoader (earUrl, currentLoader); 452 453 EarDeploymentDesc desc = null; 454 try { 455 desc = EarManagerWrapper.getDeploymentDesc(earUrl[0].getFile(), loaderCls); 456 } catch (EarDeploymentDescException e) { 457 mgtLogger.log(BasicLevel.ERROR, "Cannot get deployment descriptor for the Ear file '" + file + "' : '" 459 + e.getMessage() + "'."); 460 } 461 462 String [] ejbTags = desc.getEjbTags(); 463 if (ejbTags.length != 0) { 464 470 String javaHomeBin = System.getProperty("java.home", ""); 471 if (!("".equals(javaHomeBin))) { 472 javaHomeBin = javaHomeBin + File.separator + ".." + File.separator + "bin" + File.separator; 473 } 474 475 Cmd cmd = null; 476 cmd = new Cmd(javaHomeBin + "jar"); 477 cmd.addArgument("-xf"); 478 cmd.addArgument(file.getAbsolutePath()); 479 480 for (int i = 0; i < ejbTags.length; i++) { 481 cmd.addArgument(ejbTags[i]); 482 } 483 484 boolean exitCmd = cmd.run(); 485 if (!exitCmd) { 487 throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '" 488 + file + "'."); 489 } 490 491 for (int i = 0; i < ejbTags.length; i++) { 492 try { 493 callGenic(genicArgs, ejbTags[i]); 494 } catch (ServiceException e) { 495 throw (e); 496 } 497 } 498 cmd = new Cmd(javaHomeBin + "jar"); 500 cmd.addArgument("-uf"); 501 cmd.addArgument(file.getAbsolutePath()); 502 503 for (int i = 0; i < ejbTags.length; i++) { 504 cmd.addArgument(ejbTags[i]); 505 } 506 exitCmd = cmd.run(); 507 if (!exitCmd) { 509 throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '" 510 + file + "'."); 511 } 512 513 for (int i = 0; i < ejbTags.length; i++) { 514 File del = new File (ejbTags[i]); 515 del.delete(); 516 } 517 } 518 desc = null; 519 } 520 521 525 private void checkWebAppDeploymentDesc(File file) { 526 URL warUrl = null; 527 528 try { 529 warUrl = file.toURL(); 530 } catch (MalformedURLException mue) { 531 mgtLogger.log(BasicLevel.ERROR, "The url for the file '" + file + "' cannot be built : '" 532 + mue.getMessage() + "'."); 533 throw new RuntimeException (mue); 534 } 535 536 URLClassLoader warCl = new URLClassLoader (new URL [] {warUrl}); 537 538 try { 539 WebManagerWrapper.getDeploymentDesc(warUrl, warCl, null); 540 } catch (WebContainerDeploymentDescException e) { 541 String err = "Cannot read the deployment descriptors '" + warUrl.getFile() + "'"; 542 mgtLogger.log(BasicLevel.ERROR, err + ": " + e); 543 e.printStackTrace(System.err); 544 throw new RuntimeException (err, e); 545 } 546 547 } 548 549 553 570 571 585 586 596 public String sendFile(byte[] fileContent, String fileName, boolean replaceExisting) throws Exception { 597 598 File directoryUploadedFile = null; 599 FileOutputStream fos = null; 600 try { 601 String dir = getFolderDir(fileName); 603 604 directoryUploadedFile = new File (dir, fileName); 606 if (directoryUploadedFile.exists() && !replaceExisting) { 608 throw new Exception ("File '" + directoryUploadedFile + "' already exists on the server."); 609 } 610 611 fos = new FileOutputStream (directoryUploadedFile); 613 fos.write(fileContent); 614 } finally { 615 if (fos != null) { 616 try { 617 fos.close(); 619 } catch (IOException ioe) { 620 mgtLogger.log(BasicLevel.DEBUG, "Cannot close the output stream", ioe); 621 } 622 } 623 624 } 625 if (directoryUploadedFile != null) { 626 return directoryUploadedFile.getPath(); 627 } else { 628 return "error, no uploaded file"; 629 } 630 } 631 632 638 public Boolean removeModuleFile(String fileName) throws Exception { 639 String dir = getFolderDir(fileName); 641 642 File searchedFile = new File (dir, fileName); 643 if (searchedFile.exists()) { 644 return Boolean.valueOf(searchedFile.delete()); 645 } else { 646 throw new Exception ("File '" + searchedFile + "' was not found on the JOnAS server. Cannot remove it"); 647 } 648 649 } 650 651 652 658 private String getFolderDir(String fileName) throws Exception { 659 String jBase = JProp.getJonasBase(); 660 String dir = null; 662 if (fileName.toLowerCase().endsWith(".jar")) { 664 dir = jBase + File.separator + "ejbjars"; 665 } else if (fileName.toLowerCase().endsWith(".war")) { 666 dir = jBase + File.separator + "webapps"; 668 } else if (fileName.toLowerCase().endsWith(".ear")) { 669 dir = jBase + File.separator + "apps"; 671 } else if (fileName.toLowerCase().endsWith(".rar")) { 672 dir = jBase + File.separator + "rars"; 674 } else { 675 throw new Exception ("Invalid extension for the file '" + fileName 677 + "'. Valid are .jar, .war, .ear, .rar"); 678 } 679 return dir; 680 } 681 682 } | Popular Tags |