KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > model > J2EEDomainMdl


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 package com.sun.enterprise.management.model;
25
26 import java.util.*;
27 import javax.management.*;
28 import javax.naming.*;
29 import javax.naming.event.*;
30
31 public class J2EEDomainMdl extends J2EEEventProviderMOMdl { // FIXME: implements NotificationListener {
32
public static String JavaDoc DOMAINNAME;
33     private static ObjectName jmImpl;
34     private boolean debug = false;
35     
36   
37     static {
38         try {
39             DOMAINNAME = java.net.InetAddress.getLocalHost().getHostName();
40             jmImpl = new ObjectName("JMImplementation:type=MBeanServerDelegate");
41         } catch (Exception JavaDoc e) {
42             DOMAINNAME = "J2EE_1.3.1_RI";
43         }
44     }
45
46     private static String JavaDoc MANAGED_OBJECT_TYPE = "J2EEDomain";
47
48     private String JavaDoc [] eventTypes = new String JavaDoc [] {"j2ee.object.created","j2ee.object.deleted"};
49
50     public J2EEDomainMdl() {
51         super(DOMAINNAME, false, false);
52         /*
53         try {
54             getMBeanServer().addNotificationListener(jmImpl, this, null, "J2EEDomain");
55         } catch (InstanceNotFoundException e) {
56             e.printStackTrace();
57         }
58          */

59     }
60     public J2EEDomainMdl(String JavaDoc domainname) {
61         super(domainname, false, false);
62         DOMAINNAME = domainname;
63         /*
64         try {
65             getMBeanServer().addNotificationListener(jmImpl, this, null, "J2EEDomain");
66         } catch (InstanceNotFoundException e) {
67             e.printStackTrace();
68         }
69          */

70     }
71
72 /**
73 * A list of all J2EE Servers in this domain.
74 * @supplierCardinality 0..*
75 */

76     public String JavaDoc[] getservers(){
77         Set servers = findNames("j2eeType=J2EEServer");
78
79         Iterator it = servers.iterator();
80         String JavaDoc [] ret = new String JavaDoc[servers.size()];
81         int i =0;
82         while(it.hasNext()) {
83             ret[i++] = ((ObjectName)it.next()).toString();
84         }
85         return ret;
86     }
87
88     public String JavaDoc[] geteventTypes(){
89         return eventTypes;
90     }
91
92     public void handleNotification(Notification n, Object JavaDoc handback) {
93         /*
94         Need to foward the notifications of type jmx.mbean.* as j2ee.object.*
95         */

96         if (debug) {
97         System.out.println(handback);
98         System.out.println("message: " + n.getMessage());
99         System.out.println("sequence: " + n.getSequenceNumber());
100         System.out.println("source: " + n.getSource());
101         System.out.println("timestamp: " + n.getTimeStamp());
102         System.out.println("type: " + n.getType());
103         System.out.println("user data: " + n.getUserData());
104         System.out.println("");
105         }
106
107         if(n instanceof MBeanServerNotification) {
108             MBeanServerNotification n2 = null;
109             if (n.getType().equalsIgnoreCase("jmx.mbean.registered")) {
110                 ObjectName oname = ((MBeanServerNotification)n).getMBeanName();
111                 if (debug) System.out.println("MBean name: " + oname);
112                 if (!((String JavaDoc)oname.getKeyProperty("name")).equals(getname())) { // don't listen to self!
113
try {
114                         Boolean JavaDoc b = (Boolean JavaDoc)getMBSUtility().getAttribute(oname, "eventProvider");
115                         if (b.booleanValue()){
116                             if (debug) System.out.println("J2EEDomain adding myself as listener to " + oname);
117                             //getMBeanServer().addNotificationListener(oname, this, null, getname());
118
//FIXME
119
}
120                     } catch (Exception JavaDoc e) {
121                         if (debug) e.printStackTrace();
122                     }
123                 }
124                 n2 = new MBeanServerNotification("j2ee.object.created", getobjectName(), n.getSequenceNumber(), oname);
125             } else if (n.getType().equalsIgnoreCase("jmx.mbean.unregistered")) {
126                 n2 = new MBeanServerNotification("j2ee.object.deleted", getobjectName(), n.getSequenceNumber(), ((MBeanServerNotification)n).getMBeanName());
127             }
128             if (n2 != null) {
129                 sendNotification(n2);
130                 return;
131             }
132         }
133         if (debug) System.out.println("Forwarding original notification " + n);
134         sendNotification(n);
135     }
136
137     /**
138      * The type of the J2EEManagedObject as specified by JSR77. The class that implements a specific type must override this method and return the appropriate type string.
139      */

140     public String JavaDoc getj2eeType() {
141         return MANAGED_OBJECT_TYPE;
142     }
143     
144     /**
145      * The name of the J2EEManagedObject. All managed objects must have a unique name within the context of the management
146      * domain. The name must not be null.
147      */

148     public String JavaDoc getobjectName() {
149         Set s = findNames("j2eeType="+getj2eeType()+",name="+DOMAINNAME);
150         Object JavaDoc [] objs = s.toArray();
151         if (objs.length > 0) {
152             String JavaDoc name = ((ObjectName)objs[0]).toString();
153             return name;
154         } else {
155             return null;
156         }
157     }
158     
159 }
160
Popular Tags