KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > mbeans > MBeanFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.catalina.mbeans;
19
20 import java.io.File JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.management.MBeanException JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.management.RuntimeOperationsException JavaDoc;
27
28 import org.apache.catalina.Context;
29 import org.apache.catalina.Engine;
30 import org.apache.catalina.Host;
31 import org.apache.catalina.Server;
32 import org.apache.catalina.ServerFactory;
33 import org.apache.catalina.Service;
34 import org.apache.catalina.Valve;
35 import org.apache.catalina.authenticator.SingleSignOn;
36 import org.apache.catalina.connector.Connector;
37 import org.apache.catalina.core.ContainerBase;
38 import org.apache.catalina.core.StandardContext;
39 import org.apache.catalina.core.StandardEngine;
40 import org.apache.catalina.core.StandardHost;
41 import org.apache.catalina.core.StandardService;
42 import org.apache.catalina.loader.WebappLoader;
43 import org.apache.catalina.realm.DataSourceRealm;
44 import org.apache.catalina.realm.JDBCRealm;
45 import org.apache.catalina.realm.JNDIRealm;
46 import org.apache.catalina.realm.MemoryRealm;
47 import org.apache.catalina.realm.UserDatabaseRealm;
48 import org.apache.catalina.session.StandardManager;
49 import org.apache.catalina.startup.ContextConfig;
50 import org.apache.catalina.startup.HostConfig;
51 import org.apache.catalina.valves.AccessLogValve;
52 import org.apache.catalina.valves.RemoteAddrValve;
53 import org.apache.catalina.valves.RemoteHostValve;
54 import org.apache.catalina.valves.RequestDumperValve;
55 import org.apache.catalina.valves.ValveBase;
56 import org.apache.tomcat.util.modeler.BaseModelMBean;
57 import org.apache.tomcat.util.modeler.Registry;
58
59
60 /**
61  * <p>A <strong>ModelMBean</strong> implementation for the
62  * <code>org.apache.catalina.core.StandardServer</code> component.</p>
63  *
64  * @author Amy Roh
65  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
66  */

67
68 public class MBeanFactory extends BaseModelMBean {
69
70     private static org.apache.commons.logging.Log log =
71         org.apache.commons.logging.LogFactory.getLog(MBeanFactory.class);
72
73     /**
74      * The <code>MBeanServer</code> for this application.
75      */

76     private static MBeanServer JavaDoc mserver = MBeanUtils.createServer();
77
78     /**
79      * The configuration information registry for our managed beans.
80      */

81     private static Registry registry = MBeanUtils.createRegistry();
82
83
84     // ----------------------------------------------------------- Constructors
85

86
87     /**
88      * Construct a <code>ModelMBean</code> with default
89      * <code>ModelMBeanInfo</code> information.
90      *
91      * @exception MBeanException if the initializer of an object
92      * throws an exception
93      * @exception RuntimeOperationsException if an IllegalArgumentException
94      * occurs
95      */

96     public MBeanFactory()
97         throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
98
99         super();
100
101     }
102
103
104     // ------------------------------------------------------------- Attributes
105

106
107
108
109     // ------------------------------------------------------------- Operations
110

111
112     /**
113      * Return the managed bean definition for the specified bean type
114      *
115      * @param type MBean type
116      */

117     public String JavaDoc findObjectName(String JavaDoc type) {
118
119         if (type.equals("org.apache.catalina.core.StandardContext")) {
120             return "StandardContext";
121         } else if (type.equals("org.apache.catalina.core.StandardEngine")) {
122             return "Engine";
123         } else if (type.equals("org.apache.catalina.core.StandardHost")) {
124             return "Host";
125         } else {
126             return null;
127         }
128
129     }
130
131
132     /**
133      * Little convenience method to remove redundant code
134      * when retrieving the path string
135      *
136      * @param t path string
137      * @return empty string if t==null || t.equals("/")
138      */

139     private final String JavaDoc getPathStr(String JavaDoc t) {
140         if (t == null || t.equals("/")) {
141             return "";
142         }
143         return t;
144     }
145     
146    /**
147      * Get Parent ContainerBase to add its child component
148      * from parent's ObjectName
149      */

