Search in sources :

Example 1 with TomEERuntimeException

use of org.apache.tomee.catalina.TomEERuntimeException 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 TomEERuntimeException

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

the class EmbeddedTomEEContainer method close.

@Override
public void close() {
    final Collection<Exception> errors = new ArrayList<>();
    final EmbeddedTomEEContainer etc = tomEEContainer.get();
    if (null != etc) {
        for (final String id : deployedIds) {
            if (etc.container.getAppContexts(id) != null) {
                try {
                    etc.container.undeploy(id);
                } catch (final Exception ex) {
                    Logger.getInstance(LogCategory.OPENEJB, EmbeddedTomEEContainer.class).error(ex.getMessage(), ex);
                    errors.add(ex);
                }
            }
        }
        try {
            etc.container.close();
        } catch (final Exception ex) {
            errors.add(ex);
            Logger.getInstance(LogCategory.OPENEJB, EmbeddedTomEEContainer.class).error(ex.getMessage(), ex);
        }
    }
    deployedIds.clear();
    tomEEContainer.set(null);
    if (!errors.isEmpty()) {
        throw Exceptions.newEJBException(new TomEERuntimeException(errors.toString()));
    }
}
Also used : ArrayList(java.util.ArrayList) MalformedURLException(java.net.MalformedURLException) OpenEJBException(org.apache.openejb.OpenEJBException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) EJBException(javax.ejb.EJBException) ValidationException(javax.validation.ValidationException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException)

Example 3 with TomEERuntimeException

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

the class Container method getBaseDir.

private String getBaseDir() {
    File file;
    try {
        final String dir = configuration.getDir();
        if (dir != null) {
            final File dirFile = new File(dir);
            if (dirFile.exists()) {
                return dir;
            }
            return Files.mkdir(dirFile).getAbsolutePath();
        }
        try {
            final File target = new File("target");
            file = File.createTempFile("apache-tomee", "-home", target.exists() ? target : null);
        } catch (final Exception e) {
            final File tmp = new File(configuration.getTempDir());
            if (!tmp.exists() && !tmp.mkdirs()) {
                throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath());
            }
            file = File.createTempFile("apache-tomee", "-home", tmp);
        }
        return file.getAbsolutePath();
    } catch (final IOException e) {
        throw new TomEERuntimeException("Failed to get or create base dir: " + configuration.getDir(), e);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) NamingException(javax.naming.NamingException) UndeployException(org.apache.openejb.UndeployException) OpenEJBException(org.apache.openejb.OpenEJBException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException)

Example 4 with TomEERuntimeException

use of org.apache.tomee.catalina.TomEERuntimeException 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)

Example 5 with TomEERuntimeException

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

the class TomcatWsRegistry method removeWsContainer.

@Override
public void removeWsContainer(String path, final String moduleId) {
    if (path == null) {
        return;
    }
    // assure context root with a leading slash
    path = Strings.slashify(path);
    if (TomcatHelper.isStopping()) {
        return;
    }
    Context context = webserviceContexts.remove(new Key(path, moduleId));
    if (context == null) {
        // fake
        context = webserviceContexts.remove(new Key(path, null));
    }
    // > 0 to avoid to destroy the context if not mandatory
    Integer refs = 1;
    if (context != null) {
        final String name = context.getName();
        refs = fakeContextReferences.remove(name);
        if (refs != null && refs > 0) {
            fakeContextReferences.put(name, refs - 1);
        }
    }
    if ((WEBSERVICE_OLDCONTEXT_ACTIVE || (refs != null && refs == 0)) && context != null) {
        try {
            context.stop();
            context.destroy();
        } catch (final Exception e) {
            throw new TomEERuntimeException(e);
        }
        final Host host = (Host) context.getParent();
        host.removeChild(context);
    }
// else let tomcat manages its context
}
Also used : Context(org.apache.catalina.Context) IgnoredStandardContext(org.apache.tomee.catalina.IgnoredStandardContext) StandardContext(org.apache.catalina.core.StandardContext) Host(org.apache.catalina.Host) URISyntaxException(java.net.URISyntaxException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException)

Aggregations

TomEERuntimeException (org.apache.tomee.catalina.TomEERuntimeException)6 IOException (java.io.IOException)4 MalformedURLException (java.net.MalformedURLException)3 Properties (java.util.Properties)3 LifecycleException (org.apache.catalina.LifecycleException)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 File (java.io.File)2 NamingException (javax.naming.NamingException)2 Lifecycle (org.apache.catalina.Lifecycle)2 UndeployException (org.apache.openejb.UndeployException)2 PropertiesAdapter (org.apache.openejb.config.sys.PropertiesAdapter)2 ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)2 PropertyChangeListener (java.beans.PropertyChangeListener)1 URISyntaxException (java.net.URISyntaxException)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 Scanner (java.util.Scanner)1 EJBException (javax.ejb.EJBException)1 Bean (javax.enterprise.inject.spi.Bean)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1