1 21 22 package org.opensubsystems.core.logic; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.rmi.RemoteException ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.NamingException ; 34 import javax.rmi.PortableRemoteObject ; 35 36 import org.opensubsystems.core.error.OSSException; 37 import org.opensubsystems.core.error.OSSInternalErrorException; 38 import org.opensubsystems.core.util.Log; 39 40 52 public class J2EEControllerManager extends ControllerManager 53 { 54 56 59 protected Map m_mpControllerLookupCache = new HashMap (); 60 61 64 private static Logger s_logger = Log.getInstance(J2EEControllerManager.class); 65 66 68 71 public Object getControllerInstance( 72 Class controller 73 ) throws OSSException 74 { 75 Object controlHome = null; 76 Object controlRet = null; 77 78 StringBuffer currControllerName = new StringBuffer (); 81 currControllerName.append(controller.getName()); 82 83 controlHome = m_mpControllerLookupCache.get(currControllerName); 87 if (controlHome == null) 88 { 89 synchronized (m_mpControllerLookupCache) 90 { 91 StringBuffer sbHomeClassName = new StringBuffer (); 94 Class homeClass = null; 95 Context context = null; 96 Object lookupObject = null; 97 98 101 sbHomeClassName.append(currControllerName); 103 sbHomeClassName.insert(sbHomeClassName.lastIndexOf("."), ".impl"); 104 try 106 { 107 context = new InitialContext (); 108 lookupObject = context.lookup(currControllerName.toString()); 111 sbHomeClassName.append("Home"); 114 } 115 catch (NamingException nExc) 116 { 117 s_logger.log(Level.FINE, 119 "Local home of controller is not available at JNDI " + 120 "location [" + currControllerName.toString() + "]", 121 nExc); 122 123 try 127 { 128 currControllerName.append("Remote"); 131 132 142 context = new InitialContext (); 143 lookupObject = context.lookup(currControllerName.toString()); 144 sbHomeClassName.append("RemoteHome"); 147 } 148 catch (NamingException nExc1) 149 { 150 s_logger.log(Level.FINE, 152 "Remote home of controller is not available at JNDI " + 153 "location [" + currControllerName.toString() + "]", 154 nExc1); 155 156 controlRet = super.getControllerInstance(controller); 162 } 163 } 164 finally 165 { 166 try 167 { 168 if (context != null) 169 { 170 context.close(); 171 } 172 } 173 catch (NamingException nExc) 174 { 175 s_logger.log(Level.FINE, 177 "Unable to close context for controller manager", 178 nExc); 179 } 180 } 181 if (lookupObject != null) 182 { 183 try 185 { 186 homeClass = Class.forName(sbHomeClassName.toString()); 189 } 190 catch (ClassNotFoundException cnfExc) 191 { 192 s_logger.log(Level.WARNING, 195 "Cannot found class for home interface[" + 196 sbHomeClassName.toString() + "]", cnfExc); 197 } 198 199 if (homeClass != null) 200 { 201 controlHome = PortableRemoteObject.narrow(lookupObject, homeClass); 204 m_mpControllerLookupCache.put(currControllerName, controlHome); 205 } 206 else 207 { 208 controlRet = super.getControllerInstance(controller); 212 } 213 } 214 } 215 } 216 217 if (controlHome != null) 219 { 220 try 221 { 222 controlRet = controlHome.getClass( 226 ).getMethod("create", null).invoke(controlHome, null); 227 } 228 catch (NoSuchMethodException nsmExc) 229 { 230 throw new OSSInternalErrorException( 231 "Cannot found method create() in home interface.", nsmExc); 232 } 233 catch (InvocationTargetException itExc) 234 { 235 throw new OSSInternalErrorException( 236 "Cannot invoke method create() in home interface.", itExc); 237 } 238 catch (IllegalAccessException iaExc) 239 { 240 throw new OSSInternalErrorException( 241 "Cannot access method create() in home interface.", iaExc); 242 } 243 try 244 { 245 ((StatelessController)controlRet).constructor(); 246 } 247 catch (RemoteException rExc) 248 { 249 throw new OSSInternalErrorException("Remote error occured", rExc); 253 } 254 } 255 256 return controlRet; 257 } 258 } 259 | Popular Tags |