KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > web > catalina55 > CatalinaConnectorFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: CatalinaConnectorFactory.java,v 1.2 2005/05/05 16:09:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.web.catalina55;
27
28 import javax.management.InstanceNotFoundException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.ReflectionException JavaDoc;
33
34 import org.objectweb.jonas.server.LoaderManager;
35 import org.objectweb.jonas.jmx.CatalinaObjectName;
36
37 /**
38  * @author Adriana Danes
39  *
40  * This is a proxy class which uses the Catalina MBeanFactory to create connectors.
41  */

42 public class CatalinaConnectorFactory implements CatalinaConnectorFactoryMBean {
43     /**
44      * The reference of the MBeanServer in which are registered both
45      * Catalina's MBeanFactory and JOnAS's CatalinaConnectorFactoryMBean
46      */

47     private MBeanServer JavaDoc myServer = null;
48
49     /**
50      * Number of values for creating a connector
51      */

52     private static final int CONNECTOR_NB_VALUES = 3;
53
54
55     /**
56      * Create Catalina Connector
57      * @param type connector type
58      * @param address connector's IP address
59      * @param port connector's port number
60      * @return Stringified ObjectName of the associated MBean
61      */

62     public String JavaDoc createConnector(String JavaDoc type, String JavaDoc address, int port) {
63         String JavaDoc sObjectName = null;
64         try {
65             // Get Catalina MBeanFactory ObjectName
66
ObjectName JavaDoc onFactory = CatalinaObjectName.catalinaFactory();
67             // Get Catalina Server ObjectName
68
ObjectName JavaDoc onServer = CatalinaObjectName.catalinaServer();
69             // Get Services
70
ObjectName JavaDoc[] aonServices = (ObjectName JavaDoc[]) myServer.getAttribute(onServer, "serviceNames");
71             // Default Service : get the first one ! (see also WhereAreYou class in jonasadmin)
72
String JavaDoc currentCatalinaServiceName = aonServices[0].getKeyProperty("serviceName");
73             // Domain
74
String JavaDoc currentCatalinaDomainName = aonServices[0].getDomain();
75             // Get default Catalina Service (called Tomcat-JOnAS in default configuration file
76
ObjectName JavaDoc onService = CatalinaObjectName.catalinaService(currentCatalinaDomainName, currentCatalinaServiceName);
77             // Prepare arguments to invoke the createXxxConnector method
78
Object JavaDoc[] values = new Object JavaDoc[CONNECTOR_NB_VALUES];
79             values[0] = onService.toString();
80             values[1] = address;
81             values[2] = new Integer JavaDoc(port);
82             String JavaDoc operation = null;
83             if ("HTTP".equalsIgnoreCase(type)) {
84                 operation = "createHttpConnector"; // HTTP
85
} else if ("HTTPS".equalsIgnoreCase(type)) {
86                 operation = "createHttpsConnector"; // HTTPS
87
} else if ("AJP".equalsIgnoreCase(type)) {
88                 operation = "createAjpConnector"; // AJP(HTTP)
89
} else {
90               throw new IllegalArgumentException JavaDoc("Unknown type : '" + type + "'.");
91             }
92             // Set Catalina class loader before making the call
93
ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
94             Thread.currentThread().setContextClassLoader(LoaderManager.getInstance().getCatalinaLoader());
95             // Call the management method createXxxConnector
96
sObjectName = (String JavaDoc) myServer.invoke(onFactory, operation, values
97                 , saCreateStandardConnectorTypes);
98             // Restore the class loader
99
Thread.currentThread().setContextClassLoader(old);
100         } catch (InstanceNotFoundException JavaDoc e) {
101             // TODO Auto-generated catch block
102
e.printStackTrace();
103         } catch (MBeanException JavaDoc e) {
104             // TODO Auto-generated catch block
105
e.printStackTrace();
106         } catch (ReflectionException JavaDoc e) {
107             // TODO Auto-generated catch block
108
e.printStackTrace();
109         } catch (Exception JavaDoc e) {
110             // TODO Auto-generated catch block
111
e.printStackTrace();
112         }
113         return sObjectName;
114     }
115
116     /**
117      * Argument types for createXxxConnector management operation defined by MBeanFactory
118      */

119     private String JavaDoc[] saCreateStandardConnectorTypes = {
120             "java.lang.String", "java.lang.String", "int" // port
121
};
122     /**
123      * @return Returns the myServer.
124      */

125     public MBeanServer JavaDoc getMyServer() {
126         return myServer;
127     }
128     /**
129      * @param myServer The myServer to set.
130      */

131     public void setMyServer(MBeanServer JavaDoc myServer) {
132         this.myServer = myServer;
133     }
134 }
135
Popular Tags