| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* The contents of this file are subject to the terms of either the GNU |
| 7 |
* General Public License Version 2 only ("GPL") or the Common Development |
| 8 |
* and Distribution License("CDDL") (collectively, the "License"). You |
| 9 |
* may not use this file except in compliance with the License. You can |
| 10 |
* obtain a copy of the License at |
| 11 |
* http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 |
* or packager/legal/LICENSE.txt. See the License for the specific |
| 13 |
* language governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* When distributing the software, include this License Header Notice in each |
| 16 |
* file and include the License file at packager/legal/LICENSE.txt. |
| 17 |
* |
| 18 |
* GPL Classpath Exception: |
| 19 |
* Oracle designates this particular file as subject to the "Classpath" |
| 20 |
* exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 |
* file that accompanied this code. |
| 22 |
* |
| 23 |
* Modifications: |
| 24 |
* If applicable, add the following below the License Header, with the fields |
| 25 |
* enclosed by brackets [] replaced by your own identifying information: |
| 26 |
* "Portions Copyright [year] [name of copyright owner]" |
| 27 |
* |
| 28 |
* Contributor(s): |
| 29 |
* If you wish your version of this file to be governed by only the CDDL or |
| 30 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 |
* elects to include this software in this distribution under the [CDDL or GPL |
| 32 |
* Version 2] license." If you don't indicate a single choice of license, a |
| 33 |
* recipient has the option to distribute your version of this file under |
| 34 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 |
* its licensees as provided above. However, if you add GPL Version 2 code |
| 36 |
* and therefore, elected the GPL Version 2 license, then the option applies |
| 37 |
* only if the new code is made subject to such option by the copyright |
| 38 |
* holder. |
| 39 |
*/ |
| 40 |
|
| 41 |
package com.sun.jersey.server.impl.cdi; |
| 42 |
|
| 43 |
import java.util.Collections; |
| 44 |
import java.util.Set; |
| 45 |
import java.util.logging.Level; |
| 46 |
import java.util.logging.Logger; |
| 47 |
import javax.enterprise.context.spi.CreationalContext; |
| 48 |
import javax.enterprise.inject.AmbiguousResolutionException; |
| 49 |
import javax.enterprise.inject.spi.Bean; |
| 50 |
import javax.enterprise.inject.spi.BeanManager; |
| 51 |
|
| 52 |
/** |
| 53 |
* Utility methods for CDI BeanManager and Bean classes. |
| 54 |
* |
| 55 |
* @author Roberto Chinnici |
| 56 |
* @author Paul Sandoz (paul.sandoz at oracle.com) |
| 57 |
* @author Jakub Podlesak (jakub.podlesak at oracle.com) |
| 58 |
*/ |
| 59 |
public class Utils { |
| 60 |
|
| 61 |
private static final Logger LOGGER = Logger.getLogger(Utils.class.getName()); |
| 62 |
|
| 63 |
/** |
| 64 |
* To prevent instantiation. |
| 65 |
*/ |
| 66 |
private Utils() {} |
| 67 |
|
| 68 |
/** |
| 69 |
* Gets you a CDI bean that corresponds to the type provided as parameter. |
| 70 |
* |
| 71 |
* @param beanManager bean manager to get the bean from |
| 72 |
* @param clazz type for which a corresponding bean should be found |
| 73 |
* @return CDI bean for given type, or null if no such bean have been found |
| 74 |
* |
| 75 |
*/ |
| 76 |
public static Bean<?> getBean(BeanManager beanManager, Class<?> clazz) { |
| 77 |
|
| 78 |
final Set<Bean<?>> beans = beanManager.getBeans(clazz); |
| 79 |
if (beans.isEmpty()) { |
| 80 |
if (LOGGER.isLoggable(Level.FINE)) { |
| 81 |
LOGGER.fine(String.format("No CDI beans found in bean manager, %s, for type %s", beanManager, clazz)); |
| 82 |
} |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
try { |
| 87 |
return beanManager.resolve(beans); |
| 88 |
} catch(AmbiguousResolutionException ex) { |
| 89 |
// Check if there is a shared base class of c |
| 90 |
// If so reduce the set of beans whose class equals c and resolve |
| 91 |
if (isSharedBaseClass(clazz, beans)) { |
| 92 |
try { |
| 93 |
if (LOGGER.isLoggable(Level.FINE)) { |
| 94 |
LOGGER.fine(String.format("Ambiguous resolution exception caught when resolving bean %s. Trying to resolve by the type %s", beans, clazz)); |
| 95 |
} |
| 96 |
return beanManager.resolve(getBaseClassSubSet(clazz, beans)); |
| 97 |
} catch (AmbiguousResolutionException ex2) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
} |
| 101 |
if (LOGGER.isLoggable(Level.FINE)) { |
| 102 |
LOGGER.fine(String.format("Failed to resolve bean %s.", beans)); |
| 103 |
} |
| 104 |
return null; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public static <T> T getInstance(BeanManager beanManager, Class<T> c) { |
| 109 |
|
| 110 |
Bean<?> bean = getBean(beanManager, c); |
| 111 |
if (bean == null) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
final CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean); |
| 116 |
final Object result = beanManager.getReference(bean, c, creationalContext); |
| 117 |
|
| 118 |
return c.cast(result); |
| 119 |
} |
| 120 |
|
| 121 |
public static CDIExtension getCdiExtensionInstance(BeanManager beanManager) { |
| 122 |
|
| 123 |
final Set<Bean<?>> beans = beanManager.getBeans(CDIExtension.class); |
| 124 |
|
| 125 |
if (beans.isEmpty()) { |
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
try { |
| 130 |
return getCdiExtensionReference(beanManager.resolve(beans), beanManager); |
| 131 |
} catch(AmbiguousResolutionException ex) { |
| 132 |
// try to resolve the instance directly by looking at which one has already been initialized |
| 133 |
for (Bean<?> b : beans) { |
| 134 |
final CDIExtension cdiExtension = getCdiExtensionReference(b, beanManager); |
| 135 |
if (cdiExtension.toBeInitializedLater != null) { |
| 136 |
return cdiExtension; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
private static CDIExtension getCdiExtensionReference(final Bean extensionBean, final BeanManager beanManager) { |
| 145 |
final CreationalContext<?> creationalContext = beanManager.createCreationalContext(extensionBean); |
| 146 |
final Object result = beanManager.getReference(extensionBean, CDIExtension.class, creationalContext); |
| 147 |
return (CDIExtension)result; |
| 148 |
} |
| 149 |
|
| 150 |
private static boolean isSharedBaseClass(final Class<?> clazz, final Set<Bean<?>> beans) { |
| 151 |
for (Bean<?> bean : beans) { |
| 152 |
if (!clazz.isAssignableFrom(bean.getBeanClass())) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
} |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
private static Set<Bean<?>> getBaseClassSubSet(final Class<?> clazz, final Set<Bean<?>> beans) { |
| 160 |
for (Bean<?> bean : beans) { |
| 161 |
if (clazz == bean.getBeanClass()) { |
| 162 |
return Collections.<Bean<?>>singleton(bean); |
| 163 |
} |
| 164 |
} |
| 165 |
return beans; |
| 166 |
} |
| 167 |
} |