00001
00025 package org.objectweb.jonas.web.wrapper;
00026
00027 import java.lang.reflect.InvocationTargetException;
00028 import java.lang.reflect.Method;
00029 import java.net.URL;
00030 import java.net.URLClassLoader;
00031 import java.rmi.RemoteException;
00032 import java.util.Hashtable;
00033 import java.util.List;
00034 import java.util.Map;
00035 import java.util.Set;
00036
00037 import javax.naming.Context;
00038
00039 import org.objectweb.jonas.server.LoaderManager;
00040 import org.objectweb.jonas.service.ServiceException;
00041 import org.objectweb.jonas.web.AbsJWebContainerServiceImplMBean;
00042 import org.objectweb.jonas.web.JWebContainerServiceException;
00043 import org.objectweb.jonas.web.War;
00044
00045
00051 public class CatalinaJWebContainerServiceWrapper implements CatalinaJWebContainerService, AbsJWebContainerServiceImplMBean {
00052
00056 private ClassLoader catalinaLoader = null;
00057
00061 private Object catalinaService = null;
00062
00066 private Map methods = null;
00067
00071 private static final String CATALINA_SERVICE_CLASSNAME = "org.objectweb.jonas.web.catalina50.CatalinaJWebContainerServiceImpl";
00072
00077 public CatalinaJWebContainerServiceWrapper() throws ServiceException {
00078 LoaderManager lm = LoaderManager.getInstance();
00079 try {
00080 catalinaLoader = lm.getCatalinaLoader();
00081 } catch (Exception e) {
00082
00083 throw new ServiceException("Cannot get Catalina ClassLoader", e);
00084 }
00085 methods = new Hashtable();
00086
00087 if (catalinaService == null) {
00088 catalinaService = getCatalinaServiceInstance();
00089 }
00090
00091 }
00092
00110 public void deployWars(Context ctx) throws JWebContainerServiceException {
00111
00112 if (catalinaService == null) {
00113 catalinaService = getCatalinaServiceInstance();
00114 }
00115
00116
00117 Method m = (Method) methods.get("deployWars");
00118 if (m == null) {
00119 m = getMethod("deployWars", new Class[] {Context.class});
00120 }
00121
00122 invoke(m, new Object[] {ctx});
00123 }
00124
00130 public War getWar(URL url) {
00131
00132 if (catalinaService == null) {
00133 catalinaService = getCatalinaServiceInstance();
00134 }
00135
00136
00137 Method m = (Method) methods.get("getWar");
00138 if (m == null) {
00139 m = getMethod("getWar", new Class[] {URL.class});
00140 }
00141
00142 return (War) invoke(m, new Object[] {url});
00143
00144 }
00151 private Object invoke(Method m, Object[] params) {
00152 ClassLoader old = null;
00153 try {
00154 old = Thread.currentThread().getContextClassLoader();
00155 Thread.currentThread().setContextClassLoader(catalinaLoader);
00156 return m.invoke(catalinaService, params);
00157 } catch (InvocationTargetException e) {
00158 if (e.getTargetException() instanceof Error) {
00159 throw (Error) e.getTargetException();
00160 } else if (e.getTargetException() instanceof ServiceException) {
00161 throw (ServiceException) e.getTargetException();
00162 } else {
00163
00164 throw new ServiceException("Problems when invoking " + m.getName(), e.getTargetException());
00165 }
00166 } catch (Exception e) {
00167
00168 throw new ServiceException("Problems when invoking " + m.getName(), e);
00169 } finally {
00170 if (old != null) {
00171 Thread.currentThread().setContextClassLoader(old);
00172 }
00173 }
00174 }
00175
00183 private Method getMethod(String methodName, Class[] paramTypes) throws ServiceException {
00184 try {
00185 Method m = catalinaService.getClass().getMethod(methodName, paramTypes);
00186 methods.put(methodName, m);
00187 return m;
00188 } catch (Exception e) {
00189 throw new ServiceException("Problems when retrieving " + methodName + " method", e);
00190 }
00191 }
00192
00197 private Object getCatalinaServiceInstance() throws ServiceException {
00198 try {
00199 Class service = catalinaLoader.loadClass(CATALINA_SERVICE_CLASSNAME);
00200 return service.newInstance();
00201 } catch (Exception e) {
00202 throw new JWebContainerServiceException("Problems when loading " + CATALINA_SERVICE_CLASSNAME, e);
00203 }
00204 }
00205
00213 public void unDeployWars(URL[] urls) {
00214
00215 if (catalinaService == null) {
00216 catalinaService = getCatalinaServiceInstance();
00217 }
00218
00219 Method m = (Method) methods.get("unDeployWars");
00220 if (m == null) {
00221 m = getMethod("unDeployWars", new Class[] {URL[].class});
00222 }
00223
00224 invoke(m, new Object[] {urls});
00225
00226 }
00227
00234 public void removeCache(ClassLoader earClassLoader) {
00235
00236 if (catalinaService == null) {
00237 catalinaService = getCatalinaServiceInstance();
00238 }
00239
00240 Method m = (Method) methods.get("removeCache");
00241 if (m == null) {
00242 m = getMethod("removeCache", new Class[] {ClassLoader.class});
00243 }
00244
00245 invoke(m, new Object[] {earClassLoader});
00246
00247 }
00248
00256 public void registerWarMBean(String fileName) throws RemoteException, JWebContainerServiceException {
00257
00258 if (catalinaService == null) {
00259 catalinaService = getCatalinaServiceInstance();
00260 }
00261
00262 Method m = (Method) methods.get("registerWarMBean");
00263 if (m == null) {
00264 m = getMethod("registerWarMBean", new Class[] {String.class});
00265 }
00266
00267 invoke(m, new Object[] {fileName});
00268
00269 }
00270
00278 public void unRegisterWarMBean(String fileName) throws RemoteException, JWebContainerServiceException {
00279
00280 if (catalinaService == null) {
00281 catalinaService = getCatalinaServiceInstance();
00282 }
00283
00284 Method m = (Method) methods.get("unRegisterWarMBean");
00285 if (m == null) {
00286 m = getMethod("unRegisterWarMBean", new Class[] {String.class});
00287 }
00288
00289 invoke(m, new Object[] {fileName});
00290
00291 }
00292
00300 public List getInstalledWars() throws Exception {
00301
00302 if (catalinaService == null) {
00303 catalinaService = getCatalinaServiceInstance();
00304 }
00305
00306 Method m = (Method) methods.get("getInstalledWars");
00307 if (m == null) {
00308 m = getMethod("getInstalledWars", new Class[] {});
00309 }
00310
00311 return (List) invoke(m, new Object[] {});
00312
00313 }
00314
00318 public Integer getCurrentNumberOfWars() {
00319
00320 if (catalinaService == null) {
00321 catalinaService = getCatalinaServiceInstance();
00322 }
00323
00324 Method m = (Method) methods.get("getCurrentNumberOfWars");
00325 if (m == null) {
00326 m = getMethod("getCurrentNumberOfWars", new Class[] {});
00327 }
00328
00329 return (Integer) invoke(m, new Object[] {});
00330
00331 }
00332
00338 public Set getWarNames() {
00339
00340 if (catalinaService == null) {
00341 catalinaService = getCatalinaServiceInstance();
00342 }
00343
00344 Method m = (Method) methods.get("getWarNames");
00345 if (m == null) {
00346 m = getMethod("getWarNames", new Class[] {});
00347 }
00348
00349 return (Set) invoke(m, new Object[] {});
00350
00351 }
00352
00358 public boolean isWarLoaded(String fileName) {
00359
00360 if (catalinaService == null) {
00361 catalinaService = getCatalinaServiceInstance();
00362 }
00363
00364 Method m = (Method) methods.get("isWarLoaded");
00365 if (m == null) {
00366 m = getMethod("isWarLoaded", new Class[] {String.class});
00367 }
00368
00369 return ((Boolean) invoke(m, new Object[] {fileName})).booleanValue();
00370
00371 }
00372
00377 public String getServerName() {
00378
00379 if (catalinaService == null) {
00380 catalinaService = getCatalinaServiceInstance();
00381 }
00382
00383 Method m = (Method) methods.get("getServerName");
00384 if (m == null) {
00385 m = getMethod("getServerName", new Class[] {});
00386 }
00387
00388 return (String) invoke(m, new Object[] {});
00389
00390 }
00391
00396 public String getServerVersion() {
00397
00398 if (catalinaService == null) {
00399 catalinaService = getCatalinaServiceInstance();
00400 }
00401
00402 Method m = (Method) methods.get("getServerVersion");
00403 if (m == null) {
00404 m = getMethod("getServerVersion", new Class[] {});
00405 }
00406
00407 return (String) invoke(m, new Object[] {});
00408
00409 }
00410
00416 public List getDeployedWars() {
00417
00418 if (catalinaService == null) {
00419 catalinaService = getCatalinaServiceInstance();
00420 }
00421
00422 Method m = (Method) methods.get("getDeployedWars");
00423 if (m == null) {
00424 m = getMethod("getDeployedWars", new Class[] {});
00425 }
00426
00427 return (List) invoke(m, new Object[] {});
00428
00429 }
00430
00437 public List getDeployableWars() throws Exception {
00438
00439 if (catalinaService == null) {
00440 catalinaService = getCatalinaServiceInstance();
00441 }
00442
00443 Method m = (Method) methods.get("getDeployableWars");
00444 if (m == null) {
00445 m = getMethod("getDeployableWars", new Class[] {});
00446 }
00447
00448 return (List) invoke(m, new Object[] {});
00449
00450 }
00451
00456 public List getAutoloadDirectories() {
00457
00458 if (catalinaService == null) {
00459 catalinaService = getCatalinaServiceInstance();
00460 }
00461
00462 Method m = (Method) methods.get("getAutoloadDirectories");
00463 if (m == null) {
00464 m = getMethod("getAutoloadDirectories", new Class[] {});
00465 }
00466
00467 return (List) invoke(m, new Object[] {});
00468
00469 }
00470
00475 public String getWebappsDirectory() {
00476
00477 if (catalinaService == null) {
00478 catalinaService = getCatalinaServiceInstance();
00479 }
00480
00481 Method m = (Method) methods.get("getWebappsDirectory");
00482 if (m == null) {
00483 m = getMethod("getWebappsDirectory", new Class[] {});
00484 }
00485
00486 return (String) invoke(m, new Object[] {});
00487
00488 }
00489
00496 public void init(Context ctx) throws ServiceException {
00497
00498 if (catalinaService == null) {
00499 catalinaService = getCatalinaServiceInstance();
00500 }
00501
00502 Method m = (Method) methods.get("init");
00503 if (m == null) {
00504 m = getMethod("init", new Class[] {Context.class});
00505 }
00506
00507 invoke(m, new Object[] {ctx});
00508
00509 }
00510
00516 public void start() throws ServiceException {
00517
00518 if (catalinaService == null) {
00519 catalinaService = getCatalinaServiceInstance();
00520 }
00521
00522 Method m = (Method) methods.get("start");
00523 if (m == null) {
00524 m = getMethod("start", new Class[] {});
00525 }
00526
00527 invoke(m, new Object[] {});
00528 }
00529
00535 public void stop() throws ServiceException {
00536
00537 if (catalinaService == null) {
00538 catalinaService = getCatalinaServiceInstance();
00539 }
00540
00541 Method m = (Method) methods.get("stop");
00542 if (m == null) {
00543 m = getMethod("stop", new Class[] {});
00544 }
00545
00546 invoke(m, new Object[] {});
00547
00548 }
00549
00553 public boolean isStarted() {
00554
00555 if (catalinaService == null) {
00556 catalinaService = getCatalinaServiceInstance();
00557 }
00558
00559 Method m = (Method) methods.get("isStarted");
00560 if (m == null) {
00561 m = getMethod("isStarted", new Class[] {});
00562 }
00563
00564 return ((Boolean) invoke(m, new Object[] {})).booleanValue();
00565
00566 }
00567
00572 public void setName(String name) {
00573
00574 if (catalinaService == null) {
00575 catalinaService = getCatalinaServiceInstance();
00576 }
00577
00578 Method m = (Method) methods.get("setName");
00579 if (m == null) {
00580 m = getMethod("setName", new Class[] {String.class});
00581 }
00582
00583 invoke(m, new Object[] {name});
00584
00585 }
00586
00590 public String getName() {
00591
00592 if (catalinaService == null) {
00593 catalinaService = getCatalinaServiceInstance();
00594 }
00595
00596 Method m = (Method) methods.get("getName");
00597 if (m == null) {
00598 m = getMethod("getName", new Class[] {});
00599 }
00600
00601 return (String) invoke(m, new Object[] {});
00602
00603 }
00604
00610 public String getDefaultHost() throws JWebContainerServiceException {
00611
00612 if (catalinaService == null) {
00613 catalinaService = getCatalinaServiceInstance();
00614 }
00615
00616 Method m = (Method) methods.get("getDefaultHost");
00617 if (m == null) {
00618 m = getMethod("getDefaultHost", new Class[] {});
00619 }
00620
00621 return (String) invoke(m, new Object[] {});
00622
00623 }
00624
00631 public String getDefaultHttpPort() throws JWebContainerServiceException {
00632
00633 if (catalinaService == null) {
00634 catalinaService = getCatalinaServiceInstance();
00635 }
00636
00637 Method m = (Method) methods.get("getDefaultHttpPort");
00638 if (m == null) {
00639 m = getMethod("getDefaultHttpPort", new Class[] {});
00640 }
00641
00642 return (String) invoke(m, new Object[] {});
00643
00644 }
00645
00652 public String getDefaultHttpsPort() throws JWebContainerServiceException {
00653
00654 if (catalinaService == null) {
00655 catalinaService = getCatalinaServiceInstance();
00656 }
00657
00658 Method m = (Method) methods.get("getDefaultHttpsPort");
00659 if (m == null) {
00660 m = getMethod("getDefaultHttpsPort", new Class[] {});
00661 }
00662
00663 return (String) invoke(m, new Object[] {});
00664
00665 }
00666
00678 public URLClassLoader getClassLoader(URL warURL, String earAppName, ClassLoader parentLoader) throws JWebContainerServiceException {
00679
00680 if (catalinaService == null) {
00681 catalinaService = getCatalinaServiceInstance();
00682 }
00683
00684 Method m = (Method) methods.get("getClassLoader");
00685 if (m == null) {
00686 m = getMethod("getClassLoader", new Class[] {URL.class, String.class, ClassLoader.class});
00687 }
00688
00689 return (URLClassLoader) invoke(m, new Object[] {warURL, earAppName, parentLoader});
00690
00691 }
00692
00697 public boolean isTomcatStarted() {
00698
00699 if (catalinaService == null) {
00700 catalinaService = getCatalinaServiceInstance();
00701 }
00702
00703 Method m = (Method) methods.get("isTomcatStarted");
00704 if (m == null) {
00705 m = getMethod("isTomcatStarted", new Class[] {});
00706 }
00707
00708 return ((Boolean) invoke(m, new Object[] {})).booleanValue();
00709
00710 }
00711
00716 public ClassLoader getContextLinkedClassLoader(URL url) {
00717
00718 if (catalinaService == null) {
00719 catalinaService = getCatalinaServiceInstance();
00720 }
00721
00722 Method m = (Method) methods.get("getContextLinkedClassLoader");
00723 if (m == null) {
00724 m = getMethod("getContextLinkedClassLoader", new Class[] {URL.class});
00725 }
00726
00727 return (ClassLoader) invoke(m, new Object[] {url});
00728
00729 }
00730
00731 }