Source Code Cross Referenced for JMXRemoteHelper.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » jmx » commons » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.jmx.commons 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 2006 Bull S.A.S.
004:         * Contact: jonas@objectweb.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: JMXRemoteHelper.java 9268 2006-07-31 16:02:56Z pelletib $
023:         * --------------------------------------------------------------------------
024:         */package org.objectweb.jonas.jmx.commons;
025:
026:        import java.io.IOException;
027:        import java.net.MalformedURLException;
028:        import java.util.HashMap;
029:        import java.util.Map;
030:        import java.util.Set;
031:
032:        import javax.management.InstanceAlreadyExistsException;
033:        import javax.management.MBeanRegistrationException;
034:        import javax.management.MBeanServerConnection;
035:        import javax.management.MalformedObjectNameException;
036:        import javax.management.NotCompliantMBeanException;
037:        import javax.management.ObjectInstance;
038:        import javax.management.ObjectName;
039:        import javax.management.remote.JMXConnector;
040:        import javax.management.remote.JMXConnectorFactory;
041:        import javax.management.remote.JMXConnectorServer;
042:        import javax.management.remote.JMXConnectorServerFactory;
043:        import javax.management.remote.JMXServiceURL;
044:
045:        import org.objectweb.jonas.cluster.daemon.ClusterDaemonTools;
046:
047:        /**
048:         * This helper class allow to start a JMX remote connector allowing to connect remote applications.
049:         * This could be for example a JSR88 provider.
050:         * @author Florent Benoit
051:         */
052:        public final class JMXRemoteHelper {
053:
054:            /**
055:             * JMX connector (server side).
056:             */
057:            private static JMXConnectorServer jmxConnectorServer = null;
058:
059:            /**
060:             * Default URL.
061:             */
062:            private static final String DEFAULT_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jonasConnector";
063:
064:            /**
065:             * ObjectName for the connector.
066:             */
067:            private static final String DEFAULT_NAME_CONNECTOR = "connectors:name=JMXRemoteConnector";
068:
069:            /**
070:             * MBean server connection
071:             */
072:            private static MBeanServerConnection mbscnx = null;
073:
074:            /**
075:             * Utility class, no public constructor.
076:             */
077:            private JMXRemoteHelper() {
078:
079:            }
080:
081:            /**
082:             * Build a new JMX Remote connector
083:             * @param url JMX remote url.
084:             * @throws JMXRemoteException if jmx connector can't be built.
085:             */
086:            private static void init(String url) throws JMXRemoteException {
087:
088:                // Create connector
089:                Map environment = new HashMap();
090:                JMXServiceURL jmxServiceURL = null;
091:                try {
092:                    jmxServiceURL = new JMXServiceURL(url);
093:                } catch (MalformedURLException e) {
094:                    throw new JMXRemoteException(
095:                            "Cannot create jmxservice url with url '" + url
096:                                    + "'.", e);
097:                }
098:                environment.put("jmx.remote.jndi.rebind", "true");
099:                try {
100:                    jmxConnectorServer = JMXConnectorServerFactory
101:                            .newJMXConnectorServer(jmxServiceURL, environment,
102:                                    null);
103:                } catch (IOException e) {
104:                    throw new JMXRemoteException(
105:                            "Cannot create new JMX Connector", e);
106:                }
107:
108:            }
109:
110:            /**
111:             * Connect to a JMX Remote connector
112:             * @param url JMX remote url.
113:             * @return MBeanServerConnection MBean server connection
114:             * @throws JMXRemoteException if jmx connector can't be connected.
115:             */
116:            public static MBeanServerConnection connect(String url)
117:                    throws JMXRemoteException {
118:
119:                if (mbscnx != null)
120:                    return mbscnx;
121:
122:                JMXServiceURL jmxServiceURL = null;
123:                try {
124:                    jmxServiceURL = new JMXServiceURL(url);
125:                } catch (MalformedURLException e) {
126:                    throw new JMXRemoteException(
127:                            "Cannot create jmxservice url with url '" + url
128:                                    + "'.", e);
129:                }
130:                try {
131:                    JMXConnector jmxConnector = JMXConnectorFactory
132:                            .connect(jmxServiceURL);
133:                    mbscnx = jmxConnector.getMBeanServerConnection();
134:                } catch (IOException e) {
135:                    throw new JMXRemoteException(
136:                            "Cannot connect to JMX remote with '" + url + "'.",
137:                            e);
138:                }
139:                return mbscnx;
140:            }
141:
142:            /**
143:             * Start a JMX connector (used to do remote administration).
144:             * @param url JMX remote url
145:             * @param connectorName connector name
146:             * @throws JMXRemoteException if the connector can't be started.
147:             */
148:            public static synchronized void startConnector(String url,
149:                    String connectorName) throws JMXRemoteException {
150:                // Create connector if null
151:                if (jmxConnectorServer == null) {
152:                    if (url == null) {
153:                        init(DEFAULT_URL);
154:                    } else {
155:                        init(url);
156:                    }
157:                }
158:
159:                // Create MBean for this connector
160:                ObjectName connectorServerName = null;
161:                String objName = null;
162:                try {
163:                    if (connectorName == null) {
164:                        objName = DEFAULT_NAME_CONNECTOR;
165:                    } else {
166:                        objName = connectorName;
167:                    }
168:                    connectorServerName = new ObjectName(objName);
169:                } catch (MalformedObjectNameException e) {
170:                    throw new JMXRemoteException(
171:                            "Cannot create ObjectName with name '" + objName
172:                                    + "'", e);
173:                } catch (NullPointerException e) {
174:                    throw new JMXRemoteException(
175:                            "Cannot create ObjectName with name '" + objName
176:                                    + "'", e);
177:                }
178:
179:                // register it
180:                try {
181:                    MBeanServerHelper.getMBeanServer().registerMBean(
182:                            jmxConnectorServer, connectorServerName);
183:                } catch (InstanceAlreadyExistsException e) {
184:                    throw new JMXRemoteException(
185:                            "Cannot register Mbean with the name '"
186:                                    + connectorServerName + "'", e);
187:                } catch (MBeanRegistrationException e) {
188:                    throw new JMXRemoteException(
189:                            "Cannot register Mbean with the name '"
190:                                    + connectorServerName + "'", e);
191:                } catch (NotCompliantMBeanException e) {
192:                    throw new JMXRemoteException(
193:                            "Cannot register Mbean with the name '"
194:                                    + connectorServerName + "'", e);
195:                }
196:
197:                // Start connector
198:                try {
199:                    jmxConnectorServer.start();
200:                } catch (IOException e) {
201:                    throw new JMXRemoteException(
202:                            "Cannot start the jmx connector", e);
203:                }
204:            }
205:
206:            /**
207:             * Get ObjectInstance
208:             * @param cnx MBean server connection
209:             * @param pattern MBean name
210:             * @return ObjectInstance
211:             */
212:            public static ObjectInstance getInstance(MBeanServerConnection cnx,
213:                    String pattern) {
214:
215:                Set oiset = null;
216:                ObjectInstance oi = null;
217:                try {
218:                    ObjectName on = new ObjectName(pattern);
219:                    oiset = cnx.queryMBeans(on, null);
220:
221:                } catch (Exception ex) {
222:                    ex.printStackTrace();
223:                }
224:                if (!oiset.isEmpty())
225:                    oi = (ObjectInstance) oiset.iterator().next();
226:
227:                return oi;
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.