Search in sources :

Example 11 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class ListConfigurator method getList.

public static <T> List<T> getList(final Properties properties, final String key, final ClassLoader classloader, final Class<T> filter) {
    if (properties == null) {
        return null;
    }
    final String features = properties.getProperty(key);
    if (features == null) {
        return null;
    }
    final List<T> list = new ArrayList<T>();
    final String[] split = features.trim().split(",");
    for (final String feature : split) {
        if (feature == null || feature.trim().isEmpty()) {
            continue;
        }
        final String prefix = key + "." + feature + ".";
        final ObjectRecipe recipe = new ObjectRecipe(feature);
        for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
            final String current = entry.getKey().toString();
            if (current.startsWith(prefix)) {
                final String property = current.substring(prefix.length());
                recipe.setProperty(property, entry.getValue());
            }
        }
        final Object instance = recipe.create(classloader);
        if (!filter.isInstance(instance)) {
            throw new OpenEJBRuntimeException(feature + " is not an abstract feature");
        }
        list.add(filter.cast(instance));
    }
    if (list.isEmpty()) {
        return null;
    }
    return list;
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 12 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class DynamicProxyImplFactory method newProxy.

public static Object newProxy(final BeanContext context, final InvocationHandler invocationHandler) {
    if (QueryProxy.class.isInstance(invocationHandler)) {
        EntityManager em = null;
        for (final Injection injection : context.getInjections()) {
            if (QueryProxy.class.equals(injection.getTarget())) {
                try {
                    em = (EntityManager) context.getJndiEnc().lookup(injection.getJndiName());
                } catch (final NamingException e) {
                    throw new OpenEJBRuntimeException("a dynamic bean should reference at least one correct PersistenceContext", e);
                }
            }
        }
        if (em == null) {
            throw new OpenEJBRuntimeException("can't find the entity manager to use for the dynamic bean " + context.getEjbName());
        }
        QueryProxy.class.cast(invocationHandler).setEntityManager(em);
    }
    return newProxy(context.getBeanClass(), invocationHandler);
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) EntityManager(javax.persistence.EntityManager) NamingException(javax.naming.NamingException) Injection(org.apache.openejb.Injection)

Example 13 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class OpenEJBLifecycle method startApplication.

