1 | // jademx - JADE management using JMX |
2 | // Copyright 2005 Caboodle Networks, Inc. |
3 | // |
4 | // This library is free software; you can redistribute it and/or |
5 | // modify it under the terms of the GNU Lesser General Public |
6 | // License as published by the Free Software Foundation; either |
7 | // version 2.1 of the License, or (at your option) any later version. |
8 | // |
9 | // This library is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | // Lesser General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU Lesser General Public |
15 | // License along with this library; if not, write to the Free Software |
16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
17 | |
18 | package jade.jademx.server; |
19 | |
20 | // use reflection so can compile without access to WebLogic |
21 | import java.lang.reflect.Field; |
22 | import java.lang.reflect.Method; |
23 | |
24 | import javax.management.MBeanServer; |
25 | import javax.naming.Context; |
26 | |
27 | // equivalent done via reflection |
28 | //import weblogic.jndi.Environment; |
29 | //import weblogic.management.MBeanHome; |
30 | |
31 | /** |
32 | * JadeMXServer for WebLogic using MBeanHome located using JNDI. |
33 | * implemented via reflection so can be compiled without WebLogic. |
34 | * @author David Bernstein, <a href="http://www.caboodlenetworks.com" |
35 | * >Caboodle Networks, Inc.</a> |
36 | */ |
37 | public class WebLogicMBeanHome implements JadeMXServer { |
38 | |
39 | /** JNDI name for MBeanHome */ |
40 | private String mBeanHomeJndiName; |
41 | |
42 | /** the platform MBean server */ |
43 | private MBeanServer mBeanServer; |
44 | |
45 | /** code to get MBeanServer from local MBeanHome found JNDI */ |
46 | protected final static int LOCAL_MBEAN_HOME_JNDI_NAME = 0; |
47 | |
48 | /** name of WebLogic <code>Environment</code> class */ |
49 | private final static String CLASSNAME_ENVIRONMENT = |
50 | "weblogic.jndi.Environment"; |
51 | |
52 | /** name of WebLogic <code>MBeanHome</code> class */ |
53 | private final static String CLASSNAME_MBEAN_HOME = |
54 | "weblogic.management.MBeanHome"; |
55 | |
56 | /** |
57 | * name for field |
58 | * {@link #CLASSNAME_MBEAN_HOME}<code>.LOCAL_JNDI_NAME</code> |
59 | */ |
60 | private final static String FIELDNAME_LOCAL_JNDI_NAME = |
61 | "LOCAL_JNDI_NAME"; |
62 | |
63 | /** |
64 | * name for method |
65 | * {@link #CLASSNAME_ENVIRONMENT}<code>.getInitialContext</code> |
66 | */ |
67 | private final static String METHODNAME_GET_INITIAL_CONTEXT = |
68 | "getInitialContext"; |
69 | |
70 | /** name for method <code>MBeanHome.getMBeanServer</code> */ |
71 | private final static String METHODNAME_GET_MBEAN_SERVER = |
72 | "getMBeanServer"; |
73 | |
74 | /** |
75 | * the JadeMXServer for WebLogic MBeanServer at local MBeanHome |
76 | */ |
77 | public WebLogicMBeanHome() { |
78 | //this( MBeanHome.LOCAL_JNDI_NAME ); // do via reflection |
79 | this( LOCAL_MBEAN_HOME_JNDI_NAME ); |
80 | } |
81 | |
82 | /** |
83 | * construct via a magic code |
84 | * @see #LOCAL_MBEAN_HOME_JNDI_NAME |
85 | * @param code magic code to use |
86 | */ |
87 | protected WebLogicMBeanHome( int code ) { |
88 | if ( LOCAL_MBEAN_HOME_JNDI_NAME == code ) { |
89 | String mBeanHomeLocalJndiName = null; |
90 | try { |
91 | Class mBeanHomeClass = Class.forName( CLASSNAME_MBEAN_HOME ); |
92 | Field localJndiNameField = |
93 | mBeanHomeClass.getField( FIELDNAME_LOCAL_JNDI_NAME ); |
94 | mBeanHomeLocalJndiName = |
95 | (String)localJndiNameField.get( null ); |
96 | } |
97 | catch ( Exception e ) { |
98 | throw new RuntimeException( |
99 | "exception getting value of"+ |
100 | CLASSNAME_MBEAN_HOME + "." + |
101 | FIELDNAME_LOCAL_JNDI_NAME, |
102 | e ); |
103 | |
104 | } |
105 | findMBeanServerViaJndi( mBeanHomeLocalJndiName ); |
106 | } |
107 | else { |
108 | throw new IllegalArgumentException("code "+code+" not understood"); |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * the JadeMXServer for WebLogic MBeanServer at JNDI-specified MBeanHome |
114 | * @param mBeanHomeJndiName JNDI name for MBeanHome |
115 | */ |
116 | protected WebLogicMBeanHome( String mBeanHomeJndiName ) { |
117 | findMBeanServerViaJndi( mBeanHomeJndiName ); |
118 | } |
119 | |
120 | /** |
121 | * given the JNDI name of the <code>MBeanHome</code>, get the <code>MBeanServer</code> |
122 | * @param mBeanHomeJndiName JNDI name of the MBeanHome |
123 | */ |
124 | private void findMBeanServerViaJndi( String mBeanHomeJndiName ) { |
125 | this.mBeanHomeJndiName = mBeanHomeJndiName; |
126 | try { |
127 | // Environment env = new Environment(); |
128 | Class environmentClass = Class.forName( CLASSNAME_ENVIRONMENT ); |
129 | Object env = environmentClass.newInstance(); |
130 | // Context ctx = env.getInitialContext(); |
131 | Method getInitialContextMethod = |
132 | environmentClass.getMethod( |
133 | METHODNAME_GET_INITIAL_CONTEXT, |
134 | new Class[0] ); |
135 | Context ctx = |
136 | (Context)getInitialContextMethod.invoke( env, new Object[0] ); |
137 | // MBeanHome home = (MBeanHome)ctx.lookup( mBeanHomeJndiName ); |
138 | Object home = ctx.lookup( mBeanHomeJndiName ); |
139 | // mBeanServer = home.getMBeanServer(); |
140 | Class mBeanHomeClass = Class.forName( CLASSNAME_MBEAN_HOME ); |
141 | Method getMBeanServerMethod = |
142 | mBeanHomeClass.getMethod( |
143 | METHODNAME_GET_MBEAN_SERVER, |
144 | new Class[0] ); |
145 | mBeanServer = |
146 | (MBeanServer)getMBeanServerMethod.invoke( home, new Object[0] ); |
147 | } |
148 | catch ( Exception e ) { |
149 | throw new RuntimeException( |
150 | "exception getting MBeanServer from MBeanHome at \""+ |
151 | mBeanHomeJndiName+"\"", e ); |
152 | } |
153 | } |
154 | |
155 | /* (non-Javadoc) |
156 | * @see jade.jademx.server.JadeMXServer#getMBeanServer() |
157 | */ |
158 | public MBeanServer getMBeanServer() { |
159 | return mBeanServer; |
160 | } |
161 | |
162 | /* (non-Javadoc) |
163 | * @see java.lang.Object#toString() |
164 | */ |
165 | public String toString() { |
166 | return getClass().getName() + "(" + mBeanHomeJndiName + ")"; |
167 | } |
168 | |
169 | } |