Example usage for org.springframework.web.context.support WebApplicationContextUtils getRequiredWebApplicationContext

List of usage examples for org.springframework.web.context.support WebApplicationContextUtils getRequiredWebApplicationContext

Introduction

In this page you can find the example usage for org.springframework.web.context.support WebApplicationContextUtils getRequiredWebApplicationContext.

Prototype

public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)
        throws IllegalStateException 

Source Link

Document

Find the root WebApplicationContext for this web app, typically loaded via org.springframework.web.context.ContextLoaderListener .

Usage

From source file:org.apache.maven.archiva.web.ras.RasSearchByKeyword.java

public void init(javax.servlet.ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletConfig.getServletContext());
    userRepositories = (UserRepositories) wac
            .getBean(PlexusToSpringUtils.buildSpringId(UserRepositories.class.getName()));
    servletAuth = (ServletAuthenticator) wac
            .getBean(PlexusToSpringUtils.buildSpringId(ServletAuthenticator.class.getName()));
    httpAuth = (HttpAuthenticator) wac//from w  w  w  .  j  a v a  2  s.c  o m
            .getBean(PlexusToSpringUtils.buildSpringId(HttpAuthenticator.ROLE, "basic"));
    archivaXworkUser = (ArchivaXworkUser) wac
            .getBean(PlexusToSpringUtils.buildSpringId(ArchivaXworkUser.class));
    //instantiate DAO
    dao = (ArchivaDAO) wac.getBean(PlexusToSpringUtils.buildSpringId(ArchivaDAO.class.getName(), "jdo"));

    crossRepo = (CrossRepositorySearch) wac
            .getBean(PlexusToSpringUtils.buildSpringId(CrossRepositorySearch.class.getName(), "default"));
}

From source file:org.skyinn.quasar.struts.action.BaseAction.java

public Service getService(String servicName) {
    if (ctx == null) {
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
    }/*from  w w w . j  av a 2  s . co  m*/

    Service s = null;
    //TODO:exception catch...
    try {
        s = (Service) ctx.getBean(servicName);
    } catch (BeansException e) {
        e.printStackTrace();
    }
    return s;
}

From source file:it.scoppelletti.programmerpower.web.view.AuthorizeComponent.java

/**
 * Restituisce il gestore delle espressioni.
 * //  w  w w . j  a v a  2  s  . c  o m
 * @return Oggetto.
 */
private DefaultWebSecurityExpressionHandler getExpressionHandler() {
    ServletContext servletCtx;
    ApplicationContext applCtx;
    DefaultWebSecurityExpressionHandler exprHandler;
    Map<String, DefaultWebSecurityExpressionHandler> exprHandlers;

    servletCtx = ServletActionContext.getServletContext();
    applCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletCtx);
    exprHandlers = applCtx.getBeansOfType(DefaultWebSecurityExpressionHandler.class);

    if (exprHandlers.size() == 0) {
        throw new ObjectNotFoundException(DefaultWebSecurityExpressionHandler.class.getName());
    }

    exprHandler = (DefaultWebSecurityExpressionHandler) exprHandlers.values().toArray()[0];

    return exprHandler;
}

From source file:se.trillian.goodies.stripes.spring.SpringRuntimeConfiguration.java

/**
 * Autowires all properties for this configuration by retrieving the Spring
 * web application context and calls {@link DefaultConfiguration#init()}.
 *//* ww w. j a v a 2 s . co  m*/
@Override
public void init() {
    WebApplicationContext springContext = WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext());
    AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory();
    beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);

    super.init();
}

From source file:com.gisgraphy.webapp.listener.StartupListener.java

/**
 * This method uses the LookupManager to lookup available roles from the
 * data layer.//from www.j a  va2s .c  o  m
 * 
 * @param context
 *                The servlet context
 */
public static void setupContext(ServletContext context) {
    ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
    LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");

    // get list of possible roles
    context.setAttribute(Constants.AVAILABLE_ROLES, mgr.getAllRoles());
    log.debug("Drop-down initialization complete [OK]");
}

From source file:com.cubusmail.server.mail.security.MailboxLoginModule.java

