KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > deployment > PortalWebAppFactory


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.deployment;
10
11 import java.lang.reflect.Method JavaDoc;
12
13 import javax.management.MBeanServer JavaDoc;
14
15 import org.apache.log4j.Logger;
16 import org.jboss.web.WebApplication;
17
18 /**
19  * Creates the JBossWebApp according to the JBossWeb found.
20  *
21  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
22  * @version $Revision: 1.2 $
23  */

24 public class PortalWebAppFactory
25 {
26
27    private static Logger log = Logger.getLogger(PortalWebAppFactory.class);
28
29    private static final Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
30    private static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
31
32    private static final int UNKNOWN = 0;
33    private static final int TOMCAT4 = 1;
34    private static final int TOMCAT5 = 2;
35
36    private final MBeanServer JavaDoc server;
37
38    public PortalWebAppFactory(MBeanServer JavaDoc server)
39    {
40       this.server = server;
41    }
42
43    /**
44     * Create a portl web application.
45     *
46     * @return the portal web app or null if it cannot be created.
47     */

48    public PortalWebApp create(WebApplication webApp) throws CannotCreatePortletWebAppException
49    {
50       switch (getVersion())
51       {
52       case TOMCAT4:
53          return new PortalWebTomcat4App(webApp);
54       case TOMCAT5:
55          return new PortalWebTomcat5App(webApp, server);
56       default:
57          throw new CannotCreatePortletWebAppException("JBossWeb cannot handle it");
58       }
59    }
60
61    /**
62     * Recognize the jbossweb container and returns its version.
63     */

64    private static int getVersion()
65    {
66       try
67       {
68          // Get the classloader
69
ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
70          Class JavaDoc serverInfoClass = cl.loadClass("org.apache.catalina.util.ServerInfo");
71          Method JavaDoc getServerInfoMethod = serverInfoClass.getMethod("getServerInfo", EMPTY_CLASS_ARRAY);
72          String JavaDoc result = (String JavaDoc)getServerInfoMethod.invoke(null, EMPTY_OBJECT_ARRAY);
73          if (result != null)
74          {
75             if (result.startsWith("Apache Tomcat/5"))
76             {
77                return TOMCAT5;
78             }
79             else if (result.startsWith("Apache Tomcat/4"))
80             {
81                return TOMCAT4;
82             }
83          }
84       }
85       catch (ClassNotFoundException JavaDoc e)
86       {
87          log.error("Cannot getPortalObjectContext catalina ServerInfo class");
88       }
89       catch (NoSuchMethodException JavaDoc e)
90       {
91          log.error("Cannot invoke ServerInfo.getServerInfo()", e);
92       }
93       catch (Exception JavaDoc e)
94       {
95          log.error("Unexpected error", e);
96       }
97       return UNKNOWN;
98    }
99 }
100
Popular Tags