@Override
public void startApplication(final Object startupObject) {
    if (ServletContextEvent.class.isInstance(startupObject)) {
        // TODO: check it is relevant
        startServletContext(ServletContext.class.cast(getServletContext(startupObject)));
        return;
    } else if (!StartupObject.class.isInstance(startupObject)) {
        logger.debug("startupObject is not of StartupObject type; ignored");
        return;
    }
    final StartupObject stuff = (StartupObject) startupObject;
    final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    // Initalize Application Context
    logger.info("OpenWebBeans Container is starting...");
    final long begin = System.currentTimeMillis();
    try {
        Thread.currentThread().setContextClassLoader(stuff.getClassLoader());
        final AppContext appContext = stuff.getAppContext();
        if (stuff.getWebContext() == null) {
            // do it before any other things to keep our singleton finder working
            appContext.setWebBeansContext(webBeansContext);
        }
        //Load all plugins
        webBeansContext.getPluginLoader().startUp();
        //Get Plugin
        final CdiPlugin cdiPlugin = (CdiPlugin) webBeansContext.getPluginLoader().getEjbPlugin();
        cdiPlugin.setClassLoader(stuff.getClassLoader());
        cdiPlugin.setWebBeansContext(webBeansContext);
        //Configure EJB Deployments
        cdiPlugin.configureDeployments(stuff.getBeanContexts());
        //Resournce Injection Service
        final CdiResourceInjectionService injectionService = (CdiResourceInjectionService) webBeansContext.getService(ResourceInjectionService.class);
        // todo use startupObject allDeployments to find Comp in priority (otherwise we can keep N times comps and loose time at injection time
        injectionService.setAppContext(stuff.getAppContext(), stuff.getBeanContexts() != null ? stuff.getBeanContexts() : Collections.<BeanContext>emptyList());
        //Deploy the beans
        CdiScanner cdiScanner = null;
        try {
            //Scanning process
            logger.debug("Scanning classpaths for beans artifacts.");
            if (CdiScanner.class.isInstance(scannerService)) {
                cdiScanner = CdiScanner.class.cast(scannerService);
                cdiScanner.setContext(webBeansContext);
                cdiScanner.init(startupObject);
            } else {
                cdiScanner = new CdiScanner();
                cdiScanner.setContext(webBeansContext);
                cdiScanner.init(startupObject);
            }
            //Scan
            this.scannerService.scan();
            // just to let us write custom CDI Extension using our internals easily
            CURRENT_APP_INFO.set(stuff.getAppInfo());
            // before next event which can register custom beans (JAX-RS)
            addInternalBeans();
            SystemInstance.get().fireEvent(new WebBeansContextBeforeDeploy(webBeansContext));
            //Deploy bean from XML. Also configures deployments, interceptors, decorators.
            deployer.deploy(scannerService);
            // fire app event and also starts SingletonContext and ApplicationContext
            contextsService.init(startupObject);
        } catch (final Exception e1) {
            SystemInstance.get().getComponent(Assembler.class).logger.error("CDI Beans module deployment failed", e1);
            throw new OpenEJBRuntimeException(e1);
        } finally {
            CURRENT_APP_INFO.remove();
        }
        final Collection<Class<?>> ejbs = new ArrayList<>(stuff.getBeanContexts().size());
        for (final BeanContext bc : stuff.getBeanContexts()) {
            ejbs.add(bc.getManagedClass());
            final CdiEjbBean cdiEjbBean = bc.get(CdiEjbBean.class);
            if (cdiEjbBean == null) {
                continue;
            }
            if (AbstractProducer.class.isInstance(cdiEjbBean)) {
                AbstractProducer.class.cast(cdiEjbBean).defineInterceptorStack(cdiEjbBean, cdiEjbBean.getAnnotatedType(), cdiEjbBean.getWebBeansContext());
            }
            bc.mergeOWBAndOpenEJBInfo();
            bc.set(InterceptorResolutionService.BeanInterceptorInfo.class, InjectionTargetImpl.class.cast(cdiEjbBean.getInjectionTarget()).getInterceptorInfo());
            cdiEjbBean.initInternals();
        }
        //Start actual starting on sub-classes
        if (beanManager instanceof WebappBeanManager) {
            ((WebappBeanManager) beanManager).afterStart();
        }
        for (final Class<?> clazz : cdiScanner.getStartupClasses()) {
            if (ejbs.contains(clazz)) {
                continue;
            }
            starts(beanManager, clazz);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
        // cleanup threadlocal used to enrich cdi context manually
        OptimizedLoaderService.ADDITIONAL_EXTENSIONS.remove();
    }
    logger.info("OpenWebBeans Container has started, it took {0} ms.", Long.toString(System.currentTimeMillis() - begin));
}
Also used : InterceptorResolutionService(org.apache.webbeans.intercept.InterceptorResolutionService) AppContext(org.apache.openejb.AppContext) ArrayList(java.util.ArrayList) ResourceInjectionService(org.apache.webbeans.spi.ResourceInjectionService) ObjectStreamException(java.io.ObjectStreamException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) AbstractProducer(org.apache.webbeans.portable.AbstractProducer) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) ServletContext(javax.servlet.ServletContext) Assembler(org.apache.openejb.assembler.classic.Assembler)

Example 14 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class CdiPlugin method getSessionBeanProxy.

