KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > server > rmi > JmxServiceUrlFactory


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  * JmxServiceUrlFactory.java
26  * Indentation Information:
27  * 0. Please (try to) preserve these settings.
28  * 1. No tabs are used, all spaces.
29  * 2. In vi/vim -
30  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
31  * 3. In S1 Studio -
32  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
33  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = True.
34  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
35  * Unit Testing Information:
36  * 0. Is Standard Unit Test Written (y/n):
37  * 1. Unit Test Location: (The instructions should be in the Unit Test Class itself).
38  */

39
40 package com.sun.enterprise.admin.jmx.remote.server.rmi;
41 import javax.management.remote.JMXServiceURL JavaDoc;
42 import java.net.MalformedURLException JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /** A factory class to create the various JMXServiceURLs in various cases. This
46  * is useful because there are many ways of creating the JMXServiceURL for
47  * an intended connector server and client. The methods in this class can be
48  * used precisely for given case.
49  */

50
51 public class JmxServiceUrlFactory {
52     /** Field */
53     public static final String JavaDoc RMI_JSR160_CS_JNDI_SUFFIX = "/management/rmi-jmx-connector";
54     public static final String JavaDoc JCONSOLE_RMI_JSR160_CS_JNDI_SUFFIX = "/jmxrmi";
55     //hard-coding the logger name.
56
private static final Logger JavaDoc logger = Logger.getLogger("javax.enterprise.system.tools.admin");
57     private JmxServiceUrlFactory() {
58     }
59     
60     /** This API should be called when the <code> ConnectorServer </code> and
61      * following is true.
62      * <ul>
63      * <li> The protocol is RMI over JRMP. </li>
64      * <li> There is no JNDI involved.
65      * </ul>
66      *
67      * After the ConnectorServer is created with this URL, its address will
68      * contain the actual base-64 encoded stub that it can be used by ConnectorClients.
69      *
70      * In order to connect to such a ConnectorServer the client has to be provide
71      * the stub to form the JMXServiceURL.
72      * Bottom line: This API should be called when creating ConnectorServer.
73      * @see JMXServiceURL
74      */

75     public static JMXServiceURL JavaDoc forRmiJrmpWithoutStub(final int port) {
76         JMXServiceURL JavaDoc url;
77         try {
78             final String JavaDoc s = "service:jmx:rmi://" + localhost() + ":" + port;
79             url = new JMXServiceURL JavaDoc(s);
80             final String JavaDoc msg = "JmxServiceUrlFactory=>JMXServiceURL is: " + url;
81             logger.fine(msg);
82         }
83         catch (MalformedURLException JavaDoc m) {
84             url = null;
85             //squelching the exception purposely, as I want to be sure.
86
}
87         assert (url != null): "Something seriously wrong, can't form the JMXServiceURL";
88         return ( url );
89     }
90
91     /** This API should be called when the <code> ConnectorServer </code> and
92      * following is true.
93      * <ul>
94      * <li> The protocol is RMI over IIOP. </li>
95      * <li> There is no JNDI involved.
96      * </ul>
97      *
98      * After the ConnectorServer is created with this URL, its address will
99      * contain the actual base-64 encoded IOR that it can be used by ConnectorClients.
100
101      * In order to connect to such a ConnectorServer the client has to be provide
102      * the IIOP stub to form the JMXServiceURL.
103      * Bottom line: This API should be called when creating ConnectorServer.
104      * @see JMXServiceURL
105      */

106
107     public static JMXServiceURL JavaDoc forRmiIiopWithoutIor(final int port) {
108         JMXServiceURL JavaDoc url;
109         try {
110             final String JavaDoc s = "service:jmx:iiop://" + localhost() + ":" + port;
111             url = new JMXServiceURL JavaDoc(s);
112             final String JavaDoc msg = "JMXServiceURL is: " + url;
113             logger.fine(msg);
114         }
115         catch (MalformedURLException JavaDoc m) {
116             url = null;
117             //squelching the exception purposely, as I want to be sure.
118
}
119         assert (url != null): "Something seriously wrong, can't form the JMXServiceURL";
120         return ( url );
121     }
122     
123     /** This is a method used by both JSR 160 connector server and connector clients
124      * within the application server. <b> This API should be used by all the components within
125      * the application server. </b> Note the following points about the returned JMXServiceURL.
126      * <ul>
127      * <li> Naming Service's host and port are provided. </li>
128      * <li> The JNDI name of the stub is fixed. </li>
129      * <li> This is the "ignored-host" kind of form. It always returns the URL which looks like
130      * <code> service:jmx:rmi:///jndi/rmi://host:port/<Appserver's JNDI name></li>
131      * </ul>
132      * This is a symmetric form of URL.
133      * @param host String representing the host name
134      * @parm port int representing the port of naming service like rmi registry
135      * @see JMXServiceUrlFactory#RMI_JSR160_CS_JNDI_SUFFIX
136      */

137
138     public static JMXServiceURL JavaDoc forRmiWithJndiInAppserver(final String JavaDoc host, final int port) {
139         return ( forRmiWithJndi(host, port, RMI_JSR160_CS_JNDI_SUFFIX, false) );
140     }
141     
142     public static JMXServiceURL JavaDoc forJconsoleOverRmiWithJndiInAppserver(final String JavaDoc host, final int port) {
143         return ( forRmiWithJndi(host, port, JCONSOLE_RMI_JSR160_CS_JNDI_SUFFIX, false) );
144     }
145     
146     /** A convenience method to create the JMXServiceURL for both Connector Client and
147      * Connector Server. This API should be used when:
148      * <ul>
149      * <li> JNDI is used to obtain the stub. This must start with '/'. Must not be null. </li>
150      * <li> Naming Service's host and port are known. </li>
151      * <li> The JNDI name of the stub with which it is registered is known. </li>
152      * </ul>
153      * This is a symmetric form of URL.
154      * @param host String representing the host name
155      * @parm port int representing the port of naming service like rmi registry
156      * @param jn String representing the JNDI name of the stub. This is known to the client
157      * or server wants to specify it.
158      * @param addHost boolean indicating the URL should have the host name or not. If
159      * false what is returned is an an ignored-host form.
160      */

161     private static JMXServiceURL JavaDoc forRmiWithJndi(final String JavaDoc host, final int port, final String JavaDoc jn, final boolean addHost) {
162         JMXServiceURL JavaDoc url;
163         String JavaDoc hps = ""; //empty String
164
if (host == null || jn == null) {
165             throw new IllegalArgumentException JavaDoc("Null Argument");
166         }
167         if (! jn.startsWith("/"))
168             throw new IllegalArgumentException JavaDoc("jndi-name must start with a /");
169         if (addHost) {
170             hps = host + ":" + port;
171         }
172         try {
173             final String JavaDoc s = "service:jmx:rmi://" + hps + "/jndi/rmi://" + host + ":" + port + jn;
174             url = new JMXServiceURL JavaDoc(s);
175             final String JavaDoc msg = "JMXServiceURL is: " + url;
176             logger.fine(msg);
177         }
178         catch (MalformedURLException JavaDoc m) {
179             url = null;
180             //squelching the exception purposely, as I want to be sure. No real need to propagate.
181
}
182         assert (url != null): "Something seriously wrong, can't form the JMXServiceURL";
183         return ( url );
184     }
185     /** A variant of forRmiWithJndi(String, int, String, boolean).
186      * Returns the ignored host form with localhost as the naming host for
187      * the given jndi name.
188      */

189     private static JMXServiceURL JavaDoc forRmiWithJndi(final int port, final String JavaDoc jn) {
190         return ( forRmiWithJndi(localhost(), port, jn, false) );
191     }
192
193     /** A variant of forRmiWithJndi(String, int, String, boolean).
194      * Returns the System Jmx Connector address at the given port. The jndi
195      * name is fixed by the field RMI_JSR160_CS_JNDI_SUFFIX.
196      */

197     private static JMXServiceURL JavaDoc forRmiWithJndi(final int port) {
198         return ( forRmiWithJndi(port, RMI_JSR160_CS_JNDI_SUFFIX) );
199     }
200     /** Returns the JMXServiceURL for JMXMP and given host and port.
201      * The host may not be null.
202      */

203     public static JMXServiceURL JavaDoc forJmxmp(final String JavaDoc host, final int port) {
204         JMXServiceURL JavaDoc url;
205         if (host == null)
206             throw new IllegalArgumentException JavaDoc("Null Host");
207         try {
208             url = new JMXServiceURL JavaDoc("jmxmp", host, port);
209             final String JavaDoc msg = "JMXServiceURL is: " + url;
210             logger.fine(msg);
211         }
212         catch (MalformedURLException JavaDoc m) {
213             url = null;
214             //squelching the exception purposely, as I want to be sure.
215
}
216         assert (url != null): "Something seriously wrong, can't form the JMXServiceURL";
217         return ( url );
218     }
219     
220     public static JMXServiceURL JavaDoc forJmxmp(final int port) {
221         return ( forJmxmp(localhost(), port) );
222     }
223     
224     /*package private */static String JavaDoc localhost() throws RuntimeException JavaDoc {
225         String JavaDoc h;
226         try {
227             h = java.net.InetAddress.getLocalHost().getCanonicalHostName();
228         }
229         catch (java.net.UnknownHostException JavaDoc e) {
230             h = "localhost";
231         }
232         return ( h );
233     }
234     
235     private static String JavaDoc concat(final String JavaDoc s1, final String JavaDoc s2, final String JavaDoc s3, final String JavaDoc s4) {
236         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
237         if (s1 != null)
238             sb.append(s1);
239         if (s2 != null)
240             sb.append(s2);
241         if (s3 != null)
242             sb.append(s3);
243         if (s4 != null)
244             sb.append(s4);
245         return ( sb.toString() );
246     }
247 }
Popular Tags