150     private ContainerBase getParentContainerFromParent(ObjectName JavaDoc pname)
151         throws Exception JavaDoc {
152         
153         String JavaDoc type = pname.getKeyProperty("type");
154         String JavaDoc j2eeType = pname.getKeyProperty("j2eeType");
155         Service service = getService(pname);
156         StandardEngine engine = (StandardEngine) service.getContainer();
157         if ((j2eeType!=null) && (j2eeType.equals("WebModule"))) {
158             String JavaDoc name = pname.getKeyProperty("name");
159             name = name.substring(2);
160             int i = name.indexOf("/");
161             String JavaDoc hostName = name.substring(0,i);
162             String JavaDoc path = name.substring(i);
163             Host host = (Host) engine.findChild(hostName);
164             String JavaDoc pathStr = getPathStr(path);
165             StandardContext context = (StandardContext)host.findChild(pathStr);
166             return context;
167         } else if (type != null) {
168             if (type.equals("Engine")) {
169                 return engine;
170             } else if (type.equals("Host")) {
171                 String JavaDoc hostName = pname.getKeyProperty("host");
172                 StandardHost host = (StandardHost) engine.findChild(hostName);
173                 return host;
174             }
175         }
176         return null;
177         
178     }
179
180
181     /**
182      * Get Parent ContainerBase to add its child component
183      * from child component's ObjectName as a String
184      */

185     private ContainerBase getParentContainerFromChild(ObjectName JavaDoc oname)
186         throws Exception JavaDoc {
187         
188         String JavaDoc hostName = oname.getKeyProperty("host");
189         String JavaDoc path = oname.getKeyProperty("path");
190         Service service = getService(oname);
191         StandardEngine engine = (StandardEngine) service.getContainer();
192         if (hostName == null) {
193             // child's container is Engine
194
return engine;
195         } else if (path == null) {
196             // child's container is Host
197
StandardHost host = (StandardHost) engine.findChild(hostName);
198             return host;
199         } else {
200             // child's container is Context
201
StandardHost host = (StandardHost) engine.findChild(hostName);
202             path = getPathStr(path);
203             StandardContext context = (StandardContext) host.findChild(path);
204             return context;
205         }
206     }
207
208     
209     private Service getService(ObjectName JavaDoc oname) throws Exception JavaDoc {
210     
211         String JavaDoc domain = oname.getDomain();
212         Server JavaDoc server = ServerFactory.getServer();
213         Service[] services = server.findServices();
214         StandardService service = null;
215         for (int i = 0; i < services.length; i++) {
216             service = (StandardService) services[i];
217             if (domain.equals(service.getObjectName().getDomain())) {
218                 break;
219             }
220         }
221         if (!service.getObjectName().getDomain().equals(domain)) {
222             throw new Exception JavaDoc("Service with the domain is not found");
223         }
224         return service;
225
226     }
227     
228     
229     /**
230      * Create a new AccessLoggerValve.
231      *
232      * @param parent MBean Name of the associated parent component
233      *
234      * @exception Exception if an MBean cannot be created or registered
235      */

236     public String JavaDoc createAccessLoggerValve(String JavaDoc parent)
237         throws Exception JavaDoc {
238
239         ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
240         // Create a new AccessLogValve instance
241
AccessLogValve accessLogger = new AccessLogValve();
242         ContainerBase containerBase = getParentContainerFromParent(pname);
243         // Add the new instance to its parent component
244
containerBase.addValve(accessLogger);
245         ObjectName JavaDoc oname = accessLogger.getObjectName();
246         return (oname.toString());
247
248     }
249         
250
251     /**
252      * Create a new AjpConnector
253      *
254      * @param parent MBean Name of the associated parent component
255      * @param address The IP address on which to bind
256      * @param port TCP port number to listen on
257      *
258      * @exception Exception if an MBean cannot be created or registered
259      */

260     public String JavaDoc createAjpConnector(String JavaDoc parent, String JavaDoc address, int port)
261         throws Exception JavaDoc {
262
263         return createConnector(parent, address, port, true, false);
264     }
265     
266     /**
267      * Create a new DataSource Realm.
268      *
269      * @param parent MBean Name of the associated parent component
270      *
271      * @exception Exception if an MBean cannot be created or registered
272      */

