1 7 package org.ejtools.management.browser.model.service; 8 9 import java.util.Hashtable ; 10 11 import javax.management.j2ee.Management ; 12 import javax.management.j2ee.ManagementHome ; 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.rmi.PortableRemoteObject ; 16 17 import org.apache.log4j.Logger; 18 import org.ejtools.util.service.Profile; 19 20 26 public class MEJBConnectionService implements ConnectionService 27 { 28 29 protected boolean connected = false; 30 31 protected Management mejb = null; 32 33 protected Profile profile = null; 34 35 private static Logger logger = Logger.getLogger(MEJBConnectionService.class); 36 37 38 39 public MEJBConnectionService() 40 { 41 super(); 42 } 43 44 45 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 e) 59 { 60 logger.error("Error while connecting to MEJB", e); 61 this.setConnected(false); 62 } 63 } 64 65 66 67 public void disconnect() 68 { 69 try 70 { 71 this.mejb.remove(); 72 } 73 catch (Exception e) 74 { 75 logger.warn("Error while disconnecting from MEJB", e); 76 } 78 this.setConnected(false); 79 } 80 81 82 87 public Management getMEJB() 88 { 89 if (!this.connected) 90 { 91 logger.warn("Service is not connected"); 92 throw new IllegalStateException ("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 ("The server is null. Call 'connect' method before."); 98 } 99 return this.mejb; 100 } 101 102 103 108 public boolean isConnected() 109 { 110 return this.connected; 111 } 112 113 114 119 public void setConnected(boolean connected) 120 { 121 this.connected = connected; 122 } 123 124 125 130 protected void createMEJB() 131 throws Exception 132 { 133 Hashtable props = new Hashtable (); 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 context = new InitialContext (props); 141 Object ref = context.lookup(this.profile.getProperty(ConnectionMetaData.CONTEXT)); 142 143 ManagementHome home = (ManagementHome ) PortableRemoteObject.narrow(ref, ManagementHome .class); 144 this.mejb = home.create(); 145 } 146 } 147 | Popular Tags |