Пример #1
0
  /*
   * Registers a subcontext with red5
   */
  public void registerSubContext(String webAppKey) {
    // get the sub contexts - servlet context
    ServletContext ctx = servletContext.getContext(webAppKey);
    if (ctx == null) {
      ctx = servletContext;
    }
    ContextLoader loader = new ContextLoader();
    ConfigurableWebApplicationContext appCtx =
        (ConfigurableWebApplicationContext) loader.initWebApplicationContext(ctx);
    appCtx.setParent(applicationContext);
    appCtx.refresh();

    ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx);

    ConfigurableBeanFactory appFactory = appCtx.getBeanFactory();

    logger.debug("About to grab Webcontext bean for " + webAppKey);
    Context webContext = (Context) appCtx.getBean("web.context");
    webContext.setCoreBeanFactory(parentFactory);
    webContext.setClientRegistry(clientRegistry);
    webContext.setServiceInvoker(globalInvoker);
    webContext.setScopeResolver(globalResolver);
    webContext.setMappingStrategy(globalStrategy);

    WebScope scope = (WebScope) appFactory.getBean("web.scope");
    scope.setServer(server);
    scope.setParent(global);
    scope.register();
    scope.start();

    // register the context so we dont try to reinitialize it
    registeredContexts.add(ctx);
  }
  /**
   * Obtain a resource object for the given name and type through autowiring based on the given
   * factory.
   *
   * @param factory the factory to autowire against
   * @param element the descriptor for the annotated field/method
   * @param requestingBeanName the name of the requesting bean
   * @return the resource object (never {@code null})
   * @throws BeansException if we failed to obtain the target resource
   */
  protected Object autowireResource(
      BeanFactory factory, LookupElement element, String requestingBeanName) throws BeansException {

    Object resource;
    Set<String> autowiredBeanNames;
    String name = element.name;

    if (this.fallbackToDefaultTypeMatch
        && element.isDefaultName
        && factory instanceof AutowireCapableBeanFactory
        && !factory.containsBean(name)) {
      autowiredBeanNames = new LinkedHashSet<String>();
      resource =
          ((AutowireCapableBeanFactory) factory)
              .resolveDependency(
                  element.getDependencyDescriptor(), requestingBeanName, autowiredBeanNames, null);
    } else {
      resource = factory.getBean(name, element.lookupType);
      autowiredBeanNames = Collections.singleton(name);
    }

    if (factory instanceof ConfigurableBeanFactory) {
      ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
      for (String autowiredBeanName : autowiredBeanNames) {
        if (beanFactory.containsBean(autowiredBeanName)) {
          beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
        }
      }
    }

    return resource;
  }
Пример #3
0
 /**
  * Clearing the in-memory configuration parameters, we will receive notification that the servlet
  * context is about to be shut down
  */
 @Override
 public void contextDestroyed(ServletContextEvent sce) {
   synchronized (servletContext) {
     logger.info("Webapp shutdown");
     // XXX Paul: grabbed this from
     // http://opensource.atlassian.com/confluence/spring/display/DISC/Memory+leak+-+classloader+won%27t+let+go
     // in hopes that we can clear all the issues with J2EE containers
     // during shutdown
     try {
       ServletContext ctx = sce.getServletContext();
       // prepare spring for shutdown
       Introspector.flushCaches();
       // dereg any drivers
       for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements(); ) {
         Driver driver = (Driver) e.nextElement();
         if (driver.getClass().getClassLoader() == getClass().getClassLoader()) {
           DriverManager.deregisterDriver(driver);
         }
       }
       // shutdown jmx
       JMXAgent.shutdown();
       // shutdown the persistence thread
       FilePersistenceThread persistenceThread = FilePersistenceThread.getInstance();
       if (persistenceThread != null) {
         persistenceThread.shutdown();
       }
       // shutdown spring
       Object attr =
           ctx.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
       if (attr != null) {
         // get web application context from the servlet context
         ConfigurableWebApplicationContext applicationContext =
             (ConfigurableWebApplicationContext) attr;
         ConfigurableBeanFactory factory = applicationContext.getBeanFactory();
         // for (String scope : factory.getRegisteredScopeNames()) {
         // logger.debug("Registered scope: " + scope);
         // }
         try {
           for (String singleton : factory.getSingletonNames()) {
             logger.debug("Registered singleton: " + singleton);
             factory.destroyScopedBean(singleton);
           }
         } catch (RuntimeException e) {
         }
         factory.destroySingletons();
         ctx.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
         applicationContext.close();
       }
       getContextLoader().closeWebApplicationContext(ctx);
       //				org.apache.commons.logging.LogFactory.releaseAll();
       //				org.apache.log4j.LogManager.getLoggerRepository().shutdown();
       //				org.apache.log4j.LogManager.shutdown();
     } catch (Throwable e) {
       e.printStackTrace();
     }
   }
 }
