http://git-wip-us.apache.org/repos/asf/tomee/blob/52567075/container/openejb-core/src/main/java/org/apache/openejb/bval/BeanValidationAppendixInterceptor.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/bval/BeanValidationAppendixInterceptor.java b/container/openejb-core/src/main/java/org/apache/openejb/bval/BeanValidationAppendixInterceptor.java index 9efded1..1395f0d 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/bval/BeanValidationAppendixInterceptor.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/bval/BeanValidationAppendixInterceptor.java @@ -1,205 +1,205 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.openejb.bval; - -import org.apache.openejb.core.ThreadContext; -import org.apache.openejb.loader.SystemInstance; -import org.apache.openejb.util.LogCategory; -import org.apache.openejb.util.Logger; - -import javax.interceptor.AroundInvoke; -import javax.interceptor.InvocationContext; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.validation.ConstraintViolation; -import javax.validation.ConstraintViolationException; -import javax.validation.ValidationException; -import javax.validation.Validator; -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.Set; - -/** - * A simple interceptor to validate parameters and returned value using - * bean validation spec. It doesn't use group for now. - */ -public class BeanValidationAppendixInterceptor { - private static final Logger logger = Logger.getInstance(LogCategory.OPENEJB, BeanValidationAppendixInterceptor.class); - private static final Class<?> APACHE_BVAL_METHOD_CLASS = initApache(); - private static final Class<?> HIBERNATE_METHOD_CLASS = initHibernate(); - public static final Class<?>[] BVAL_ARG_TYPES = new Class<?>[]{ - Class.class, Method.class, Object[].class, Class[].class - }; - public static final Class<?>[] HIBERNATE_ARG_TYPES = new Class<?>[]{ - Object.class, Method.class, Object[].class, Class[].class - }; - - private static final boolean ON; - - static { - boolean on = true; - try { - BeanValidationAppendixInterceptor.class.getClassLoader().loadClass("javax.validation.executable.ExecutableValidator"); - on = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.bval.10.interceptor.force", "false")); - if (!on) { - logger.debug(BeanValidationAppendixInterceptor.class.getName() + " deactivated since BVal 1.1 is usable, use openejb.bval.10.interceptor.force=true to force it"); - } - } catch (final Throwable e) { - // no-op - } - ON = on; - } - - @AroundInvoke - public Object aroundInvoke(final InvocationContext ejbContext) throws Exception { - if (!BeanValidationAppendixInterceptor.ON) { - return ejbContext.proceed(); - } - - Object validatorObject = null; - Validator validator = null; - try { - validator = (Validator) new InitialContext().lookup("java:comp/Validator"); - } catch (final NamingException ne) { - // no-op - } catch (final Exception e) { - logger.warning("BeanValidationAppendixInterceptor unexpected error: " + e); - return ejbContext.proceed(); - } - - final ThreadContext threadContext = ThreadContext.getThreadContext(); - Class<?> bvalClazzToValidate = ejbContext.getTarget().getClass(); - if (threadContext != null && ejbContext.getTarget().getClass().getInterfaces().length > 0) { - bvalClazzToValidate = threadContext.getInvokedInterface(); - } - - Method method = ejbContext.getMethod(); - if (!bvalClazzToValidate.equals(ejbContext.getTarget().getClass())) { - method = bvalClazzToValidate.getMethod(method.getName(), method.getParameterTypes()); - } - - Set<?> violations = Collections.emptySet(); - if (APACHE_BVAL_METHOD_CLASS != null && validator != null) { - validatorObject = validator.unwrap(APACHE_BVAL_METHOD_CLASS); - violations = call(Set.class, validatorObject, "validateParameters", - new Object[]{ - bvalClazzToValidate, method, ejbContext.getParameters(), new Class[0] - }, - BVAL_ARG_TYPES); - } else if (HIBERNATE_METHOD_CLASS != null && validator != null) { - validatorObject = validator.unwrap(HIBERNATE_METHOD_CLASS); - violations = call(Set.class, validatorObject, "validateAllParameters", - new Object[]{ - ejbContext.getTarget(), ejbContext.getMethod(), ejbContext.getParameters(), new Class[0] - }, - HIBERNATE_ARG_TYPES); - } else { // a warning message to inform Apache Bean Validation is not present - if (validator == null) { - logger.error("can't find validator"); - } else { - logger.warning("Apache Bean Validation is not present, " - + BeanValidationAppendixInterceptor.class.getName() + " will not work. " - + "Please put it if you want to validate your parameters and returned values " - + "with bean validation JSR."); - } - } - - if (violations.size() > 0) { - throw buildValidationException((Set<ConstraintViolation<?>>) violations); - } - - final Object returnedValue = ejbContext.proceed(); - - violations = Collections.emptySet(); - if (validatorObject != null && APACHE_BVAL_METHOD_CLASS != null) { - violations = call(Set.class, validatorObject, "validateReturnedValue", - new Object[]{ - bvalClazzToValidate, method, returnedValue, new Class[0] - }, - new Class<?>[]{ - Class.class, Method.class, Object.class, Class[].class - }); - } else if (validatorObject != null && HIBERNATE_METHOD_CLASS != null) { - violations = call(Set.class, validatorObject, "validateReturnValue", - new Object[]{ - ejbContext.getTarget(), ejbContext.getMethod(), returnedValue, new Class[0] - }, - new Class<?>[]{ - Object.class, Method.class, Object.class, Class[].class - }); - } - - if (violations.size() > 0) { - throw buildValidationException((Set<ConstraintViolation<?>>) violations); - } - - return returnedValue; - } - - - // just a simple EJBException for now - private RuntimeException buildValidationException(final Set<ConstraintViolation<?>> violations) { - return new ConstraintViolationException(violations); - } - - private static <T> T call(final Class<T> returnedType, final Object o, final String methodName, final Object[] params, final Class<?>[] types) { - Method method = null; - boolean accessible = true; - try { - method = o.getClass().getMethod(methodName, types); - accessible = method.isAccessible(); - if (!accessible) { - accessible = false; - method.setAccessible(true); - } - return returnedType.cast(method.invoke(o, params)); - } catch (final Exception e) { - e.printStackTrace(); - throw new ValidationException("can't call method " + methodName + " on " + o, e); - } finally { - if (method != null) { - method.setAccessible(accessible); - } - } - } - - private static ClassLoader getClassLaoder() { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader == null) { - classLoader = BeanValidationAppendixInterceptor.class.getClassLoader(); - } - return classLoader; - } - - private static Class<?> initApache() { - try { - return getClassLaoder().loadClass("org.apache.bval.jsr303.extensions.MethodValidator"); - } catch (final ClassNotFoundException e) { - return null; - } - } - - private static Class<?> initHibernate() { - try { - return getClassLaoder().loadClass("org.hibernate.validator.method.MethodValidator"); - } catch (final ClassNotFoundException e) { - return null; - } - } -} - +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.bval; + +import org.apache.openejb.core.ThreadContext; +import org.apache.openejb.loader.SystemInstance; +import org.apache.openejb.util.LogCategory; +import org.apache.openejb.util.Logger; + +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.validation.ValidationException; +import javax.validation.Validator; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.Set; + +/** + * A simple interceptor to validate parameters and returned value using + * bean validation spec. It doesn't use group for now. + */ +public class BeanValidationAppendixInterceptor { + private static final Logger logger = Logger.getInstance(LogCategory.OPENEJB, BeanValidationAppendixInterceptor.class); + private static final Class<?> APACHE_BVAL_METHOD_CLASS = initApache(); + private static final Class<?> HIBERNATE_METHOD_CLASS = initHibernate(); + public static final Class<?>[] BVAL_ARG_TYPES = new Class<?>[]{ + Class.class, Method.class, Object[].class, Class[].class + }; + public static final Class<?>[] HIBERNATE_ARG_TYPES = new Class<?>[]{ + Object.class, Method.class, Object[].class, Class[].class + }; + + private static final boolean ON; + + static { + boolean on = true; + try { + BeanValidationAppendixInterceptor.class.getClassLoader().loadClass("javax.validation.executable.ExecutableValidator"); + on = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.bval.10.interceptor.force", "false")); + if (!on) { + logger.debug(BeanValidationAppendixInterceptor.class.getName() + " deactivated since BVal 1.1 is usable, use openejb.bval.10.interceptor.force=true to force it"); + } + } catch (final Throwable e) { + // no-op + } + ON = on; + } + + @AroundInvoke + public Object aroundInvoke(final InvocationContext ejbContext) throws Exception { + if (!BeanValidationAppendixInterceptor.ON) { + return ejbContext.proceed(); + } + + Object validatorObject = null; + Validator validator = null; + try { + validator = (Validator) new InitialContext().lookup("java:comp/Validator"); + } catch (final NamingException ne) { + // no-op + } catch (final Exception e) { + logger.warning("BeanValidationAppendixInterceptor unexpected error: " + e); + return ejbContext.proceed(); + } + + final ThreadContext threadContext = ThreadContext.getThreadContext(); + Class<?> bvalClazzToValidate = ejbContext.getTarget().getClass(); + if (threadContext != null && ejbContext.getTarget().getClass().getInterfaces().length > 0) { + bvalClazzToValidate = threadContext.getInvokedInterface(); + } + + Method method = ejbContext.getMethod(); + if (!bvalClazzToValidate.equals(ejbContext.getTarget().getClass())) { + method = bvalClazzToValidate.getMethod(method.getName(), method.getParameterTypes()); + } + + Set<?> violations = Collections.emptySet(); + if (APACHE_BVAL_METHOD_CLASS != null && validator != null) { + validatorObject = validator.unwrap(APACHE_BVAL_METHOD_CLASS); + violations = call(Set.class, validatorObject, "validateParameters", + new Object[]{ + bvalClazzToValidate, method, ejbContext.getParameters(), new Class[0] + }, + BVAL_ARG_TYPES); + } else if (HIBERNATE_METHOD_CLASS != null && validator != null) { + validatorObject = validator.unwrap(HIBERNATE_METHOD_CLASS); + violations = call(Set.class, validatorObject, "validateAllParameters", + new Object[]{ + ejbContext.getTarget(), ejbContext.getMethod(), ejbContext.getParameters(), new Class[0] + }, + HIBERNATE_ARG_TYPES); + } else { // a warning message to inform Apache Bean Validation is not present + if (validator == null) { + logger.error("can't find validator"); + } else { + logger.warning("Apache Bean Validation is not present, " + + BeanValidationAppendixInterceptor.class.getName() + " will not work. " + + "Please put it if you want to validate your parameters and returned values " + + "with bean validation JSR."); + } + } + + if (violations.size() > 0) { + throw buildValidationException((Set<ConstraintViolation<?>>) violations); + } + + final Object returnedValue = ejbContext.proceed(); + + violations = Collections.emptySet(); + if (validatorObject != null && APACHE_BVAL_METHOD_CLASS != null) { + violations = call(Set.class, validatorObject, "validateReturnedValue", + new Object[]{ + bvalClazzToValidate, method, returnedValue, new Class[0] + }, + new Class<?>[]{ + Class.class, Method.class, Object.class, Class[].class + }); + } else if (validatorObject != null && HIBERNATE_METHOD_CLASS != null) { + violations = call(Set.class, validatorObject, "validateReturnValue", + new Object[]{ + ejbContext.getTarget(), ejbContext.getMethod(), returnedValue, new Class[0] + }, + new Class<?>[]{ + Object.class, Method.class, Object.class, Class[].class + }); + } + + if (violations.size() > 0) { + throw buildValidationException((Set<ConstraintViolation<?>>) violations); + } + + return returnedValue; + } + + + // just a simple EJBException for now + private RuntimeException buildValidationException(final Set<ConstraintViolation<?>> violations) { + return new ConstraintViolationException(violations); + } + + private static <T> T call(final Class<T> returnedType, final Object o, final String methodName, final Object[] params, final Class<?>[] types) { + Method method = null; + boolean accessible = true; + try { + method = o.getClass().getMethod(methodName, types); + accessible = method.isAccessible(); + if (!accessible) { + accessible = false; + method.setAccessible(true); + } + return returnedType.cast(method.invoke(o, params)); + } catch (final Exception e) { + e.printStackTrace(); + throw new ValidationException("can't call method " + methodName + " on " + o, e); + } finally { + if (method != null) { + method.setAccessible(accessible); + } + } + } + + private static ClassLoader getClassLaoder() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader == null) { + classLoader = BeanValidationAppendixInterceptor.class.getClassLoader(); + } + return classLoader; + } + + private static Class<?> initApache() { + try { + return getClassLaoder().loadClass("org.apache.bval.jsr303.extensions.MethodValidator"); + } catch (final ClassNotFoundException e) { + return null; + } + } + + private static Class<?> initHibernate() { + try { + return getClassLaoder().loadClass("org.hibernate.validator.method.MethodValidator"); + } catch (final ClassNotFoundException e) { + return null; + } + } +} +
http://git-wip-us.apache.org/repos/asf/tomee/blob/52567075/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBLifecycle.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBLifecycle.java b/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBLifecycle.java index 81195c0..0a945a3 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBLifecycle.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBLifecycle.java @@ -1,419 +1,419 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.openejb.cdi; - -import org.apache.openejb.AppContext; -import org.apache.openejb.BeanContext; -import org.apache.openejb.OpenEJBRuntimeException; -import org.apache.openejb.assembler.classic.AppInfo; -import org.apache.openejb.assembler.classic.Assembler; -import org.apache.openejb.loader.SystemInstance; -import org.apache.openejb.util.LogCategory; -import org.apache.openejb.util.Logger; -import org.apache.webbeans.config.BeansDeployer; -import org.apache.webbeans.config.OpenWebBeansConfiguration; -import org.apache.webbeans.config.WebBeansContext; -import org.apache.webbeans.config.WebBeansFinder; -import org.apache.webbeans.container.BeanManagerImpl; -import org.apache.webbeans.intercept.InterceptorResolutionService; -import org.apache.webbeans.portable.AbstractProducer; -import org.apache.webbeans.portable.InjectionTargetImpl; -import org.apache.webbeans.portable.events.discovery.BeforeShutdownImpl; -import org.apache.webbeans.spi.ContainerLifecycle; -import org.apache.webbeans.spi.ContextsService; -import org.apache.webbeans.spi.JNDIService; -import org.apache.webbeans.spi.ResourceInjectionService; -import org.apache.webbeans.spi.ScannerService; -import org.apache.webbeans.spi.adaptor.ELAdaptor; -import org.apache.webbeans.util.WebBeansConstants; -import org.apache.webbeans.util.WebBeansUtil; -import org.apache.webbeans.xml.WebBeansXMLConfigurator; - -import javax.el.ELResolver; -import javax.enterprise.inject.spi.BeanManager; -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.jsp.JspApplicationContext; -import javax.servlet.jsp.JspFactory; -import java.util.Properties; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; - -/** - * @version $Rev:$ $Date:$ - */ -public class OpenEJBLifecycle implements ContainerLifecycle { - public static final ThreadLocal<AppInfo> CURRENT_APP_INFO = new ThreadLocal<AppInfo>(); - - //Logger instance - private static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_CDI, OpenEJBLifecycle.class); - - public static final String OPENEJB_CDI_SKIP_CLASS_NOT_FOUND = "openejb.cdi.skip-class-not-found"; - - /** - * Discover bean classes - */ - protected ScannerService scannerService; - - protected final ContextsService contextsService; - - /** - * Deploy discovered beans - */ - private final BeansDeployer deployer; - - /** - * XML discovery. - */ - //XML discovery is removed from the specification. It is here for next revisions of spec. - private final WebBeansXMLConfigurator xmlDeployer; - - /** - * Using for lookup operations - */ - private final JNDIService jndiService; - - /** - * Root container. - */ - private final BeanManagerImpl beanManager; - private final WebBeansContext webBeansContext; - /** - * Manages unused conversations - */ - private ScheduledExecutorService service; - - public OpenEJBLifecycle(final WebBeansContext webBeansContext) { - this.webBeansContext = webBeansContext; - beforeInitApplication(null); - - this.beanManager = webBeansContext.getBeanManagerImpl(); - this.xmlDeployer = new WebBeansXMLConfigurator(); - this.deployer = new BeansDeployer(this.xmlDeployer, webBeansContext); - this.jndiService = webBeansContext.getService(JNDIService.class); - this.beanManager.setXMLConfigurator(this.xmlDeployer); - this.scannerService = webBeansContext.getScannerService(); - this.contextsService = webBeansContext.getContextsService(); - - initApplication(null); - } - - @Override - public BeanManager getBeanManager() { - return this.beanManager; - } - - @Override - public void startApplication(final Object startupObject) { - if (ServletContextEvent.class.isInstance( startupObject)) { - startServletContext(ServletContext.class.cast(getServletContext(startupObject))); // TODO: check it is relevant - 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()); - - //Before Start - beforeStartApplication(startupObject); - - - //Load all plugins - webBeansContext.getPluginLoader().startUp(); - - //Get Plugin - final CdiPlugin cdiPlugin = (CdiPlugin) webBeansContext.getPluginLoader().getEjbPlugin(); - - final AppContext appContext = stuff.getAppContext(); - if (stuff.getWebContext() == null) { - appContext.setWebBeansContext(webBeansContext); - } - - cdiPlugin.setClassLoader(stuff.getClassLoader()); - cdiPlugin.setWebBeansContext(webBeansContext); - cdiPlugin.startup(); - - //Configure EJB Deployments - cdiPlugin.configureDeployments(stuff.getBeanContexts()); - - //Resournce Injection Service - final CdiResourceInjectionService injectionService = (CdiResourceInjectionService) webBeansContext.getService(ResourceInjectionService.class); - injectionService.setAppContext(stuff.getAppContext()); - - //Deploy the beans - try { - //Initialize contexts - this.contextsService.init(startupObject); - - //Scanning process - logger.debug("Scanning classpaths for beans artifacts."); - - if (CdiScanner.class.isInstance(scannerService)) { - final CdiScanner service = CdiScanner.class.cast(scannerService); - service.init(startupObject); - } else { - new CdiScanner().init(startupObject); - } - - //Scan - this.scannerService.scan(); - - // just to let us write custom CDI Extension using our internals easily - CURRENT_APP_INFO.set(StartupObject.class.cast(startupObject).getAppInfo()); - - //Deploy bean from XML. Also configures deployments, interceptors, decorators. - deployer.deploy(scannerService); - } catch (final Exception e1) { - SystemInstance.get().getComponent(Assembler.class).getLogger().error("CDI Beans module deployment failed", e1); - throw new OpenEJBRuntimeException(e1); - } finally { - CURRENT_APP_INFO.remove(); - } - - for (final BeanContext bc : stuff.getBeanContexts()) { - 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 - afterStartApplication(startupObject); - } finally { - Thread.currentThread().setContextClassLoader(oldCl); - - // cleanup threadlocal used to enrich cdi context manually - OptimizedLoaderService.ADDITIONAL_EXTENSIONS.remove(); - CdiScanner.ADDITIONAL_CLASSES.remove(); - } - - logger.info("OpenWebBeans Container has started, it took {0} ms.", Long.toString(System.currentTimeMillis() - begin)); - } - - @Override - public void stopApplication(final Object endObject) { - logger.debug("OpenWebBeans Container is stopping."); - - try { - //Sub-classes operations - beforeStopApplication(null); - - //Fire shut down - if (WebappBeanManager.class.isInstance(beanManager)) { - WebappBeanManager.class.cast(beanManager).beforeStop(); - } - this.beanManager.fireEvent(new BeforeShutdownImpl()); - - //Destroys context - this.contextsService.destroy(null); - - //Unbind BeanManager - if (jndiService != null) { - jndiService.unbind(WebBeansConstants.WEB_BEANS_MANAGER_JNDI_NAME); - } - - //Free all plugin resources - ((CdiPlugin) webBeansContext.getPluginLoader().getEjbPlugin()).clearProxies(); - webBeansContext.getPluginLoader().shutDown(); - - //Clear extensions - webBeansContext.getExtensionLoader().clear(); - - //Delete Resolutions Cache - beanManager.getInjectionResolver().clearCaches(); - - //Delete AnnotateTypeCache - webBeansContext.getAnnotatedElementFactory().clear(); - - //After Stop - afterStopApplication(null); - - // Clear BeanManager - this.beanManager.clear(); - - // Clear singleton list - WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader()); - - } catch (final Exception e) { - logger.error("An error occured while stopping the container.", e); - } - - } - - /** - * @return the scannerService - */ - protected ScannerService getScannerService() { - return scannerService; - } - - /** - * @return the contextsService - */ - public ContextsService getContextService() { - return contextsService; - } - - /** - * @return the xmlDeployer - */ - protected WebBeansXMLConfigurator getXmlDeployer() { - return xmlDeployer; - } - - /** - * @return the jndiService - */ - protected JNDIService getJndiService() { - return jndiService; - } - - @Override - public void initApplication(final Properties properties) { - afterInitApplication(properties); - } - - protected void beforeInitApplication(final Properties properties) { - //Do nothing as default - } - - protected void afterInitApplication(final Properties properties) { - //Do nothing as default - } - - protected void afterStartApplication(final Object startupObject) { - if (WebappBeanManager.class.isInstance(beanManager)) { - WebappBeanManager.class.cast(beanManager).afterStart(); - } - } - - public void startServletContext(final ServletContext servletContext) { - service = initializeServletContext(servletContext, webBeansContext); - } - - public static ScheduledExecutorService initializeServletContext(final ServletContext servletContext, final WebBeansContext context) { - final String strDelay = context.getOpenWebBeansConfiguration().getProperty(OpenWebBeansConfiguration.CONVERSATION_PERIODIC_DELAY, "150000"); - final long delay = Long.parseLong(strDelay); - - final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1, new ThreadFactory() { - @Override - public Thread newThread(final Runnable runable) { - final Thread t = new Thread(runable, "OwbConversationCleaner-" + servletContext.getContextPath()); - t.setDaemon(true); - return t; - } - }); - executorService.scheduleWithFixedDelay(new ConversationCleaner(context), delay, delay, TimeUnit.MILLISECONDS); - - final ELAdaptor elAdaptor = context.getService(ELAdaptor.class); - final ELResolver resolver = elAdaptor.getOwbELResolver(); - //Application is configured as JSP - if (context.getOpenWebBeansConfiguration().isJspApplication()) { - logger.debug("Application is configured as JSP. Adding EL Resolver."); - - final JspFactory factory = JspFactory.getDefaultFactory(); - if (factory != null) { - final JspApplicationContext applicationCtx = factory.getJspApplicationContext(servletContext); - applicationCtx.addELResolver(resolver); - } else { - logger.debug("Default JspFactory instance was not found"); - } - } - - // Add BeanManager to the 'javax.enterprise.inject.spi.BeanManager' servlet context attribute - servletContext.setAttribute(BeanManager.class.getName(), context.getBeanManagerImpl()); - - return executorService; - } - - /** - * Conversation cleaner thread, that - * clears unused conversations. - */ - private static final class ConversationCleaner implements Runnable { - private final WebBeansContext webBeansContext; - - private ConversationCleaner(final WebBeansContext webBeansContext) { - this.webBeansContext = webBeansContext; - } - - public void run() { - webBeansContext.getConversationManager().destroyWithRespectToTimout(); - - } - } - - protected void afterStopApplication(final Object stopObject) throws Exception { - - //Clear the resource injection service - final ResourceInjectionService injectionServices = webBeansContext.getService(ResourceInjectionService.class); - if (injectionServices != null) { - injectionServices.clear(); - } - - //Comment out for commit OWB-502 - //ContextFactory.cleanUpContextFactory(); - - CdiAppContextsService.class.cast(contextsService).removeThreadLocals(); - - WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader()); - } - - /** - * Returns servlet context otherwise throws exception. - * - * @param object object - * @return servlet context - */ - private Object getServletContext(Object object) { - if (ServletContextEvent.class.isInstance(object)) { - object = ServletContextEvent.class.cast(object).getServletContext(); - return object; - } - return object; - } - - protected void beforeStartApplication(final Object startupObject) { - //Do nothing as default - } - - protected void beforeStopApplication(final Object stopObject) throws Exception { - if (service != null) { - service.shutdownNow(); - } - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.cdi; + +import org.apache.openejb.AppContext; +import org.apache.openejb.BeanContext; +import org.apache.openejb.OpenEJBRuntimeException; +import org.apache.openejb.assembler.classic.AppInfo; +import org.apache.openejb.assembler.classic.Assembler; +import org.apache.openejb.loader.SystemInstance; +import org.apache.openejb.util.LogCategory; +import org.apache.openejb.util.Logger; +import org.apache.webbeans.config.BeansDeployer; +import org.apache.webbeans.config.OpenWebBeansConfiguration; +import org.apache.webbeans.config.WebBeansContext; +import org.apache.webbeans.config.WebBeansFinder; +import org.apache.webbeans.container.BeanManagerImpl; +import org.apache.webbeans.intercept.InterceptorResolutionService; +import org.apache.webbeans.portable.AbstractProducer; +import org.apache.webbeans.portable.InjectionTargetImpl; +import org.apache.webbeans.portable.events.discovery.BeforeShutdownImpl; +import org.apache.webbeans.spi.ContainerLifecycle; +import org.apache.webbeans.spi.ContextsService; +import org.apache.webbeans.spi.JNDIService; +import org.apache.webbeans.spi.ResourceInjectionService; +import org.apache.webbeans.spi.ScannerService; +import org.apache.webbeans.spi.adaptor.ELAdaptor; +import org.apache.webbeans.util.WebBeansConstants; +import org.apache.webbeans.util.WebBeansUtil; +import org.apache.webbeans.xml.WebBeansXMLConfigurator; + +import javax.el.ELResolver; +import javax.enterprise.inject.spi.BeanManager; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.jsp.JspApplicationContext; +import javax.servlet.jsp.JspFactory; +import java.util.Properties; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +/** + * @version $Rev:$ $Date:$ + */ +public class OpenEJBLifecycle implements ContainerLifecycle { + public static final ThreadLocal<AppInfo> CURRENT_APP_INFO = new ThreadLocal<AppInfo>(); + + //Logger instance + private static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_CDI, OpenEJBLifecycle.class); + + public static final String OPENEJB_CDI_SKIP_CLASS_NOT_FOUND = "openejb.cdi.skip-class-not-found"; + + /** + * Discover bean classes + */ + protected ScannerService scannerService; + + protected final ContextsService contextsService; + + /** + * Deploy discovered beans + */ + private final BeansDeployer deployer; + + /** + * XML discovery. + */ + //XML discovery is removed from the specification. It is here for next revisions of spec. + private final WebBeansXMLConfigurator xmlDeployer; + + /** + * Using for lookup operations + */ + private final JNDIService jndiService; + + /** + * Root container. + */ + private final BeanManagerImpl beanManager; + private final WebBeansContext webBeansContext; + /** + * Manages unused conversations + */ + private ScheduledExecutorService service; + + public OpenEJBLifecycle(final WebBeansContext webBeansContext) { + this.webBeansContext = webBeansContext; + beforeInitApplication(null); + + this.beanManager = webBeansContext.getBeanManagerImpl(); + this.xmlDeployer = new WebBeansXMLConfigurator(); + this.deployer = new BeansDeployer(this.xmlDeployer, webBeansContext); + this.jndiService = webBeansContext.getService(JNDIService.class); + this.beanManager.setXMLConfigurator(this.xmlDeployer); + this.scannerService = webBeansContext.getScannerService(); + this.contextsService = webBeansContext.getContextsService(); + + initApplication(null); + } + + @Override + public BeanManager getBeanManager() { + return this.beanManager; + } + + @Override + public void startApplication(final Object startupObject) { + if (ServletContextEvent.class.isInstance( startupObject)) { + startServletContext(ServletContext.class.cast(getServletContext(startupObject))); // TODO: check it is relevant + 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()); + + //Before Start + beforeStartApplication(startupObject); + + + //Load all plugins + webBeansContext.getPluginLoader().startUp(); + + //Get Plugin + final CdiPlugin cdiPlugin = (CdiPlugin) webBeansContext.getPluginLoader().getEjbPlugin(); + + final AppContext appContext = stuff.getAppContext(); + if (stuff.getWebContext() == null) { + appContext.setWebBeansContext(webBeansContext); + } + + cdiPlugin.setClassLoader(stuff.getClassLoader()); + cdiPlugin.setWebBeansContext(webBeansContext); + cdiPlugin.startup(); + + //Configure EJB Deployments + cdiPlugin.configureDeployments(stuff.getBeanContexts()); + + //Resournce Injection Service + final CdiResourceInjectionService injectionService = (CdiResourceInjectionService) webBeansContext.getService(ResourceInjectionService.class); + injectionService.setAppContext(stuff.getAppContext()); + + //Deploy the beans + try { + //Initialize contexts + this.contextsService.init(startupObject); + + //Scanning process + logger.debug("Scanning classpaths for beans artifacts."); + + if (CdiScanner.class.isInstance(scannerService)) { + final CdiScanner service = CdiScanner.class.cast(scannerService); + service.init(startupObject); + } else { + new CdiScanner().init(startupObject); + } + + //Scan + this.scannerService.scan(); + + // just to let us write custom CDI Extension using our internals easily + CURRENT_APP_INFO.set(StartupObject.class.cast(startupObject).getAppInfo()); + + //Deploy bean from XML. Also configures deployments, interceptors, decorators. + deployer.deploy(scannerService); + } catch (final Exception e1) { + SystemInstance.get().getComponent(Assembler.class).getLogger().error("CDI Beans module deployment failed", e1); + throw new OpenEJBRuntimeException(e1); + } finally { + CURRENT_APP_INFO.remove(); + } + + for (final BeanContext bc : stuff.getBeanContexts()) { + 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 + afterStartApplication(startupObject); + } finally { + Thread.currentThread().setContextClassLoader(oldCl); + + // cleanup threadlocal used to enrich cdi context manually + OptimizedLoaderService.ADDITIONAL_EXTENSIONS.remove(); + CdiScanner.ADDITIONAL_CLASSES.remove(); + } + + logger.info("OpenWebBeans Container has started, it took {0} ms.", Long.toString(System.currentTimeMillis() - begin)); + } + + @Override + public void stopApplication(final Object endObject) { + logger.debug("OpenWebBeans Container is stopping."); + + try { + //Sub-classes operations + beforeStopApplication(null); + + //Fire shut down + if (WebappBeanManager.class.isInstance(beanManager)) { + WebappBeanManager.class.cast(beanManager).beforeStop(); + } + this.beanManager.fireEvent(new BeforeShutdownImpl()); + + //Destroys context + this.contextsService.destroy(null); + + //Unbind BeanManager + if (jndiService != null) { + jndiService.unbind(WebBeansConstants.WEB_BEANS_MANAGER_JNDI_NAME); + } + + //Free all plugin resources + ((CdiPlugin) webBeansContext.getPluginLoader().getEjbPlugin()).clearProxies(); + webBeansContext.getPluginLoader().shutDown(); + + //Clear extensions + webBeansContext.getExtensionLoader().clear(); + + //Delete Resolutions Cache + beanManager.getInjectionResolver().clearCaches(); + + //Delete AnnotateTypeCache + webBeansContext.getAnnotatedElementFactory().clear(); + + //After Stop + afterStopApplication(null); + + // Clear BeanManager + this.beanManager.clear(); + + // Clear singleton list + WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader()); + + } catch (final Exception e) { + logger.error("An error occured while stopping the container.", e); + } + + } + + /** + * @return the scannerService + */ + protected ScannerService getScannerService() { + return scannerService; + } + + /** + * @return the contextsService + */ + public ContextsService getContextService() { + return contextsService; + } + + /** + * @return the xmlDeployer + */ + protected WebBeansXMLConfigurator getXmlDeployer() { + return xmlDeployer; + } + + /** + * @return the jndiService + */ + protected JNDIService getJndiService() { + return jndiService; + } + + @Override + public void initApplication(final Properties properties) { + afterInitApplication(properties); + } + + protected void beforeInitApplication(final Properties properties) { + //Do nothing as default + } + + protected void afterInitApplication(final Properties properties) { + //Do nothing as default + } + + protected void afterStartApplication(final Object startupObject) { + if (WebappBeanManager.class.isInstance(beanManager)) { + WebappBeanManager.class.cast(beanManager).afterStart(); + } + } + + public void startServletContext(final ServletContext servletContext) { + service = initializeServletContext(servletContext, webBeansContext); + } + + public static ScheduledExecutorService initializeServletContext(final ServletContext servletContext, final WebBeansContext context) { + final String strDelay = context.getOpenWebBeansConfiguration().getProperty(OpenWebBeansConfiguration.CONVERSATION_PERIODIC_DELAY, "150000"); + final long delay = Long.parseLong(strDelay); + + final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1, new ThreadFactory() { + @Override + public Thread newThread(final Runnable runable) { + final Thread t = new Thread(runable, "OwbConversationCleaner-" + servletContext.getContextPath()); + t.setDaemon(true); + return t; + } + }); + executorService.scheduleWithFixedDelay(new ConversationCleaner(context), delay, delay, TimeUnit.MILLISECONDS); + + final ELAdaptor elAdaptor = context.getService(ELAdaptor.class); + final ELResolver resolver = elAdaptor.getOwbELResolver(); + //Application is configured as JSP + if (context.getOpenWebBeansConfiguration().isJspApplication()) { + logger.debug("Application is configured as JSP. Adding EL Resolver."); + + final JspFactory factory = JspFactory.getDefaultFactory(); + if (factory != null) { + final JspApplicationContext applicationCtx = factory.getJspApplicationContext(servletContext); + applicationCtx.addELResolver(resolver); + } else { + logger.debug("Default JspFactory instance was not found"); + } + } + + // Add BeanManager to the 'javax.enterprise.inject.spi.BeanManager' servlet context attribute + servletContext.setAttribute(BeanManager.class.getName(), context.getBeanManagerImpl()); + + return executorService; + } + + /** + * Conversation cleaner thread, that + * clears unused conversations. + */ + private static final class ConversationCleaner implements Runnable { + private final WebBeansContext webBeansContext; + + private ConversationCleaner(final WebBeansContext webBeansContext) { + this.webBeansContext = webBeansContext; + } + + public void run() { + webBeansContext.getConversationManager().destroyWithRespectToTimout(); + + } + } + + protected void afterStopApplication(final Object stopObject) throws Exception { + + //Clear the resource injection service + final ResourceInjectionService injectionServices = webBeansContext.getService(ResourceInjectionService.class); + if (injectionServices != null) { + injectionServices.clear(); + } + + //Comment out for commit OWB-502 + //ContextFactory.cleanUpContextFactory(); + + CdiAppContextsService.class.cast(contextsService).removeThreadLocals(); + + WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader()); + } + + /** + * Returns servlet context otherwise throws exception. + * + * @param object object + * @return servlet context + */ + private Object getServletContext(Object object) { + if (ServletContextEvent.class.isInstance(object)) { + object = ServletContextEvent.class.cast(object).getServletContext(); + return object; + } + return object; + } + + protected void beforeStartApplication(final Object startupObject) { + //Do nothing as default + } + + protected void beforeStopApplication(final Object stopObject) throws Exception { + if (service != null) { + service.shutdownNow(); + } + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/52567075/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java b/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java index 52e3fe6..fc657dd 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/cipher/PasswordCipher.java @@ -1,51 +1,51 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.openejb.cipher; - -/** - * Implementations of {@link org.apache.openejb.cipher.PasswordCipher} allow to encode and decode passwords - * used to connect to a database. - * <p/> - * Several implementations may exist, as several encryption algorithms may be - * supported. One-way encryption algorithm (hash) can't be used as we need to - * give a plain password to the database. {@link #encrypt(String)} method is not - * mandatory as we don't need to encode a password, but it's useful to get the - * encrypted value for a given plain text password. In the case you have - * implemented both methods, you can use the PasswordCodec command line tool to - * encode/decode a password. - */ -public interface PasswordCipher { - - /** - * Encodes a given plain text password and returns the encoded password. - * - * @param plainPassword The password to encode. May not be <code>null</code>, nor empty. - * @return The encoded password. - */ - char[] encrypt(String plainPassword); - - /** - * Decodes an encoded password and returns a plain text password. - * - * @param encryptedPassword The ciphered password to decode. May not be <code>null</code>, - * nor empty. - * @return The plain text password. - */ - String decrypt(char[] encryptedPassword); - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.cipher; + +/** + * Implementations of {@link org.apache.openejb.cipher.PasswordCipher} allow to encode and decode passwords + * used to connect to a database. + * <p/> + * Several implementations may exist, as several encryption algorithms may be + * supported. One-way encryption algorithm (hash) can't be used as we need to + * give a plain password to the database. {@link #encrypt(String)} method is not + * mandatory as we don't need to encode a password, but it's useful to get the + * encrypted value for a given plain text password. In the case you have + * implemented both methods, you can use the PasswordCodec command line tool to + * encode/decode a password. + */ +public interface PasswordCipher { + + /** + * Encodes a given plain text password and returns the encoded password. + * + * @param plainPassword The password to encode. May not be <code>null</code>, nor empty. + * @return The encoded password. + */ + char[] encrypt(String plainPassword); + + /** + * Decodes an encoded password and returns a plain text password. + * + * @param encryptedPassword The ciphered password to decode. May not be <code>null</code>, + * nor empty. + * @return The plain text password. + */ + String decrypt(char[] encryptedPassword); + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/52567075/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java b/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java index 4b71dad..099b4ef 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/cipher/PlainTextPasswordCipher.java @@ -1,54 +1,54 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.openejb.cipher; - -/** - * This {@link org.apache.openejb.cipher.PlainTextPasswordCipher} is an {@link org.apache.openejb.cipher.PasswordCipher} - * implementation that does not use any encryption/decryption algorithm at all. - */ -public class PlainTextPasswordCipher implements PasswordCipher { - - /** - * Returns the <code>encryptedPassword</code> as plain text string. - * - * @param encryptedPassword the encoded password - * @return String the decoded password - * @see org.apache.openejb.cipher.PasswordCipher#decrypt(char[]) - */ - public String decrypt(final char[] encryptedPassword) { - if (null == encryptedPassword) { - throw new IllegalArgumentException("encodedPassword cannot be null."); - } - return new String(encryptedPassword); - } - - /** - * Returns the <code>plainPassword</code> as plain text character array. - * - * @param plainPassword the plain-text password - * @return the plain-text password as character array - * @see org.apache.openejb.cipher.PasswordCipher#encrypt(String) - */ - public char[] encrypt(final String plainPassword) { - if (null == plainPassword) { - throw new IllegalArgumentException("plainPassword cannot be null."); - } - return plainPassword.toCharArray(); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.cipher; + +/** + * This {@link org.apache.openejb.cipher.PlainTextPasswordCipher} is an {@link org.apache.openejb.cipher.PasswordCipher} + * implementation that does not use any encryption/decryption algorithm at all. + */ +public class PlainTextPasswordCipher implements PasswordCipher { + + /** + * Returns the <code>encryptedPassword</code> as plain text string. + * + * @param encryptedPassword the encoded password + * @return String the decoded password + * @see org.apache.openejb.cipher.PasswordCipher#decrypt(char[]) + */ + public String decrypt(final char[] encryptedPassword) { + if (null == encryptedPassword) { + throw new IllegalArgumentException("encodedPassword cannot be null."); + } + return new String(encryptedPassword); + } + + /** + * Returns the <code>plainPassword</code> as plain text character array. + * + * @param plainPassword the plain-text password + * @return the plain-text password as character array + * @see org.apache.openejb.cipher.PasswordCipher#encrypt(String) + */ + public char[] encrypt(final String plainPassword) { + if (null == plainPassword) { + throw new IllegalArgumentException("plainPassword cannot be null."); + } + return plainPassword.toCharArray(); + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/52567075/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java b/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java index f6cd88f..1741e4f 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/cipher/StaticDESPasswordCipher.java @@ -1,97 +1,97 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.openejb.cipher; - -import org.apache.openejb.OpenEJBRuntimeException; -import org.apache.openejb.util.Base64; - -import javax.crypto.Cipher; -import javax.crypto.spec.SecretKeySpec; - -/** - * This {@link org.apache.openejb.cipher.PasswordCipher} implementation uses a the Triple-DES encryption - * algorithm. - */ -public class StaticDESPasswordCipher implements PasswordCipher { - - private static final byte[] _3desData = { - (byte) 0x76, (byte) 0x6F, (byte) 0xBA, (byte) 0x39, (byte) 0x31, - (byte) 0x2F, (byte) 0x0D, (byte) 0x4A, (byte) 0xA3, (byte) 0x90, - (byte) 0x55, (byte) 0xFE, (byte) 0x55, (byte) 0x65, (byte) 0x61, - (byte) 0x13, (byte) 0x34, (byte) 0x82, (byte) 0x12, (byte) 0x17, - (byte) 0xAC, (byte) 0x77, (byte) 0x39, (byte) 0x19}; - - private static final SecretKeySpec KEY = new SecretKeySpec(_3desData, "DESede"); - - /** - * The name of the transformation defines Triple-DES encryption - */ - private static final String TRANSFORMATION = "DESede"; - - /** - * @throws RuntimeException in any case of error. - * @see org.apache.openejb.cipher.PasswordCipher#encrypt(String) - */ - public char[] encrypt(final String plainPassword) { - if (null == plainPassword || plainPassword.length() == 0) { - throw new IllegalArgumentException("plainPassword cannot be null nor empty."); - } - - final byte[] plaintext = plainPassword.getBytes(); - try { - // Get a 3DES Cipher object - final Cipher cipher = Cipher.getInstance(TRANSFORMATION); - // Set it into encryption mode - cipher.init(Cipher.ENCRYPT_MODE, KEY); - - // Encrypt data - final byte[] cipherText = cipher.doFinal(plaintext); - return new String(Base64.encodeBase64(cipherText)).toCharArray(); - - } catch (final Exception e) { - throw new OpenEJBRuntimeException(e); - } - } - - /** - * @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); - } - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.cipher; + +import org.apache.openejb.OpenEJBRuntimeException; +import org.apache.openejb.util.Base64; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; + +/** + * This {@link org.apache.openejb.cipher.PasswordCipher} implementation uses a the Triple-DES encryption + * algorithm. + */ +public class StaticDESPasswordCipher implements PasswordCipher { + + private static final byte[] _3desData = { + (byte) 0x76, (byte) 0x6F, (byte) 0xBA, (byte) 0x39, (byte) 0x31, + (byte) 0x2F, (byte) 0x0D, (byte) 0x4A, (byte) 0xA3, (byte) 0x90, + (byte) 0x55, (byte) 0xFE, (byte) 0x55, (byte) 0x65, (byte) 0x61, + (byte) 0x13, (byte) 0x34, (byte) 0x82, (byte) 0x12, (byte) 0x17, + (byte) 0xAC, (byte) 0x77, (byte) 0x39, (byte) 0x19}; + + private static final SecretKeySpec KEY = new SecretKeySpec(_3desData, "DESede"); + + /** + * The name of the transformation defines Triple-DES encryption + */ + private static final String TRANSFORMATION = "DESede"; + + /** + * @throws RuntimeException in any case of error. + * @see org.apache.openejb.cipher.PasswordCipher#encrypt(String) + */ + public char[] encrypt(final String plainPassword) { + if (null == plainPassword || plainPassword.length() == 0) { + throw new IllegalArgumentException("plainPassword cannot be null nor empty."); + } + + final byte[] plaintext = plainPassword.getBytes(); + try { + // Get a 3DES Cipher object + final Cipher cipher = Cipher.getInstance(TRANSFORMATION); + // Set it into encryption mode + cipher.init(Cipher.ENCRYPT_MODE, KEY); + + // Encrypt data + final byte[] cipherText = cipher.doFinal(plaintext); + return new String(Base64.encodeBase64(cipherText)).toCharArray(); + + } catch (final Exception e) { + throw new OpenEJBRuntimeException(e); + } + } + + /** + * @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); + } + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/52567075/container/openejb-core/src/main/java/org/apache/openejb/classloader/FalseFilter.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/classloader/FalseFilter.java b/container/openejb-core/src/main/java/org/apache/openejb/classloader/FalseFilter.java index 3378221..3452f19 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/classloader/FalseFilter.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/classloader/FalseFilter.java @@ -1,29 +1,29 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.openejb.classloader; - -import org.apache.xbean.finder.filter.Filter; - -public class FalseFilter implements Filter { - public static final FalseFilter INSTANCE = new FalseFilter(); - - @Override - public boolean accept(final String name) { - return false; - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.classloader; + +import org.apache.xbean.finder.filter.Filter; + +public class FalseFilter implements Filter { + public static final FalseFilter INSTANCE = new FalseFilter(); + + @Override + public boolean accept(final String name) { + return false; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/52567075/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentFilterable.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentFilterable.java b/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentFilterable.java index ec3365a..f4b9be6 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentFilterable.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentFilterable.java @@ -1,33 +1,33 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.openejb.config; - -/** - * @version $Rev$ - */ -public interface DeploymentFilterable { - String DEPLOYMENTS_CLASSPATH_PROPERTY = "openejb.deployments.classpath"; - String SEARCH_CLASSPATH_FOR_DEPLOYMENTS_PROPERTY = DEPLOYMENTS_CLASSPATH_PROPERTY; - String CLASSPATH_INCLUDE = "openejb.deployments.classpath.include"; - String CLASSPATH_EXCLUDE = "openejb.deployments.classpath.exclude"; - String PACKAGE_INCLUDE = "openejb.deployments.package.include"; - String PACKAGE_EXCLUDE = "openejb.deployments.package.exclude"; - String CLASSPATH_REQUIRE_DESCRIPTOR = RequireDescriptors.PROPERTY; - String CLASSPATH_FILTER_DESCRIPTORS = "openejb.deployments.classpath.filter.descriptors"; - String CLASSPATH_FILTER_SYSTEMAPPS = "openejb.deployments.classpath.filter.systemapps"; -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.openejb.config; + +/** + * @version $Rev$ + */ +public interface DeploymentFilterable { + String DEPLOYMENTS_CLASSPATH_PROPERTY = "openejb.deployments.classpath"; + String SEARCH_CLASSPATH_FOR_DEPLOYMENTS_PROPERTY = DEPLOYMENTS_CLASSPATH_PROPERTY; + String CLASSPATH_INCLUDE = "openejb.deployments.classpath.include"; + String CLASSPATH_EXCLUDE = "openejb.deployments.classpath.exclude"; + String PACKAGE_INCLUDE = "openejb.deployments.package.include"; + String PACKAGE_EXCLUDE = "openejb.deployments.package.exclude"; + String CLASSPATH_REQUIRE_DESCRIPTOR = RequireDescriptors.PROPERTY; + String CLASSPATH_FILTER_DESCRIPTORS = "openejb.deployments.classpath.filter.descriptors"; + String CLASSPATH_FILTER_SYSTEMAPPS = "openejb.deployments.classpath.filter.systemapps"; +}
