Search in sources :

Example 1 with PropertiesAdapter

use of org.apache.openejb.config.sys.PropertiesAdapter 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 2 with PropertiesAdapter

use of org.apache.openejb.config.sys.PropertiesAdapter in project tomee by apache.

the class LazyRealm method instance.

private Realm instance() {
    if (delegate == null) {
        synchronized (this) {
            if (delegate == null) {
                final Object instance;
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                if (container != null && container.getLoader() != null && container.getLoader().getClassLoader() != null) {
                    cl = container.getLoader().getClassLoader();
                }
                final Class<?> clazz;
                try {
                    clazz = cl.loadClass(realmClass);
                } catch (final ClassNotFoundException e) {
                    throw new TomEERuntimeException(e);
                }
                if (!cdi) {
                    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);
                    }
                } else {
                    final WebBeansContext webBeansContext;
                    try {
                        webBeansContext = WebBeansContext.currentInstance();
                        if (webBeansContext == null) {
                            return null;
                        }
                    } catch (final IllegalStateException ise) {
                        // too early to have a cdi bean, skip these methods - mainly init() but @PostConstruct works then
                        return null;
                    }
                    final BeanManager bm = webBeansContext.getBeanManagerImpl();
                    final Set<Bean<?>> beans = bm.getBeans(clazz);
                    final Bean<?> bean = bm.resolve(beans);
                    if (bean == null) {
                        return null;
                    }
                    creationalContext = bm.createCreationalContext(null);
                    instance = bm.getReference(bean, clazz, creationalContext);
                }
                if (instance == null) {
                    throw new TomEERuntimeException("realm can't be retrieved from cdi");
                }
                if (instance instanceof Realm) {
                    delegate = (Realm) instance;
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                    if (Lifecycle.class.isInstance(delegate)) {
                        if (init) {
                            try {
                                final Lifecycle lifecycle = Lifecycle.class.cast(delegate);
                                lifecycle.init();
                                if (start) {
                                    lifecycle.start();
                                }
                            } catch (final LifecycleException e) {
                            // no-op
                            }
                        }
                    }
                } else {
                    delegate = new LowTypedRealm(instance);
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                }
                for (final PropertyChangeListener listener : support.getPropertyChangeListeners()) {
                    delegate.addPropertyChangeListener(listener);
                }
            }
        }
    }
    return delegate;
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) PropertyChangeListener(java.beans.PropertyChangeListener) Lifecycle(org.apache.catalina.Lifecycle) Properties(java.util.Properties) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) Bean(javax.enterprise.inject.spi.Bean) PropertiesAdapter(org.apache.openejb.config.sys.PropertiesAdapter) WebBeansContext(org.apache.webbeans.config.WebBeansContext) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) BeanManager(javax.enterprise.inject.spi.BeanManager) Realm(org.apache.catalina.Realm)

Aggregations

IOException (java.io.IOException)2 Properties (java.util.Properties)2 Lifecycle (org.apache.catalina.Lifecycle)2 LifecycleException (org.apache.catalina.LifecycleException)2 PropertiesAdapter (org.apache.openejb.config.sys.PropertiesAdapter)2 TomEERuntimeException (org.apache.tomee.catalina.TomEERuntimeException)2 ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)2 PropertyChangeListener (java.beans.PropertyChangeListener)1 Bean (javax.enterprise.inject.spi.Bean)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 ServletException (javax.servlet.ServletException)1 Contained (org.apache.catalina.Contained)1 LifecycleListener (org.apache.catalina.LifecycleListener)1 Realm (org.apache.catalina.Realm)1 Valve (org.apache.catalina.Valve)1 ClusterValve (org.apache.catalina.ha.ClusterValve)1 WebBeansContext (org.apache.webbeans.config.WebBeansContext)1