PicoBasedContainer provideRequestContainer(RequestInfo request) { HttpSession session = request.getRequest().getSession(); MutablePicoContainer sessionScope = (MutablePicoContainer) session.getAttribute(CONTAINER_SESSION_KEY); if (sessionScope == null) { sessionScope = createSessionContainer(session); } if (logger.isDebugEnabled()) { logger.debug("Request components are " + requestScoped); } MutablePicoContainer requestContainer = new DefaultPicoContainer( new Caching(), new JavaEE5LifecycleStrategy(new NullComponentMonitor()), sessionScope); for (Map.Entry<Class<?>, Class<?>> entry : requestScoped.entrySet()) { requestContainer.addComponent(entry.getKey(), entry.getValue()); } for (Map.Entry<Class<?>, Class<?>> entry : prototypeScoped.entrySet()) { requestContainer.as(Characteristics.NO_CACHE).addComponent(entry.getKey(), entry.getValue()); } requestContainer .addComponent(request) .addComponent(request.getRequest()) .addComponent(request.getResponse()); registerComponentFactories(requestContainer, componentFactoryRegistry.getRequestMap()); return new PicoBasedContainer(requestContainer, this.appContainer.getComponent(Router.class)); }
@SuppressWarnings("unchecked") public void register(Class<?> requiredType, Class<?> type) { logger.debug("Registering " + requiredType.getName() + " with " + type.getName()); boolean overriding = alreadyRegistered(requiredType); if (overriding) { logger.debug("Overriding interface " + requiredType.getName() + " with " + type.getName()); } if (type.isAnnotationPresent(ApplicationScoped.class)) { logger.debug("Registering " + type.getName() + " as an application component"); this.applicationScoped.put(requiredType, type); if (initialized) { logger.warn( "VRaptor was already initialized, the contexts were created but you are registering a component." + "This is nasty. The original component might already be in use." + "Avoid doing it: " + requiredType.getName()); this.appContainer.addComponent(requiredType, type); } } else if (type.isAnnotationPresent(SessionScoped.class)) { logger.debug("Registering " + type.getName() + " as a session component"); this.sessionScoped.put(requiredType, type); } else if (type.isAnnotationPresent(PrototypeScoped.class)) { logger.debug("Registering " + type.getName() + " as a prototype component"); this.prototypeScoped.put(requiredType, type); } else { // default behaviour: even without @RequestScoped if (!type.isAnnotationPresent(RequestScoped.class)) { logger.info( "Class being registered as @RequestScoped, since there is no Scope annotation " + type); } logger.debug("Registering " + type.getName() + " as a request component"); this.requestScoped.put(requiredType, type); } if (ComponentFactory.class.isAssignableFrom(type) && !requiredType.equals(ComponentFactory.class)) { componentFactoryRegistry.register((Class<? extends ComponentFactory<?>>) type); if (type.isAnnotationPresent(ApplicationScoped.class) && initialized) { if (initialized) { logger.warn( "VRaptor was already initialized, the contexts were created but you are registering a component." + "This is nasty. The original component might already be in use." + "Avoid doing it: " + requiredType.getName()); Class<?> targetType = new ComponentFactoryIntrospector().targetTypeForComponentFactory(type); this.appContainer.addAdapter( new PicoComponentAdapter(targetType, (Class<? extends ComponentFactory<?>>) type)); } } } }
/** Registers all application scoped elements into the container. */ public void init() { logger.info("Initializing VRaptor IoC Container implementation based on PicoContainer"); for (Map.Entry<Class<?>, Class<?>> entry : applicationScoped.entrySet()) { logger.debug( "Initializing application scope with key: " + entry.getKey() + ", for component: " + entry.getValue()); this.appContainer.addComponent(entry.getKey(), entry.getValue()); } registerComponentFactories(appContainer, componentFactoryRegistry.getApplicationMap()); logger.debug("Session components to initialize: " + sessionScoped.keySet()); logger.debug("Requets components to initialize: " + requestScoped.keySet()); this.initialized = true; }
private MutablePicoContainer createSessionContainer(HttpSession session) { MutablePicoContainer sessionContainer = new DefaultPicoContainer( new Caching(), new JavaEE5LifecycleStrategy(new NullComponentMonitor()), this.appContainer); sessionContainer.addComponent(HttpSession.class, session); session.setAttribute(CONTAINER_SESSION_KEY, sessionContainer); if (logger.isDebugEnabled()) { logger.debug("Session components are " + sessionScoped); } for (Map.Entry<Class<?>, Class<?>> entry : sessionScoped.entrySet()) { sessionContainer.addComponent(entry.getKey(), entry.getValue()); } registerComponentFactories(sessionContainer, componentFactoryRegistry.getSessionMap()); sessionContainer.start(); return sessionContainer; }