273     public String JavaDoc createDataSourceRealm(String JavaDoc parent, String JavaDoc dataSourceName,
274         String JavaDoc roleNameCol, String JavaDoc userCredCol, String JavaDoc userNameCol,
275         String JavaDoc userRoleTable, String JavaDoc userTable) throws Exception JavaDoc {
276
277         // Create a new DataSourceRealm instance
278
DataSourceRealm realm = new DataSourceRealm();
279         realm.setDataSourceName(dataSourceName);
280         realm.setRoleNameCol(roleNameCol);
281         realm.setUserCredCol(userCredCol);
282         realm.setUserNameCol(userNameCol);
283         realm.setUserRoleTable(userRoleTable);
284         realm.setUserTable(userTable);
285
286         // Add the new instance to its parent component
287
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
288         ContainerBase containerBase = getParentContainerFromParent(pname);
289         // Add the new instance to its parent component
290
containerBase.setRealm(realm);
291         // Return the corresponding MBean name
292
ObjectName JavaDoc oname = realm.getObjectName();
293         if (oname != null) {
294             return (oname.toString());
295         } else {
296             return null;
297         }
298
299     }
300
301     /**
302      * Create a new HttpConnector
303      *
304      * @param parent MBean Name of the associated parent component
305      * @param address The IP address on which to bind
306      * @param port TCP port number to listen on
307      *
308      * @exception Exception if an MBean cannot be created or registered
309      */

310     public String JavaDoc createHttpConnector(String JavaDoc parent, String JavaDoc address, int port)
311         throws Exception JavaDoc {
312     return createConnector(parent, address, port, false, false);
313     }
314
315     /**
316      * Create a new Connector
317      *
318      * @param parent MBean Name of the associated parent component
319      * @param address The IP address on which to bind
320      * @param port TCP port number to listen on
321      * @param isAjp Create a AJP/1.3 Connector
322      * @param isSSL Create a secure Connector
323      *
324      * @exception Exception if an MBean cannot be created or registered
325      */

326     private String JavaDoc createConnector(String JavaDoc parent, String JavaDoc address, int port, boolean isAjp, boolean isSSL)
327         throws Exception JavaDoc {
328         Connector retobj = new Connector();
329         if ((address!=null) && (address.length()>0)) {
330             retobj.setProperty("address", address);
331         }
332         // Set port number
333
retobj.setPort(port);
334         // Set the protocol
335
retobj.setProtocol(isAjp ? "AJP/1.3" : "HTTP/1.1");
336         // Set SSL
337
retobj.setSecure(isSSL);
338         retobj.setScheme(isSSL ? "https" : "http");
339         // Add the new instance to its parent component
340
// FIX ME - addConnector will fail
341
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
342         Service service = getService(pname);
343         service.addConnector(retobj);
344         
345         // Return the corresponding MBean name
346
ObjectName JavaDoc coname = retobj.getObjectName();
347         
348         return (coname.toString());
349     }
350
351
352     /**
353      * Create a new HttpsConnector
354      *
355      * @param parent MBean Name of the associated parent component
356      * @param address The IP address on which to bind
357      * @param port TCP port number to listen on
358      *
359      * @exception Exception if an MBean cannot be created or registered
360      */

361     public String JavaDoc createHttpsConnector(String JavaDoc parent, String JavaDoc address, int port)
362         throws Exception JavaDoc {
363         return createConnector(parent, address, port, false, true);
364     }
365
366     /**
367      * Create a new JDBC Realm.
368      *
369      * @param parent MBean Name of the associated parent component
370      *
371      * @exception Exception if an MBean cannot be created or registered
372      */

373     public String JavaDoc createJDBCRealm(String JavaDoc parent, String JavaDoc driverName,
374         String JavaDoc connectionName, String JavaDoc connectionPassword, String JavaDoc connectionURL)
375         throws Exception JavaDoc {
376
377         // Create a new JDBCRealm instance
378
JDBCRealm realm = new JDBCRealm();
379         realm.setDriverName(driverName);
380         realm.setConnectionName(connectionName);
381         realm.setConnectionPassword(connectionPassword);
382         realm.setConnectionURL(connectionURL);
383
384         // Add the new instance to its parent component
385
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
386         ContainerBase containerBase = getParentContainerFromParent(pname);
387         // Add the new instance to its parent component
388
containerBase.setRealm(realm);
389         // Return the corresponding MBean name
390
ObjectName JavaDoc oname = realm.getObjectName();
391
392         if (oname != null) {
393             return (oname.toString());
394         } else {
395             return null;
396         }
397
398     }
399
400
401     /**
402      * Create a new JNDI Realm.
403      *
404      * @param parent MBean Name of the associated parent component
405      *
406      * @exception Exception if an MBean cannot be created or registered
407      */

