1 22 package org.jboss.ejb3.client; 23 24 import java.lang.annotation.Annotation ; 25 import java.lang.reflect.AccessibleObject ; 26 import java.lang.reflect.Field ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NameClassPair ; 38 import javax.naming.NameNotFoundException ; 39 import javax.naming.NamingEnumeration ; 40 41 import org.jboss.ejb3.Container; 42 import org.jboss.ejb3.DependencyPolicy; 43 import org.jboss.ejb3.entity.PersistenceUnitDeployment; 44 import org.jboss.ejb3.metamodel.ApplicationClientDD; 45 import org.jboss.injection.DependsHandler; 46 import org.jboss.injection.EJBHandler; 47 import org.jboss.injection.EncInjector; 48 import org.jboss.injection.InjectionContainer; 49 import org.jboss.injection.InjectionHandler; 50 import org.jboss.injection.InjectionUtil; 51 import org.jboss.injection.Injector; 52 import org.jboss.injection.JndiInjectHandler; 53 import org.jboss.injection.PersistenceContextHandler; 54 import org.jboss.injection.PersistenceUnitHandler; 55 import org.jboss.injection.ResourceHandler; 56 import org.jboss.injection.WebServiceRefHandler; 57 import org.jboss.logging.Logger; 58 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 59 import org.jboss.util.NotImplementedException; 60 61 67 public class ClientContainer implements InjectionContainer 68 { 69 private static final Logger log = Logger.getLogger(ClientContainer.class); 70 71 private Class <?> mainClass; 72 private ApplicationClientDD xml; 73 private String applicationClientName; 74 75 private List <Injector> injectors = new ArrayList <Injector>(); 77 private Map <String , Map <AccessibleObject , Injector>> encInjections = new HashMap <String , Map <AccessibleObject , Injector>>(); 78 private Map <String , EncInjector> encInjectors = new HashMap <String , EncInjector>(); 79 80 private Context enc; 81 private Context encEnv; 82 83 public ClientContainer(ApplicationClientDD xml, Class <?> mainClass, String applicationClientName) throws Exception 84 { 85 this.xml = xml; 86 this.mainClass = mainClass; 87 this.applicationClientName = applicationClientName; 88 89 Context ctx = new InitialContext (); 91 enc = (Context ) ctx.lookup(applicationClientName); 92 NamingEnumeration <NameClassPair > e = enc.list(""); 93 while(e.hasMore()) 94 { 95 NameClassPair ncp = e.next(); 96 log.debug(" " + ncp); 97 } 98 encEnv = (Context ) enc.lookup("env"); 99 102 processMetadata(null); 103 104 110 for(Injector injector : injectors) 111 { 112 log.trace("injector: " + injector); 113 injector.inject((Object ) null); 114 } 115 } 116 117 120 public <T extends Annotation > T getAnnotation(Class <T> annotationClass, Class <?> clazz) 121 { 122 return clazz.getAnnotation(annotationClass); 123 } 124 125 128 public <T extends Annotation > T getAnnotation(Class <T> annotationClass, Class <?> clazz, Method method) 129 { 130 return method.getAnnotation(annotationClass); 131 } 132 133 136 public <T extends Annotation > T getAnnotation(Class <T> annotationClass, Method method) 137 { 138 return method.getAnnotation(annotationClass); 139 } 140 141 144 public <T extends Annotation > T getAnnotation(Class <T> annotationClass, Class <?> clazz, Field field) 145 { 146 return field.getAnnotation(annotationClass); 147 } 148 149 152 public <T extends Annotation > T getAnnotation(Class <T> annotationClass, Field field) 153 { 154 return field.getAnnotation(annotationClass); 155 } 156 157 160 public ClassLoader getClassloader() 161 { 162 throw new RuntimeException ("NYI"); 163 } 164 165 168 public DependencyPolicy getDependencyPolicy() 169 { 170 throw new RuntimeException ("NYI"); 171 } 172 173 176 public String getDeploymentDescriptorType() 177 { 178 return "application-client.xml"; 179 } 180 181 184 public String getEjbJndiName(Class businessInterface) throws NameNotFoundException 185 { 186 throw new RuntimeException ("NYI"); 187 } 189 190 193 public String getEjbJndiName(String link, Class businessInterface) 194 { 195 throw new NotImplementedException(); 196 } 200 201 204 public Context getEnc() 205 { 206 return enc; 207 } 208 209 212 public Context getEncEnv() 213 { 214 return encEnv; 215 } 216 217 220 public Map <String , Map <AccessibleObject , Injector>> getEncInjections() 221 { 222 return encInjections; 223 } 224 225 228 public Map <String , EncInjector> getEncInjectors() 229 { 230 return encInjectors; 231 } 232 233 236 public EnvironmentRefGroup getEnvironmentRefGroup() 237 { 238 return xml; 239 } 240 241 244 public String getIdentifier() 245 { 246 return applicationClientName; 250 } 251 252 255 public List <Injector> getInjectors() 256 { 257 throw new NotImplementedException(); 258 } 259 260 public Class <?> getMainClass() 261 { 262 return mainClass; 263 } 264 265 268 public PersistenceUnitDeployment getPersistenceUnitDeployment(String unitName) throws NameNotFoundException 269 { 270 throw new NotImplementedException(); 271 } 272 273 public void invokeMain(String args[]) throws SecurityException , NoSuchMethodException , IllegalArgumentException , IllegalAccessException , InvocationTargetException 274 { 275 Class parameterTypes[] = { args.getClass() }; 276 Method method = mainClass.getDeclaredMethod("main", parameterTypes); 277 method.invoke(null, (Object ) args); 278 } 279 280 private void processMetadata(DependencyPolicy dependencyPolicy) 281 { 282 Collection <InjectionHandler> handlers = new ArrayList <InjectionHandler>(); 284 handlers.add(new EJBHandler()); 285 handlers.add(new DependsHandler()); 287 handlers.add(new JndiInjectHandler()); 288 handlers.add(new PersistenceContextHandler()); 289 handlers.add(new PersistenceUnitHandler()); 290 handlers.add(new ResourceHandler()); 291 handlers.add(new WebServiceRefHandler()); 292 293 try 297 { 298 for (InjectionHandler handler : handlers) handler.loadXml(xml, this); 300 301 Map <AccessibleObject , Injector> tmp = InjectionUtil.processAnnotations(this, handlers, getMainClass()); 302 injectors.addAll(tmp.values()); 303 304 } 319 finally 320 { 321 } 323 } 324 325 328 public Container resolveEjbContainer(String link, Class businessIntf) 329 { 330 log.warn("resolveEjbContainer(" + link + ", " + businessIntf + ") not implemented"); 331 return null; 332 } 333 334 337 public Container resolveEjbContainer(Class businessIntf) throws NameNotFoundException 338 { 339 return null; 340 } 341 } 342 | Popular Tags |