KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > JmxConnectorMBean


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  * $Id: JmxConnectorMBean.java,v 1.2 2006/03/18 00:34:36 kravtch Exp $
26  */

27
28 package com.sun.enterprise.admin.mbeans;
29
30 //JMX imports
31
import javax.management.AttributeNotFoundException JavaDoc;
32 import javax.management.ReflectionException JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34
35 import com.sun.enterprise.admin.jmx.remote.server.rmi.JmxServiceUrlFactory;
36 import javax.management.remote.JMXServiceURL JavaDoc;
37
38 // commons imports
39
import com.sun.enterprise.util.i18n.StringManager;
40
41 //admin/config imports
42
import com.sun.enterprise.admin.config.BaseConfigMBean;
43 import com.sun.enterprise.admin.config.MBeanConfigException;
44
45 //config imports
46
import com.sun.enterprise.config.serverbeans.JmxConnector;
47 import com.sun.enterprise.config.ConfigException;
48
49 // java.net
50
import java.net.InetAddress JavaDoc;
51 import java.net.UnknownHostException JavaDoc;
52
53 public class JmxConnectorMBean extends BaseConfigMBean
54 {
55     final private static String JavaDoc HOST_HOLDER_VALUE = "<host-name>";
56     final private static int PORT_HOLDER_VALUE = 12345;
57
58     private static final StringManager localStrings =
59             StringManager.getManager(DomainMBean.class);
60     
61     private String JavaDoc getJMXServiceURL()
62        throws UnknownHostException JavaDoc
63     {
64         
65         //first create template
66
JMXServiceURL JavaDoc url =
67         JmxServiceUrlFactory.forJconsoleOverRmiWithJndiInAppserver(
68                 HOST_HOLDER_VALUE, PORT_HOLDER_VALUE);
69         String JavaDoc strUrl = url.toString();
70         //now - modify it
71
JmxConnector bean = (JmxConnector)this.getBaseConfigBean();
72         String JavaDoc host = bean.getAddress();
73         if(host!=null && host.trim().equals("0.0.0.0"))
74         {
75            //host = InetAddress.getLocalHost().getHostName();
76
host = null; //left <host-name> placeholder in url
77
}
78         if(host!=null)
79             strUrl = strUrl.replaceAll(HOST_HOLDER_VALUE, host);
80         try {
81            int port = Integer.parseInt(bean.getPort());
82            strUrl = strUrl.replaceAll(String.valueOf(PORT_HOLDER_VALUE),
83                    String.valueOf(port));
84         } catch (Exception JavaDoc e)
85         {
86            strUrl = strUrl.replaceAll(String.valueOf(PORT_HOLDER_VALUE),
87                    "<port>");
88         }
89         
90         return strUrl;
91     }
92     
93     /** overriding of the super getAttribute()
94      * to provide "in mbean only" JMXServiceURL attribute
95      **/

96     public Object JavaDoc getAttribute(String JavaDoc name)
97        throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
98
99         if(name.equals(JMX_SERVICE_URL_ATTRNAME))
100         {
101             try {
102                 return getJMXServiceURL();
103             } catch (UnknownHostException JavaDoc uhe) {
104                 throw new MBeanException JavaDoc(uhe);
105             }
106         }
107         return super.getAttribute(name);
108     }
109     
110     public static final String JavaDoc JMX_SERVICE_URL_ATTRNAME = "JMXServiceURL";
111 }
112
Popular Tags