public boolean login() throws LoginException {

    if (this.callbackHandler == null) {
        log.fatal("callbackHandler is null");
        throw new LoginException(IErrorCodes.EXCEPTION_AUTHENTICATION_FAILED);
    }/*w  w w  .jav a  2 s. c o m*/

    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Username");
    callbacks[1] = new PasswordCallback("Password", false);

    try {
        this.callbackHandler.handle(callbacks);
        String username = ((NameCallback) callbacks[0]).getName();

        char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
        if (tmpPassword == null) {
            // treat a NULL password as an empty password
            tmpPassword = new char[0];
        }
        char[] password = new char[tmpPassword.length];
        System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);
        ((PasswordCallback) callbacks[1]).clearPassword();

        // start authentication
        // TODO: very dirty, must be replaced by Spring Security stuff
        ApplicationContext context = WebApplicationContextUtils
                .getRequiredWebApplicationContext(SessionManager.getRequest().getSession().getServletContext());
        MailboxFactory factory = context.getBean(MailboxFactory.class);
        IMailbox mailbox = factory.createMailbox(IMailbox.TYPE_IMAP);
        mailbox.init(username, new String(password));

        log.debug("Start login...");
        mailbox.login();
        log.debug("Login successful");

        this.mailboxPrincipal = new MailboxPrincipal(username, mailbox);
        this.succeeded = true;
    } catch (IOException ioe) {
        log.error(ioe.getMessage(), ioe);
        throw new LoginException(ioe.toString());
    } catch (UnsupportedCallbackException uce) {
        log.error(uce.getMessage(), uce);
        throw new LoginException(IErrorCodes.EXCEPTION_AUTHENTICATION_FAILED);
    } catch (MessagingException e) {
        log.error(e.getMessage(), e);
        mapMessagingException(e);
    }

    return this.succeeded;
}

From source file:org.jasig.portlet.announcements.spring.PortletApplicationContextLocator.java

/**
 * @return The WebApplicationContext for the portal
 * @throws IllegalStateException if no ServletContext is available to retrieve a WebApplicationContext for or if the root WebApplicationContext could not be found
 * @deprecated This method is a work-around for areas in uPortal that do not have the ability to use the {@link org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext(javax.servlet.ServletContext)} directly.
 *//*  www . j  av a 2s  .  c o m*/
@Deprecated
public static WebApplicationContext getRequiredWebApplicationContext() {
    final ServletContext context = servletContext;
    if (context == null) {
        throw new IllegalStateException(
                "No ServletContext is available to load a WebApplicationContext for. Is this ServletContextListener not configured or has the ServletContext been destroyed?");
    }

    return WebApplicationContextUtils.getRequiredWebApplicationContext(context);
}

From source file:com.edgenius.wiki.integration.rest.ShellValidatorServlet.java

private SpaceService getSpaceServiceBean() {
    if (spaceService == null) {
        ServletContext context = this.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        spaceService = (SpaceService) ctx.getBean(SpaceService.SERVICE_NAME);
    }// ww  w. ja v a 2s .c o m
    return spaceService;
}

From source file:gov.nih.nci.cabig.ctms.acegi.csm.web.CSMAccessControlTag.java

/**
 * Allows test cases to override where application context obtained from.
 * /*  w ww .j  av  a2 s .  c om*/
 * @param pageContext
 *            so the <code>ServletContext</code> can be accessed as
 *            required by Spring's <code>WebApplicationContextUtils</code>
 * 
 * @return the Spring application context (never <code>null</code>)
 */
protected ApplicationContext getContext(PageContext pageContext) {
    ServletContext servletContext = pageContext.getServletContext();

    return WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}

From source file:org.dphibernate.serialization.SpringContextSerializerFactory.java

private ContextReference getContextReference(Class beanClass) {
    HttpServletRequest request = FlexContext.getHttpRequest();
    // Try to find the context for the correct DipsatcherServlet.
    WebApplicationContext context;//from www .  j ava 2s.c o  m
    String beanName;
    context = RequestContextUtils.getWebApplicationContext(request);
    if (context != null) {
        beanName = getUUniqueBeanName(context, beanClass);
        if (beanName != null) {
            return new ContextReference(beanName, context);
        }
    }
    // Get the root instead.
    ServletContext servletContext = FlexContext.getServletContext();
    context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    if (context != null) {
        beanName = getUUniqueBeanName(context, beanClass);
        if (beanName != null) {
            return new ContextReference(beanName, context);
        }
    }
    return null;
}