Source Code Cross Referenced for MBeansHelper.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » jmx » 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 » ow2 easybeans » org.ow2.easybeans.jmx 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006-2007 Bull S.A.S.
004:         * Contact: easybeans@ow2.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: MBeansHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.jmx;
025:
026:        import java.util.Hashtable;
027:        import java.util.Map;
028:
029:        import javax.management.MalformedObjectNameException;
030:        import javax.management.ObjectName;
031:
032:        import org.ow2.easybeans.api.jmx.EZBManagementIdentifier;
033:        import org.ow2.easybeans.util.loader.ClassUtils;
034:        import org.ow2.util.log.Log;
035:        import org.ow2.util.log.LogFactory;
036:
037:        /**
038:         * Singleton object.Creates the MBeans and register them.
039:         * @author florent
040:         *
041:         */
042:        public final class MBeansHelper {
043:            /**
044:             * Logger.
045:             */
046:            private static Log logger = LogFactory.getLog(MBeansHelper.class);
047:
048:            /**
049:             * Singleton instance.
050:             */
051:            private static MBeansHelper instance = null;
052:
053:            /**
054:             * The Identifier in charge of creating the right ObjectName for a given
055:             * instance.
056:             */
057:            private Map<Class, EZBManagementIdentifier> identifiers = null;
058:
059:            /**
060:             * Is Management activated ?
061:             */
062:            private boolean activate;
063:
064:            /**
065:             * Domain name to enforce if set.
066:             */
067:            private static String domainName = null;
068:
069:            /**
070:             * Server name to enforce if set.
071:             */
072:            private static String serverName = null;
073:
074:            /**
075:             * Singleton class, no public constructor.
076:             */
077:            private MBeansHelper() {
078:                identifiers = new Hashtable<Class, EZBManagementIdentifier>();
079:            }
080:
081:            /**
082:             * @return Returns the Singleton MBeansHelper instance.
083:             */
084:            public static MBeansHelper getInstance() {
085:                if (instance == null) {
086:                    instance = new MBeansHelper();
087:                }
088:                return instance;
089:            }
090:
091:            /**
092:             * Activate the MBeans registration.
093:             * @param activate <code>true</code> if mbeans should be
094:             *        registered, <code>false</code> otherwise.
095:             */
096:            public void activate(final boolean activate) {
097:                this .activate = activate;
098:            }
099:
100:            /**
101:             * Register the instance as a ModelMBean using the delegate.
102:             * @param <T> instance Type
103:             * @param instance Object instance to be managed
104:             * @throws MBeansException if registration fails.
105:             */
106:            public <T> void registerMBean(final T instance)
107:                    throws MBeansException {
108:
109:                if (activate) {
110:                    String on = getObjectName(instance);
111:
112:                    // register
113:                    try {
114:                        CommonsModelerHelper.registerModelerMBean(instance, on);
115:                    } catch (CommonsModelerException e) {
116:                        throw new MBeansException("Cannot register MBean", e);
117:                    }
118:                }
119:            }
120:
121:            /**
122:             * Unregister the given Object.
123:             * @param <T> instance Type
124:             * @param instance Instance to be deregistered.
125:             * @throws MBeansException if unregistration fails.
126:             */
127:            public <T> void unregisterMBean(final T instance)
128:                    throws MBeansException {
129:
130:                if (activate) {
131:                    String on = getObjectName(instance);
132:
133:                    // unregister
134:                    try {
135:                        CommonsModelerHelper
136:                                .unregisterModelerMBean(new ObjectName(on));
137:                    } catch (MalformedObjectNameException e) {
138:                        logger.warn("Cannot unregister MBean '" + on + "' : "
139:                                + e.getMessage());
140:                    } catch (NullPointerException e) {
141:                        logger.warn("Cannot unregister MBean '" + on + "' : "
142:                                + e.getMessage());
143:                    }
144:                }
145:            }
146:
147:            /**
148:             * @param <T> instance Type
149:             * @param instance Object instance to be managed
150:             * @return Returns the instance ObjectName.
151:             * @throws MBeansException if registration fails.
152:             */
153:            public <T> String getObjectName(final T instance)
154:                    throws MBeansException {
155:
156:                EZBManagementIdentifier<T> identifier = getIdentifier(instance);
157:
158:                // gather the ObjectName
159:                if (identifier != null) {
160:                    StringBuilder sb = new StringBuilder();
161:
162:                    sb.append(identifier.getDomain());
163:                    sb.append(":");
164:                    sb.append(identifier.getTypeProperty());
165:                    String additionnal = identifier
166:                            .getAdditionnalProperties(instance);
167:                    if (additionnal != null && (!"".equals(additionnal))) {
168:                        sb.append(",");
169:                        sb.append(additionnal);
170:                    }
171:                    sb.append(",");
172:                    sb.append("name=");
173:                    sb.append(identifier.getNamePropertyValue(instance));
174:                    return sb.toString();
175:                }
176:
177:                return null;
178:
179:            }
180:
181:            /**
182:             * @param <T> instance type
183:             * @param instance instance to be managed.
184:             * @return Returns an {@link EZBManagementIdentifier} for the given Resource type.
185:             * @throws MBeansException if the Identifier cannot be returned.
186:             */
187:            @SuppressWarnings("unchecked")
188:            private <T> EZBManagementIdentifier<T> getIdentifier(
189:                    final T instance) throws MBeansException {
190:
191:                // try to use the cached identifier
192:                Class clz = instance.getClass();
193:                if (identifiers.containsKey(clz)) {
194:                    return identifiers.get(clz);
195:                }
196:
197:                // the identifier was not cached
198:                String mbeanClassname = instance.getClass().getName();
199:                String mbeanPackage = mbeanClassname.substring(0,
200:                        mbeanClassname.lastIndexOf(".") + 1);
201:
202:                // looking for a class named :
203:                // <mbean-package>.management.<mbean-model-name>Identifier
204:                // ex : org.ow2.easybeans.container.management.JContainer3Identifier
205:                String identifierClassname = mbeanPackage + "management."
206:                        + clz.getSimpleName() + "Identifier";
207:
208:                // Instantiate it ...
209:                try {
210:                    Class<? extends EZBManagementIdentifier> cls = ClassUtils
211:                            .forName(identifierClassname).asSubclass(
212:                                    EZBManagementIdentifier.class);
213:                    EZBManagementIdentifier<T> id = cls.newInstance();
214:
215:                    // Sets domain if there is a need to override value
216:                    if (domainName != null) {
217:                        id.setDomain(domainName);
218:                    }
219:
220:                    // Sets Server if there is a need to override value
221:                    if (serverName != null) {
222:                        id.setServerName(serverName);
223:                    }
224:
225:                    // cache the identifier
226:                    identifiers.put(clz, id);
227:
228:                    return id;
229:                } catch (ClassNotFoundException e) {
230:                    throw new MBeansException("Identifier Class not found", e);
231:                } catch (InstantiationException e) {
232:                    throw new MBeansException(
233:                            "Identifier Class not instantiated", e);
234:                } catch (IllegalAccessException e) {
235:                    throw new MBeansException(
236:                            "Identifier Class not instantiated", e);
237:                }
238:            }
239:
240:            /**
241:             * Gets the domain name used by this helper.
242:             * @return the domain name used by this helper.
243:             */
244:            public static String getDomainName() {
245:                return MBeansHelper.domainName;
246:            }
247:
248:            /**
249:             * Sets the domain name to be used by this helper.
250:             * @param domainName the domain name to be used by this helper
251:             */
252:            public static void setDomainName(final String domainName) {
253:                MBeansHelper.domainName = domainName;
254:            }
255:
256:            /**
257:             * Gets the server name used by this helper.
258:             * @return the domain name used by this helper.
259:             */
260:            public static String getServerName() {
261:                return MBeansHelper.serverName;
262:            }
263:
264:            /**
265:             * Sets the server name used by this helper.
266:             * @param serverName the server name used by this helper.
267:             */
268:            public static void setServerName(final String serverName) {
269:                MBeansHelper.serverName = serverName;
270:            }
271:
272:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.