WLSMBeanServerConnectionFactory.java
001 package com.bea.medrec.utils;
002 
003 import java.io.IOException;
004 import java.util.ArrayList;
005 import java.util.Hashtable;
006 import java.util.Iterator;
007 import java.net.MalformedURLException;
008 import javax.management.MBeanServerConnection;
009 import javax.management.remote.JMXConnector;
010 import javax.management.remote.JMXConnectorFactory;
011 import javax.management.remote.JMXServiceURL;
012 import javax.naming.Context;
013 import org.apache.log4j.Logger;
014 import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
015 import weblogic.management.mbeanservers.edit.EditServiceMBean;
016 import weblogic.management.mbeanservers.runtime.RuntimeServiceMBean;
017 
018 /**
019  * Utility to obtain WLS MBean Objects.
020  *
021  @author Copyright (c) 2006 by BEA Systems. All Rights Reserved.
022  */
023 public class WLSMBeanServerConnectionFactory {
024 
025   private static Logger logger =
026       MedRecLog4jFactory.getLogger(WLSMBeanServerConnectionFactory.class.getName());
027 
028   private static final String JNDI = "/jndi/";
029 
030   // Holds value of property serverUrl.
031   private String serverUrl;
032 
033   // Holds value of property username.
034   private String username;
035 
036   // Holds value of property password.
037   private String password;
038 
039   // Holds value of property editServer.
040   private MBeanServerConnection editServer;
041 
042   // Holds value of property runtimeServer.
043   private MBeanServerConnection runtimeServer;
044 
045   // Holds value of property domainServer.
046   private MBeanServerConnection domainServer;
047 
048   // Holds value of property host.
049   private String host;
050 
051   // Holds value of property port.
052   private int port;
053 
054   // Holds value of property protocol.
055   private String protocol = "t3";
056 
057   ArrayList<Object> connectorList =  new ArrayList<Object>();
058 
059   private WLSMBeanServerConnectionFactory(String user,
060                                          String password,
061                                          String host,
062                                          int port)
063   {
064     // connect to an MBeanServer using the BH process name
065     setHost(host);
066     setPort(port);
067     setServerUrl(getProtocol()+"://"+getHost()+":"+ getPort());
068     setUsername(user);
069     setPassword(password);
070   }
071 
072   public static WLSMBeanServerConnectionFactory getInstance(String user,
073                                                             String password,
074                                                             String host,
075                                                             int port) {
076     return new WLSMBeanServerConnectionFactory(user, password, host, port);
077   }
078 
079   /**
080    * Getter for property serverUrl.
081    @return Value of property serverUrl.
082    */
083   public String getServerUrl() {
084     return this.serverUrl;
085   }
086 
087   /**
088    * Setter for property serverUrl.
089    @param serverUrl New value of property serverUrl.
090    */
091   public void setServerUrl(String serverUrl) {
092     this.serverUrl = serverUrl;
093   }
094 
095   /**
096    * Getter for property username.
097    @return Value of property username.
098    */
099   public String getUsername() {
100     return this.username;
101   }
102 
103   /**
104    * Setter for property username.
105    @param username New value of property username.
106    */
107   public void setUsername(String username) {
108     this.username = username;
109   }
110 
111   /**
112    * Getter for property password.
113    @return Value of property password.
114    */
115   public String getPassword() {
116     return this.password;
117   }
118 
119   /**
120    * Setter for property password.
121    @param password New value of property password.
122    */
123   public void setPassword(String password) {
124     this.password = password;
125   }
126 
127   /**
128    * Getter for property host.
129    @return Value of property host.
130    */
131   public String getHost() {
132     return this.host;
133   }
134 
135   /**
136    * Setter for property host.
137    @param host New value of property host.
138    */
139   public void setHost(String host) {
140     this.host = host;
141   }
142 
143   /**
144    * Getter for property port.
145    @return Value of property port.
146    */
147   public int getPort() {
148     return this.port;
149   }
150 
151   /**
152    * Setter for property port.
153    @param port New value of property port.
154    */
155   public void setPort(int port) {
156     this.port = port;
157   }
158 
159   /**
160    * Getter for property protocol.
161    @return Value of property protocol.
162    */
163   public String getProtocol() {
164     return this.protocol;
165   }
166 
167   /**
168    * Setter for property protocol.
169    @param protocol New value of property protocol.
170    */
171   public void setProtocol(String protocol) {
172     this.protocol = protocol;
173   }
174 
175   public MBeanServerConnection getEditMBeanServer() throws IOException {
176     if (editServer == null) {
177       editServer =
178         getMBeanServerConnection(JNDI+
179           EditServiceMBean.MBEANSERVER_JNDI_NAME);
180     }
181     return editServer;
182   }
183 
184   public MBeanServerConnection getRuntimeMBeanServer() throws IOException {
185     if (runtimeServer == null) {
186       runtimeServer =
187         getMBeanServerConnection(JNDI+
188           RuntimeServiceMBean.MBEANSERVER_JNDI_NAME);
189     }
190     return runtimeServer;
191   }
192 
193   public MBeanServerConnection getDomainRuntimeMBeanServer() throws IOException
194   {
195     if (domainServer == null) {
196       domainServer =
197         getMBeanServerConnection(
198           JNDI+DomainRuntimeServiceMBean.MBEANSERVER_JNDI_NAME
199           );
200     }
201     return domainServer;
202   }
203 
204   public void closeConnections() throws IOException {
205     Iterator it = connectorList.iterator();
206     try {
207       while (it.hasNext()) {
208         JMXConnector c = (JMXConnectorit.next();
209         try {
210           c.close();
211         catch (IOException ioex) {
212           logger.debug("Caught IOException closing JMXConnector with id " +
213                  c.getConnectionId());
214         }
215       }
216     finally {
217       connectorList.clear();
218     }
219   }
220 
221   private MBeanServerConnection getMBeanServerConnection(String jndiName)
222     throws IOException
223   {
224     JMXServiceURL serviceURL = null;
225     JMXConnector connector = null;
226     Hashtable<String,String> h = new Hashtable<String,String>();
227     MBeanServerConnection connection = null;
228 
229     try {
230       serviceURL =
231         new JMXServiceURL("t3", getHost(), getPort(), jndiName);
232     }
233     catch (MalformedURLException mue) {
234       logger.error(
235         "While trying to get JMXServiceURL got a malformed URL: "+
236         host+":"+port+" due to: "+mue.getMessage());
237       throw mue;
238     }
239 
240     logger.debug("user name ["+getUsername()+"]");
241     logger.debug("password ["+getPassword()+"]");
242     h = new Hashtable<String, String>();
243     h.put(Context.SECURITY_PRINCIPAL, getUsername());
244     h.put(Context.SECURITY_CREDENTIALS, getPassword());
245     h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
246           "weblogic.management.remote");
247 
248     try {
249       connector = JMXConnectorFactory.connect(serviceURL, h);
250       logger.debug("Using JMX Connector to connect to serviceURL="+serviceURL);
251       connectorList.add(connector);
252     }
253     catch (IOException ioe) {
254       logger.error(
255         "Could not get JMXConnector due to "+ioe.getMessage());
256       throw ioe;
257     }
258 
259     try {
260       connection = connector.getMBeanServerConnection();
261       logger.debug("Got connection");
262     }
263     catch (IOException ioe) {
264       logger.error(
265         "ERROR: Could not get MBeanServerConnection due to "+ioe.getMessage());
266       throw ioe;
267     }
268     return connection;
269   }
270 }