Created
April 20, 2013 23:29
-
-
Save os890/5427831 to your computer and use it in GitHub Desktop.
ds with multiple cdi implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.deltaspike.core.api.provider; | |
import org.apache.deltaspike.core.util.ClassUtils; | |
import javax.enterprise.inject.spi.BeanManager; | |
import javax.enterprise.inject.spi.Extension; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.ServiceLoader; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class BeanManagerProvider implements Extension | |
{ | |
private static BeanManagerProvider bmpSingleton = null; | |
private Map<ClassLoader, BeanManager> beanManagerCache = new ConcurrentHashMap<ClassLoader, BeanManager>(); | |
public static BeanManagerProvider getInstance() | |
{ | |
if (bmpSingleton == null) | |
{ | |
bmpSingleton = new BeanManagerProvider(); | |
} | |
return bmpSingleton; | |
} | |
public BeanManager getBeanManager() | |
{ | |
BeanManager beanManager = beanManagerCache.get(ClassUtils.getClassLoader(null)); | |
if (beanManager != null) | |
{ | |
return beanManager; | |
} | |
synchronized (BeanManagerProvider.class) | |
{ | |
beanManager = beanManagerCache.get(ClassUtils.getClassLoader(null)); | |
if (beanManager != null) | |
{ | |
return beanManager; | |
} | |
ServiceLoader<BeanManagerResolver> beanManagerResolverLoader = | |
ServiceLoader.load(BeanManagerResolver.class); | |
Iterator<BeanManagerResolver> beanManagerResolverIterator = beanManagerResolverLoader.iterator(); | |
if (beanManagerResolverIterator.hasNext()) | |
{ | |
beanManager = beanManagerResolverIterator.next().getBeanManager(); | |
beanManagerCache.put(ClassUtils.getClassLoader(null), beanManager); | |
} | |
} | |
return beanManagerCache.get(ClassUtils.getClassLoader(null)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.deltaspike.core.spi.resolver; | |
import javax.enterprise.inject.spi.BeanManager; | |
public interface BeanManagerResolver | |
{ | |
BeanManager getBeanManager(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.deltaspike.cdise.owb; | |
import org.apache.deltaspike.cdise.api.CdiContainer; | |
import org.apache.deltaspike.cdise.api.CdiContainerLoader; | |
import org.apache.deltaspike.core.spi.resolver.BeanManagerResolver; | |
import javax.enterprise.inject.spi.BeanManager; | |
public class DefaultBeanManagerResolver implements BeanManagerResolver | |
{ | |
@Override | |
public BeanManager getBeanManager() | |
{ | |
CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer(); | |
cdiContainer.boot(); //a second call will get ignored | |
return cdiContainer.getBeanManager(); | |
} | |
} | |
//don't forget to configure it |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.deltaspike.cdise.owb; | |
import org.apache.deltaspike.cdise.spi.ServletContextEventResolver; | |
import org.apache.deltaspike.core.util.ClassUtils; | |
import javax.servlet.ServletContextEvent; | |
import javax.servlet.ServletContextListener; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class DefaultServletContextEventResolver implements ServletContextListener, ServletContextEventResolver | |
{ | |
private static final Map<ClassLoader, ServletContextEvent> SERVLET_CONTEXT_EVENT_CACHE = | |
new ConcurrentHashMap<ClassLoader, ServletContextEvent>(); | |
@Override | |
public ServletContextEvent get() | |
{ | |
return SERVLET_CONTEXT_EVENT_CACHE.get(ClassUtils.getClassLoader(null)); | |
} | |
@Override | |
public void contextInitialized(ServletContextEvent sce) | |
{ | |
SERVLET_CONTEXT_EVENT_CACHE.put(ClassUtils.getClassLoader(null), sce); | |
} | |
@Override | |
public void contextDestroyed(ServletContextEvent sce) | |
{ | |
SERVLET_CONTEXT_EVENT_CACHE.remove(ClassUtils.getClassLoader(null)); | |
} | |
} | |
//don't forget to configure it |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.deltaspike.cdise.owb; | |
import org.apache.deltaspike.cdise.api.CdiContainer; | |
import org.apache.deltaspike.cdise.api.ContextControl; | |
import org.apache.deltaspike.cdise.spi.ServletContextEventResolver; | |
import org.apache.webbeans.config.WebBeansContext; | |
import org.apache.webbeans.spi.ContainerLifecycle; | |
import javax.enterprise.context.spi.CreationalContext; | |
import javax.enterprise.inject.spi.Bean; | |
import javax.enterprise.inject.spi.BeanManager; | |
import javax.servlet.ServletContextEvent; | |
import java.util.Iterator; | |
import java.util.ServiceLoader; | |
import java.util.Set; | |
import java.util.logging.Logger; | |
/** | |
* OpenWebBeans specific implementation of {@link org.apache.deltaspike.cdise.api.CdiContainer}. | |
*/ | |
public class OpenWebBeansContainerControl implements CdiContainer | |
{ | |
private static final Logger LOG = Logger.getLogger(OpenWebBeansContainerControl.class.getName()); | |
private ContainerLifecycle lifecycle; | |
private ContextControl ctxCtrl = null; | |
private Bean<ContextControl> ctxCtrlBean = null; | |
private CreationalContext<ContextControl> ctxCtrlCreationalContext = null; | |
@Override | |
public BeanManager getBeanManager() | |
{ | |
if (lifecycle == null) | |
{ | |
lifecycle = WebBeansContext.currentInstance().getService(ContainerLifecycle.class); | |
} | |
return lifecycle.getBeanManager(); | |
} | |
@Override | |
public synchronized void boot() | |
{ | |
if (lifecycle != null) | |
{ | |
return; | |
} | |
lifecycle = WebBeansContext.currentInstance().getService(ContainerLifecycle.class); | |
lifecycle.startApplication(getServletContextEvent()); | |
} | |
@Override | |
public synchronized void shutdown() | |
{ | |
if (ctxCtrl != null) | |
{ | |
ctxCtrlBean.destroy(ctxCtrl, ctxCtrlCreationalContext); | |
} | |
if (lifecycle != null) | |
{ | |
lifecycle.stopApplication(getServletContextEvent()); | |
} | |
lifecycle = null; | |
} | |
@Override | |
public synchronized ContextControl getContextControl() | |
{ | |
if (ctxCtrl == null) | |
{ | |
Set<Bean<?>> beans = getBeanManager().getBeans(ContextControl.class); | |
BeanManager beanManager = getBeanManager(); | |
if (beanManager == null) | |
{ | |
LOG.warning("If the CDI-container was started by the environment, you can't use this helper." + | |
"Instead you can resolve ContextControl manually " + | |
"(e.g. via BeanProvider.getContextualReference(ContextControl.class) ). " + | |
"If the container wasn't started already, you have to use CdiContainer#boot before."); | |
return null; | |
} | |
ctxCtrlBean = (Bean<ContextControl>) beanManager.resolve(beans); | |
ctxCtrlCreationalContext = getBeanManager().createCreationalContext(ctxCtrlBean); | |
ctxCtrl = (ContextControl) | |
getBeanManager().getReference(ctxCtrlBean, ContextControl.class, ctxCtrlCreationalContext); | |
} | |
return ctxCtrl; | |
} | |
private ServletContextEvent getServletContextEvent() | |
{ | |
ServiceLoader<ServletContextEventResolver> servletContextResolverLoader = | |
ServiceLoader.load(ServletContextEventResolver.class); | |
Iterator<ServletContextEventResolver> servletContextResolverIterator = servletContextResolverLoader.iterator(); | |
ServletContextEvent servletContextEvent = null; | |
if (servletContextResolverIterator.hasNext()) | |
{ | |
servletContextEvent = servletContextResolverIterator.next().get(); | |
} | |
return servletContextEvent; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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.deltaspike.cdise.spi; | |
import javax.servlet.ServletContextEvent; | |
public interface ServletContextEventResolver | |
{ | |
ServletContextEvent get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment