Search in sources :

Example 21 with Valve

use of org.apache.catalina.Valve in project tomee by apache.

the class LazyValve method instance.

private Valve instance() {
    if (delegate == null) {
        synchronized (this) {
            if (delegate == null) {
                final Object instance;
                ClassLoader cl = loader();
                if (cl == null) {
                    return null;
                }
                final Class<?> clazz;
                try {
                    clazz = cl.loadClass(delegateClassName);
                } catch (final ClassNotFoundException e) {
                    throw new TomEERuntimeException(e);
                }
                try {
                    final ObjectRecipe recipe = new ObjectRecipe(clazz);
                    recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
                    recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
                    recipe.allow(Option.FIELD_INJECTION);
                    recipe.allow(Option.PRIVATE_PROPERTIES);
                    if (properties != null) {
                        final Properties props = new PropertiesAdapter().unmarshal(properties.trim().replaceAll("\\p{Space}*(\\p{Alnum}*)=", "\n$1="));
                        recipe.setAllProperties(props);
                    }
                    instance = recipe.create();
                } catch (final Exception e) {
                    throw new TomEERuntimeException(e);
                }
                delegate = Valve.class.cast(instance);
                delegate.setNext(next);
                if (Contained.class.isInstance(delegate)) {
                    Contained.class.cast(delegate).setContainer(container);
                }
                if (Lifecycle.class.isInstance(delegate)) {
                    if (init) {
                        try {
                            final Lifecycle lifecycle = Lifecycle.class.cast(delegate);
                            for (final LifecycleListener listener : lifecycleListeners) {
                                lifecycle.addLifecycleListener(listener);
                            }
                            lifecycle.init();
                            if (start) {
                                lifecycle.start();
                            }
                        } catch (final LifecycleException e) {
                        // no-op
                        }
                    }
                }
                if (ClusterValve.class.isInstance(delegate)) {
                    ClusterValve.class.cast(delegate).setCluster(cluster);
                }
            }
        }
    }
    return delegate;
}
Also used : Contained(org.apache.catalina.Contained) LifecycleException(org.apache.catalina.LifecycleException) Lifecycle(org.apache.catalina.Lifecycle) LifecycleListener(org.apache.catalina.LifecycleListener) Properties(java.util.Properties) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) LifecycleException(org.apache.catalina.LifecycleException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) ClusterValve(org.apache.catalina.ha.ClusterValve) PropertiesAdapter(org.apache.openejb.config.sys.PropertiesAdapter) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Valve(org.apache.catalina.Valve) ClusterValve(org.apache.catalina.ha.ClusterValve)

Example 22 with Valve

use of org.apache.catalina.Valve in project tomee by apache.

the class TomcatHessianRegistry method deploy.

@Override
public String deploy(final ClassLoader loader, final HessianServer listener, final String hostname, final String app, final String authMethod, final String transportGuarantee, final String realmName, final String name) throws URISyntaxException {
    Container host = engine.findChild(hostname);
    if (host == null) {
        host = engine.findChild(engine.getDefaultHost());
        if (host == null) {
            throw new IllegalArgumentException("Invalid virtual host '" + engine.getDefaultHost() + "'.  Do you have a matchiing Host entry in the server.xml?");
        }
    }
    final String contextRoot = contextName(app);
    Context context = Context.class.cast(host.findChild(contextRoot));
    if (context == null) {
        Pair<Context, Integer> fakeContext = fakeContexts.get(contextRoot);
        if (fakeContext != null) {
            context = fakeContext.getLeft();
            fakeContext.setValue(fakeContext.getValue() + 1);
        } else {
            context = Context.class.cast(host.findChild(contextRoot));
            if (context == null) {
                fakeContext = fakeContexts.get(contextRoot);
                if (fakeContext == null) {
                    context = createNewContext(loader, authMethod, transportGuarantee, realmName, app);
                    fakeContext = new MutablePair<>(context, 1);
                    fakeContexts.put(contextRoot, fakeContext);
                } else {
                    context = fakeContext.getLeft();
                    fakeContext.setValue(fakeContext.getValue() + 1);
                }
            }
        }
    }
    final String servletMapping = generateServletPath(name);
    Wrapper wrapper = Wrapper.class.cast(context.findChild(servletMapping));
    if (wrapper != null) {
        throw new IllegalArgumentException("Servlet " + servletMapping + " in web application context " + context.getName() + " already exists");
    }
    wrapper = context.createWrapper();
    wrapper.setName(HESSIAN.replace("/", "") + "_" + name);
    wrapper.setServlet(new OpenEJBHessianServlet(listener));
    context.addChild(wrapper);
    context.addServletMappingDecoded(servletMapping, wrapper.getName());
    if ("BASIC".equals(authMethod) && StandardContext.class.isInstance(context)) {
        final StandardContext standardContext = StandardContext.class.cast(context);
        boolean found = false;
        for (final Valve v : standardContext.getPipeline().getValves()) {
            if (LimitedBasicValve.class.isInstance(v) || BasicAuthenticator.class.isInstance(v)) {
                found = true;
                break;
            }
        }
        if (!found) {
            standardContext.addValve(new LimitedBasicValve());
        }
    }
    final List<String> addresses = new ArrayList<>();
    for (final Connector connector : connectors) {
        for (final String mapping : wrapper.findMappings()) {
            final URI address = new URI(connector.getScheme(), null, host.getName(), connector.getPort(), contextRoot + mapping, null, null);
            addresses.add(address.toString());
        }
    }
    return HttpUtil.selectSingleAddress(addresses);
}
Also used : Context(org.apache.catalina.Context) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) Wrapper(org.apache.catalina.Wrapper) Connector(org.apache.catalina.connector.Connector) ArrayList(java.util.ArrayList) URI(java.net.URI) Container(org.apache.catalina.Container) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) OpenEJBValve(org.apache.tomee.catalina.OpenEJBValve) Valve(org.apache.catalina.Valve)

