Source Code Cross Referenced for ConnectorFactory.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » 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 » JOnAS 4.8.6 » org.objectweb.jonas.jmx 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999 Bull S.A.
004:         * Contact: jonas-team@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: ConnectorFactory.java 9148 2006-07-12 06:26:36Z durieuxp $
023:         * --------------------------------------------------------------------------
024:         */package org.objectweb.jonas.jmx;
025:
026:        import java.util.HashSet;
027:        import java.util.Properties;
028:        import java.util.Set;
029:        import javax.naming.Context;
030:        import javax.naming.InitialContext;
031:        import javax.naming.NamingException;
032:        import javax.naming.NameClassPair;
033:        import javax.naming.NamingEnumeration;
034:        import javax.rmi.PortableRemoteObject;
035:
036:        // JOnAS Log
037:        import org.objectweb.jonas.common.Log;
038:
039:        // Monolog
040:        import org.objectweb.util.monolog.api.Logger;
041:        import org.objectweb.util.monolog.api.BasicLevel;
042:
043:        /**
044:         * Manages the current "jonas provided" RMIConnector used to communicate with the MBEanServer.
045:         * @author Bruno Michel and Guillaume Riviere
046:         */
047:        public class ConnectorFactory {
048:
049:            private static String currentRMIConnectorName = null;
050:            private static RMIConnector rmic = null; // current RMIConnector
051:            private static Context context = null;
052:            private static Logger logger = Log
053:                    .getLogger("objectweb.org.jonas.jmx");
054:
055:            /**
056:             * @return Context the current application context, create an initial context
057:             * if there is no current context.
058:             */
059:            public static Context getContext()
060:                    throws javax.naming.NamingException {
061:                if (context == null) {
062:                    context = new InitialContext();
063:                }
064:                return context;
065:            }
066:
067:            /**
068:             * @return String the name of the current RMI connector.
069:             * If no RMI connector is selected, returns the first RMI connector
070:             * found with the getRMIConnectorsNames method.
071:             * Return null if no RMI connector is available.
072:             */
073:            public static String getCurrentRMIConnectorName() {
074:                try {
075:                    if (currentRMIConnectorName == null) { // may be not set yet
076:                        HashSet connectorNames = (HashSet) getRMIConnectorsNames();
077:                        if (!connectorNames.isEmpty()) {
078:                            // Pick up the first connector name in the list
079:                            String firstName = (String) connectorNames
080:                                    .toArray()[0];
081:                            setCurrentRMIConnectorName(firstName);
082:                        } // else, currentRMIConnector rests null
083:                    }
084:                    return currentRMIConnectorName;
085:                } catch (javax.naming.NamingException ne) {
086:                    return null;
087:                }
088:            }
089:
090:            /**
091:             * Set the currentRMIConnectorName to the specified value, then lookup for the RMI connector
092:             */
093:            public static void setCurrentRMIConnectorName(String name)
094:                    throws NamingException {
095:                currentRMIConnectorName = name;
096:                // The rmi connector name may have changed, so we lookup for the corresponding rmi connector
097:                lookupRMIConnector();
098:            }
099:
100:            /**
101:             * Set the currentRMIConnectorName to null
102:             */
103:            public static void resetCurrentRMIConnectorName() {
104:                currentRMIConnectorName = null;
105:            }
106:
107:            /**
108:             * @return Set a set containning all RMI connector names available in the current context.
109:             */
110:            public static Set getRMIConnectorsNames()
111:                    throws javax.naming.NamingException {
112:                try {
113:                    HashSet res = new HashSet();
114:                    Context ctx = getContext();
115:                    // looking for all object registered in this registry
116:                    for (NamingEnumeration e = ctx.list(""); e
117:                            .hasMoreElements();) {
118:                        String name = ((NameClassPair) e.nextElement())
119:                                .getName();
120:                        // we select only rmi connectors (start with 'RMIConnector')
121:                        if (name.startsWith("RMIConnector"))
122:                            res.add(name);
123:                    }
124:                    return res;
125:                } catch (javax.naming.NamingException ne) {
126:                    throw new javax.naming.NamingException(
127:                            "Cannot enumerates the names bound in the named context: '"
128:                                    + getJonasNamingServiceURL()
129:                                    + "' (registry probably not launched).");
130:                }
131:            }
132:
133:            /**
134:             * @return String the value of the PROVIDER_URL property in the current context.
135:             * If a javax.naming.NamingException is catch, the exception message is returned.
136:             */
137:            public static String getJonasNamingServiceURL() {
138:                try {
139:                    return (String) getContext().getEnvironment().get(
140:                            Context.PROVIDER_URL);
141:                } catch (javax.naming.NamingException ne) {
142:                    return ne.getMessage();
143:                }
144:            }
145:
146:            /**
147:             * Create a new naming context based on the given env. properties
148:             * @param env properties to create a new naming context
149:             */
150:            public static void setNamingEnvCtx(Properties env)
151:                    throws javax.naming.NamingException {
152:                context = new InitialContext(env);
153:                rmic = null;
154:                currentRMIConnectorName = null;
155:            }
156:
157:            /**
158:             * Sets  the PROVIDER_URL property to the specified value. Then, sets the current RMI connector to null.
159:             */
160:            public static void setJonasNamingServiceURL(String url)
161:                    throws javax.naming.NamingException {
162:
163:                // Properties for the new naming context
164:                Properties p = new Properties();
165:
166:                // Current context
167:                Context ctx = getContext();
168:                // Re-use the same values for the INITIAL_CONTEXT_FACTORY and URL_PKG_PREFIXES
169:                p.put(Context.INITIAL_CONTEXT_FACTORY, ctx.getEnvironment()
170:                        .get(Context.INITIAL_CONTEXT_FACTORY));
171:                p.put(Context.URL_PKG_PREFIXES, ctx.getEnvironment().get(
172:                        Context.URL_PKG_PREFIXES));
173:                // Use the url parameter for the PROVIDER_URL property
174:                p.put(Context.PROVIDER_URL, url);
175:
176:                try {
177:                    context = new InitialContext(p);
178:                } catch (NamingException e) {
179:                    if (logger.isLoggable(BasicLevel.DEBUG)) {
180:                        logger.log(BasicLevel.DEBUG, "Cannot create context : "
181:                                + e);
182:                        logger.log(BasicLevel.DEBUG, "Environment used :");
183:                        logger
184:                                .log(
185:                                        BasicLevel.DEBUG,
186:                                        Context.INITIAL_CONTEXT_FACTORY
187:                                                + " = "
188:                                                + p
189:                                                        .get(Context.INITIAL_CONTEXT_FACTORY));
190:                        logger.log(BasicLevel.DEBUG, Context.URL_PKG_PREFIXES
191:                                + " = " + p.get(Context.URL_PKG_PREFIXES));
192:                        logger.log(BasicLevel.DEBUG, Context.PROVIDER_URL
193:                                + " = " + p.get(Context.PROVIDER_URL));
194:                    }
195:                    throw e;
196:                }
197:
198:                rmic = null;
199:                currentRMIConnectorName = null;
200:
201:            }
202:
203:            /**
204:             * @return RMIConnector the current RMI connector, if null, lookup for a new one.
205:             */
206:            public static RMIConnector getRMIConnector() throws NamingException {
207:                if (rmic == null) {
208:                    lookupRMIConnector();
209:                }
210:                if (rmic == null) {
211:                    logger.log(BasicLevel.DEBUG, "Null RMIConnector");
212:                }
213:                return rmic;
214:            }
215:
216:            /**
217:             * Lookup for the RMI connector registered with the currentRMIConnectorName JNDI name.
218:             */
219:            public static void lookupRMIConnector() throws NamingException {
220:                String curr = getCurrentRMIConnectorName();
221:                if (curr != null) {
222:                    rmic = (RMIConnector) PortableRemoteObject.narrow(
223:                            getContext().lookup(curr), RMIConnector.class);
224:                }
225:            }
226:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.