Debian Codegraph @

jersey-servlet/src/main/java/com/sun/jersey/server/impl/cdi/CDIComponentProviderFactory.java

raw
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2010-2015 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 package com.sun.jersey.server.impl.cdi;
41
42 import java.lang.annotation.Annotation;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.Map;
46 import java.util.Set;
47 import java.util.logging.Logger;
48
49 import javax.annotation.ManagedBean;
50 import javax.enterprise.context.ApplicationScoped;
51 import javax.enterprise.context.Dependent;
52 import javax.enterprise.context.RequestScoped;
53 import javax.enterprise.context.spi.CreationalContext;
54 import javax.enterprise.inject.spi.Bean;
55 import javax.enterprise.inject.spi.BeanManager;
56
57 import com.sun.jersey.api.core.ResourceConfig;
58 import com.sun.jersey.core.spi.component.ComponentContext;
59 import com.sun.jersey.core.spi.component.ComponentScope;
60 import com.sun.jersey.core.spi.component.ioc.IoCComponentProvider;
61 import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory;
62 import com.sun.jersey.core.spi.component.ioc.IoCDestroyable;
63 import com.sun.jersey.core.spi.component.ioc.IoCFullyManagedComponentProvider;
64 import com.sun.jersey.core.spi.component.ioc.IoCInstantiatedComponentProvider;
65 import com.sun.jersey.spi.container.WebApplication;
66 import com.sun.jersey.spi.container.WebApplicationListener;
67 import com.sun.jersey.spi.container.servlet.ServletContainer;
68
69 import static com.sun.jersey.server.impl.cdi.Utils.getBean;
70
71 /**
72 * Factory for IoCComponentProvider for CDI beans.
73 *
74 * Crucially, it passes the WebApplication and ResourceConfig on to the CDIExtension,
75 * allowing beans registered by the extension to (finally) do some work.
76 *
77 * @author [email protected]
78 * @author robc
79 */
80 public class CDIComponentProviderFactory implements
81 IoCComponentProviderFactory, WebApplicationListener {
82
83 private static final Logger LOGGER = Logger.getLogger(
84 CDIComponentProviderFactory.class.getName());
85
86 private final BeanManager beanManager;
87 private final CDIExtension extension;
88
89 public CDIComponentProviderFactory(Object bm, ResourceConfig rc, WebApplication wa) {
90 beanManager = (BeanManager)bm;
91 // work around proxying bug in Weld
92 if (CDIExtension.lookupExtensionInBeanManager) {
93 extension = CDIExtension.getInitializedExtensionFromBeanManager(beanManager);
94 } else {
95 extension = CDIExtension.getInitializedExtension();
96 }
97 extension.setWebApplication(wa);
98 extension.setResourceConfig(rc);
99 }
100
101 public void onWebApplicationReady() {
102 extension.lateInitialize();
103 }
104
105 public IoCComponentProvider getComponentProvider(Class<?> c) {
106 return getComponentProvider(null, c);
107 }
108
109 public IoCComponentProvider getComponentProvider(ComponentContext cc, final Class<?> c) {
110 final Bean<?> b = getBean(beanManager, c);
111 if (b == null) {
112 return null;
113 }
114
115 final Class<? extends Annotation> s = b.getScope();
116 final ComponentScope cs = getComponentScope(b);
117
118 if (s == Dependent.class) {
119 if (!extension.getResourceConfig().getFeature(ServletContainer.FEATURE_ALLOW_RAW_MANAGED_BEANS)
120 && !c.isAnnotationPresent(ManagedBean.class)) {
121 return null;
122 }
123
124 LOGGER.fine("Binding the CDI managed bean " + c.getName() +
125 " in scope " + s.getName() +
126 " to CDIComponentProviderFactory");
127
128 return new ComponentProviderDestroyable() {
129
130 // IoCInstantiatedComponentProvider
131
132 public Object getInjectableInstance(Object o) {
133 return o;
134 }
135
136 public Object getInstance() {
137 final CreationalContext<?> bcc = beanManager.createCreationalContext(b);
138 return c.cast(beanManager.getReference(b, c, bcc));
139 }
140
141 // IoCDestroyable
142
143 public void destroy(Object o) {
144 final CreationalContext cc = beanManager.createCreationalContext(b);
145 ((Bean)b).destroy(o, cc);
146 }
147 };
148 }
149 else {
150 LOGGER.fine("Binding the CDI managed bean " + c.getName() +
151 " in scope " + s.getName() +
152 " to CDIComponentProviderFactory in scope " + cs);
153
154 return new IoCFullyManagedComponentProvider() {
155 public ComponentScope getScope() {
156 return cs;
157 }
158
159 public Object getInstance() {
160 final CreationalContext<?> bcc = beanManager.createCreationalContext(b);
161 return c.cast(beanManager.getReference(b, c, bcc));
162 }
163 };
164 }
165 }
166
167 private interface ComponentProviderDestroyable extends IoCInstantiatedComponentProvider, IoCDestroyable {
168 };
169
170 private ComponentScope getComponentScope(Bean<?> b) {
171 ComponentScope cs = scopeMap.get(b.getScope());
172 return (cs != null) ? cs : ComponentScope.Undefined;
173 }
174
175 private final Map<Class<? extends Annotation>, ComponentScope> scopeMap = createScopeMap();
176
177 private Map<Class<? extends Annotation>, ComponentScope> createScopeMap() {
178 Map<Class<? extends Annotation>, ComponentScope> m =
179 new HashMap<Class<? extends Annotation>, ComponentScope>();
180 m.put(ApplicationScoped.class, ComponentScope.Singleton);
181 m.put(RequestScoped.class, ComponentScope.PerRequest);
182 m.put(Dependent.class, ComponentScope.PerRequest);
183 return Collections.unmodifiableMap(m);
184 }
185 }