KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > custom > BasicCustomMBeanConfigQueries


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.enterprise.admin.mbeans.custom;
26
27 import com.sun.enterprise.admin.common.MBeanServerFactory;
28 import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
29 import com.sun.enterprise.admin.server.core.AdminService;
30 import com.sun.enterprise.admin.target.Target;
31 import com.sun.enterprise.admin.target.TargetBuilder;
32 import com.sun.enterprise.config.ConfigContext;
33 import com.sun.enterprise.config.serverbeans.Mbean;
34 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
35 import com.sun.enterprise.config.serverbeans.ServerTags;
36 import com.sun.enterprise.util.SystemPropertyConstants;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39 import javax.management.MBeanServerConnection JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41
42
43 public class BasicCustomMBeanConfigQueries implements CustomMBeanConfigQueries {
44     
45     protected final ConfigContext acc;
46     /** Creates a new instance of BasicCustomMBeanConfigQueries */
47     public BasicCustomMBeanConfigQueries() {
48         this.acc = MBeanRegistryFactory.getAdminContext().getAdminConfigContext();
49     }
50
51     public boolean existsMBean(String JavaDoc target, String JavaDoc name) throws CustomMBeanException {
52         final List JavaDoc<String JavaDoc> names = this.listMBeanNames(target);
53         boolean exists = false;
54         for (String JavaDoc aName : names) {
55             if (aName.equals(name)) {
56                 exists = true;
57                 break;
58             }
59         }
60         return ( exists );
61     }
62
63     public boolean isMBeanEnabled(String JavaDoc target, String JavaDoc name) throws CustomMBeanException {
64         boolean enabled = false;
65         if (!existsMBean(target, name)) {
66             final String JavaDoc msg = CMBStrings.get("MBeanNotFound", name, target);
67             throw new CustomMBeanException(msg);
68         }
69         final List JavaDoc<ObjectName JavaDoc> ons = this.listMBeanConfigObjectNames(target);
70         //get the attribute called "enabled" on these ObjectNames.
71
try {
72             final MBeanServerConnection JavaDoc mbsc = MBeanServerFactory.getMBeanServer(); // best to be prepared toward remoteness
73
final String JavaDoc noe = ServerTags.ENABLED;
74             for (ObjectName JavaDoc on : ons) {
75                 final String JavaDoc s = (String JavaDoc) mbsc.getAttribute(on, noe);
76                 if (Boolean.valueOf(s).booleanValue()) {
77                     enabled = true;
78                     break;
79                 }
80             }
81             return ( enabled );
82         } catch (final Exception JavaDoc e) {
83             throw new CustomMBeanException (e);
84         }
85     }
86
87     public List JavaDoc<ObjectName JavaDoc> listMBeanConfigObjectNames(String JavaDoc target) throws CustomMBeanException {
88         Target t = null;
89         try {
90             t = TargetBuilder.INSTANCE.createTarget(target, this.acc);
91         } catch (final Exception JavaDoc e) {
92             throw new CustomMBeanException(e);
93         }
94         return ( this.listMBeanConfigObjectNamesForServer(t.getName()) );
95     }
96
97     public List JavaDoc<ObjectName JavaDoc> listMBeanConfigObjectNames(String JavaDoc target, int type, boolean state) throws CustomMBeanException {
98         throw new UnsupportedOperationException JavaDoc(CMBStrings.get("NYI", "com.sun.enterprise.admin.mbeans.custom.BasicCustomMBeanConfigQueries.listMBeanConfigObjectNames"));
99     }
100
101     public List JavaDoc<String JavaDoc> listMBeanNames(String JavaDoc target) throws CustomMBeanException {
102         final List JavaDoc<ObjectName JavaDoc> ons = this.listMBeanConfigObjectNames(target);
103         final List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc> ();
104         //get the attribute called "name" on these ObjectNames.
105
try {
106             final MBeanServerConnection JavaDoc mbsc = MBeanServerFactory.getMBeanServer(); // best to be prepared toward remoteness
107
final String JavaDoc noa = ServerTags.NAME;
108             for (ObjectName JavaDoc on : ons) {
109                 final String JavaDoc voa = (String JavaDoc) mbsc.getAttribute(on, noa);
110                 names.add(voa);
111             }
112             return ( names );
113         } catch(final Exception JavaDoc e) {
114             throw new CustomMBeanException(e);
115         }
116     }
117     
118     /** Method to get the ObjectNames of the Config MBeans that correspond to custom-mbean-definitions
119      * that are referenced from a server instance. This method does bulk of the work in this class.
120      */

121     protected List JavaDoc<ObjectName JavaDoc> listMBeanConfigObjectNamesForServer(final String JavaDoc s) throws RuntimeException JavaDoc {
122         //when here, assume that the server exists, with the name "s"
123
try {
124             final List JavaDoc<Mbean> refdMbeans = ServerBeansFactory.getReferencedMBeans(acc, s);
125             return ( this.mbeans2ConfigMBeanObjectNames(refdMbeans) );
126         } catch (final Exception JavaDoc e) {
127             throw new RuntimeException JavaDoc(e);
128         }
129     }
130     protected List JavaDoc<ObjectName JavaDoc> mbeans2ConfigMBeanObjectNames(List JavaDoc <Mbean> mbeans) throws RuntimeException JavaDoc {
131         try {
132             final List JavaDoc<ObjectName JavaDoc> ons = new ArrayList JavaDoc<ObjectName JavaDoc> ();
133             for (Mbean m : mbeans) {
134                 final String JavaDoc domain = AdminService.PRIVATE_MBEAN_DOMAIN_NAME;
135                 final ObjectName JavaDoc on = MBeanRegistryFactory.getAdminMBeanRegistry().getObjectNameForConfigBean(m, domain);
136                 ons.add(on);
137             }
138             return ( ons ) ;
139         } catch (final Exception JavaDoc e) {
140             throw new RuntimeException JavaDoc(e);
141         }
142     }
143 }
144
Popular Tags