0001: /*
0002: * Copyright 1999-2002,2004 The Apache Software Foundation.
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016:
0017: package org.apache.catalina.mbeans;
0018:
0019: import java.lang.reflect.Constructor;
0020: import java.lang.reflect.Method;
0021: import java.util.Vector;
0022:
0023: import javax.management.MBeanException;
0024: import javax.management.MBeanServer;
0025: import javax.management.ObjectName;
0026: import javax.management.RuntimeOperationsException;
0027:
0028: import org.apache.catalina.Connector;
0029: import org.apache.catalina.Context;
0030: import org.apache.catalina.DefaultContext;
0031: import org.apache.catalina.Engine;
0032: import org.apache.catalina.Host;
0033: import org.apache.catalina.Server;
0034: import org.apache.catalina.ServerFactory;
0035: import org.apache.catalina.Service;
0036: import org.apache.catalina.Valve;
0037: import org.apache.catalina.authenticator.SingleSignOn;
0038: import org.apache.catalina.core.ContainerBase;
0039: import org.apache.catalina.core.StandardContext;
0040: import org.apache.catalina.core.StandardDefaultContext;
0041: import org.apache.catalina.core.StandardEngine;
0042: import org.apache.catalina.core.StandardHost;
0043: import org.apache.catalina.core.StandardService;
0044: import org.apache.catalina.loader.WebappLoader;
0045: import org.apache.catalina.logger.FileLogger;
0046: import org.apache.catalina.logger.SystemErrLogger;
0047: import org.apache.catalina.logger.SystemOutLogger;
0048: import org.apache.catalina.realm.DataSourceRealm;
0049: import org.apache.catalina.realm.JDBCRealm;
0050: import org.apache.catalina.realm.JNDIRealm;
0051: import org.apache.catalina.realm.MemoryRealm;
0052: import org.apache.catalina.realm.UserDatabaseRealm;
0053: import org.apache.catalina.session.StandardManager;
0054: import org.apache.catalina.startup.ContextConfig;
0055: import org.apache.catalina.startup.HostConfig;
0056: import org.apache.catalina.valves.AccessLogValve;
0057: import org.apache.catalina.valves.RemoteAddrValve;
0058: import org.apache.catalina.valves.RemoteHostValve;
0059: import org.apache.catalina.valves.RequestDumperValve;
0060: import org.apache.catalina.valves.ValveBase;
0061: import org.apache.commons.modeler.BaseModelMBean;
0062: import org.apache.commons.modeler.ManagedBean;
0063: import org.apache.commons.modeler.Registry;
0064: import org.apache.coyote.tomcat5.CoyoteConnector;
0065:
0066: /**
0067: * <p>A <strong>ModelMBean</strong> implementation for the
0068: * <code>org.apache.catalina.core.StandardServer</code> component.</p>
0069: *
0070: * @author Amy Roh
0071: * @version $Revision: 1.24 $ $Date: 2004/04/17 02:50:26 $
0072: */
0073:
0074: public class MBeanFactory extends BaseModelMBean {
0075:
0076: /**
0077: * The <code>MBeanServer</code> for this application.
0078: */
0079: private static MBeanServer mserver = MBeanUtils.createServer();
0080:
0081: /**
0082: * The configuration information registry for our managed beans.
0083: */
0084: private static Registry registry = MBeanUtils.createRegistry();
0085:
0086: // ----------------------------------------------------------- Constructors
0087:
0088: /**
0089: * Construct a <code>ModelMBean</code> with default
0090: * <code>ModelMBeanInfo</code> information.
0091: *
0092: * @exception MBeanException if the initializer of an object
0093: * throws an exception
0094: * @exception RuntimeOperationsException if an IllegalArgumentException
0095: * occurs
0096: */
0097: public MBeanFactory() throws MBeanException,
0098: RuntimeOperationsException {
0099:
0100: super ();
0101:
0102: }
0103:
0104: // ------------------------------------------------------------- Attributes
0105:
0106: // ------------------------------------------------------------- Operations
0107:
0108: /**
0109: * Return the managed bean definition for the specified bean type
0110: *
0111: * @param type MBean type
0112: */
0113: public String findObjectName(String type) {
0114:
0115: if (type.equals("org.apache.catalina.core.StandardContext")) {
0116: return "StandardContext";
0117: } else if (type
0118: .equals("org.apache.catalina.core.StandardDefaultContext")) {
0119: return "DefaultContext";
0120: } else if (type
0121: .equals("org.apache.catalina.core.StandardEngine")) {
0122: return "Engine";
0123: } else if (type.equals("org.apache.catalina.core.StandardHost")) {
0124: return "Host";
0125: } else {
0126: return null;
0127: }
0128:
0129: }
0130:
0131: /**
0132: * Little convenience method to remove redundant code
0133: * when retrieving the path string
0134: *
0135: * @param t path string
0136: * @return empty string if t==null || t.equals("/")
0137: */
0138: private final String getPathStr(String t) {
0139: if (t == null || t.equals("/")) {
0140: return "";
0141: }
0142: return t;
0143: }
0144:
0145: /**
0146: * Get Parent DefaultContext to add its child component
0147: * from parent's ObjectName
0148: */
0149: private DefaultContext getDefaultContext(ObjectName pname)
0150: throws Exception {
0151:
0152: String type = pname.getKeyProperty("type");
0153: if (type.equals("DefaultContext")) {
0154: Service service = getService(pname);
0155: StandardEngine engine = (StandardEngine) service
0156: .getContainer();
0157: String hostName = pname.getKeyProperty("host");
0158: if (hostName != null) {
0159: StandardHost host = (StandardHost) engine
0160: .findChild(hostName);
0161: return host.getDefaultContext();
0162: } else {
0163: return engine.getDefaultContext();
0164: }
0165: }
0166: return null;
0167: }
0168:
0169: /**
0170: * Get Parent ContainerBase to add its child component
0171: * from parent's ObjectName
0172: */
0173: private ContainerBase getParentContainerFromParent(ObjectName pname)
0174: throws Exception {
0175:
0176: String type = pname.getKeyProperty("type");
0177: String j2eeType = pname.getKeyProperty("j2eeType");
0178: Service service = getService(pname);
0179: StandardEngine engine = (StandardEngine) service.getContainer();
0180: if ((j2eeType != null) && (j2eeType.equals("WebModule"))) {
0181: String name = pname.getKeyProperty("name");
0182: name = name.substring(2);
0183: int i = name.indexOf("/");
0184: String hostName = name.substring(0, i);
0185: String path = name.substring(i);
0186: Host host = (Host) engine.findChild(hostName);
0187: String pathStr = getPathStr(path);
0188: StandardContext context = (StandardContext) host
0189: .findChild(pathStr);
0190: return context;
0191: } else if (type != null) {
0192: if (type.equals("Engine")) {
0193: return engine;
0194: } else if (type.equals("Host")) {
0195: String hostName = pname.getKeyProperty("host");
0196: StandardHost host = (StandardHost) engine
0197: .findChild(hostName);
0198: return host;
0199: }
0200: }
0201: return null;
0202:
0203: }
0204:
0205: /**
0206: * Get Parent ContainerBase to add its child component
0207: * from child component's ObjectName as a String
0208: */
0209: private ContainerBase getParentContainerFromChild(ObjectName oname)
0210: throws Exception {
0211:
0212: String hostName = oname.getKeyProperty("host");
0213: String path = oname.getKeyProperty("path");
0214: Service service = getService(oname);
0215: StandardEngine engine = (StandardEngine) service.getContainer();
0216: if (hostName == null) {
0217: // child's container is Engine
0218: return engine;
0219: } else if (path == null) {
0220: // child's container is Host
0221: StandardHost host = (StandardHost) engine
0222: .findChild(hostName);
0223: return host;
0224: } else {
0225: // child's container is Context
0226: StandardHost host = (StandardHost) engine
0227: .findChild(hostName);
0228: path = getPathStr(path);
0229: StandardContext context = (StandardContext) host
0230: .findChild(path);
0231: return context;
0232: }
0233: }
0234:
0235: private Service getService(ObjectName oname) throws Exception {
0236:
0237: String domain = oname.getDomain();
0238: Server server = ServerFactory.getServer();
0239: Service[] services = server.findServices();
0240: StandardService service = null;
0241: for (int i = 0; i < services.length; i++) {
0242: service = (StandardService) services[i];
0243: if (domain.equals(service.getObjectName().getDomain())) {
0244: break;
0245: }
0246: }
0247: if (!service.getObjectName().getDomain().equals(domain)) {
0248: throw new Exception("Service with the domain is not found");
0249: }
0250: return service;
0251:
0252: }
0253:
0254: /**
0255: * Create a new AccessLoggerValve.
0256: *
0257: * @param parent MBean Name of the associated parent component
0258: *
0259: * @exception Exception if an MBean cannot be created or registered
0260: */
0261: public String createAccessLoggerValve(String parent)
0262: throws Exception {
0263:
0264: ObjectName pname = new ObjectName(parent);
0265: // Create a new AccessLogValve instance
0266: AccessLogValve accessLogger = new AccessLogValve();
0267: ContainerBase containerBase = getParentContainerFromParent(pname);
0268: // Add the new instance to its parent component
0269: containerBase.addValve(accessLogger);
0270: ObjectName oname = accessLogger.getObjectName();
0271: return (oname.toString());
0272:
0273: }
0274:
0275: /**
0276: * Create a new AjpConnector
0277: *
0278: * @param parent MBean Name of the associated parent component
0279: * @param address The IP address on which to bind
0280: * @param port TCP port number to listen on
0281: *
0282: * @exception Exception if an MBean cannot be created or registered
0283: */
0284: public String createAjpConnector(String parent, String address,
0285: int port) throws Exception {
0286:
0287: Object retobj = null;
0288: try {
0289: // Create a new CoyoteConnector instance for AJP
0290: // use reflection to avoid j-t-c compile-time circular dependencies
0291: Class cls = Class
0292: .forName("org.apache.coyote.tomcat5.CoyoteConnector");
0293: Constructor ct = cls.getConstructor(null);
0294: retobj = ct.newInstance(null);
0295: Class partypes1[] = new Class[1];
0296: // Set address
0297: String str = new String();
0298: if ((address != null) && (address.length() > 0)) {
0299: partypes1[0] = str.getClass();
0300: Method meth1 = cls.getMethod("setAddress", partypes1);
0301: Object arglist1[] = new Object[1];
0302: arglist1[0] = address;
0303: meth1.invoke(retobj, arglist1);
0304: }
0305: // Set port number
0306: Class partypes2[] = new Class[1];
0307: partypes2[0] = Integer.TYPE;
0308: Method meth2 = cls.getMethod("setPort", partypes2);
0309: Object arglist2[] = new Object[1];
0310: arglist2[0] = new Integer(port);
0311: meth2.invoke(retobj, arglist2);
0312: // set protocolHandlerClassName for AJP
0313: Class partypes3[] = new Class[1];
0314: partypes3[0] = str.getClass();
0315: Method meth3 = cls.getMethod("setProtocolHandlerClassName",
0316: partypes3);
0317: Object arglist3[] = new Object[1];
0318: arglist3[0] = new String(
0319: "org.apache.jk.server.JkCoyoteHandler");
0320: meth3.invoke(retobj, arglist3);
0321:
0322: // Add the new instance to its parent component
0323: ObjectName pname = new ObjectName(parent);
0324: Service service = getService(pname);
0325: service.addConnector((Connector) retobj);
0326: Method getObjectName = cls.getMethod("getObjectName", null);
0327:
0328: // Return the corresponding MBean name
0329: //ObjectName coname = (ObjectName)getObjectName.invoke(retobj, null);
0330: ObjectName coname = MBeanUtils.createObjectName(pname
0331: .getDomain(), (Connector) retobj);
0332: return (coname.toString());
0333:
0334: } catch (Exception e) {
0335: throw new MBeanException(e);
0336: }
0337:
0338: }
0339:
0340: /**
0341: * Create a new DataSource Realm.
0342: *
0343: * @param parent MBean Name of the associated parent component
0344: *
0345: * @exception Exception if an MBean cannot be created or registered
0346: */
0347: public String createDataSourceRealm(String parent,
0348: String dataSourceName, String roleNameCol,
0349: String userCredCol, String userNameCol,
0350: String userRoleTable, String userTable) throws Exception {
0351:
0352: // Create a new DataSourceRealm instance
0353: DataSourceRealm realm = new DataSourceRealm();
0354: realm.setDataSourceName(dataSourceName);
0355: realm.setRoleNameCol(roleNameCol);
0356: realm.setUserCredCol(userCredCol);
0357: realm.setUserNameCol(userNameCol);
0358: realm.setUserRoleTable(userRoleTable);
0359: realm.setUserTable(userTable);
0360:
0361: // Add the new instance to its parent component
0362: ObjectName pname = new ObjectName(parent);
0363: ContainerBase containerBase = getParentContainerFromParent(pname);
0364: // Add the new instance to its parent component
0365: containerBase.setRealm(realm);
0366: // Return the corresponding MBean name
0367: ObjectName oname = realm.getObjectName();
0368: if (oname != null) {
0369: return (oname.toString());
0370: } else {
0371: return null;
0372: }
0373:
0374: }
0375:
0376: /**
0377: * Create a new DefaultContext.
0378: *
0379: * @param parent MBean Name of the associated parent component
0380: *
0381: * @exception Exception if an MBean cannot be created or registered
0382: */
0383: public String createDefaultContext(String parent) throws Exception {
0384: // XXX FIXME
0385: // Create a new StandardDefaultContext instance
0386: StandardDefaultContext context = new StandardDefaultContext();
0387:
0388: // Add the new instance to its parent component
0389: ObjectName pname = new ObjectName(parent);
0390: Service service = getService(pname);
0391: Engine engine = (Engine) service.getContainer();
0392: String hostName = pname.getKeyProperty("host");
0393: if (hostName == null) { //if DefaultContext is nested in Engine
0394: context.setParent(engine);
0395: engine.addDefaultContext(context);
0396: } else { // if DefaultContext is nested in Host
0397: Host host = (Host) engine.findChild(hostName);
0398: context.setParent(host);
0399: host.addDefaultContext(context);
0400: }
0401:
0402: // Return the corresponding MBean name
0403: ManagedBean managed = registry
0404: .findManagedBean("DefaultContext");
0405: ObjectName oname = MBeanUtils.createObjectName(managed
0406: .getDomain(), context);
0407: return (oname.toString());
0408:
0409: }
0410:
0411: /**
0412: * Create a new FileLogger.
0413: *
0414: * @param parent MBean Name of the associated parent component
0415: *
0416: * @exception Exception if an MBean cannot be created or registered
0417: */
0418: public String createFileLogger(String parent) throws Exception {
0419:
0420: // Create a new FileLogger instance
0421: FileLogger fileLogger = new FileLogger();
0422:
0423: // Add the new instance to its parent component
0424: ObjectName pname = new ObjectName(parent);
0425: ContainerBase containerBase = getParentContainerFromParent(pname);
0426: // Add the new instance to its parent component
0427: containerBase.setLogger(fileLogger);
0428: // Return the corresponding MBean name
0429: ObjectName oname = fileLogger.getObjectName();
0430: return (oname.toString());
0431:
0432: }
0433:
0434: /**
0435: * Create a new HttpConnector
0436: *
0437: * @param parent MBean Name of the associated parent component
0438: * @param address The IP address on which to bind
0439: * @param port TCP port number to listen on
0440: *
0441: * @exception Exception if an MBean cannot be created or registered
0442: */
0443: public String createHttpConnector(String parent, String address,
0444: int port) throws Exception {
0445:
0446: Object retobj = null;
0447: try {
0448: // Create a new CoyoteConnector instance
0449: // use reflection to avoid j-t-c compile-time circular dependencies
0450: Class cls = Class
0451: .forName("org.apache.coyote.tomcat5.CoyoteConnector");
0452: Constructor ct = cls.getConstructor(null);
0453: retobj = ct.newInstance(null);
0454: Class partypes1[] = new Class[1];
0455: // Set address
0456: String str = new String();
0457: if ((address != null) && (address.length() > 0)) {
0458: partypes1[0] = str.getClass();
0459: Method meth1 = cls.getMethod("setAddress", partypes1);
0460: Object arglist1[] = new Object[1];
0461: arglist1[0] = address;
0462: meth1.invoke(retobj, arglist1);
0463: }
0464: // Set port number
0465: Class partypes2[] = new Class[1];
0466: partypes2[0] = Integer.TYPE;
0467: Method meth2 = cls.getMethod("setPort", partypes2);
0468: Object arglist2[] = new Object[1];
0469: arglist2[0] = new Integer(port);
0470: meth2.invoke(retobj, arglist2);
0471: // Add the new instance to its parent component
0472: ObjectName pname = new ObjectName(parent);
0473: Service service = getService(pname);
0474: service.addConnector((Connector) retobj);
0475: Method getObjectName = cls.getMethod("getObjectName", null);
0476:
0477: // Return the corresponding MBean name
0478: ObjectName coname = (ObjectName) getObjectName.invoke(
0479: retobj, null);
0480: //ObjectName coname =
0481: // MBeanUtils.createObjectName(pname.getDomain(), (Connector)retobj);
0482: return (coname.toString());
0483: } catch (Exception e) {
0484: throw new MBeanException(e);
0485: }
0486:
0487: }
0488:
0489: /**
0490: * Create a new HttpsConnector
0491: *
0492: * @param parent MBean Name of the associated parent component
0493: * @param address The IP address on which to bind
0494: * @param port TCP port number to listen on
0495: *
0496: * @exception Exception if an MBean cannot be created or registered
0497: */
0498: public String createHttpsConnector(String parent, String address,
0499: int port) throws Exception {
0500:
0501: Object retobj = null;
0502: // Create a new CoyoteConnector instance
0503: // use reflection to avoid j-t-c compile-time circular dependencies
0504: Class cls = Class
0505: .forName("org.apache.coyote.tomcat5.CoyoteConnector");
0506: try {
0507: Constructor ct = cls.getConstructor(null);
0508: retobj = ct.newInstance(null);
0509: Class partypes1[] = new Class[1];
0510: // Set address
0511: String str = new String();
0512: if ((address != null) && (address.length() > 0)) {
0513: partypes1[0] = str.getClass();
0514: Method meth1 = cls.getMethod("setAddress", partypes1);
0515: Object arglist1[] = new Object[1];
0516: arglist1[0] = address;
0517: meth1.invoke(retobj, arglist1);
0518: }
0519: // Set port number
0520: Class partypes2[] = new Class[1];
0521: partypes2[0] = Integer.TYPE;
0522: Method meth2 = cls.getMethod("setPort", partypes2);
0523: Object arglist2[] = new Object[1];
0524: arglist2[0] = new Integer(port);
0525: meth2.invoke(retobj, arglist2);
0526: // Set scheme
0527: Class partypes3[] = new Class[1];
0528: partypes3[0] = str.getClass();
0529: Method meth3 = cls.getMethod("setScheme", partypes3);
0530: Object arglist3[] = new Object[1];
0531: arglist3[0] = new String("https");
0532: meth3.invoke(retobj, arglist3);
0533: // Set secure
0534: Class partypes4[] = new Class[1];
0535: partypes4[0] = Boolean.TYPE;
0536: Method meth4 = cls.getMethod("setSecure", partypes4);
0537: Object arglist4[] = new Object[1];
0538: arglist4[0] = new Boolean(true);
0539: meth4.invoke(retobj, arglist4);
0540: // Set factory
0541: Class serverSocketFactoryCls = Class
0542: .forName("org.apache.catalina.net.ServerSocketFactory");
0543: Class coyoteServerSocketFactoryCls = Class
0544: .forName("org.apache.coyote.tomcat5.CoyoteServerSocketFactory");
0545: Constructor factoryConst = coyoteServerSocketFactoryCls
0546: .getConstructor(null);
0547: Object factoryObj = factoryConst.newInstance(null);
0548: Class partypes5[] = new Class[1];
0549: partypes5[0] = serverSocketFactoryCls;
0550: Method meth5 = cls.getMethod("setFactory", partypes5);
0551: Object arglist5[] = new Object[1];
0552: arglist5[0] = factoryObj;
0553: meth5.invoke(retobj, arglist5);
0554: } catch (Exception e) {
0555: throw new MBeanException(e);
0556: }
0557:
0558: try {
0559: // Add the new instance to its parent component
0560: ObjectName pname = new ObjectName(parent);
0561: Service service = getService(pname);
0562: service.addConnector((Connector) retobj);
0563: Method getObjectName = cls.getMethod("getObjectName", null);
0564:
0565: // Return the corresponding MBean name
0566: //ObjectName coname = (ObjectName)getObjectName.invoke(retobj, null);
0567: ObjectName coname = MBeanUtils.createObjectName(pname
0568: .getDomain(), (Connector) retobj);
0569: return (coname.toString());
0570: } catch (Exception e) {
0571: // FIXME
0572: // disply error message
0573: // the user needs to use keytool to configure SSL first
0574: // addConnector will fail otherwise
0575: return null;
0576: }
0577:
0578: }
0579:
0580: /**
0581: * Create a new JDBC Realm.
0582: *
0583: * @param parent MBean Name of the associated parent component
0584: *
0585: * @exception Exception if an MBean cannot be created or registered
0586: */
0587: public String createJDBCRealm(String parent, String driverName,
0588: String connectionName, String connectionPassword,
0589: String connectionURL) throws Exception {
0590:
0591: // Create a new JDBCRealm instance
0592: JDBCRealm realm = new JDBCRealm();
0593: realm.setDriverName(driverName);
0594: realm.setConnectionName(connectionName);
0595: realm.setConnectionPassword(connectionPassword);
0596: realm.setConnectionURL(connectionURL);
0597:
0598: // Add the new instance to its parent component
0599: ObjectName pname = new ObjectName(parent);
0600: ContainerBase containerBase = getParentContainerFromParent(pname);
0601: // Add the new instance to its parent component
0602: containerBase.setRealm(realm);
0603: // Return the corresponding MBean name
0604: ObjectName oname = realm.getObjectName();
0605: // FIXME getObjectName() returns null
0606: //ObjectName oname =
0607: // MBeanUtils.createObjectName(pname.getDomain(), realm);
0608: if (oname != null) {
0609: return (oname.toString());
0610: } else {
0611: return null;
0612: }
0613:
0614: }
0615:
0616: /**
0617: * Create a new JNDI Realm.
0618: *
0619: * @param parent MBean Name of the associated parent component
0620: *
0621: * @exception Exception if an MBean cannot be created or registered
0622: */
0623: public String createJNDIRealm(String parent) throws Exception {
0624:
0625: // Create a new JNDIRealm instance
0626: JNDIRealm realm = new JNDIRealm();
0627:
0628: // Add the new instance to its parent component
0629: ObjectName pname = new ObjectName(parent);
0630: ContainerBase containerBase = getParentContainerFromParent(pname);
0631: // Add the new instance to its parent component
0632: containerBase.setRealm(realm);
0633: // Return the corresponding MBean name
0634: ObjectName oname = realm.getObjectName();
0635: // FIXME getObjectName() returns null
0636: //ObjectName oname =
0637: // MBeanUtils.createObjectName(pname.getDomain(), realm);
0638: if (oname != null) {
0639: return (oname.toString());
0640: } else {
0641: return null;
0642: }
0643:
0644: }
0645:
0646: /**
0647: * Create a new Memory Realm.
0648: *
0649: * @param parent MBean Name of the associated parent component
0650: *
0651: * @exception Exception if an MBean cannot be created or registered
0652: */
0653: public String createMemoryRealm(String parent) throws Exception {
0654:
0655: // Create a new MemoryRealm instance
0656: MemoryRealm realm = new MemoryRealm();
0657:
0658: // Add the new instance to its parent component
0659: ObjectName pname = new ObjectName(parent);
0660: ContainerBase containerBase = getParentContainerFromParent(pname);
0661: // Add the new instance to its parent component
0662: containerBase.setRealm(realm);
0663: // Return the corresponding MBean name
0664: //ObjectName oname = realm.getObjectName();
0665: // FIXME getObjectName() returns null
0666: ObjectName oname = MBeanUtils.createObjectName(pname
0667: .getDomain(), realm);
0668: if (oname != null) {
0669: return (oname.toString());
0670: } else {
0671: return null;
0672: }
0673:
0674: }
0675:
0676: /**
0677: * Create a new Remote Address Filter Valve.
0678: *
0679: * @param parent MBean Name of the associated parent component
0680: *
0681: * @exception Exception if an MBean cannot be created or registered
0682: */
0683: public String createRemoteAddrValve(String parent) throws Exception {
0684:
0685: // Create a new RemoteAddrValve instance
0686: RemoteAddrValve valve = new RemoteAddrValve();
0687:
0688: // Add the new instance to its parent component
0689: ObjectName pname = new ObjectName(parent);
0690: ContainerBase containerBase = getParentContainerFromParent(pname);
0691: containerBase.addValve(valve);
0692: ObjectName oname = valve.getObjectName();
0693: return (oname.toString());
0694:
0695: }
0696:
0697: /**
0698: * Create a new Remote Host Filter Valve.
0699: *
0700: * @param parent MBean Name of the associated parent component
0701: *
0702: * @exception Exception if an MBean cannot be created or registered
0703: */
0704: public String createRemoteHostValve(String parent) throws Exception {
0705:
0706: // Create a new RemoteHostValve instance
0707: RemoteHostValve valve = new RemoteHostValve();
0708:
0709: // Add the new instance to its parent component
0710: ObjectName pname = new ObjectName(parent);
0711: ContainerBase containerBase = getParentContainerFromParent(pname);
0712: containerBase.addValve(valve);
0713: ObjectName oname = valve.getObjectName();
0714: return (oname.toString());
0715:
0716: }
0717:
0718: /**
0719: * Create a new Request Dumper Valve.
0720: *
0721: * @param parent MBean Name of the associated parent component
0722: *
0723: * @exception Exception if an MBean cannot be created or registered
0724: */
0725: public String createRequestDumperValve(String parent)
0726: throws Exception {
0727:
0728: // Create a new RequestDumperValve instance
0729: RequestDumperValve valve = new RequestDumperValve();
0730:
0731: // Add the new instance to its parent component
0732: ObjectName pname = new ObjectName(parent);
0733: ContainerBase containerBase = getParentContainerFromParent(pname);
0734: containerBase.addValve(valve);
0735: ObjectName oname = valve.getObjectName();
0736: return (oname.toString());
0737:
0738: }
0739:
0740: /**
0741: * Create a new Single Sign On Valve.
0742: *
0743: * @param parent MBean Name of the associated parent component
0744: *
0745: * @exception Exception if an MBean cannot be created or registered
0746: */
0747: public String createSingleSignOn(String parent) throws Exception {
0748:
0749: // Create a new SingleSignOn instance
0750: SingleSignOn valve = new SingleSignOn();
0751:
0752: // Add the new instance to its parent component
0753: ObjectName pname = new ObjectName(parent);
0754: ContainerBase containerBase = getParentContainerFromParent(pname);
0755: containerBase.addValve(valve);
0756: ObjectName oname = valve.getObjectName();
0757: return (oname.toString());
0758:
0759: }
0760:
0761: /**
0762: * Create a new StandardContext.
0763: *
0764: * @param parent MBean Name of the associated parent component
0765: * @param path The context path for this Context
0766: * @param docBase Document base directory (or WAR) for this Context
0767: *
0768: * @exception Exception if an MBean cannot be created or registered
0769: */
0770: public String createStandardContext(String parent, String path,
0771: String docBase) throws Exception {
0772:
0773: // XXX for backward compatibility. Remove it once supported by the admin
0774: return createStandardContext(parent, path, docBase, false,
0775: false, false, false);
0776: }
0777:
0778: /**
0779: * Create a new StandardContext.
0780: *
0781: * @param parent MBean Name of the associated parent component
0782: * @param path The context path for this Context
0783: * @param docBase Document base directory (or WAR) for this Context
0784: *
0785: * @exception Exception if an MBean cannot be created or registered
0786: */
0787: public String createStandardContext(String parent, String path,
0788: String docBase, boolean xmlValidation,
0789: boolean xmlNamespaceAware, boolean tldValidation,
0790: boolean tldNamespaceAware) throws Exception {
0791:
0792: // Create a new StandardContext instance
0793: StandardContext context = new StandardContext();
0794: path = getPathStr(path);
0795: context.setPath(path);
0796: context.setDocBase(docBase);
0797: context.setXmlValidation(xmlValidation);
0798: context.setXmlNamespaceAware(xmlNamespaceAware);
0799: context.setTldValidation(tldValidation);
0800: context.setTldNamespaceAware(tldNamespaceAware);
0801:
0802: ContextConfig contextConfig = new ContextConfig();
0803: context.addLifecycleListener(contextConfig);
0804:
0805: // Add the new instance to its parent component
0806: ObjectName pname = new ObjectName(parent);
0807: Service service = getService(pname);
0808: Engine engine = (Engine) service.getContainer();
0809: Host host = (Host) engine.findChild(pname
0810: .getKeyProperty("host"));
0811: host.addChild(context);
0812:
0813: // Return the corresponding MBean name
0814: ObjectName oname = MBeanUtils.createObjectName(pname
0815: .getDomain(), context);
0816: return (oname.toString());
0817:
0818: }
0819:
0820: /**
0821: * Create a new StandardEngine.
0822: *
0823: * @param parent MBean Name of the associated parent component
0824: * @param name Unique name of this Engine
0825: * @param defaultHost Default hostname of this Engine
0826: *
0827: * @exception Exception if an MBean cannot be created or registered
0828: */
0829: // replaced by createStandardEngineService to create with service
0830: // public String createStandardEngine(String parent, String name,
0831: // String defaultHost)
0832: // throws Exception {
0833: // Create a new StandardEngine instance
0834: // StandardEngine engine = new StandardEngine();
0835: // engine.setName(name);
0836: // engine.setDefaultHost(defaultHost);
0837: // Add the new instance to its parent component
0838: // ObjectName pname = new ObjectName(parent);
0839: // Server server = ServerFactory.getServer();
0840: // Service service = server.findService(name);
0841: // service.setContainer(engine);
0842: // Return the corresponding MBean name
0843: //ManagedBean managed = registry.findManagedBean("StandardEngine");
0844: // ObjectName oname =
0845: // MBeanUtils.createObjectName(name, engine);
0846: // return (oname.toString());
0847: // }
0848:
0849: public Vector createStandardEngineService(String parent,
0850: String engineName, String defaultHost, String serviceName)
0851: throws Exception {
0852:
0853: // Create a new StandardService instance
0854: StandardService service = new StandardService();
0855: service.setName(serviceName);
0856: // Create a new StandardEngine instance
0857: StandardEngine engine = new StandardEngine();
0858: engine.setName(engineName);
0859: engine.setDefaultHost(defaultHost);
0860: // Need to set engine before adding it to server in order to set domain
0861: service.setContainer(engine);
0862: // Add the new instance to its parent component
0863: Server server = ServerFactory.getServer();
0864: server.addService(service);
0865: Vector onames = new Vector();
0866: // FIXME service & engine.getObjectName
0867: //ObjectName oname = engine.getObjectName();
0868: ObjectName oname = MBeanUtils.createObjectName(engineName,
0869: engine);
0870: onames.add(0, oname);
0871: //oname = service.getObjectName();
0872: oname = MBeanUtils.createObjectName(engineName, service);
0873: onames.add(1, oname);
0874: return (onames);
0875:
0876: }
0877:
0878: /**
0879: * Create a new StandardHost.
0880: *
0881: * @param parent MBean Name of the associated parent component
0882: * @param name Unique name of this Host
0883: * @param appBase Application base directory name
0884: * @param autoDeploy Should we auto deploy?
0885: * @param deployXML Should we deploy Context XML config files property?
0886: * @param liveDeploy Should we live deploy?
0887: * @param unpackWARs Should we unpack WARs when auto deploying?
0888: * @param xmlNamespaceAware Should we turn on/off XML namespace awareness?
0889: * @param xmlValidation Should we turn on/off XML validation?
0890: *
0891: * @exception Exception if an MBean cannot be created or registered
0892: */
0893: public String createStandardHost(String parent, String name,
0894: String appBase, boolean autoDeploy, boolean deployXML,
0895: boolean liveDeploy, boolean unpackWARs,
0896: boolean xmlNamespaceAware, boolean xmlValidation)
0897: throws Exception {
0898:
0899: // Create a new StandardHost instance
0900: StandardHost host = new StandardHost();
0901: host.setName(name);
0902: host.setAppBase(appBase);
0903: host.setAutoDeploy(autoDeploy);
0904: host.setDeployXML(deployXML);
0905: host.setLiveDeploy(liveDeploy);
0906: host.setUnpackWARs(unpackWARs);
0907: host.setXmlNamespaceAware(xmlNamespaceAware);
0908: host.setXmlValidation(xmlValidation);
0909:
0910: // add HostConfig for active reloading
0911: HostConfig hostConfig = new HostConfig();
0912: host.addLifecycleListener(hostConfig);
0913:
0914: // Add the new instance to its parent component
0915: ObjectName pname = new ObjectName(parent);
0916: Service service = getService(pname);
0917: Engine engine = (Engine) service.getContainer();
0918: engine.addChild(host);
0919:
0920: // Return the corresponding MBean name
0921: return (host.getObjectName().toString());
0922:
0923: }
0924:
0925: /**
0926: * Create a new StandardManager.
0927: *
0928: * @param parent MBean Name of the associated parent component
0929: *
0930: * @exception Exception if an MBean cannot be created or registered
0931: */
0932: public String createStandardManager(String parent) throws Exception {
0933:
0934: // Create a new StandardManager instance
0935: StandardManager manager = new StandardManager();
0936:
0937: // Add the new instance to its parent component
0938: ObjectName pname = new ObjectName(parent);
0939: ContainerBase containerBase = getParentContainerFromParent(pname);
0940: if (containerBase != null) {
0941: containerBase.setManager(manager);
0942: }
0943: ObjectName oname = manager.getObjectName();
0944: if (oname != null) {
0945: return (oname.toString());
0946: } else {
0947: return null;
0948: }
0949:
0950: }
0951:
0952: /**
0953: * Create a new StandardService.
0954: *
0955: * @param parent MBean Name of the associated parent component
0956: * @param name Unique name of this StandardService
0957: *
0958: * @exception Exception if an MBean cannot be created or registered
0959: */
0960: public String createStandardService(String parent, String name,
0961: String domain) throws Exception {
0962:
0963: // Create a new StandardService instance
0964: StandardService service = new StandardService();
0965: service.setName(name);
0966:
0967: // Add the new instance to its parent component
0968: Server server = ServerFactory.getServer();
0969: server.addService(service);
0970:
0971: // Return the corresponding MBean name
0972: return (service.getObjectName().toString());
0973:
0974: }
0975:
0976: /**
0977: * Create a new System Error Logger.
0978: *
0979: * @param parent MBean Name of the associated parent component
0980: *
0981: * @exception Exception if an MBean cannot be created or registered
0982: */
0983: public String createSystemErrLogger(String parent) throws Exception {
0984:
0985: // Create a new SystemErrLogger instance
0986: SystemErrLogger logger = new SystemErrLogger();
0987:
0988: // Add the new instance to its parent component
0989: ObjectName pname = new ObjectName(parent);
0990: ContainerBase containerBase = getParentContainerFromParent(pname);
0991: containerBase.setLogger(logger);
0992: ObjectName oname = logger.getObjectName();
0993: return (oname.toString());
0994:
0995: }
0996:
0997: /**
0998: * Create a new System Output Logger.
0999: *
1000: * @param parent MBean Name of the associated parent component
1001: *
1002: * @exception Exception if an MBean cannot be created or registered
1003: */
1004: public String createSystemOutLogger(String parent) throws Exception {
1005:
1006: // Create a new SystemOutLogger instance
1007: SystemOutLogger logger = new SystemOutLogger();
1008:
1009: // Add the new instance to its parent component
1010: ObjectName pname = new ObjectName(parent);
1011: ContainerBase containerBase = getParentContainerFromParent(pname);
1012: containerBase.setLogger(logger);
1013: ObjectName oname = logger.getObjectName();
1014: return (oname.toString());
1015:
1016: }
1017:
1018: /**
1019: * Create a new UserDatabaseRealm.
1020: *
1021: * @param parent MBean Name of the associated parent component
1022: * @param resourceName Global JNDI resource name of the associated
1023: * UserDatabase
1024: *
1025: * @exception Exception if an MBean cannot be created or registered
1026: */
1027: public String createUserDatabaseRealm(String parent,
1028: String resourceName) throws Exception {
1029:
1030: // Create a new UserDatabaseRealm instance
1031: UserDatabaseRealm realm = new UserDatabaseRealm();
1032: realm.setResourceName(resourceName);
1033:
1034: // Add the new instance to its parent component
1035: ObjectName pname = new ObjectName(parent);
1036: ContainerBase containerBase = getParentContainerFromParent(pname);
1037: // Add the new instance to its parent component
1038: containerBase.setRealm(realm);
1039: // Return the corresponding MBean name
1040: ObjectName oname = realm.getObjectName();
1041: // FIXME getObjectName() returns null
1042: //ObjectName oname =
1043: // MBeanUtils.createObjectName(pname.getDomain(), realm);
1044: if (oname != null) {
1045: return (oname.toString());
1046: } else {
1047: return null;
1048: }
1049:
1050: }
1051:
1052: /**
1053: * Create a new Web Application Loader.
1054: *
1055: * @param parent MBean Name of the associated parent component
1056: *
1057: * @exception Exception if an MBean cannot be created or registered
1058: */
1059: public String createWebappLoader(String parent) throws Exception {
1060:
1061: // Create a new WebappLoader instance
1062: WebappLoader loader = new WebappLoader();
1063:
1064: // Add the new instance to its parent component
1065: ObjectName pname = new ObjectName(parent);
1066: ContainerBase containerBase = getParentContainerFromParent(pname);
1067: if (containerBase != null) {
1068: containerBase.setLoader(loader);
1069: }
1070: // FIXME add Loader.getObjectName
1071: //ObjectName oname = loader.getObjectName();
1072: ObjectName oname = MBeanUtils.createObjectName(pname
1073: .getDomain(), loader);
1074: return (oname.toString());
1075:
1076: }
1077:
1078: /**
1079: * Remove an existing Connector.
1080: *
1081: * @param name MBean Name of the comonent to remove
1082: *
1083: * @param serviceName Service name of the connector to remove
1084: *
1085: * @exception Exception if a component cannot be removed
1086: */
1087: public void removeConnector(String name) throws Exception {
1088:
1089: // Acquire a reference to the component to be removed
1090: ObjectName oname = new ObjectName(name);
1091: Server server = ServerFactory.getServer();
1092: Service service = getService(oname);
1093: String port = oname.getKeyProperty("port");
1094: //String address = oname.getKeyProperty("address");
1095:
1096: Connector conns[] = (Connector[]) service.findConnectors();
1097:
1098: for (int i = 0; i < conns.length; i++) {
1099: Class cls = conns[i].getClass();
1100: Method getAddrMeth = cls.getMethod("getAddress", null);
1101: Object addrObj = getAddrMeth.invoke(conns[i], null);
1102: String connAddress = null;
1103: if (addrObj != null) {
1104: connAddress = addrObj.toString();
1105: }
1106: Method getPortMeth = cls.getMethod("getPort", null);
1107: Object portObj = getPortMeth.invoke(conns[i], null);
1108: String connPort = new String();
1109: if (portObj != null) {
1110: connPort = portObj.toString();
1111: }
1112: // if (((address.equals("null")) &&
1113: if ((connAddress == null) && port.equals(connPort)) {
1114: service.removeConnector(conns[i]);
1115: ((CoyoteConnector) conns[i]).destroy();
1116: break;
1117: }
1118: // } else if (address.equals(connAddress))
1119: if (port.equals(connPort)) {
1120: // Remove this component from its parent component
1121: service.removeConnector(conns[i]);
1122: ((CoyoteConnector) conns[i]).destroy();
1123: break;
1124: }
1125: }
1126:
1127: }
1128:
1129: /**
1130: * Remove an existing Context.
1131: *
1132: * @param name MBean Name of the comonent to remove
1133: *
1134: * @exception Exception if a component cannot be removed
1135: */
1136: public void removeContext(String contextName) throws Exception {
1137:
1138: // Acquire a reference to the component to be removed
1139: ObjectName oname = new ObjectName(contextName);
1140: String domain = oname.getDomain();
1141: StandardService service = (StandardService) getService(oname);
1142: if (!service.getObjectName().getDomain().equals(domain)) {
1143: throw new Exception("Service with the domain is not found");
1144: }
1145: Engine engine = (Engine) service.getContainer();
1146: String name = oname.getKeyProperty("name");
1147: name = name.substring(2);
1148: int i = name.indexOf("/");
1149: String hostName = name.substring(0, i);
1150: String path = name.substring(i);
1151: Host host = (Host) engine.findChild(hostName);
1152: String pathStr = getPathStr(path);
1153: Context context = (Context) host.findChild(pathStr);
1154: // Remove this component from its parent component
1155: host.removeChild(context);
1156:
1157: }
1158:
1159: /**
1160: * Remove an existing Host.
1161: *
1162: * @param name MBean Name of the comonent to remove
1163: *
1164: * @exception Exception if a component cannot be removed
1165: */
1166: public void removeHost(String name) throws Exception {
1167:
1168: // Acquire a reference to the component to be removed
1169: ObjectName oname = new ObjectName(name);
1170: String hostName = oname.getKeyProperty("host");
1171: Service service = getService(oname);
1172: Engine engine = (Engine) service.getContainer();
1173: Host host = (Host) engine.findChild(hostName);
1174:
1175: // Remove this component from its parent component
1176: engine.removeChild(host);
1177:
1178: }
1179:
1180: /**
1181: * Remove an existing Logger.
1182: *
1183: * @param name MBean Name of the comonent to remove
1184: *
1185: * @exception Exception if a component cannot be removed
1186: */
1187: public void removeLogger(String name) throws Exception {
1188:
1189: ObjectName oname = new ObjectName(name);
1190: // Acquire a reference to the component to be removed
1191: ContainerBase container = getParentContainerFromChild(oname);
1192: container.setLogger(null);
1193:
1194: }
1195:
1196: /**
1197: * Remove an existing Loader.
1198: *
1199: * @param name MBean Name of the comonent to remove
1200: *
1201: * @exception Exception if a component cannot be removed
1202: */
1203: public void removeLoader(String name) throws Exception {
1204:
1205: ObjectName oname = new ObjectName(name);
1206: // Acquire a reference to the component to be removed
1207: ContainerBase container = getParentContainerFromChild(oname);
1208: container.setLoader(null);
1209:
1210: }
1211:
1212: /**
1213: * Remove an existing Manager.
1214: *
1215: * @param name MBean Name of the comonent to remove
1216: *
1217: * @exception Exception if a component cannot be removed
1218: */
1219: public void removeManager(String name) throws Exception {
1220:
1221: ObjectName oname = new ObjectName(name);
1222: // Acquire a reference to the component to be removed
1223: ContainerBase container = getParentContainerFromChild(oname);
1224: container.setManager(null);
1225:
1226: }
1227:
1228: /**
1229: * Remove an existing Realm.
1230: *
1231: * @param name MBean Name of the comonent to remove
1232: *
1233: * @exception Exception if a component cannot be removed
1234: */
1235: public void removeRealm(String name) throws Exception {
1236:
1237: ObjectName oname = new ObjectName(name);
1238: // Acquire a reference to the component to be removed
1239: ContainerBase container = getParentContainerFromChild(oname);
1240: container.setRealm(null);
1241: }
1242:
1243: /**
1244: * Remove an existing Service.
1245: *
1246: * @param name MBean Name of the component to remove
1247: *
1248: * @exception Exception if a component cannot be removed
1249: */
1250: public void removeService(String name) throws Exception {
1251:
1252: // Acquire a reference to the component to be removed
1253: ObjectName oname = new ObjectName(name);
1254: String serviceName = oname.getKeyProperty("serviceName");
1255: Server server = ServerFactory.getServer();
1256: Service service = server.findService(serviceName);
1257:
1258: // Remove this component from its parent component
1259: server.removeService(service);
1260:
1261: }
1262:
1263: /**
1264: * Remove an existing Valve.
1265: *
1266: * @param name MBean Name of the comonent to remove
1267: *
1268: * @exception Exception if a component cannot be removed
1269: */
1270: public void removeValve(String name) throws Exception {
1271:
1272: // Acquire a reference to the component to be removed
1273: ObjectName oname = new ObjectName(name);
1274: ContainerBase container = getParentContainerFromChild(oname);
1275: String sequence = oname.getKeyProperty("seq");
1276: Valve[] valves = (Valve[]) container.getValves();
1277: for (int i = 0; i < valves.length; i++) {
1278: ObjectName voname = ((ValveBase) valves[i]).getObjectName();
1279: if (voname.equals(oname)) {
1280: container.removeValve(valves[i]);
1281: }
1282: }
1283: }
1284:
1285: }
|