408     public String JavaDoc createJNDIRealm(String JavaDoc parent)
409         throws Exception JavaDoc {
410
411          // Create a new JNDIRealm instance
412
JNDIRealm realm = new JNDIRealm();
413
414         // Add the new instance to its parent component
415
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
416         ContainerBase containerBase = getParentContainerFromParent(pname);
417         // Add the new instance to its parent component
418
containerBase.setRealm(realm);
419         // Return the corresponding MBean name
420
ObjectName JavaDoc oname = realm.getObjectName();
421
422         if (oname != null) {
423             return (oname.toString());
424         } else {
425             return null;
426         }
427
428
429     }
430
431
432     /**
433      * Create a new Memory Realm.
434      *
435      * @param parent MBean Name of the associated parent component
436      *
437      * @exception Exception if an MBean cannot be created or registered
438      */

439     public String JavaDoc createMemoryRealm(String JavaDoc parent)
440         throws Exception JavaDoc {
441
442          // Create a new MemoryRealm instance
443
MemoryRealm realm = new MemoryRealm();
444
445         // Add the new instance to its parent component
446
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
447         ContainerBase containerBase = getParentContainerFromParent(pname);
448         // Add the new instance to its parent component
449
containerBase.setRealm(realm);
450         // Return the corresponding MBean name
451
ObjectName JavaDoc oname = realm.getObjectName();
452         if (oname != null) {
453             return (oname.toString());
454         } else {
455             return null;
456         }
457
458     }
459
460
461     /**
462      * Create a new Remote Address Filter Valve.
463      *
464      * @param parent MBean Name of the associated parent component
465      *
466      * @exception Exception if an MBean cannot be created or registered
467      */

468     public String JavaDoc createRemoteAddrValve(String JavaDoc parent)
469         throws Exception JavaDoc {
470
471         // Create a new RemoteAddrValve instance
472
RemoteAddrValve valve = new RemoteAddrValve();
473
474         // Add the new instance to its parent component
475
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
476         ContainerBase containerBase = getParentContainerFromParent(pname);
477         containerBase.addValve(valve);
478         ObjectName JavaDoc oname = valve.getObjectName();
479         return (oname.toString());
480
481     }
482
483
484      /**
485      * Create a new Remote Host Filter Valve.
486      *
487      * @param parent MBean Name of the associated parent component
488      *
489      * @exception Exception if an MBean cannot be created or registered
490      */

491     public String JavaDoc createRemoteHostValve(String JavaDoc parent)
492         throws Exception JavaDoc {
493
494         // Create a new RemoteHostValve instance
495
RemoteHostValve valve = new RemoteHostValve();
496
497         // Add the new instance to its parent component
498
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
499         ContainerBase containerBase = getParentContainerFromParent(pname);
500         containerBase.addValve(valve);
501         ObjectName JavaDoc oname = valve.getObjectName();
502         return (oname.toString());
503         
504     }
505
506
507     /**
508      * Create a new Request Dumper Valve.
509      *
510      * @param parent MBean Name of the associated parent component
511      *
512      * @exception Exception if an MBean cannot be created or registered
513      */

514     public String JavaDoc createRequestDumperValve(String JavaDoc parent)
515         throws Exception JavaDoc {
516
517         // Create a new RequestDumperValve instance
518
RequestDumperValve valve = new RequestDumperValve();
519
520         // Add the new instance to its parent component
521
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
522         ContainerBase containerBase = getParentContainerFromParent(pname);
523         containerBase.addValve(valve);
524         ObjectName JavaDoc oname = valve.getObjectName();
525         return (oname.toString());
526
527     }
528
529
530     /**
531      * Create a new Single Sign On Valve.
532      *
533      * @param parent MBean Name of the associated parent component
534      *
535      * @exception Exception if an MBean cannot be created or registered
536      */

537     public String JavaDoc createSingleSignOn(String JavaDoc parent)
538         throws Exception JavaDoc {
539
540         // Create a new SingleSignOn instance
541
SingleSignOn valve = new SingleSignOn();
542
543         // Add the new instance to its parent component
544
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
545         ContainerBase containerBase = getParentContainerFromParent(pname);
546         containerBase.addValve(valve);
547         ObjectName JavaDoc oname = valve.getObjectName();
548         return (oname.toString());
549
550     }
551     
552     
553    /**
554      * Create a new StandardContext.
555      *
556      * @param parent MBean Name of the associated parent component
557      * @param path The context path for this Context
558      * @param docBase Document base directory (or WAR) for this Context
559      *
560      * @exception Exception if an MBean cannot be created or registered
561      */

