| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2013 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 com.sun.jersey.api.core.ResourceConfig; |
| 44 |
import com.sun.jersey.server.impl.InitialContextHelper; |
| 45 |
import com.sun.jersey.spi.container.WebApplication; |
| 46 |
import com.sun.jersey.spi.container.servlet.WebConfig; |
| 47 |
|
| 48 |
import javax.naming.InitialContext; |
| 49 |
import javax.naming.NamingException; |
| 50 |
import javax.servlet.ServletContext; |
| 51 |
import java.util.logging.Level; |
| 52 |
import java.util.logging.Logger; |
| 53 |
|
| 54 |
/** |
| 55 |
* Initializes the factory for IoCComponentProvider(s) for CDI beans. |
| 56 |
* |
| 57 |
* @author [email protected] |
| 58 |
* @author robc |
| 59 |
*/ |
| 60 |
public class CDIComponentProviderFactoryInitializer { |
| 61 |
|
| 62 |
private static final Logger LOGGER = Logger.getLogger(CDIComponentProviderFactoryInitializer.class.getName()); |
| 63 |
|
| 64 |
private static final String BEAN_MANAGER_CLASS = "javax.enterprise.inject.spi.BeanManager"; |
| 65 |
private static final String WELD_SERVLET_PACKAGE = "org.jboss.weld.environment.servlet"; |
| 66 |
|
| 67 |
public static void initialize(WebConfig wc, ResourceConfig rc, WebApplication wa) { |
| 68 |
ServletContext sc = wc.getServletContext(); |
| 69 |
|
| 70 |
Object beanManager = lookup(sc); |
| 71 |
if (beanManager == null) { |
| 72 |
LOGGER.config("The CDI BeanManager is not available. JAX-RS CDI support is disabled."); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
rc.getSingletons().add(new CDIComponentProviderFactory(beanManager, rc, wa)); |
| 77 |
LOGGER.info("CDI support is enabled"); |
| 78 |
} |
| 79 |
|
| 80 |
private static Object lookup(ServletContext sc) { |
| 81 |
Object beanManager = null; |
| 82 |
|
| 83 |
beanManager = lookupInJndi("java:comp/BeanManager"); |
| 84 |
if (beanManager != null) { |
| 85 |
return beanManager; |
| 86 |
} |
| 87 |
|
| 88 |
// Standard in CDI 1.1 |
| 89 |
beanManager = lookupInServletContext(sc, BEAN_MANAGER_CLASS); |
| 90 |
if (beanManager != null) { |
| 91 |
return beanManager; |
| 92 |
} |
| 93 |
|
| 94 |
// For older Weld versions |
| 95 |
beanManager = lookupInServletContext(sc, WELD_SERVLET_PACKAGE + "." + BEAN_MANAGER_CLASS); |
| 96 |
if (beanManager != null) { |
| 97 |
return beanManager; |
| 98 |
} |
| 99 |
|
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
private static Object lookupInJndi(String name) { |
| 104 |
try { |
| 105 |
InitialContext ic = InitialContextHelper.getInitialContext(); |
| 106 |
if (ic == null) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
Object beanManager = ic.lookup(name); |
| 111 |
// Some implementations of InitialContext return null instead of |
| 112 |
// throwing NamingException if there is no Object associated with |
| 113 |
// the name |
| 114 |
if (beanManager == null) { |
| 115 |
LOGGER.config("The CDI BeanManager is not available at " + name); |
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
LOGGER.config("The CDI BeanManager is at " + name); |
| 120 |
return beanManager; |
| 121 |
|
| 122 |
} catch (NamingException ex) { |
| 123 |
LOGGER.log(Level.CONFIG, "The CDI BeanManager is not available at " + name, ex); |
| 124 |
} |
| 125 |
|
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
private static Object lookupInServletContext(ServletContext sc, String name) { |
| 130 |
Object beanManager = sc.getAttribute(name); |
| 131 |
if (beanManager == null) { |
| 132 |
LOGGER.config("The CDI BeanManager is not available at " + name); |
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
LOGGER.config("The CDI BeanManager is at " + name); |
| 137 |
return beanManager; |
| 138 |
} |
| 139 |
} |