001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: CatalinaConnectorFactory.java 6702 2005-05-05 16:09:14Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.web.catalina55;
025:
026: import javax.management.InstanceNotFoundException;
027: import javax.management.MBeanException;
028: import javax.management.MBeanServer;
029: import javax.management.ObjectName;
030: import javax.management.ReflectionException;
031:
032: import org.objectweb.jonas.server.LoaderManager;
033: import org.objectweb.jonas.jmx.CatalinaObjectName;
034:
035: /**
036: * @author Adriana Danes
037: *
038: * This is a proxy class which uses the Catalina MBeanFactory to create connectors.
039: */
040: public class CatalinaConnectorFactory implements
041: CatalinaConnectorFactoryMBean {
042: /**
043: * The reference of the MBeanServer in which are registered both
044: * Catalina's MBeanFactory and JOnAS's CatalinaConnectorFactoryMBean
045: */
046: private MBeanServer myServer = null;
047:
048: /**
049: * Number of values for creating a connector
050: */
051: private static final int CONNECTOR_NB_VALUES = 3;
052:
053: /**
054: * Create Catalina Connector
055: * @param type connector type
056: * @param address connector's IP address
057: * @param port connector's port number
058: * @return Stringified ObjectName of the associated MBean
059: */
060: public String createConnector(String type, String address, int port) {
061: String sObjectName = null;
062: try {
063: // Get Catalina MBeanFactory ObjectName
064: ObjectName onFactory = CatalinaObjectName.catalinaFactory();
065: // Get Catalina Server ObjectName
066: ObjectName onServer = CatalinaObjectName.catalinaServer();
067: // Get Services
068: ObjectName[] aonServices = (ObjectName[]) myServer
069: .getAttribute(onServer, "serviceNames");
070: // Default Service : get the first one ! (see also WhereAreYou class in jonasadmin)
071: String currentCatalinaServiceName = aonServices[0]
072: .getKeyProperty("serviceName");
073: // Domain
074: String currentCatalinaDomainName = aonServices[0]
075: .getDomain();
076: // Get default Catalina Service (called Tomcat-JOnAS in default configuration file
077: ObjectName onService = CatalinaObjectName.catalinaService(
078: currentCatalinaDomainName,
079: currentCatalinaServiceName);
080: // Prepare arguments to invoke the createXxxConnector method
081: Object[] values = new Object[CONNECTOR_NB_VALUES];
082: values[0] = onService.toString();
083: values[1] = address;
084: values[2] = new Integer(port);
085: String operation = null;
086: if ("HTTP".equalsIgnoreCase(type)) {
087: operation = "createHttpConnector"; // HTTP
088: } else if ("HTTPS".equalsIgnoreCase(type)) {
089: operation = "createHttpsConnector"; // HTTPS
090: } else if ("AJP".equalsIgnoreCase(type)) {
091: operation = "createAjpConnector"; // AJP(HTTP)
092: } else {
093: throw new IllegalArgumentException("Unknown type : '"
094: + type + "'.");
095: }
096: // Set Catalina class loader before making the call
097: ClassLoader old = Thread.currentThread()
098: .getContextClassLoader();
099: Thread.currentThread().setContextClassLoader(
100: LoaderManager.getInstance().getCatalinaLoader());
101: // Call the management method createXxxConnector
102: sObjectName = (String) myServer.invoke(onFactory,
103: operation, values, saCreateStandardConnectorTypes);
104: // Restore the class loader
105: Thread.currentThread().setContextClassLoader(old);
106: } catch (InstanceNotFoundException e) {
107: // TODO Auto-generated catch block
108: e.printStackTrace();
109: } catch (MBeanException e) {
110: // TODO Auto-generated catch block
111: e.printStackTrace();
112: } catch (ReflectionException e) {
113: // TODO Auto-generated catch block
114: e.printStackTrace();
115: } catch (Exception e) {
116: // TODO Auto-generated catch block
117: e.printStackTrace();
118: }
119: return sObjectName;
120: }
121:
122: /**
123: * Argument types for createXxxConnector management operation defined by MBeanFactory
124: */
125: private String[] saCreateStandardConnectorTypes = {
126: "java.lang.String", "java.lang.String", "int" // port
127: };
128:
129: /**
130: * @return Returns the myServer.
131: */
132: public MBeanServer getMyServer() {
133: return myServer;
134: }
135:
136: /**
137: * @param myServer The myServer to set.
138: */
139: public void setMyServer(MBeanServer myServer) {
140: this.myServer = myServer;
141: }
142: }
|