KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > management > browser > model > service > MEJBConnectionService


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.management.browser.model.service;
8
9 import java.util.Hashtable JavaDoc;
10
11 import javax.management.j2ee.Management JavaDoc;
12 import javax.management.j2ee.ManagementHome JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.rmi.PortableRemoteObject JavaDoc;
16
17 import org.apache.log4j.Logger;
18 import org.ejtools.util.service.Profile;
19
20 /**
21  * MEJB connection that holds the EJB handle to the J2EE server
22  *
23  * @author Laurent Etiemble
24  * @version $Revision: 1.6 $
25  */

26 public class MEJBConnectionService implements ConnectionService
27 {
28    /** Connected state */
29    protected boolean connected = false;
30    /** Description of the Field */
31    protected Management JavaDoc mejb = null;
32    /** The connection profile */
33    protected Profile profile = null;
34    /** Description of the Field */
35    private static Logger logger = Logger.getLogger(MEJBConnectionService.class);
36
37
38    /** Constructor for MEJBConnectionService. */
39    public MEJBConnectionService()
40    {
41       super();
42    }
43
44
45    /**
46     * Connect to the MBean server with this profile
47     *
48     * @param profile Connection profile
49     */

50    public void connect(Profile profile)
51    {
52       this.profile = profile;
53       try
54       {
55          this.createMEJB();
56          this.setConnected(true);
57       }
58       catch (Exception JavaDoc e)
59       {
60          logger.error("Error while connecting to MEJB", e);
61          this.setConnected(false);
62       }
63    }
64
65
66    /** Disconnect from the MBean server */
67    public void disconnect()
68    {
69       try
70       {
71          this.mejb.remove();
72       }
73       catch (Exception JavaDoc e)
74       {
75          logger.warn("Error while disconnecting from MEJB", e);
76          // Do nothing
77
}
78       this.setConnected(false);
79    }
80
81
82    /**
83     * Gets the proxied MBean server
84     *
85     * @return The MBeanServer proxy
86     */

87    public Management JavaDoc getMEJB()
88    {
89       if (!this.connected)
90       {
91          logger.warn("Service is not connected");
92          throw new IllegalStateException JavaDoc("The service is not connected. Call 'connect' method before.");
93       }
94       if (this.mejb == null)
95       {
96          logger.warn("MEJB reference is null");
97          throw new IllegalStateException JavaDoc("The server is null. Call 'connect' method before.");
98       }
99       return this.mejb;
100    }
101
102
103    /**
104     * Return whether the connector is connector
105     *
106     * @return true if connected
107     */

108    public boolean isConnected()
109    {
110       return this.connected;
111    }
112
113
114    /**
115     * Sets the connection state value
116     *
117     * @param connected The connection state
118     */

119    public void setConnected(boolean connected)
120    {
121       this.connected = connected;
122    }
123
124
125    /**
126     * Create the proxy to the MBean server. This method must set the MBean server after a successful creation.
127     *
128     * @exception Exception Description of the Exception
129     */

130    protected void createMEJB()
131       throws Exception JavaDoc
132    {
133       Hashtable JavaDoc props = new Hashtable JavaDoc();
134       props.put(Context.INITIAL_CONTEXT_FACTORY, this.profile.getProperty(ConnectionMetaData.FACTORY));
135       props.put(Context.URL_PKG_PREFIXES, this.profile.getProperty(ConnectionMetaData.PACKAGES));
136       props.put(Context.PROVIDER_URL, this.profile.getProperty(ConnectionMetaData.URL));
137       props.put(Context.SECURITY_PRINCIPAL, this.profile.getProperty(ConnectionMetaData.PRINCIPAL));
138       props.put(Context.SECURITY_CREDENTIALS, this.profile.getProperty(ConnectionMetaData.CREDENTIALS));
139
140       InitialContext JavaDoc context = new InitialContext JavaDoc(props);
141       Object JavaDoc ref = context.lookup(this.profile.getProperty(ConnectionMetaData.CONTEXT));
142
143       ManagementHome JavaDoc home = (ManagementHome JavaDoc) PortableRemoteObject.narrow(ref, ManagementHome JavaDoc.class);
144       this.mejb = home.create();
145    }
146 }
147
Popular Tags