Search in sources :

Example 31 with ServletConfig

use of javax.servlet.ServletConfig in project opennms by OpenNMS.

the class PollerConfigServlet method reloadFiles.

/**
 * <p>reloadFiles</p>
 *
 * @throws javax.servlet.ServletException if any.
 */
public void reloadFiles() throws ServletException {
    ServletConfig config = this.getServletConfig();
    try {
        PollerConfigFactory.init();
        pollerFactory = PollerConfigFactory.getInstance();
        pollerConfig = pollerFactory.getConfiguration();
        if (pollerConfig == null) {
            throw new ServletException("Poller Configuration file is empty");
        }
    } catch (Throwable e) {
        throw new ServletException(e.getMessage());
    }
    initPollerServices();
    this.redirectSuccess = config.getInitParameter("redirect.success");
    if (this.redirectSuccess == null) {
        throw new ServletException("Missing required init parameter: redirect.success");
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletConfig(javax.servlet.ServletConfig)

Example 32 with ServletConfig

use of javax.servlet.ServletConfig in project opennms by OpenNMS.

the class ImportAssetsServlet method init.

/**
 * Looks up the <code>redirect.success</code> parameter in the servlet's
 * configuration. If not present, this servlet will throw an exception so it will
 * be marked unavailable.
 *
 * @throws javax.servlet.ServletException if any.
 */
@Override
public void init() throws ServletException {
    ServletConfig config = this.getServletConfig();
    this.redirectSuccess = config.getInitParameter("redirect.success");
    if (this.redirectSuccess == null) {
        throw new UnavailableException("Require a redirect.success init parameter.");
    }
    this.model = new AssetModel();
}
Also used : ServletConfig(javax.servlet.ServletConfig) UnavailableException(javax.servlet.UnavailableException)

Example 33 with ServletConfig

use of javax.servlet.ServletConfig in project opennms by OpenNMS.

the class MailerServlet method init.

/**
 * <p>init</p>
 *
 * @throws javax.servlet.ServletException if any.
 */
@Override
public void init() throws ServletException {
    ServletConfig config = this.getServletConfig();
    this.redirectSuccess = config.getInitParameter("redirect.success");
    this.mailProgram = config.getInitParameter("mail.program");
    if (this.redirectSuccess == null) {
        throw new ServletException("Missing required init parameter: redirect.success");
    }
    if (this.mailProgram == null) {
        throw new ServletException("Missing required init parameter: mail.program");
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletConfig(javax.servlet.ServletConfig)

Example 34 with ServletConfig

use of javax.servlet.ServletConfig in project tomee by apache.

the class HttpUtil method addServlet.

public static boolean addServlet(final String classname, final WebContext wc, final String mapping) {
    final HttpListenerRegistry registry = SystemInstance.get().getComponent(HttpListenerRegistry.class);
    if (registry == null || mapping == null) {
        return false;
    }
    final ServletListener listener;
    try {
        ServletContext servletContext = wc.getServletContext();
        if (servletContext == null) {
            servletContext = SystemInstance.get().getComponent(ServletContext.class);
        }
        if ("javax.faces.webapp.FacesServlet".equals(classname)) {
            try {
                // faking it to let the FacesServlet starting
                // NOTE: needs myfaces-impl + tomcat-jasper (JspFactory)
                // TODO: handle the whole lifecycle (cleanup mainly) + use myfaces SPI to make scanning really faster (take care should work in tomee were we already have it impl)
                final Class<?> mfListenerClass = wc.getClassLoader().loadClass("org.apache.myfaces.webapp.StartupServletContextListener");
                final ServletContextListener servletContextListener = ServletContextListener.class.cast(mfListenerClass.newInstance());
                servletContext.setAttribute("javax.enterprise.inject.spi.BeanManager", new InjectableBeanManager(wc.getWebBeansContext().getBeanManagerImpl()));
                final Thread thread = Thread.currentThread();
                final ClassLoader old = setClassLoader(wc, thread);
                try {
                    servletContextListener.contextInitialized(new ServletContextEvent(servletContext));
                } finally {
                    thread.setContextClassLoader(old);
                }
                servletContext.removeAttribute("javax.enterprise.inject.spi.BeanManager");
            } catch (final Exception e) {
            // no-op
            }
        }
        final Thread thread = Thread.currentThread();
        final ClassLoader old = setClassLoader(wc, thread);
        try {
            listener = new ServletListener((Servlet) wc.newInstance(wc.getClassLoader().loadClass(classname)), wc.getContextRoot());
            final ServletContext sc = servletContext;
            listener.getDelegate().init(new ServletConfig() {

                @Override
                public String getServletName() {
                    return classname;
                }

                @Override
                public ServletContext getServletContext() {
                    return sc;
                }

                @Override
                public String getInitParameter(final String s) {
                    return sc.getInitParameter(s);
                }

                @Override
                public Enumeration<String> getInitParameterNames() {
                    final Enumeration<String> parameterNames = sc.getInitParameterNames();
                    return parameterNames == null ? Collections.<String>emptyEnumeration() : parameterNames;
                }
            });
        } finally {
            thread.setContextClassLoader(old);
        }
    } catch (final Exception e) {
        throw new OpenEJBRuntimeException(e);
    }
    registry.addHttpListener(listener, pattern(wc.getContextRoot(), "/".equals(mapping) ? "/*" : mapping));
    return true;
}
Also used : Enumeration(java.util.Enumeration) ServletContextListener(javax.servlet.ServletContextListener) InjectableBeanManager(org.apache.webbeans.container.InjectableBeanManager) ServletConfig(javax.servlet.ServletConfig) HttpListenerRegistry(org.apache.openejb.server.httpd.HttpListenerRegistry) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ServletListener(org.apache.openejb.server.httpd.ServletListener) ServletContext(javax.servlet.ServletContext) Servlet(javax.servlet.Servlet) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 35 with ServletConfig

use of javax.servlet.ServletConfig in project tomee by apache.

the class ServerServletTest method activated.

@Test
public void activated() {
    final ServerServlet serverServlet = new ServerServlet();
    serverServlet.init(new ServletConfig() {

        @Override
        public String getServletName() {
            return null;
        }

        @Override
        public ServletContext getServletContext() {
            return null;
        }

        @Override
        public String getInitParameter(final String name) {
            return "activated".equals(name) ? Boolean.toString(activated) : null;
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return null;
        }
    });
    assertEquals(activated == null || activated, Reflections.get(serverServlet, "activated"));
}
Also used : Enumeration(java.util.Enumeration) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Aggregations

ServletConfig (javax.servlet.ServletConfig)79 ServletContext (javax.servlet.ServletContext)54 Enumeration (java.util.Enumeration)38 ServletException (javax.servlet.ServletException)24 BeforeMethod (org.testng.annotations.BeforeMethod)21 Test (org.junit.Test)17 IOException (java.io.IOException)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)8 HttpServlet (javax.servlet.http.HttpServlet)7 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 UnavailableException (javax.servlet.UnavailableException)5 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)3