Example 23 with Valve

use of org.apache.catalina.Valve in project tomcat by apache.

the class StandardEngineSF method storeChildren.

/**
     * Store the specified Engine properties.
     *
     * @param aWriter
     *            PrintWriter to which we are storing
     * @param indent
     *            Number of spaces to indent this element
     * @param aEngine
     *            Object whose properties are being stored
     *
     * @exception Exception
     *                if an exception occurs while storing
     */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aEngine, StoreDescription parentDesc) throws Exception {
    if (aEngine instanceof StandardEngine) {
        StandardEngine engine = (StandardEngine) aEngine;
        // Store nested <Listener> elements
        LifecycleListener[] listeners = ((Lifecycle) engine).findLifecycleListeners();
        storeElementArray(aWriter, indent, listeners);
        // Store nested <Realm> element
        Realm realm = engine.getRealm();
        Realm parentRealm = null;
        // TODO is this case possible? (see it a old Server 5.0 impl)
        if (engine.getParent() != null) {
            parentRealm = engine.getParent().getRealm();
        }
        if (realm != parentRealm) {
            storeElement(aWriter, indent, realm);
        }
        // Store nested <Valve> elements
        Valve[] valves = engine.getPipeline().getValves();
        if (valves != null && valves.length > 0) {
            List<Valve> engineValves = new ArrayList<>();
            for (int i = 0; i < valves.length; i++) {
                if (!(valves[i] instanceof ClusterValve))
                    engineValves.add(valves[i]);
            }
            storeElementArray(aWriter, indent, engineValves.toArray());
        }
        // store all <Cluster> elements
        Cluster cluster = engine.getCluster();
        if (cluster != null) {
            storeElement(aWriter, indent, cluster);
        }
        // store all <Host> elements
        Container[] children = engine.findChildren();
        storeElementArray(aWriter, indent, children);
    }
}
Also used : Lifecycle(org.apache.catalina.Lifecycle) ArrayList(java.util.ArrayList) Cluster(org.apache.catalina.Cluster) LifecycleListener(org.apache.catalina.LifecycleListener) ClusterValve(org.apache.catalina.ha.ClusterValve) Container(org.apache.catalina.Container) StandardEngine(org.apache.catalina.core.StandardEngine) ClusterValve(org.apache.catalina.ha.ClusterValve) Valve(org.apache.catalina.Valve) Realm(org.apache.catalina.Realm)

Example 24 with Valve

use of org.apache.catalina.Valve in project tomcat by apache.

the class ContextConfig method authenticatorConfig.

/**
     * Set up an Authenticator automatically if required, and one has not
     * already been configured.
     */