@Override
public Object getSessionBeanProxy(final Bean<?> inBean, final Class<?> interfce, final CreationalContext<?> creationalContext) {
    Object instance = cacheProxies.get(inBean);
    if (instance != null) {
        return instance;
    }
    synchronized (inBean) {
        // singleton for the app so safe to sync on it
        instance = cacheProxies.get(inBean);
        if (instance != null) {
            return instance;
        }
        final Class<? extends Annotation> scopeClass = inBean.getScope();
        final CdiEjbBean<Object> cdiEjbBean = (CdiEjbBean<Object>) inBean;
        final CreationalContext<Object> cc = (CreationalContext<Object>) creationalContext;
        if (scopeClass == null || Dependent.class == scopeClass) {
            // no need to add any layer, null = @New
            return cdiEjbBean.createEjb(cc);
        }
        // only stateful normally
        final InstanceBean<Object> bean = new InstanceBean<Object>(cdiEjbBean);
        if (webBeansContext.getBeanManagerImpl().isNormalScope(scopeClass)) {
            final BeanContext beanContext = cdiEjbBean.getBeanContext();
            final Provider provider = webBeansContext.getNormalScopeProxyFactory().getInstanceProvider(beanContext.getClassLoader(), cdiEjbBean);
            if (!beanContext.isLocalbean()) {
                final List<Class> interfaces = new ArrayList<Class>();
                final InterfaceType type = beanContext.getInterfaceType(interfce);
                if (type != null) {
                    interfaces.addAll(beanContext.getInterfaces(type));
                } else {
                    // can happen when looked up from impl instead of API in OWB -> default to business local
                    interfaces.addAll(beanContext.getInterfaces(InterfaceType.BUSINESS_LOCAL));
                }
                interfaces.add(Serializable.class);
                interfaces.add(IntraVmProxy.class);
                if (BeanType.STATEFUL.equals(beanContext.getComponentType()) || BeanType.MANAGED.equals(beanContext.getComponentType())) {
                    interfaces.add(BeanContext.Removable.class);
                }
                try {
                    instance = ProxyManager.newProxyInstance(interfaces.toArray(new Class<?>[interfaces.size()]), new InvocationHandler() {

                        @Override
                        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                            try {
                                return method.invoke(provider.get(), args);
                            } catch (final InvocationTargetException ite) {
                                throw ite.getCause();
                            }
                        }
                    });
                } catch (final IllegalAccessException e) {
                    throw new OpenEJBRuntimeException(e);
                }
            } else {
                final NormalScopeProxyFactory normalScopeProxyFactory = webBeansContext.getNormalScopeProxyFactory();
                final Class<?> proxyClass = normalScopeProxyFactory.createProxyClass(beanContext.getClassLoader(), beanContext.getBeanClass());
                instance = normalScopeProxyFactory.createProxyInstance(proxyClass, provider);
            }
            cacheProxies.put(inBean, instance);
        } else {
            final Context context = webBeansContext.getBeanManagerImpl().getContext(scopeClass);
            instance = context.get(bean, cc);
        }
        bean.setOwbProxy(instance);
        return instance;
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) NormalScopeProxyFactory(org.apache.webbeans.proxy.NormalScopeProxyFactory) ArrayList(java.util.ArrayList) Dependent(javax.enterprise.context.Dependent) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) Provider(javax.inject.Provider) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) CreationalContext(javax.enterprise.context.spi.CreationalContext) BeanContext(org.apache.openejb.BeanContext) InterfaceType(org.apache.openejb.InterfaceType)

Example 15 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class StaticDESPasswordCipher method decrypt.

/**
     * @throws RuntimeException in any case of error.
     * @see org.apache.openejb.cipher.PasswordCipher#decrypt(char[])
     */
public String decrypt(final char[] encodedPassword) {
    if (null == encodedPassword || encodedPassword.length == 0) {
        throw new IllegalArgumentException("encodedPassword cannot be null nor empty.");
    }
    try {
        final byte[] cipherText = Base64.decodeBase64(String.valueOf(encodedPassword).getBytes());
        // Get a 3DES Cipher object
        final Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        // Set it into decryption mode
        cipher.init(Cipher.DECRYPT_MODE, KEY);
        // Decrypt data
        return new String(cipher.doFinal(cipherText));
    } catch (final Exception e) {
        throw new OpenEJBRuntimeException(e);
    }
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) Cipher(javax.crypto.Cipher) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Aggregations

OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)54 IOException (java.io.IOException)20 OpenEJBException (org.apache.openejb.OpenEJBException)17 ArrayList (java.util.ArrayList)13 Properties (java.util.Properties)12 NamingException (javax.naming.NamingException)9 MalformedURLException (java.net.MalformedURLException)8 BeanContext (org.apache.openejb.BeanContext)8 ObjectStreamException (java.io.ObjectStreamException)6 URISyntaxException (java.net.URISyntaxException)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 File (java.io.File)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Method (java.lang.reflect.Method)4 URL (java.net.URL)4 ApplicationException (org.apache.openejb.ApplicationException)4 InvalidObjectException (java.io.InvalidObjectException)3 AccessException (java.rmi.AccessException)3 RemoteException (java.rmi.RemoteException)3