562     public String JavaDoc createStandardContext(String JavaDoc parent,
563                                         String JavaDoc path,
564                                         String JavaDoc docBase)
565         throws Exception JavaDoc {
566                                             
567         // XXX for backward compatibility. Remove it once supported by the admin
568
return
569             createStandardContext(parent,path,docBase,false,false,false,false);
570     }
571
572     /**
573      * Given a context path, get the config file name.
574      */

575     private String JavaDoc getConfigFile(String JavaDoc path) {
576         String JavaDoc basename = null;
577         if (path.equals("")) {
578             basename = "ROOT";
579         } else {
580             basename = path.substring(1).replace('/', '#');
581         }
582         return (basename);
583     }
584
585    /**
586      * Create a new StandardContext.
587      *
588      * @param parent MBean Name of the associated parent component
589      * @param path The context path for this Context
590      * @param docBase Document base directory (or WAR) for this Context
591      *
592      * @exception Exception if an MBean cannot be created or registered
593      */

594     public String JavaDoc createStandardContext(String JavaDoc parent,
595                                         String JavaDoc path,
596                                         String JavaDoc docBase,
597                                         boolean xmlValidation,
598                                         boolean xmlNamespaceAware,
599                                         boolean tldValidation,
600                                         boolean tldNamespaceAware)
601         throws Exception JavaDoc {
602
603         // Create a new StandardContext instance
604
StandardContext context = new StandardContext();
605         path = getPathStr(path);
606         context.setPath(path);
607         context.setDocBase(docBase);
608         context.setXmlValidation(xmlValidation);
609         context.setXmlNamespaceAware(xmlNamespaceAware);
610         context.setTldValidation(tldValidation);
611         context.setTldNamespaceAware(tldNamespaceAware);
612         
613         ContextConfig contextConfig = new ContextConfig();
614         context.addLifecycleListener(contextConfig);
615
616         // Add the new instance to its parent component
617
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
618         ObjectName JavaDoc deployer = new ObjectName JavaDoc(pname.getDomain()+
619                                              ":type=Deployer,host="+
620                                              pname.getKeyProperty("host"));
621         if(mserver.isRegistered(deployer)) {
622             String JavaDoc contextPath = context.getPath();
623             mserver.invoke(deployer, "addServiced",
624                            new Object JavaDoc [] {contextPath},
625                            new String JavaDoc [] {"java.lang.String"});
626             String JavaDoc configPath = (String JavaDoc)mserver.getAttribute(deployer,
627                                                              "configBaseName");
628             String JavaDoc baseName = getConfigFile(contextPath);
629             File JavaDoc configFile = new File JavaDoc(new File JavaDoc(configPath), baseName+".xml");
630             context.setConfigFile(configFile.getAbsolutePath());
631             mserver.invoke(deployer, "manageApp",
632                            new Object JavaDoc[] {context},
633                            new String JavaDoc[] {"org.apache.catalina.Context"});
634             mserver.invoke(deployer, "removeServiced",
635                            new Object JavaDoc [] {contextPath},
636                            new String JavaDoc [] {"java.lang.String"});
637         } else {
638             log.warn("Deployer not found for "+pname.getKeyProperty("host"));
639             Service service = getService(pname);
640             Engine engine = (Engine) service.getContainer();
641             Host host = (Host) engine.findChild(pname.getKeyProperty("host"));
642             host.addChild(context);
643         }
644
645         // Return the corresponding MBean name
646
ObjectName JavaDoc oname = context.getJmxName();
647
648         return (oname.toString());
649
650     }
651
652
653    /**
654      * Create a new StandardEngine.
655      *
656      * @param parent MBean Name of the associated parent component
657      * @param engineName Unique name of this Engine
658      * @param defaultHost Default hostname of this Engine
659      * @param serviceName Unique name of this Service
660      *
661      * @exception Exception if an MBean cannot be created or registered
662      */

