Package org.springframework.web.context.support

Examples of org.springframework.web.context.support.XmlWebApplicationContext


    ServletContext application = event.getServletContext();

    //load logging system manually so that we get the placeholders ${} swapped out for real values
    PropertyConfigurator.configure(application.getRealPath("/")+"/WEB-INF/log4j.miso.properties");

    XmlWebApplicationContext context = (XmlWebApplicationContext)WebApplicationContextUtils.getRequiredWebApplicationContext(application);

    //resolve property file configuration placeholders
    MisoPropertyExporter exporter = (MisoPropertyExporter) context.getBean("propertyConfigurer");
    Map<String, String> misoProperties = exporter.getResolvedProperties();
           
    log.info("Checking MISO storage paths...");
    String baseStoragePath = misoProperties.get("miso.baseDirectory");
    context.getServletContext().setAttribute("miso.baseDirectory", baseStoragePath);

    String taxonLookupEnabled = misoProperties.get("miso.taxonLookup.enabled");
    context.getServletContext().setAttribute("taxonLookupEnabled", Boolean.parseBoolean(taxonLookupEnabled));
   
    Map<String, String> dirchecks = MisoWebUtils.checkStorageDirectories(baseStoragePath);
    if (dirchecks.keySet().contains("error")) {
      log.error(dirchecks.get("error"));
    }
    else {
      log.info(dirchecks.get("ok"));
    }

    //set headless property so JFreeChart doesn't try to use the X rendering system to generate images
    System.setProperty("java.awt.headless", "true");

    //set up naming schemes
    MisoEntityNamingSchemeResolverService entityNamingSchemeResolverService = (MisoEntityNamingSchemeResolverService)context.getBean("entityNamingSchemeResolverService");
    Collection<MisoNamingScheme<?>> mnss = entityNamingSchemeResolverService.getNamingSchemes();

    MisoNameGeneratorResolverService nameGeneratorResolverService = (MisoNameGeneratorResolverService)context.getBean("nameGeneratorResolverService");
    Collection<NameGenerator<?>> ngs = nameGeneratorResolverService.getNameGenerators();

    for (MisoNamingScheme<?> mns : mnss) {
      log.info("Got naming scheme: " + mns.getSchemeName());
      String classname = mns.namingSchemeFor().getSimpleName().toLowerCase();

      if (misoProperties.containsKey("miso.naming.scheme."+classname) && misoProperties.get("miso.naming.scheme."+classname).equals(mns.getSchemeName())) {
        for (String key : misoProperties.keySet()) {
          if (key.startsWith("miso.naming.generator."+classname)) {
            String genprop = key.substring(key.lastIndexOf(".")+1);
            NameGenerator ng = nameGeneratorResolverService.getNameGenerator(misoProperties.get("miso.naming.generator."+classname+"."+genprop));
            if (ng != null) {
              mns.registerCustomNameGenerator(genprop, ng);
            }
          }
        }

        if ("nameable".equals(classname)) {
          log.info("Replacing default global namingScheme with " + mns.getSchemeName());
          ((DefaultListableBeanFactory)context.getBeanFactory()).removeBeanDefinition("namingScheme");
          context.getBeanFactory().registerSingleton("namingScheme", mns);
        }
        else {
          log.info("Replacing default "+classname+"NamingScheme with " + mns.getSchemeName());
          ((DefaultListableBeanFactory)context.getBeanFactory()).removeBeanDefinition(classname+"NamingScheme");
          context.getBeanFactory().registerSingleton(classname+"NamingScheme", mns);
        }
      }

      for (String key : misoProperties.keySet()) {
        if (key.startsWith("miso.naming.validation."+classname)) {
          String prop = key.substring(key.lastIndexOf(".")+1);

          try {
            mns.setValidationRegex(prop, misoProperties.get("miso.naming.validation."+classname+"."+prop));
          }
          catch (MisoNamingException e) {
            log.error("Cannot set new validation regex for field '"+prop+"'. Reverting to default: " + e);
            e.printStackTrace();
          }
        }
      }
    }

    //set up printers
    PrintManager printManager = (PrintManager)context.getBean("printManager");
    Collection<PrintContext> pcs = printManager.getPrintContexts();
    for (PrintContext pc : pcs) {
      log.info(pc.getName() + " : " + pc.getDescription());
    }

    try {
      Collection<MisoPrintService> mpss = printManager.listAllPrintServices();
      for (MisoPrintService mps : mpss) {
        log.info("Got print service: " + mps.toString());
      }
    }
    catch (Exception e) {
      log.error("Could not list print services. This does not bode well for printing.", e);
      e.printStackTrace();
    }

    //set up Tag Barcode strategies
    TagBarcodeStrategyResolverService tagBarcodeService = (TagBarcodeStrategyResolverService)context.getBean("tagBarcodeStrategyResolverService");
    Collection<TagBarcodeStrategy> tbss = tagBarcodeService.getTagBarcodeStrategies();
    for (TagBarcodeStrategy tbs : tbss) {
      log.info("Got Tag Barcode Index service: " + tbs.getName());
    }

    //set up alerting
    if ("true".equals(misoProperties.get("miso.alerting.enabled"))) {
      //set up indexers and alerters
      MisoRequestManager rm = new MisoRequestManager();
      rm.setProjectStore((ProjectStore)context.getBean("projectStore"));
      rm.setRunStore((RunStore)context.getBean("runStore"));
      rm.setRunQcStore((RunQcStore) context.getBean("runQcStore"));
      rm.setPoolStore((PoolStore)context.getBean("poolStore"));

      SecurityManager sm = (com.eaglegenomics.simlims.core.manager.SecurityManager)context.getBean("securityManager");

      RunAlertManager ram = (RunAlertManager)context.getBean("runAlertManager");
      ram.setRequestManager(rm);
      ram.setSecurityManager(sm);

      ProjectAlertManager pam = (ProjectAlertManager)context.getBean("projectAlertManager");
      pam.setRequestManager(rm);
      pam.setSecurityManager(sm);

      PoolAlertManager poam = (PoolAlertManager)context.getBean("poolAlertManager");
      poam.setRequestManager(rm);
      poam.setSecurityManager(sm);
    }

    if (misoProperties.containsKey("miso.db.caching.mappers.enabled")) {
      boolean mapperCachingEnabled = Boolean.parseBoolean(misoProperties.get("miso.db.caching.mappers.enabled"));
      //TODO do something with this - probably set caching throughout DAOs
    }

    if ("true".equals(misoProperties.get("miso.db.caching.precache.enabled"))) {
      log.info("Precaching. This may take a while.");
      try {
        RequestManager rm = (RequestManager)context.getBean("requestManager");

        User userdetails = new User("precacher", "none", true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_ADMIN,ROLE_INTERNAL"));
        PreAuthenticatedAuthenticationToken newAuthentication = new PreAuthenticatedAuthenticationToken(userdetails, userdetails.getPassword(), userdetails.getAuthorities());
        newAuthentication.setAuthenticated(true);
        newAuthentication.setDetails(userdetails);

        try {
          SecurityContext sc = SecurityContextHolder.getContextHolderStrategy().getContext();
          sc.setAuthentication(newAuthentication);
          SecurityContextHolder.getContextHolderStrategy().setContext(sc);
        }
        catch (AuthenticationException a) {
          a.printStackTrace();
        }

        log.info("\\_ projects...");
        log.info("" + rm.listAllProjects().size());
        log.info("\\_ samples...");
        log.info("" + rm.listAllSamples().size());
        log.info("\\_ libraries...");
        log.info("" + rm.listAllLibraries().size());
        log.info("\\_ dilutions...");
        log.info("" + rm.listAllLibraryDilutions().size());
        log.info("" + rm.listAllEmPcrDilutions().size());
        log.info("\\_ pools...");
        log.info("" + rm.listAllPools().size());
        log.info("\\_ plates...");
        log.info("" + rm.listAllPlates().size());
        log.info("\\_ runs...");
        log.info("" + rm.listAllRuns().size());
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }

    if ("true".equals(misoProperties.get("miso.issuetracker.enabled"))) {
      String trackerType = misoProperties.get("miso.issuetracker.tracker");
      if (trackerType != null && !"".equals(trackerType)) {
        try {
          IssueTrackerManager manager = IssueTrackerFactory.newInstance().getTrackerManager(trackerType);
          if (manager != null) {
            for (String key : misoProperties.keySet()) {
              if (key.startsWith("miso.issuetracker."+trackerType)) {
                String prop = key.substring(key.lastIndexOf(".")+1);
                String methodName = "set"+ LimsUtils.capitalise(prop); //prop.substring(0,1).toUpperCase() + prop.substring(1);
                Method m = manager.getClass().getDeclaredMethod(methodName, String.class);
                m.invoke(manager, misoProperties.get(key));
              }
            }
            ((DefaultListableBeanFactory)context.getBeanFactory()).removeBeanDefinition("issueTrackerManager");
            context.getBeanFactory().registerSingleton("issueTrackerManager", manager);
          }
          else {
            log.error("No such issue tracker available with given type: " + trackerType);
          }
        }
        catch (NoSuchMethodException e) {
          log.error("Unable to start the defined issuetracker " + trackerType + ": " + e.getMessage());
          e.printStackTrace();
        }
        catch (InvocationTargetException e) {
          log.error("Unable to start the defined issuetracker " + trackerType + ": " + e.getMessage());
          e.printStackTrace();
        }
        catch (IllegalAccessException e) {
          log.error("Unable to start the defined issuetracker " + trackerType + ": " + e.getMessage());
          e.printStackTrace();
        }
      }
    }

    if ("true".equals(misoProperties.get("miso.statsdb.enabled"))) {
      try {
        JndiObjectFactoryBean jndiBean = new JndiObjectFactoryBean();
        jndiBean.setLookupOnStartup(true);
        jndiBean.setResourceRef(true);
        jndiBean.setJndiName("jdbc/STATSDB");
        jndiBean.setExpectedType(javax.sql.DataSource.class);
        jndiBean.afterPropertiesSet();

        DataSource datasource = (DataSource)jndiBean.getObject();

        JdbcTemplate template = new JdbcTemplate();
        template.setDataSource(datasource);
        template.setNativeJdbcExtractor(new CommonsDbcpNativeJdbcExtractor());
        context.getBeanFactory().registerSingleton("statsInterfaceTemplate", template);

        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(datasource);
        context.getBeanFactory().registerSingleton("statsTransactionManager", transactionManager);

        RunStatsManager rsm = new RunStatsManager(template);
        context.getBeanFactory().registerSingleton("runStatsManager", rsm);
      }
      catch (NamingException e) {
        log.error("Cannot initiate statsdb connection: " + e.getMessage());
        e.printStackTrace();
      }
View Full Code Here


   */
  public void setParentContext(String parentContextKey, String appContextId) {
    log.debug("Set parent context {} on {}", parentContextKey, appContextId);
    ApplicationContext parentContext = getContext(parentContextKey);
    if (parentContext != null) {
      XmlWebApplicationContext childContext = (XmlWebApplicationContext) getContext(appContextId);
      if (childContext != null) {
        childContext.setParent(parentContext);
      } else {
        log.debug("Child context not found");
      }
    } else {
      log.debug("Parent context not found");
View Full Code Here

    @BeforeClass
    public static void initialize() {
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/resource-bundle.xml");
        assertNotNull(applicationContext);
       
        webApplicationContext = new XmlWebApplicationContext();
        webApplicationContext.setConfigLocation("file:src/main/webapp/WEB-INF/spring/*-context.xml");
        webApplicationContext.refresh();
        assertNotNull(webApplicationContext);
    }
View Full Code Here

*/
public class WebAppInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext container) {
    XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    appContext.setConfigLocation("/WEB-INF/applicationContext.xml");

    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
View Full Code Here

*/
public class WebAppInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext container) {
    XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    appContext.setConfigLocation("/WEB-INF/applicationContext.xml");

    ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
View Full Code Here

public class SimpleBeanAccessTests {
 
  @Test
  public void test() {

    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setConfigLocation("file:src/main/webapp/WEB-INF/spring/root-context.xml");
    wac.setServletContext(new MockServletContext());
    wac.refresh();
   
    ServletWebRequest webRequest = new ServletWebRequest(new MockHttpServletRequest());
    RequestContextHolder.setRequestAttributes(webRequest);
   
    MyBeanClass myBeanClass = wac.getBean("myBeanClass", MyBeanClass.class);
    assertNull(myBeanClass);
   
    MyBeanClass myProxiedBeanClass = wac.getBean("myProxiedBeanClass", MyBeanClass.class);
    assertNotNull(myProxiedBeanClass);
    assertEquals("foo", myProxiedBeanClass.getFoo())// this will cause NPE

    RequestContextHolder.resetRequestAttributes();
  }
View Full Code Here

   *
   * @return application context instance
   */
  protected ConfigurableWebApplicationContext newApplicationContext()
  {
    return new XmlWebApplicationContext();
  }
View Full Code Here

    public void testWithSpringContext() throws Exception {
        Container container = EasyMock.createNiceMock(Container.class);
        EasyMock.replay(container);

        ConfigurableWebApplicationContext ac = new XmlWebApplicationContext();
        ServletContext msc = (ServletContext) new MockServletContext();
        msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
        ac.setServletContext(msc);
        ac.setConfigLocations(new String[] {"org/apache/struts2/spring/StrutsSpringObjectFactoryTest-applicationContext.xml"});
        ac.refresh();
        StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory("constructor", null, null, msc, null, container);

        assertEquals(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, fac.getAutowireStrategy());
    }
View Full Code Here

   *
   * @return application context instance
   */
  protected ConfigurableWebApplicationContext newApplicationContext()
  {
    return new XmlWebApplicationContext();
  }
View Full Code Here

    private WebApplicationContext createWebapplicationContext(ServletContext servletContext) {
        String webconsoleType = System.getProperty("webconsole.type", "embedded");
        String configuration = "/WEB-INF/webconsole-" + webconsoleType + ".xml";

        XmlWebApplicationContext context = new XmlWebApplicationContext();
        context.setServletContext(servletContext);
        context.setConfigLocations(new String[] {
            configuration
        });
        context.refresh();
        context.start();

        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);

        return context;
    }
View Full Code Here

TOP

Related Classes of org.springframework.web.context.support.XmlWebApplicationContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.