1 25 package org.objectweb.jonas.web.wrapper.catalina50; 26 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 import java.rmi.RemoteException ; 32 import java.util.Hashtable ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 import javax.naming.Context ; 38 39 import org.objectweb.jonas.server.LoaderManager; 40 import org.objectweb.jonas.service.ServiceException; 41 import org.objectweb.jonas.web.AbsJWebContainerServiceImplMBean; 42 import org.objectweb.jonas.web.JWebContainerServiceException; 43 import org.objectweb.jonas.web.War; 44 import org.objectweb.jonas.web.wrapper.CatalinaJWebContainerService; 45 46 47 52 public class CatalinaJWebContainerServiceWrapper implements CatalinaJWebContainerService, AbsJWebContainerServiceImplMBean { 53 54 57 private ClassLoader catalinaLoader = null; 58 59 62 private Object catalinaService = null; 63 64 67 private Map methods = null; 68 69 72 private static final String CATALINA_SERVICE_CLASSNAME = "org.objectweb.jonas.web.catalina50.CatalinaJWebContainerServiceImpl"; 73 74 78 public CatalinaJWebContainerServiceWrapper() throws ServiceException { 79 LoaderManager lm = LoaderManager.getInstance(); 80 try { 81 catalinaLoader = lm.getCatalinaLoader(); 82 } catch (Exception e) { 83 throw new ServiceException("Cannot get Catalina ClassLoader", e); 85 } 86 methods = new Hashtable (); 87 88 if (catalinaService == null) { 89 catalinaService = getCatalinaServiceInstance(); 90 } 91 92 } 93 94 111 public void deployWars(Context ctx) throws JWebContainerServiceException { 112 113 if (catalinaService == null) { 114 catalinaService = getCatalinaServiceInstance(); 115 } 116 117 Method m = (Method ) methods.get("deployWars"); 119 if (m == null) { 120 m = getMethod("deployWars", new Class [] {Context .class}); 121 } 122 123 invoke(m, new Object [] {ctx}); 124 } 125 126 131 public War getWar(URL url) { 132 133 if (catalinaService == null) { 134 catalinaService = getCatalinaServiceInstance(); 135 } 136 137 Method m = (Method ) methods.get("getWar"); 139 if (m == null) { 140 m = getMethod("getWar", new Class [] {URL .class}); 141 } 142 143 return (War) invoke(m, new Object [] {url}); 144 145 } 146 152 private Object invoke(Method m, Object [] params) { 153 ClassLoader old = null; 154 try { 155 old = Thread.currentThread().getContextClassLoader(); 156 Thread.currentThread().setContextClassLoader(catalinaLoader); 157 return m.invoke(catalinaService, params); 158 } catch (InvocationTargetException e) { 159 if (e.getTargetException() instanceof Error ) { 160 throw (Error ) e.getTargetException(); 161 } else if (e.getTargetException() instanceof ServiceException) { 162 throw (ServiceException) e.getTargetException(); 163 } else { 164 throw new ServiceException("Problems when invoking " + m.getName(), e.getTargetException()); 166 } 167 } catch (Exception e) { 168 throw new ServiceException("Problems when invoking " + m.getName(), e); 170 } finally { 171 if (old != null) { 172 Thread.currentThread().setContextClassLoader(old); 173 } 174 } 175 } 176 177 184 private Method getMethod(String methodName, Class [] paramTypes) throws ServiceException { 185 try { 186 Method m = catalinaService.getClass().getMethod(methodName, paramTypes); 187 methods.put(methodName, m); 188 return m; 189 } catch (Exception e) { 190 throw new ServiceException("Problems when retrieving " + methodName + " method", e); 191 } 192 } 193 194 198 private Object getCatalinaServiceInstance() throws ServiceException { 199 try { 200 Class service = catalinaLoader.loadClass(CATALINA_SERVICE_CLASSNAME); 201 return service.newInstance(); 202 } catch (Exception e) { 203 throw new JWebContainerServiceException("Problems when loading " + CATALINA_SERVICE_CLASSNAME, e); 204 } 205 } 206 207 214 public void unDeployWars(URL [] urls) { 215 216 if (catalinaService == null) { 217 catalinaService = getCatalinaServiceInstance(); 218 } 219 Method m = (Method ) methods.get("unDeployWars"); 221 if (m == null) { 222 m = getMethod("unDeployWars", new Class [] {URL [].class}); 223 } 224 225 invoke(m, new Object [] {urls}); 226 227 } 228 229 235 public void removeCache(ClassLoader earClassLoader) { 236 237 if (catalinaService == null) { 238 catalinaService = getCatalinaServiceInstance(); 239 } 240 Method m = (Method ) methods.get("removeCache"); 242 if (m == null) { 243 m = getMethod("removeCache", new Class [] {ClassLoader .class}); 244 } 245 246 invoke(m, new Object [] {earClassLoader}); 247 248 } 249 250 257 public void registerWarMBean(String fileName) throws RemoteException , JWebContainerServiceException { 258 259 if (catalinaService == null) { 260 catalinaService = getCatalinaServiceInstance(); 261 } 262 Method m = (Method ) methods.get("registerWarMBean"); 264 if (m == null) { 265 m = getMethod("registerWarMBean", new Class [] {String .class}); 266 } 267 268 invoke(m, new Object [] {fileName}); 269 270 } 271 272 279 public void unRegisterWarMBean(String fileName) throws RemoteException , JWebContainerServiceException { 280 281 if (catalinaService == null) { 282 catalinaService = getCatalinaServiceInstance(); 283 } 284 Method m = (Method ) methods.get("unRegisterWarMBean"); 286 if (m == null) { 287 m = getMethod("unRegisterWarMBean", new Class [] {String .class}); 288 } 289 290 invoke(m, new Object [] {fileName}); 291 292 } 293 294 301 public List getInstalledWars() throws Exception { 302 303 if (catalinaService == null) { 304 catalinaService = getCatalinaServiceInstance(); 305 } 306 Method m = (Method ) methods.get("getInstalledWars"); 308 if (m == null) { 309 m = getMethod("getInstalledWars", new Class [] {}); 310 } 311 312 return (List ) invoke(m, new Object [] {}); 313 314 } 315 316 319 public Integer getCurrentNumberOfWars() { 320 321 if (catalinaService == null) { 322 catalinaService = getCatalinaServiceInstance(); 323 } 324 Method m = (Method ) methods.get("getCurrentNumberOfWars"); 326 if (m == null) { 327 m = getMethod("getCurrentNumberOfWars", new Class [] {}); 328 } 329 330 return (Integer ) invoke(m, new Object [] {}); 331 332 } 333 334 339 public Set getWarNames() { 340 341 if (catalinaService == null) { 342 catalinaService = getCatalinaServiceInstance(); 343 } 344 Method m = (Method ) methods.get("getWarNames"); 346 if (m == null) { 347 m = getMethod("getWarNames", new Class [] {}); 348 } 349 350 return (Set ) invoke(m, new Object [] {}); 351 352 } 353 354 359 public boolean isWarLoaded(String fileName) { 360 361 if (catalinaService == null) { 362 catalinaService = getCatalinaServiceInstance(); 363 } 364 Method m = (Method ) methods.get("isWarLoaded"); 366 if (m == null) { 367 m = getMethod("isWarLoaded", new Class [] {String .class}); 368 } 369 370 return ((Boolean ) invoke(m, new Object [] {fileName})).booleanValue(); 371 372 } 373 374 378 public String getServerName() { 379 380 if (catalinaService == null) { 381 catalinaService = getCatalinaServiceInstance(); 382 } 383 Method m = (Method ) methods.get("getServerName"); 385 if (m == null) { 386 m = getMethod("getServerName", new Class [] {}); 387 } 388 389 return (String ) invoke(m, new Object [] {}); 390 391 } 392 393 397 public String getServerVersion() { 398 399 if (catalinaService == null) { 400 catalinaService = getCatalinaServiceInstance(); 401 } 402 Method m = (Method ) methods.get("getServerVersion"); 404 if (m == null) { 405 m = getMethod("getServerVersion", new Class [] {}); 406 } 407 408 return (String ) invoke(m, new Object [] {}); 409 410 } 411 412 417 public List getDeployedWars() { 418 419 if (catalinaService == null) { 420 catalinaService = getCatalinaServiceInstance(); 421 } 422 Method m = (Method ) methods.get("getDeployedWars"); 424 if (m == null) { 425 m = getMethod("getDeployedWars", new Class [] {}); 426 } 427 428 return (List ) invoke(m, new Object [] {}); 429 430 } 431 432 438 public List getDeployableWars() throws Exception { 439 440 if (catalinaService == null) { 441 catalinaService = getCatalinaServiceInstance(); 442 } 443 Method m = (Method ) methods.get("getDeployableWars"); 445 if (m == null) { 446 m = getMethod("getDeployableWars", new Class [] {}); 447 } 448 449 return (List ) invoke(m, new Object [] {}); 450 451 } 452 453 457 public List getAutoloadDirectories() { 458 459 if (catalinaService == null) { 460 catalinaService = getCatalinaServiceInstance(); 461 } 462 Method m = (Method ) methods.get("getAutoloadDirectories"); 464 if (m == null) { 465 m = getMethod("getAutoloadDirectories", new Class [] {}); 466 } 467 468 return (List ) invoke(m, new Object [] {}); 469 470 } 471 472 476 public String getWebappsDirectory() { 477 478 if (catalinaService == null) { 479 catalinaService = getCatalinaServiceInstance(); 480 } 481 Method m = (Method ) methods.get("getWebappsDirectory"); 483 if (m == null) { 484 m = getMethod("getWebappsDirectory", new Class [] {}); 485 } 486 487 return (String ) invoke(m, new Object [] {}); 488 489 } 490 491 497 public void init(Context ctx) throws ServiceException { 498 499 if (catalinaService == null) { 500 catalinaService = getCatalinaServiceInstance(); 501 } 502 Method m = (Method ) methods.get("init"); 504 if (m == null) { 505 m = getMethod("init", new Class [] {Context .class}); 506 } 507 508 invoke(m, new Object [] {ctx}); 509 510 } 511 512 517 public void start() throws ServiceException { 518 519 if (catalinaService == null) { 520 catalinaService = getCatalinaServiceInstance(); 521 } 522 Method m = (Method ) methods.get("start"); 524 if (m == null) { 525 m = getMethod("start", new Class [] {}); 526 } 527 528 invoke(m, new Object [] {}); 529 } 530 531 536 public void stop() throws ServiceException { 537 538 if (catalinaService == null) { 539 catalinaService = getCatalinaServiceInstance(); 540 } 541 Method m = (Method ) methods.get("stop"); 543 if (m == null) { 544 m = getMethod("stop", new Class [] {}); 545 } 546 547 invoke(m, new Object [] {}); 548 549 } 550 551 554 public boolean isStarted() { 555 556 if (catalinaService == null) { 557 catalinaService = getCatalinaServiceInstance(); 558 } 559 Method m = (Method ) methods.get("isStarted"); 561 if (m == null) { 562 m = getMethod("isStarted", new Class [] {}); 563 } 564 565 return ((Boolean ) invoke(m, new Object [] {})).booleanValue(); 566 567 } 568 569 573 public void setName(String name) { 574 575 if (catalinaService == null) { 576 catalinaService = getCatalinaServiceInstance(); 577 } 578 Method m = (Method ) methods.get("setName"); 580 if (m == null) { 581 m = getMethod("setName", new Class [] {String .class}); 582 } 583 584 invoke(m, new Object [] {name}); 585 586 } 587 588 591 public String getName() { 592 593 if (catalinaService == null) { 594 catalinaService = getCatalinaServiceInstance(); 595 } 596 Method m = (Method ) methods.get("getName"); 598 if (m == null) { 599 m = getMethod("getName", new Class [] {}); 600 } 601 602 return (String ) invoke(m, new Object [] {}); 603 604 } 605 606 611 public String getDefaultHost() throws JWebContainerServiceException { 612 613 if (catalinaService == null) { 614 catalinaService = getCatalinaServiceInstance(); 615 } 616 Method m = (Method ) methods.get("getDefaultHost"); 618 if (m == null) { 619 m = getMethod("getDefaultHost", new Class [] {}); 620 } 621 622 return (String ) invoke(m, new Object [] {}); 623 624 } 625 626 632 public String getDefaultHttpPort() throws JWebContainerServiceException { 633 634 if (catalinaService == null) { 635 catalinaService = getCatalinaServiceInstance(); 636 } 637 Method m = (Method ) methods.get("getDefaultHttpPort"); 639 if (m == null) { 640 m = getMethod("getDefaultHttpPort", new Class [] {}); 641 } 642 643 return (String ) invoke(m, new Object [] {}); 644 645 } 646 647 653 public String getDefaultHttpsPort() throws JWebContainerServiceException { 654 655 if (catalinaService == null) { 656 catalinaService = getCatalinaServiceInstance(); 657 } 658 Method m = (Method ) methods.get("getDefaultHttpsPort"); 660 if (m == null) { 661 m = getMethod("getDefaultHttpsPort", new Class [] {}); 662 } 663 664 return (String ) invoke(m, new Object [] {}); 665 666 } 667 668 679 public URLClassLoader getClassLoader(URL warURL, String earAppName, ClassLoader parentLoader) throws JWebContainerServiceException { 680 681 if (catalinaService == null) { 682 catalinaService = getCatalinaServiceInstance(); 683 } 684 Method m = (Method ) methods.get("getClassLoader"); 686 if (m == null) { 687 m = getMethod("getClassLoader", new Class [] {URL .class, String .class, ClassLoader .class}); 688 } 689 690 return (URLClassLoader ) invoke(m, new Object [] {warURL, earAppName, parentLoader}); 691 692 } 693 694 698 public boolean isTomcatStarted() { 699 700 if (catalinaService == null) { 701 catalinaService = getCatalinaServiceInstance(); 702 } 703 Method m = (Method ) methods.get("isTomcatStarted"); 705 if (m == null) { 706 m = getMethod("isTomcatStarted", new Class [] {}); 707 } 708 709 return ((Boolean ) invoke(m, new Object [] {})).booleanValue(); 710 711 } 712 713 717 public ClassLoader getContextLinkedClassLoader(URL url) { 718 719 if (catalinaService == null) { 720 catalinaService = getCatalinaServiceInstance(); 721 } 722 Method m = (Method ) methods.get("getContextLinkedClassLoader"); 724 if (m == null) { 725 m = getMethod("getContextLinkedClassLoader", new Class [] {URL .class}); 726 } 727 728 return (ClassLoader ) invoke(m, new Object [] {url}); 729 730 } 731 732 } 733 | Popular Tags |