663
664     public Vector JavaDoc createStandardEngineService(String JavaDoc parent,
665             String JavaDoc engineName, String JavaDoc defaultHost, String JavaDoc serviceName)
666         throws Exception JavaDoc {
667
668         // Create a new StandardService instance
669
StandardService service = new StandardService();
670         service.setName(serviceName);
671         // Create a new StandardEngine instance
672
StandardEngine engine = new StandardEngine();
673         engine.setName(engineName);
674         engine.setDefaultHost(defaultHost);
675         // Need to set engine before adding it to server in order to set domain
676
service.setContainer(engine);
677         // Add the new instance to its parent component
678
Server JavaDoc server = ServerFactory.getServer();
679         server.addService(service);
680         Vector JavaDoc onames = new Vector JavaDoc();
681         // FIXME service & engine.getObjectName
682
//ObjectName oname = engine.getObjectName();
683
ObjectName JavaDoc oname =
684             MBeanUtils.createObjectName(engineName, engine);
685         onames.add(0, oname);
686         //oname = service.getObjectName();
687
oname =
688             MBeanUtils.createObjectName(engineName, service);
689         onames.add(1, oname);
690         return (onames);
691
692     }
693
694
695     /**
696      * Create a new StandardHost.
697      *
698      * @param parent MBean Name of the associated parent component
699      * @param name Unique name of this Host
700      * @param appBase Application base directory name
701      * @param autoDeploy Should we auto deploy?
702      * @param deployOnStartup Deploy on server startup?
703      * @param deployXML Should we deploy Context XML config files property?
704      * @param unpackWARs Should we unpack WARs when auto deploying?
705      * @param xmlNamespaceAware Should we turn on/off XML namespace awareness?
706      * @param xmlValidation Should we turn on/off XML validation?
707      *
708      * @exception Exception if an MBean cannot be created or registered
709      */

710     public String JavaDoc createStandardHost(String JavaDoc parent, String JavaDoc name,
711                                      String JavaDoc appBase,
712                                      boolean autoDeploy,
713                                      boolean deployOnStartup,
714                                      boolean deployXML,
715                                      boolean unpackWARs,
716                                      boolean xmlNamespaceAware,
717                                      boolean xmlValidation)
718         throws Exception JavaDoc {
719
720         // Create a new StandardHost instance
721
StandardHost host = new StandardHost();
722         host.setName(name);
723         host.setAppBase(appBase);
724         host.setAutoDeploy(autoDeploy);
725         host.setDeployOnStartup(deployOnStartup);
726         host.setDeployXML(deployXML);
727         host.setUnpackWARs(unpackWARs);
728         host.setXmlNamespaceAware(xmlNamespaceAware);
729         host.setXmlValidation(xmlValidation);
730     
731         // add HostConfig for active reloading
732
HostConfig hostConfig = new HostConfig();
733         host.addLifecycleListener(hostConfig);
734
735         // Add the new instance to its parent component
736
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
737         Service service = getService(pname);
738         Engine engine = (Engine) service.getContainer();
739         engine.addChild(host);
740
741         // Return the corresponding MBean name
742
return (host.getObjectName().toString());
743
744     }
745
746
747     /**
748      * Create a new StandardManager.
749      *
750      * @param parent MBean Name of the associated parent component
751      *
752      * @exception Exception if an MBean cannot be created or registered
753      */

754     public String JavaDoc createStandardManager(String JavaDoc parent)
755         throws Exception JavaDoc {
756
757         // Create a new StandardManager instance
758
StandardManager manager = new StandardManager();
759
760         // Add the new instance to its parent component
761
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
762         ContainerBase containerBase = getParentContainerFromParent(pname);
763         if (containerBase != null) {
764             containerBase.setManager(manager);
765         }
766         ObjectName JavaDoc oname = manager.getObjectName();
767         if (oname != null) {
768             return (oname.toString());
769         } else {
770             return null;
771         }
772         
773     }
774
775
776     /**
777      * Create a new StandardService.
778      *
779      * @param parent MBean Name of the associated parent component
780      * @param name Unique name of this StandardService
781      *
782      * @exception Exception if an MBean cannot be created or registered
783      */

784     public String JavaDoc createStandardService(String JavaDoc parent, String JavaDoc name, String JavaDoc domain)
785         throws Exception JavaDoc {
786
787         // Create a new StandardService instance
788
StandardService service = new StandardService();
789         service.setName(name);
790
791         // Add the new instance to its parent component
792
Server JavaDoc server = ServerFactory.getServer();
793         server.addService(service);
794
795         // Return the corresponding MBean name
796
return (service.getObjectName().toString());
797
798     }
799
800
801
802     /**
803      * Create a new UserDatabaseRealm.
804      *
805      * @param parent MBean Name of the associated parent component
806      * @param resourceName Global JNDI resource name of the associated
807      * UserDatabase
808      *
809      * @exception Exception if an MBean cannot be created or registered
810      */