protected void authenticatorConfig() {
    LoginConfig loginConfig = context.getLoginConfig();
    SecurityConstraint[] constraints = context.findConstraints();
    if (context.getIgnoreAnnotations() && (constraints == null || constraints.length == 0) && !context.getPreemptiveAuthentication()) {
        return;
    } else {
        if (loginConfig == null) {
            // Not metadata-complete or security constraints present, need
            // an authenticator to support @ServletSecurity annotations
            // and/or constraints
            loginConfig = DUMMY_LOGIN_CONFIG;
            context.setLoginConfig(loginConfig);
        }
    }
    // Has an authenticator been configured already?
    if (context.getAuthenticator() != null) {
        return;
    }
    // Has a Realm been configured for us to authenticate against?
    if (context.getRealm() == null) {
        log.error(sm.getString("contextConfig.missingRealm"));
        ok = false;
        return;
    }
    /*
         * First check to see if there is a custom mapping for the login
         * method. If so, use it. Otherwise, check if there is a mapping in
         * org/apache/catalina/startup/Authenticators.properties.
         */
    Valve authenticator = null;
    if (customAuthenticators != null) {
        authenticator = (Valve) customAuthenticators.get(loginConfig.getAuthMethod());
    }
    if (authenticator == null) {
        if (authenticators == null) {
            log.error(sm.getString("contextConfig.authenticatorResources"));
            ok = false;
            return;
        }
        // Identify the class name of the Valve we should configure
        String authenticatorName = authenticators.getProperty(loginConfig.getAuthMethod());
        if (authenticatorName == null) {
            log.error(sm.getString("contextConfig.authenticatorMissing", loginConfig.getAuthMethod()));
            ok = false;
            return;
        }
        // Instantiate and install an Authenticator of the requested class
        try {
            Class<?> authenticatorClass = Class.forName(authenticatorName);
            authenticator = (Valve) authenticatorClass.newInstance();
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString("contextConfig.authenticatorInstantiate", authenticatorName), t);
            ok = false;
        }
    }
    if (authenticator != null) {
        Pipeline pipeline = context.getPipeline();
        if (pipeline != null) {
            pipeline.addValve(authenticator);
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("contextConfig.authenticatorConfigured", loginConfig.getAuthMethod()));
            }
        }
    }
}
Also used : LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) Valve(org.apache.catalina.Valve) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) Pipeline(org.apache.catalina.Pipeline)

Example 25 with Valve

use of org.apache.catalina.Valve in project tomcat by apache.

the class ContainerMBean method removeValve.

/**
     * Remove an existing Valve.
     *
     * @param valveName MBean Name of the Valve to remove
     *
     * @exception MBeanException if a component cannot be removed
     */
public void removeValve(String valveName) throws MBeanException {
    Container container = doGetManagedResource();
    ObjectName oname;
    try {
        oname = new ObjectName(valveName);
    } catch (MalformedObjectNameException e) {
        throw new MBeanException(e);
    } catch (NullPointerException e) {
        throw new MBeanException(e);
    }
    if (container != null) {
        Valve[] valves = container.getPipeline().getValves();
        for (int i = 0; i < valves.length; i++) {
            if (valves[i] instanceof JmxEnabled) {
                ObjectName voname = ((JmxEnabled) valves[i]).getObjectName();
                if (voname.equals(oname)) {
                    container.getPipeline().removeValve(valves[i]);
                }
            }
        }
    }
}
Also used : Container(org.apache.catalina.Container) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException) Valve(org.apache.catalina.Valve) ObjectName(javax.management.ObjectName) JmxEnabled(org.apache.catalina.JmxEnabled)

Aggregations

Valve (org.apache.catalina.Valve)34 Container (org.apache.catalina.Container)10 ArrayList (java.util.ArrayList)8 Lifecycle (org.apache.catalina.Lifecycle)8 JmxEnabled (org.apache.catalina.JmxEnabled)6 LifecycleException (org.apache.catalina.LifecycleException)6 LifecycleListener (org.apache.catalina.LifecycleListener)6 ObjectName (javax.management.ObjectName)5 Realm (org.apache.catalina.Realm)5 ClusterValve (org.apache.catalina.ha.ClusterValve)5 Contained (org.apache.catalina.Contained)4 Pipeline (org.apache.catalina.Pipeline)4 Cluster (org.apache.catalina.Cluster)3 StandardContext (org.apache.catalina.core.StandardContext)3 IOException (java.io.IOException)2 ServletContext (javax.servlet.ServletContext)2 JvmRouteBinderValve (org.apache.catalina.ha.session.JvmRouteBinderValve)2 AccessLogValve (org.apache.catalina.valves.AccessLogValve)2 RemoteIpValve (org.apache.catalina.valves.RemoteIpValve)2 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2