Пример #4
0
  @Test
  public void shouldReturnAllDependenciesForBean() {
    AutowireCapableBeanFactory capableBeanFactory =
        applicationContext.getAutowireCapableBeanFactory();
    if (capableBeanFactory instanceof ConfigurableBeanFactory) {
      ConfigurableBeanFactory singleBeanRegistry = (ConfigurableBeanFactory) capableBeanFactory;
      String[] dependenciesForBean = singleBeanRegistry.getDependenciesForBean("bookingService");
      Assert.assertThat(dependenciesForBean, Matchers.hasItemInArray("jdbcTemplate"));

      System.out.format(
          "Bean bookingService : dependent on : %s", Arrays.toString(dependenciesForBean));
    }
  }
Пример #5
0
  // Notification that the web application is ready to process requests
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    if (null != servletContext) {
      return;
    }
    System.setProperty("red5.deployment.type", "war");

    servletContext = sce.getServletContext();
    String prefix = servletContext.getRealPath("/");

    long time = System.currentTimeMillis();

    logger.info("RED5 Server (http://www.osflash.org/red5)");
    logger.info("WAR loader");
    logger.debug("Path: " + prefix);

    try {
      // instance the context loader
      contextLoader = createContextLoader();
      applicationContext =
          (ConfigurableWebApplicationContext)
              contextLoader.initWebApplicationContext(servletContext);
      logger.debug("Root context path: " + applicationContext.getServletContext().getContextPath());

      ConfigurableBeanFactory factory = applicationContext.getBeanFactory();

      // register default
      factory.registerSingleton("default.context", applicationContext);

      // get the main factory
      parentFactory = (DefaultListableBeanFactory) factory.getParentBeanFactory();

    } catch (Throwable t) {
      logger.error("", t);
    }

    long startupIn = System.currentTimeMillis() - time;
    logger.info("Startup done in: " + startupIn + " ms");
  }
 @Override
 protected Object getResourceToInject(Object target, String requestingBeanName) {
   if (StringUtils.hasLength(this.beanName)) {
     if (beanFactory != null && beanFactory.containsBean(this.beanName)) {
       // Local match found for explicitly specified local bean name.
       Object bean = beanFactory.getBean(this.beanName, this.lookupType);
       if (beanFactory instanceof ConfigurableBeanFactory) {
         ((ConfigurableBeanFactory) beanFactory)
             .registerDependentBean(this.beanName, requestingBeanName);
       }
       return bean;
     } else if (this.isDefaultName && !StringUtils.hasLength(this.mappedName)) {
       throw new NoSuchBeanDefinitionException(
           this.beanName,
           "Cannot resolve 'beanName' in local BeanFactory. Consider specifying a general 'name' value instead.");
     }
   }
   // JNDI name lookup - may still go to a local BeanFactory.
   return getResource(this, requestingBeanName);
 }
 @Override
 protected void registerMockFlowBeans(ConfigurableBeanFactory flowBeanFactory) {
   flowBeanFactory.registerSingleton("hospitalManager", hospitalManager);
 }
Пример #8
0
 @Override
 public void afterPropertiesSet() throws Exception {
   configurableBeanFactory.registerScope("session", new SimpleThreadScope());
 }