811     public String JavaDoc createUserDatabaseRealm(String JavaDoc parent, String JavaDoc resourceName)
812         throws Exception JavaDoc {
813
814          // Create a new UserDatabaseRealm instance
815
UserDatabaseRealm realm = new UserDatabaseRealm();
816         realm.setResourceName(resourceName);
817         
818         // Add the new instance to its parent component
819
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
820         ContainerBase containerBase = getParentContainerFromParent(pname);
821         // Add the new instance to its parent component
822
containerBase.setRealm(realm);
823         // Return the corresponding MBean name
824
ObjectName JavaDoc oname = realm.getObjectName();
825         // FIXME getObjectName() returns null
826
//ObjectName oname =
827
// MBeanUtils.createObjectName(pname.getDomain(), realm);
828
if (oname != null) {
829             return (oname.toString());
830         } else {
831             return null;
832         }
833
834     }
835
836
837     /**
838      * Create a new Web Application Loader.
839      *
840      * @param parent MBean Name of the associated parent component
841      *
842      * @exception Exception if an MBean cannot be created or registered
843      */

844     public String JavaDoc createWebappLoader(String JavaDoc parent)
845         throws Exception JavaDoc {
846
847         // Create a new WebappLoader instance
848
WebappLoader loader = new WebappLoader();
849
850         // Add the new instance to its parent component
851
ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
852         ContainerBase containerBase = getParentContainerFromParent(pname);
853         if (containerBase != null) {
854             containerBase.setLoader(loader);
855         }
856         // FIXME add Loader.getObjectName
857
//ObjectName oname = loader.getObjectName();
858
ObjectName JavaDoc oname =
859             MBeanUtils.createObjectName(pname.getDomain(), loader);
860         return (oname.toString());
861         
862     }
863
864
865     /**
866      * Remove an existing Connector.
867      *
868      * @param name MBean Name of the component to remove
869      *
870      * @exception Exception if a component cannot be removed
871      */

872     public void removeConnector(String JavaDoc name) throws Exception JavaDoc {
873
874         // Acquire a reference to the component to be removed
875
ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
876         Server JavaDoc server = ServerFactory.getServer();
877         Service service = getService(oname);
878         String JavaDoc port = oname.getKeyProperty("port");
879         //String address = oname.getKeyProperty("address");
880

881         Connector conns[] = (Connector[]) service.findConnectors();
882
883         for (int i = 0; i < conns.length; i++) {
884             String JavaDoc connAddress = String.valueOf(conns[i].getProperty("address"));
885             String JavaDoc connPort = ""+conns[i].getPort();
886
887             // if (((address.equals("null")) &&
888
if ((connAddress==null) && port.equals(connPort)) {
889                 service.removeConnector(conns[i]);
890                 conns[i].destroy();
891                 break;
892             }
893             // } else if (address.equals(connAddress))
894
if (port.equals(connPort)) {
895                 // Remove this component from its parent component
896
service.removeConnector(conns[i]);
897                 conns[i].destroy();
898                 break;
899             }
900         }
901
902     }
903
904
905     /**
906      * Remove an existing Context.
907      *
908      * @param contextName MBean Name of the comonent to remove
909      *
910      * @exception Exception if a component cannot be removed
911      */

912     public void removeContext(String JavaDoc contextName) throws Exception JavaDoc {
913
914         // Acquire a reference to the component to be removed
915
ObjectName JavaDoc oname = new ObjectName JavaDoc(contextName);
916         String JavaDoc domain = oname.getDomain();
917         StandardService service = (StandardService) getService(oname);
918
919         Engine engine = (Engine) service.getContainer();
920         String JavaDoc name = oname.getKeyProperty("name");
921         name = name.substring(2);
922         int i = name.indexOf("/");
923         String JavaDoc hostName = name.substring(0,i);
924         String JavaDoc path = name.substring(i);
925         ObjectName JavaDoc deployer = new ObjectName JavaDoc(domain+":type=Deployer,host="+
926                                              hostName);
927         String JavaDoc pathStr = getPathStr(path);
928         if(mserver.isRegistered(deployer)) {
929             mserver.invoke(deployer,"addServiced",
930                            new Object JavaDoc[]{pathStr},
931                            new String JavaDoc[] {"java.lang.String"});
932             mserver.invoke(deployer,"unmanageApp",
933                            new Object JavaDoc[] {pathStr},
934                            new String JavaDoc[] {"java.lang.String"});
935             mserver.invoke(deployer,"removeServiced",
936                            new Object JavaDoc[] {pathStr},
937                            new String JavaDoc[] {"java.lang.String"});
938         } else {
939             log.warn("Deployer not found for "+hostName);
940             Host host = (Host) engine.findChild(hostName);
941             Context context = (Context) host.findChild(pathStr);
942             // Remove this component from its parent component
943
host.removeChild(context);
944             if(context instanceof StandardContext)
945             try {
946                 ((StandardContext)context).destroy();
947             } catch (Exception JavaDoc e) {
948                 log.warn("Error during context [" + context.getName() + "] destroy ", e);
949            }
950    
951         }
952
953     }
954
955
956     /**
957      * Remove an existing Host.
958      *
959      * @param name MBean Name of the comonent to remove
960      *
961      * @exception Exception if a component cannot be removed
962      */

963     public void removeHost(String JavaDoc name) throws Exception JavaDoc {
964
965         // Acquire a reference to the component to be removed
966
ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
967         String JavaDoc hostName = oname.getKeyProperty("host");
968         Service service = getService(oname);
969         Engine engine = (Engine) service.getContainer();
970         Host host = (Host) engine.findChild(hostName);
971
972         // Remove this component from its parent component
973
if(host!=null) {
974             if(host instanceof StandardHost)
975                 ((StandardHost)host).destroy();
976             else
977                 engine.removeChild(host);
978         }
979
980     }
981
982
983     /**
984      * Remove an existing Loader.
985      *
986      * @param name MBean Name of the comonent to remove
987      *
988      * @exception Exception if a component cannot be removed
989      */

990     public void removeLoader(String JavaDoc name) throws Exception JavaDoc {
991
992         ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
993         // Acquire a reference to the component to be removed
994
ContainerBase container = getParentContainerFromChild(oname);
995         container.setLoader(null);
996         
997     }
998
999
1000    /**
1001     * Remove an existing Manager.
1002     *
1003     * @param name MBean Name of the comonent to remove
1004     *
1005     * @exception Exception if a component cannot be removed
1006     */

1007    public void removeManager(String JavaDoc name) throws Exception JavaDoc {
1008
1009        ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
1010        // Acquire a reference to the component to be removed
1011
ContainerBase container = getParentContainerFromChild(oname);
1012        container.setManager(null);
1013
1014    }
1015
1016
1017    /**
1018     * Remove an existing Realm.
1019     *
1020     * @param name MBean Name of the comonent to remove
1021     *
1022     * @exception Exception if a component cannot be removed
1023     */

1024    public void removeRealm(String JavaDoc name) throws Exception JavaDoc {
1025
1026        ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
1027        // Acquire a reference to the component to be removed
1028
ContainerBase container = getParentContainerFromChild(oname);
1029        container.setRealm(null);
1030    }
1031
1032
1033    /**
1034     * Remove an existing Service.
1035     *
1036     * @param name MBean Name of the component to remove
1037     *
1038     * @exception Exception if a component cannot be removed
1039     */

1040    public void removeService(String JavaDoc name) throws Exception JavaDoc {
1041
1042        // Acquire a reference to the component to be removed
1043
ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
1044        String JavaDoc serviceName = oname.getKeyProperty("serviceName");
1045        Server JavaDoc server = ServerFactory.getServer();
1046        Service service = server.findService(serviceName);
1047
1048        // Remove this component from its parent component
1049
server.removeService(service);
1050
1051    }
1052
1053
1054    /**
1055     * Remove an existing Valve.
1056     *
1057     * @param name MBean Name of the comonent to remove
1058     *
1059     * @exception Exception if a component cannot be removed
1060     */

1061    public void removeValve(String JavaDoc name) throws Exception JavaDoc {
1062
1063        // Acquire a reference to the component to be removed
1064
ObjectName JavaDoc oname = new ObjectName JavaDoc(name);
1065        ContainerBase container = getParentContainerFromChild(oname);
1066        String JavaDoc sequence = oname.getKeyProperty("seq");
1067        Valve[] valves = (Valve[])container.getValves();
1068        for (int i = 0; i < valves.length; i++) {
1069            ObjectName JavaDoc voname = ((ValveBase) valves[i]).getObjectName();
1070            if (voname.equals(oname)) {
1071                container.removeValve(valves[i]);
1072            }
1073        }
1074    }
1075
1076}
1077
1078
Popular Tags