| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2016 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.lang.annotation.Annotation; |
| 44 |
import java.lang.reflect.Array; |
| 45 |
import java.lang.reflect.GenericArrayType; |
| 46 |
import java.lang.reflect.Method; |
| 47 |
import java.lang.reflect.Modifier; |
| 48 |
import java.lang.reflect.ParameterizedType; |
| 49 |
import java.lang.reflect.Type; |
| 50 |
import java.util.ArrayList; |
| 51 |
import java.util.Collections; |
| 52 |
import java.util.HashMap; |
| 53 |
import java.util.HashSet; |
| 54 |
import java.util.List; |
| 55 |
import java.util.Map; |
| 56 |
import java.util.Set; |
| 57 |
import java.util.concurrent.ConcurrentHashMap; |
| 58 |
import java.util.logging.Logger; |
| 59 |
|
| 60 |
import javax.enterprise.context.spi.CreationalContext; |
| 61 |
import javax.enterprise.event.Observes; |
| 62 |
import javax.enterprise.inject.spi.AfterBeanDiscovery; |
| 63 |
import javax.enterprise.inject.spi.AnnotatedCallable; |
| 64 |
import javax.enterprise.inject.spi.AnnotatedConstructor; |
| 65 |
import javax.enterprise.inject.spi.AnnotatedField; |
| 66 |
import javax.enterprise.inject.spi.AnnotatedMethod; |
| 67 |
import javax.enterprise.inject.spi.AnnotatedParameter; |
| 68 |
import javax.enterprise.inject.spi.AnnotatedType; |
| 69 |
import javax.enterprise.inject.spi.Bean; |
| 70 |
import javax.enterprise.inject.spi.BeanManager; |
| 71 |
import javax.enterprise.inject.spi.BeforeBeanDiscovery; |
| 72 |
import javax.enterprise.inject.spi.Extension; |
| 73 |
import javax.enterprise.inject.spi.InjectionPoint; |
| 74 |
import javax.enterprise.inject.spi.ProcessAnnotatedType; |
| 75 |
import javax.enterprise.inject.spi.ProcessInjectionTarget; |
| 76 |
import javax.enterprise.inject.spi.ProcessManagedBean; |
| 77 |
import javax.enterprise.util.AnnotationLiteral; |
| 78 |
import javax.inject.Inject; |
| 79 |
import javax.inject.Provider; |
| 80 |
import javax.naming.InitialContext; |
| 81 |
import javax.naming.Name; |
| 82 |
import javax.naming.NamingException; |
| 83 |
import javax.ws.rs.CookieParam; |
| 84 |
import javax.ws.rs.DefaultValue; |
| 85 |
import javax.ws.rs.Encoded; |
| 86 |
import javax.ws.rs.FormParam; |
| 87 |
import javax.ws.rs.HeaderParam; |
| 88 |
import javax.ws.rs.MatrixParam; |
| 89 |
import javax.ws.rs.PathParam; |
| 90 |
import javax.ws.rs.QueryParam; |
| 91 |
import javax.ws.rs.core.Application; |
| 92 |
import javax.ws.rs.core.Context; |
| 93 |
import javax.ws.rs.core.HttpHeaders; |
| 94 |
import javax.ws.rs.core.Request; |
| 95 |
import javax.ws.rs.core.SecurityContext; |
| 96 |
import javax.ws.rs.core.UriInfo; |
| 97 |
import javax.ws.rs.ext.Providers; |
| 98 |
|
| 99 |
import com.sun.jersey.api.core.ExtendedUriInfo; |
| 100 |
import com.sun.jersey.api.core.HttpContext; |
| 101 |
import com.sun.jersey.api.core.HttpRequestContext; |
| 102 |
import com.sun.jersey.api.core.HttpResponseContext; |
| 103 |
import com.sun.jersey.api.core.ResourceConfig; |
| 104 |
import com.sun.jersey.api.core.ResourceContext; |
| 105 |
import com.sun.jersey.api.model.Parameter; |
| 106 |
import com.sun.jersey.core.spi.component.ComponentScope; |
| 107 |
import com.sun.jersey.core.util.FeaturesAndProperties; |
| 108 |
import com.sun.jersey.server.impl.InitialContextHelper; |
| 109 |
import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable; |
| 110 |
import com.sun.jersey.spi.MessageBodyWorkers; |
| 111 |
import com.sun.jersey.spi.container.ExceptionMapperContext; |
| 112 |
import com.sun.jersey.spi.container.WebApplication; |
| 113 |
import com.sun.jersey.spi.inject.Errors; |
| 114 |
import com.sun.jersey.spi.inject.Injectable; |
| 115 |
|
| 116 |
/** |
| 117 |
* |
| 118 |
* @author robc |
| 119 |
*/ |
| 120 |
public class CDIExtension implements Extension { |
| 121 |
|
| 122 |
private static final Logger LOGGER = Logger.getLogger(CDIExtension.class.getName()); |
| 123 |
|
| 124 |
private static class ContextAnnotationLiteral extends AnnotationLiteral<Context> implements Context { } |
| 125 |
|
| 126 |
private final Context contextAnnotationLiteral = new ContextAnnotationLiteral(); |
| 127 |
|
| 128 |
private static class InjectAnnotationLiteral extends AnnotationLiteral<Inject> implements Inject { } |
| 129 |
|
| 130 |
private final Inject injectAnnotationLiteral = new InjectAnnotationLiteral(); |
| 131 |
|
| 132 |
private static class SyntheticQualifierAnnotationImpl extends AnnotationLiteral<SyntheticQualifier> |
| 133 |
implements SyntheticQualifier { |
| 134 |
private int value; |
| 135 |
|
| 136 |
public SyntheticQualifierAnnotationImpl(int value) { |
| 137 |
this.value = value; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public int value() { |
| 142 |
return value; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// annotations to be turned into qualifiers |
| 147 |
private static final Set<Class<? extends Annotation>> knownParameterQualifiers = initializeKnownParamQualifiers(); |
| 148 |
private static Set<Class<? extends Annotation>> initializeKnownParamQualifiers() { |
| 149 |
Set<Class<? extends Annotation>> classes = new HashSet<Class<? extends Annotation>>(); |
| 150 |
classes.add(CookieParam.class); |
| 151 |
classes.add(FormParam.class); |
| 152 |
classes.add(HeaderParam.class); |
| 153 |
classes.add(MatrixParam.class); |
| 154 |
classes.add(PathParam.class); |
| 155 |
classes.add(QueryParam.class); |
| 156 |
classes.add(Context.class); |
| 157 |
return Collections.unmodifiableSet(classes); |
| 158 |
} |
| 159 |
|
| 160 |
// used to map a qualifier to a Parameter.Source |
| 161 |
private static final Map<Class<? extends Annotation>, Parameter.Source> paramQualifiersMap = initializeKnownParamQualifiersMap(); |
| 162 |
private static Map<Class<? extends Annotation>, Parameter.Source> initializeKnownParamQualifiersMap() { |
| 163 |
Map<Class<? extends Annotation>, Parameter.Source> map = new HashMap<Class<? extends Annotation>, Parameter.Source>(); |
| 164 |
map.put(CookieParam.class, Parameter.Source.COOKIE); |
| 165 |
map.put(FormParam.class, Parameter.Source.FORM); |
| 166 |
map.put(HeaderParam.class, Parameter.Source.HEADER); |
| 167 |
map.put(MatrixParam.class, Parameter.Source.MATRIX); |
| 168 |
map.put(PathParam.class, Parameter.Source.PATH); |
| 169 |
map.put(QueryParam.class, Parameter.Source.QUERY); |
| 170 |
map.put(Context.class, Parameter.Source.CONTEXT); |
| 171 |
return Collections.unmodifiableMap(map); |
| 172 |
} |
| 173 |
|
| 174 |
// pre-defined contextual types |
| 175 |
private static final Set<Class<?>> staticallyDefinedContextBeans = initializeStaticallyDefinedContextBeans(); |
| 176 |
private static Set<Class<?>> initializeStaticallyDefinedContextBeans() { |
| 177 |
Set<Class<?>> classes = new HashSet<Class<?>>(); |
| 178 |
// standard JAX-RS types |
| 179 |
classes.add(Application.class); |
| 180 |
classes.add(HttpHeaders.class); |
| 181 |
classes.add(Providers.class); |
| 182 |
classes.add(Request.class); |
| 183 |
classes.add(SecurityContext.class); |
| 184 |
classes.add(UriInfo.class); |
| 185 |
// Jersey extensions |
| 186 |
classes.add(ExceptionMapperContext.class); |
| 187 |
classes.add(ExtendedUriInfo.class); |
| 188 |
classes.add(FeaturesAndProperties.class); |
| 189 |
classes.add(HttpContext.class); |
| 190 |
classes.add(HttpRequestContext.class); |
| 191 |
classes.add(HttpResponseContext.class); |
| 192 |
classes.add(MessageBodyWorkers.class); |
| 193 |
classes.add(ResourceContext.class); |
| 194 |
classes.add(WebApplication.class); |
| 195 |
return Collections.unmodifiableSet(classes); |
| 196 |
} |
| 197 |
|
| 198 |
private final Map<ClassLoader, WebApplication> webApplications = new HashMap<ClassLoader, WebApplication>(); |
| 199 |
private volatile ResourceConfig resourceConfig; |
| 200 |
|
| 201 |
private volatile Map<Class<? extends Annotation>, Set<DiscoveredParameter>> discoveredParameterMap; |
| 202 |
private volatile Map<DiscoveredParameter, SyntheticQualifier> syntheticQualifierMap; |
| 203 |
private int nextSyntheticQualifierValue = 0; |
| 204 |
|
| 205 |
/* package */ volatile List<InitializedLater> toBeInitializedLater; |
| 206 |
|
| 207 |
private static final String JNDI_CDIEXTENSION_NAME = "CDIExtension"; |
| 208 |
private static final String JNDI_CDIEXTENSION_CTX = "com/sun/jersey/config"; |
| 209 |
|
| 210 |
/* |
| 211 |
* Setting this system property to "true" will force use of the BeanManager to look up the bean for the active CDIExtension, |
| 212 |
* rather than going through a thread local. |
| 213 |
*/ |
| 214 |
private static final String LOOKUP_EXTENSION_IN_BEAN_MANAGER_SYSTEM_PROPERTY = "com.sun.jersey.server.impl.cdi.lookupExtensionInBeanManager"; |
| 215 |
|
| 216 |
public static final boolean lookupExtensionInBeanManager = getLookupExtensionInBeanManager(); |
| 217 |
|
| 218 |
private static boolean getLookupExtensionInBeanManager() { |
| 219 |
return Boolean.parseBoolean(System.getProperty(LOOKUP_EXTENSION_IN_BEAN_MANAGER_SYSTEM_PROPERTY, "false")); |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* Returns the instance of CDIExtension that was initialized previously in this same thread, if any. |
| 224 |
*/ |
| 225 |
public static CDIExtension getInitializedExtension() { |
| 226 |
try { |
| 227 |
InitialContext ic = InitialContextHelper.getInitialContext(); |
| 228 |
if (ic == null) { |
| 229 |
throw new RuntimeException(); |
| 230 |
} |
| 231 |
return (CDIExtension)lookupJerseyConfigJNDIContext(ic).lookup(JNDI_CDIEXTENSION_NAME); |
| 232 |
} catch (NamingException ex) { |
| 233 |
throw new RuntimeException(ex); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
public static CDIExtension getInitializedExtensionFromBeanManager(BeanManager bm) { |
| 238 |
CDIExtension bean = Utils.getCdiExtensionInstance(bm); |
| 239 |
if (bean == null) { |
| 240 |
throw new RuntimeException("Initialized Extension not found."); |
| 241 |
} |
| 242 |
return bean; |
| 243 |
} |
| 244 |
|
| 245 |
public CDIExtension() {} |
| 246 |
|
| 247 |
private void initialize(BeanManager manager) { |
| 248 |
// initialize in a separate method because Weld creates a proxy for the extension |
| 249 |
// and we don't want to waste time initializing it |
| 250 |
|
| 251 |
// workaround for Weld proxy bug |
| 252 |
if (!lookupExtensionInBeanManager) { |
| 253 |
try { |
| 254 |
InitialContext ic = InitialContextHelper.getInitialContext(); |
| 255 |
if (ic != null) { |
| 256 |
javax.naming.Context jerseyConfigJNDIContext = createJerseyConfigJNDIContext(ic); |
| 257 |
jerseyConfigJNDIContext.rebind(JNDI_CDIEXTENSION_NAME, this); |
| 258 |
} |
| 259 |
} catch (NamingException ex) { |
| 260 |
throw new RuntimeException(ex); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// tracks all discovered parameters |
| 265 |
Map<Class<? extends Annotation>, Set<DiscoveredParameter>> map2 = new HashMap<Class<? extends Annotation>, Set<DiscoveredParameter>>(); |
| 266 |
for (Class<? extends Annotation> qualifier : knownParameterQualifiers) { |
| 267 |
map2.put(qualifier, new HashSet<DiscoveredParameter>()); |
| 268 |
} |
| 269 |
discoveredParameterMap = Collections.unmodifiableMap(map2); |
| 270 |
|
| 271 |
// tracks the synthetic qualifiers we have to create to handle a specific |
| 272 |
// combination of JAX-RS injection annotation + default value + encoded |
| 273 |
syntheticQualifierMap = new HashMap<DiscoveredParameter, SyntheticQualifier>(); |
| 274 |
|
| 275 |
// things to do in a second time, i.e. once Jersey has been initialized, |
| 276 |
// as opposed to when CDI delivers the SPI events to its extensions |
| 277 |
toBeInitializedLater = new ArrayList<InitializedLater>(); |
| 278 |
} |
| 279 |
|
| 280 |
private static interface JNDIContextDiver { |
| 281 |
javax.naming.Context stepInto(javax.naming.Context currentContext, String currentName) throws NamingException; |
| 282 |
} |
| 283 |
|
| 284 |
private static javax.naming.Context diveIntoJNDIContext(javax.naming.Context initialContext, JNDIContextDiver diver) throws NamingException { |
| 285 |
Name jerseyConfigCtxName = initialContext.getNameParser("").parse(JNDI_CDIEXTENSION_CTX); |
| 286 |
javax.naming.Context currentContext = initialContext; |
| 287 |
for (int i=0; i<jerseyConfigCtxName.size(); i++) { |
| 288 |
currentContext = diver.stepInto(currentContext, jerseyConfigCtxName.get(i)); |
| 289 |
} |
| 290 |
return currentContext; |
| 291 |
} |
| 292 |
|
| 293 |
private static javax.naming.Context createJerseyConfigJNDIContext(javax.naming.Context initialContext) throws NamingException { |
| 294 |
return diveIntoJNDIContext(initialContext, new JNDIContextDiver() { |
| 295 |
|
| 296 |
@Override |
| 297 |
public javax.naming.Context stepInto(javax.naming.Context ctx, String name) throws NamingException { |
| 298 |
try { |
| 299 |
return (javax.naming.Context) ctx.lookup(name); |
| 300 |
} catch (NamingException e) { |
| 301 |
return ctx.createSubcontext(name); |
| 302 |
} |
| 303 |
} |
| 304 |
}); |
| 305 |
} |
| 306 |
|
| 307 |
private static javax.naming.Context lookupJerseyConfigJNDIContext(javax.naming.Context initialContext) throws NamingException { |
| 308 |
return diveIntoJNDIContext(initialContext, new JNDIContextDiver() { |
| 309 |
@Override |
| 310 |
public javax.naming.Context stepInto(javax.naming.Context ctx, String name) throws NamingException { |
| 311 |
return (javax.naming.Context) ctx.lookup(name); |
| 312 |
} |
| 313 |
}); |
| 314 |
} |
| 315 |
|
| 316 |
void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event, BeanManager manager) { |
| 317 |
LOGGER.fine("Handling BeforeBeanDiscovery event"); |
| 318 |
initialize(manager); |
| 319 |
|
| 320 |
// turn JAX-RS injection annotations into CDI qualifiers |
| 321 |
for (Class<? extends Annotation> qualifier : knownParameterQualifiers) { |
| 322 |
event.addQualifier(qualifier); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/* |
| 327 |
* Holds information on one site (constructor/method argument or field) to be patched. |
| 328 |
*/ |
| 329 |
private static class PatchInformation { |
| 330 |
private DiscoveredParameter parameter; |
| 331 |
private SyntheticQualifier syntheticQualifier; |
| 332 |
private Annotation annotation; |
| 333 |
private boolean mustAddInject; |
| 334 |
|
| 335 |
public PatchInformation(DiscoveredParameter parameter, SyntheticQualifier syntheticQualifier, boolean mustAddInject) { |
| 336 |
this(parameter, syntheticQualifier, null, mustAddInject); |
| 337 |
} |
| 338 |
|
| 339 |
public PatchInformation(DiscoveredParameter parameter, SyntheticQualifier syntheticQualifier, Annotation annotation, boolean mustAddInject) { |
| 340 |
this.parameter = parameter; |
| 341 |
this.syntheticQualifier = syntheticQualifier; |
| 342 |
this.annotation = annotation; |
| 343 |
this.mustAddInject = mustAddInject; |
| 344 |
} |
| 345 |
|
| 346 |
public DiscoveredParameter getParameter() { |
| 347 |
return parameter; |
| 348 |
} |
| 349 |
|
| 350 |
public SyntheticQualifier getSyntheticQualifier() { |
| 351 |
return syntheticQualifier; |
| 352 |
} |
| 353 |
|
| 354 |
public Annotation getAnnotation() { |
| 355 |
return annotation; |
| 356 |
} |
| 357 |
|
| 358 |
public boolean mustAddInject() { |
| 359 |
return mustAddInject; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
<T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> event) { |
| 364 |
LOGGER.fine("Handling ProcessAnnotatedType event for " + event.getAnnotatedType().getJavaClass().getName()); |
| 365 |
|
| 366 |
AnnotatedType<T> type = event.getAnnotatedType(); |
| 367 |
|
| 368 |
/* |
| 369 |
// only scan managed beans |
| 370 |
if (!type.isAnnotationPresent(ManagedBean.class)) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
// only scan root resource classes for now |
| 375 |
if (!type.isAnnotationPresent(Path.class)) { |
| 376 |
return; |
| 377 |
} |
| 378 |
*/ |
| 379 |
|
| 380 |
// first pass to determine if we need to patch any sites |
| 381 |
// we also record any qualifiers with parameters we encounter |
| 382 |
// so we can create beans for them later |
| 383 |
|
| 384 |
// TODO - maybe we should detect cases in which the constructor selection |
| 385 |
// rules in CDI and JAX-RS are in conflict -- CDI should win, but |
| 386 |
// the result may surprise the user |
| 387 |
|
| 388 |
boolean classHasEncodedAnnotation = type.isAnnotationPresent(Encoded.class); |
| 389 |
|
| 390 |
Set<AnnotatedConstructor<T>> mustPatchConstructors = new HashSet<AnnotatedConstructor<T>>(); |
| 391 |
Map<AnnotatedParameter<? super T>, PatchInformation> parameterToPatchInfoMap = new HashMap<AnnotatedParameter<? super T>, PatchInformation>(); |
| 392 |
|
| 393 |
for (AnnotatedConstructor<T> constructor : type.getConstructors()) { |
| 394 |
if (processAnnotatedConstructor(constructor, classHasEncodedAnnotation, parameterToPatchInfoMap)) { |
| 395 |
mustPatchConstructors.add(constructor); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
Set<AnnotatedField<? super T>> mustPatchFields = new HashSet<AnnotatedField<? super T>>(); |
| 400 |
Map<AnnotatedField<? super T>, PatchInformation> fieldToPatchInfoMap = new HashMap<AnnotatedField<? super T>, PatchInformation>(); |
| 401 |
|
| 402 |
for (AnnotatedField<? super T> field : type.getFields()) { |
| 403 |
if (processAnnotatedField(field, type.getJavaClass(), classHasEncodedAnnotation, fieldToPatchInfoMap)) { |
| 404 |
mustPatchFields.add(field); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
Set<AnnotatedMethod<? super T>> mustPatchMethods = new HashSet<AnnotatedMethod<? super T>>(); |
| 409 |
Set<AnnotatedMethod<? super T>> setterMethodsWithoutInject = new HashSet<AnnotatedMethod<? super T>>(); |
| 410 |
|
| 411 |
for (AnnotatedMethod<? super T> method : type.getMethods()) { |
| 412 |
if (processAnnotatedMethod(method, type.getJavaClass(), classHasEncodedAnnotation, parameterToPatchInfoMap, setterMethodsWithoutInject)) { |
| 413 |
mustPatchMethods.add(method); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
boolean typeNeedsPatching = !(mustPatchConstructors.isEmpty() && mustPatchFields.isEmpty() && mustPatchMethods.isEmpty()); |
| 418 |
|
| 419 |
// second pass |
| 420 |
if (typeNeedsPatching) { |
| 421 |
AnnotatedTypeImpl<T> newType = new AnnotatedTypeImpl(type); |
| 422 |
|
| 423 |
Set<AnnotatedConstructor<T>> newConstructors = new HashSet<AnnotatedConstructor<T>>(); |
| 424 |
for (AnnotatedConstructor<T> constructor : type.getConstructors()) { |
| 425 |
AnnotatedConstructorImpl<T> newConstructor = new AnnotatedConstructorImpl(constructor, newType); |
| 426 |
if (mustPatchConstructors.contains(constructor)) { |
| 427 |
patchAnnotatedCallable(constructor, newConstructor, parameterToPatchInfoMap); |
| 428 |
} |
| 429 |
else { |
| 430 |
copyParametersOfAnnotatedCallable(constructor, newConstructor); |
| 431 |
} |
| 432 |
newConstructors.add(newConstructor); |
| 433 |
} |
| 434 |
|
| 435 |
Set<AnnotatedField<? super T>> newFields = new HashSet<AnnotatedField<? super T>>(); |
| 436 |
for (AnnotatedField<? super T> field : type.getFields()) { |
| 437 |
if (mustPatchFields.contains(field)) { |
| 438 |
PatchInformation patchInfo = fieldToPatchInfoMap.get(field); |
| 439 |
Set<Annotation> annotations = new HashSet<Annotation>(); |
| 440 |
if (patchInfo.mustAddInject()) { |
| 441 |
annotations.add(injectAnnotationLiteral); |
| 442 |
} |
| 443 |
if (patchInfo.getSyntheticQualifier() != null) { |
| 444 |
annotations.add(patchInfo.getSyntheticQualifier()); |
| 445 |
Annotation skippedQualifier = patchInfo.getParameter().getAnnotation(); |
| 446 |
for (Annotation annotation : field.getAnnotations()) { |
| 447 |
if (annotation != skippedQualifier) { |
| 448 |
annotations.add(annotation); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
else { |
| 453 |
annotations.addAll(field.getAnnotations()); |
| 454 |
} |
| 455 |
if (patchInfo.getAnnotation() != null) { |
| 456 |
annotations.add(patchInfo.getAnnotation()); |
| 457 |
} |
| 458 |
newFields.add(new AnnotatedFieldImpl<T>(field, annotations, newType)); |
| 459 |
} |
| 460 |
else { |
| 461 |
// copy and reparent |
| 462 |
newFields.add(new AnnotatedFieldImpl<T>(field, newType)); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
Set<AnnotatedMethod<? super T>> newMethods = new HashSet<AnnotatedMethod<? super T>>(); |
| 467 |
for (AnnotatedMethod<? super T> method : type.getMethods()) { |
| 468 |
if (mustPatchMethods.contains((AnnotatedMethod<T>)method)) { |
| 469 |
if (setterMethodsWithoutInject.contains((AnnotatedMethod<T>)method)) { |
| 470 |
Set<Annotation> annotations = new HashSet<Annotation>(); |
| 471 |
annotations.add(injectAnnotationLiteral); |
| 472 |
for (Annotation annotation : method.getAnnotations()) { |
| 473 |
if (!knownParameterQualifiers.contains(annotation.annotationType())) { |
| 474 |
annotations.add(annotation); |
| 475 |
} |
| 476 |
} |
| 477 |
AnnotatedMethodImpl<T> newMethod = new AnnotatedMethodImpl<T>(method, annotations, newType); |
| 478 |
patchAnnotatedCallable(method, newMethod, parameterToPatchInfoMap); |
| 479 |
newMethods.add(newMethod); |
| 480 |
} |
| 481 |
else { |
| 482 |
AnnotatedMethodImpl<T> newMethod = new AnnotatedMethodImpl<T>(method, newType); |
| 483 |
patchAnnotatedCallable(method, newMethod, parameterToPatchInfoMap); |
| 484 |
newMethods.add(newMethod); |
| 485 |
} |
| 486 |
} |
| 487 |
else { |
| 488 |
AnnotatedMethodImpl<T> newMethod = new AnnotatedMethodImpl<T>(method, newType); |
| 489 |
copyParametersOfAnnotatedCallable(method, newMethod); |
| 490 |
newMethods.add(newMethod); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
newType.setConstructors(newConstructors); |
| 495 |
newType.setFields(newFields); |
| 496 |
newType.setMethods(newMethods); |
| 497 |
event.setAnnotatedType(newType); |
| 498 |
|
| 499 |
LOGGER.fine(" replaced annotated type for " + type.getJavaClass()); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
private <T> boolean processAnnotatedConstructor(AnnotatedConstructor<T> constructor, |
| 504 |
boolean classHasEncodedAnnotation, |
| 505 |
Map<AnnotatedParameter<? super T>, PatchInformation> parameterToPatchInfoMap) { |
| 506 |
boolean mustPatch = false; |
| 507 |
|
| 508 |
if (constructor.getAnnotation(Inject.class) != null) { |
| 509 |
boolean methodHasEncodedAnnotation = constructor.isAnnotationPresent(Encoded.class); |
| 510 |
for (AnnotatedParameter<T> parameter : constructor.getParameters()) { |
| 511 |
for (Annotation annotation : parameter.getAnnotations()) { |
| 512 |
Set<DiscoveredParameter> discovered = discoveredParameterMap.get(annotation.annotationType()); |
| 513 |
if (discovered != null) { |
| 514 |
if (knownParameterQualifiers.contains(annotation.annotationType())) { |
| 515 |
if (methodHasEncodedAnnotation || |
| 516 |
classHasEncodedAnnotation || |
| 517 |
parameter.isAnnotationPresent(DefaultValue.class)) { |
| 518 |
mustPatch = true; |
| 519 |
} |
| 520 |
|
| 521 |
boolean encoded = parameter.isAnnotationPresent(Encoded.class) || methodHasEncodedAnnotation || classHasEncodedAnnotation; |
| 522 |
DefaultValue defaultValue = parameter.getAnnotation(DefaultValue.class); |
| 523 |
if (defaultValue != null) { |
| 524 |
mustPatch = true; |
| 525 |
} |
| 526 |
DiscoveredParameter jerseyParameter = new DiscoveredParameter(annotation, parameter.getBaseType(), defaultValue, encoded); |
| 527 |
discovered.add(jerseyParameter); |
| 528 |
LOGGER.fine(" recorded " + jerseyParameter); |
| 529 |
parameterToPatchInfoMap.put(parameter, new PatchInformation(jerseyParameter, getSyntheticQualifierFor(jerseyParameter), false)); |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
return mustPatch; |
| 537 |
} |
| 538 |
|
| 539 |
private <T> boolean processAnnotatedMethod(AnnotatedMethod<? super T> method, |
| 540 |
Class<T> token, |
| 541 |
boolean classHasEncodedAnnotation, |
| 542 |
Map<AnnotatedParameter<? super T>, PatchInformation> parameterToPatchInfoMap, |
| 543 |
Set<AnnotatedMethod<? super T>> setterMethodsWithoutInject) { |
| 544 |
boolean mustPatch = false; |
| 545 |
|
| 546 |
if (method.getAnnotation(Inject.class) != null) { |
| 547 |
// a method already annotated with @Inject -- we assume the user is |
| 548 |
// aware of CDI and all we need to do is to detect the need for |
| 549 |
// a synthetic qualifier so as to take @DefaultValue and @Encoded into |
| 550 |
// account |
| 551 |
boolean methodHasEncodedAnnotation = method.isAnnotationPresent(Encoded.class); |
| 552 |
for (AnnotatedParameter<? super T> parameter : method.getParameters()) { |
| 553 |
for (Annotation annotation : parameter.getAnnotations()) { |
| 554 |
Set<DiscoveredParameter> discovered = discoveredParameterMap.get(annotation.annotationType()); |
| 555 |
if (discovered != null) { |
| 556 |
if (knownParameterQualifiers.contains(annotation.annotationType())) { |
| 557 |
if (methodHasEncodedAnnotation || |
| 558 |
classHasEncodedAnnotation || |
| 559 |
parameter.isAnnotationPresent(DefaultValue.class)) { |
| 560 |
mustPatch = true; |
| 561 |
} |
| 562 |
|
| 563 |
boolean encoded = parameter.isAnnotationPresent(Encoded.class) || methodHasEncodedAnnotation || classHasEncodedAnnotation; |
| 564 |
DefaultValue defaultValue = parameter.getAnnotation(DefaultValue.class); |
| 565 |
if (defaultValue != null) { |
| 566 |
mustPatch = true; |
| 567 |
} |
| 568 |
DiscoveredParameter jerseyParameter = new DiscoveredParameter(annotation, parameter.getBaseType(), defaultValue, encoded); |
| 569 |
discovered.add(jerseyParameter); |
| 570 |
LOGGER.fine(" recorded " + jerseyParameter); |
| 571 |
parameterToPatchInfoMap.put(parameter, new PatchInformation(jerseyParameter, getSyntheticQualifierFor(jerseyParameter), false)); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
else { |
| 578 |
// a method *not* annotated with @Inject -- here we only deal with |
| 579 |
// setter methods with a JAX-RS "qualifier" (Context, QueryParam, etc.) |
| 580 |
// on the method itself |
| 581 |
if (isSetterMethod(method)) { |
| 582 |
boolean methodHasEncodedAnnotation = method.isAnnotationPresent(Encoded.class); |
| 583 |
for (Annotation annotation : method.getAnnotations()) { |
| 584 |
Set<DiscoveredParameter> discovered = discoveredParameterMap.get(annotation.annotationType()); |
| 585 |
if (discovered != null) { |
| 586 |
if (knownParameterQualifiers.contains(annotation.annotationType())) { |
| 587 |
mustPatch = true; |
| 588 |
setterMethodsWithoutInject.add(method); |
| 589 |
for (AnnotatedParameter<? super T> parameter : method.getParameters()) { |
| 590 |
boolean encoded = parameter.isAnnotationPresent(Encoded.class) || methodHasEncodedAnnotation || classHasEncodedAnnotation; |
| 591 |
DefaultValue defaultValue = parameter.getAnnotation(DefaultValue.class); |
| 592 |
if (defaultValue == null) { |
| 593 |
defaultValue = method.getAnnotation(DefaultValue.class); |
| 594 |
} |
| 595 |
DiscoveredParameter jerseyParameter = new DiscoveredParameter(annotation, parameter.getBaseType(), defaultValue, encoded); |
| 596 |
discovered.add(jerseyParameter); |
| 597 |
LOGGER.fine(" recorded " + jerseyParameter); |
| 598 |
SyntheticQualifier syntheticQualifier = getSyntheticQualifierFor(jerseyParameter); |
| 599 |
// if there is no synthetic qualifier, add to the parameter the annotation that was on the method itself |
| 600 |
Annotation addedAnnotation = syntheticQualifier == null ? annotation : null; |
| 601 |
parameterToPatchInfoMap.put(parameter, new PatchInformation(jerseyParameter, syntheticQualifier, addedAnnotation, false)); |
| 602 |
} |
| 603 |
break; |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
return mustPatch; |
| 611 |
} |
| 612 |
|
| 613 |
private <T> boolean isSetterMethod(AnnotatedMethod<T> method) { |
| 614 |
Method javaMethod = method.getJavaMember(); |
| 615 |
if ((javaMethod.getModifiers() & Modifier.PUBLIC) != 0 && |
| 616 |
(javaMethod.getReturnType() == Void.TYPE) && |
| 617 |
(javaMethod.getName().startsWith("set"))) { |
| 618 |
List<AnnotatedParameter<T>> parameters = method.getParameters(); |
| 619 |
if (parameters.size() == 1) { |
| 620 |
return true; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
return false; |
| 625 |
} |
| 626 |
|
| 627 |
private <T> boolean processAnnotatedField(AnnotatedField<? super T> field, |
| 628 |
Class<T> token, |
| 629 |
boolean classHasEncodedAnnotation, |
| 630 |
Map<AnnotatedField<? super T>, PatchInformation> fieldToPatchInfoMap) { |
| 631 |
boolean mustPatch = false; |
| 632 |
for (Annotation annotation : field.getAnnotations()) { |
| 633 |
if (knownParameterQualifiers.contains(annotation.annotationType())) { |
| 634 |
boolean mustAddInjectAnnotation = !field.isAnnotationPresent(Inject.class); |
| 635 |
|
| 636 |
if (field.isAnnotationPresent(Encoded.class) || |
| 637 |
classHasEncodedAnnotation || |
| 638 |
mustAddInjectAnnotation || |
| 639 |
field.isAnnotationPresent(DefaultValue.class)) { |
| 640 |
mustPatch = true; |
| 641 |
} |
| 642 |
|
| 643 |
Set<DiscoveredParameter> discovered = discoveredParameterMap.get(annotation.annotationType()); |
| 644 |
if (discovered != null) { |
| 645 |
boolean encoded = field.isAnnotationPresent(Encoded.class) || classHasEncodedAnnotation; |
| 646 |
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class); |
| 647 |
DiscoveredParameter parameter = new DiscoveredParameter(annotation, field.getBaseType(), defaultValue, encoded); |
| 648 |
discovered.add(parameter); |
| 649 |
LOGGER.fine(" recorded " + parameter); |
| 650 |
fieldToPatchInfoMap.put(field, new PatchInformation(parameter, getSyntheticQualifierFor(parameter), mustAddInjectAnnotation)); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
return mustPatch; |
| 656 |
} |
| 657 |
|
| 658 |
private <T> void patchAnnotatedCallable(AnnotatedCallable<? super T> callable, |
| 659 |
AnnotatedCallableImpl<T> newCallable, |
| 660 |
Map<AnnotatedParameter<? super T>, PatchInformation> parameterToPatchInfoMap) { |
| 661 |
List<AnnotatedParameter<T>> newParams = new ArrayList<AnnotatedParameter<T>>(); |
| 662 |
for (AnnotatedParameter<? super T> parameter : callable.getParameters()) { |
| 663 |
PatchInformation patchInfo = parameterToPatchInfoMap.get(parameter); |
| 664 |
if (patchInfo != null) { |
| 665 |
Set<Annotation> annotations = new HashSet<Annotation>(); |
| 666 |
// in reality, this cannot happen |
| 667 |
if (patchInfo.mustAddInject()) { |
| 668 |
annotations.add(injectAnnotationLiteral); |
| 669 |
} |
| 670 |
|
| 671 |
if (patchInfo.getSyntheticQualifier() != null) { |
| 672 |
annotations.add(patchInfo.getSyntheticQualifier()); |
| 673 |
Annotation skippedQualifier = patchInfo.getParameter().getAnnotation(); |
| 674 |
for (Annotation annotation : parameter.getAnnotations()) { |
| 675 |
if (annotation != skippedQualifier) { |
| 676 |
annotations.add(annotation); |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
else { |
| 681 |
annotations.addAll(parameter.getAnnotations()); |
| 682 |
} |
| 683 |
if (patchInfo.getAnnotation() != null) { |
| 684 |
annotations.add(patchInfo.getAnnotation()); |
| 685 |
} |
| 686 |
newParams.add(new AnnotatedParameterImpl<T>(parameter, annotations, newCallable)); |
| 687 |
} |
| 688 |
else { |
| 689 |
newParams.add(new AnnotatedParameterImpl<T>(parameter, newCallable)); |
| 690 |
} |
| 691 |
} |
| 692 |
newCallable.setParameters(newParams); |
| 693 |
} |
| 694 |
|
| 695 |
private <T> void copyParametersOfAnnotatedCallable(AnnotatedCallable<? super T> callable, AnnotatedCallableImpl<T> newCallable) { |
| 696 |
// copy and reparent all the parameters |
| 697 |
List<AnnotatedParameter<T>> newParams = new ArrayList<AnnotatedParameter<T>>(); |
| 698 |
for (AnnotatedParameter<? super T> parameter : callable.getParameters()) { |
| 699 |
newParams.add(new AnnotatedParameterImpl<T>(parameter, newCallable)); |
| 700 |
} |
| 701 |
newCallable.setParameters(newParams); |
| 702 |
} |
| 703 |
|
| 704 |
private SyntheticQualifier getSyntheticQualifierFor(DiscoveredParameter parameter) { |
| 705 |
SyntheticQualifier result = syntheticQualifierMap.get(parameter); |
| 706 |
if (result == null) { |
| 707 |
// only create a synthetic qualifier if we're dealing with @DefaultValue |
| 708 |
// or @Encoded; this way the application can still use vanilla param |
| 709 |
// annotations as qualifiers |
| 710 |
if (parameter.isEncoded() || parameter.getDefaultValue() != null) { |
| 711 |
result = new SyntheticQualifierAnnotationImpl(nextSyntheticQualifierValue++); |
| 712 |
syntheticQualifierMap.put(parameter, result); |
| 713 |
LOGGER.fine(" created synthetic qualifier " + result); |
| 714 |
} |
| 715 |
} |
| 716 |
return result; |
| 717 |
} |
| 718 |
|
| 719 |
// taken from ReflectionHelper |
| 720 |
private static Class getClassOfType(Type type) { |
| 721 |
if (type instanceof Class) { |
| 722 |
return (Class)type; |
| 723 |
} else if (type instanceof GenericArrayType) { |
| 724 |
GenericArrayType arrayType = (GenericArrayType)type; |
| 725 |
Type t = arrayType.getGenericComponentType(); |
| 726 |
if (t instanceof Class) { |
| 727 |
Class c = (Class)t; |
| 728 |
try { |
| 729 |
// TODO is there a better way to get the Class object |
| 730 |
// representing an array |
| 731 |
Object o = Array.newInstance(c, 0); |
| 732 |
return o.getClass(); |
| 733 |
} catch (Exception e) { |
| 734 |
throw new IllegalArgumentException(e); |
| 735 |
} |
| 736 |
} |
| 737 |
} else if (type instanceof ParameterizedType) { |
| 738 |
ParameterizedType subType = (ParameterizedType)type; |
| 739 |
Type t = subType.getRawType(); |
| 740 |
if (t instanceof Class) { |
| 741 |
return (Class)t; |
| 742 |
} |
| 743 |
} |
| 744 |
return null; |
| 745 |
} |
| 746 |
|
| 747 |
<T> void processInjectionTarget(@Observes ProcessInjectionTarget<T> event) { |
| 748 |
LOGGER.fine("Handling ProcessInjectionTarget event for " + event.getAnnotatedType().getJavaClass().getName()); |
| 749 |
} |
| 750 |
|
| 751 |
/* |
| 752 |
void processBean(@Observes ProcessBean<?> event) { |
| 753 |
LOGGER.fine("Handling ProcessBean event for " + event.getBean().getBeanClass().getName()); |
| 754 |
} |
| 755 |
*/ |
| 756 |
|
| 757 |
void processManagedBean(@Observes ProcessManagedBean<?> event) { |
| 758 |
LOGGER.fine("Handling ProcessManagedBean event for " + event.getBean().getBeanClass().getName()); |
| 759 |
|
| 760 |
// TODO - here we should check that all the rules have been followed |
| 761 |
// and call addDefinitionError for each problem we encountered |
| 762 |
|
| 763 |
Bean<?> bean = event.getBean(); |
| 764 |
for (InjectionPoint injectionPoint : bean.getInjectionPoints()) { |
| 765 |
StringBuilder sb = new StringBuilder(); |
| 766 |
sb.append(" found injection point "); |
| 767 |
sb.append(injectionPoint.getType()); |
| 768 |
for (Annotation annotation : injectionPoint.getQualifiers()) { |
| 769 |
sb.append(" "); |
| 770 |
sb.append(annotation); |
| 771 |
} |
| 772 |
LOGGER.fine(sb.toString()); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
void afterBeanDiscovery(@Observes AfterBeanDiscovery event) { |
| 777 |
LOGGER.fine("Handling AfterBeanDiscovery event"); |
| 778 |
|
| 779 |
addPredefinedContextBeans(event); |
| 780 |
|
| 781 |
// finally define beans for all qualifiers we discovered |
| 782 |
|
| 783 |
BeanGenerator beanGenerator = new BeanGenerator("com/sun/jersey/server/impl/cdi/generated/Bean"); |
| 784 |
|
| 785 |
for (Set<DiscoveredParameter> parameters : discoveredParameterMap.values()) { |
| 786 |
for (DiscoveredParameter parameter : parameters) { |
| 787 |
Annotation annotation = parameter.getAnnotation(); |
| 788 |
Class<?> klass = getClassOfType(parameter.getType()); |
| 789 |
|
| 790 |
if (annotation.annotationType() == Context.class && |
| 791 |
staticallyDefinedContextBeans.contains(klass) && |
| 792 |
!parameter.isEncoded() && |
| 793 |
parameter.getDefaultValue() == null) { |
| 794 |
continue; |
| 795 |
} |
| 796 |
|
| 797 |
SyntheticQualifier syntheticQualifier = syntheticQualifierMap.get(parameter); |
| 798 |
Annotation theQualifier = syntheticQualifier != null ? syntheticQualifier : annotation; |
| 799 |
|
| 800 |
Set<Annotation> annotations = new HashSet<Annotation>(); |
| 801 |
annotations.add(theQualifier); |
| 802 |
|
| 803 |
// TODO - here we pass a single annotation as the second argument, |
| 804 |
// i.e. the qualifier itself, but to be true to Jersey semantics we |
| 805 |
// should pass in all the annotations that were on the original program |
| 806 |
// element. |
| 807 |
// The problem here is that (1) we don't have the original program |
| 808 |
// element any more and (2) a single DiscoveredParameter may have |
| 809 |
// been encountered in multiple places with different annotations |
| 810 |
|
| 811 |
Parameter jerseyParameter = new Parameter( |
| 812 |
new Annotation[]{ annotation }, |
| 813 |
annotation, |
| 814 |
paramQualifiersMap.get(annotation.annotationType()), |
| 815 |
parameter.getValue(), |
| 816 |
parameter.getType(), |
| 817 |
klass, |
| 818 |
parameter.isEncoded(), |
| 819 |
(parameter.getDefaultValue() == null ? null : parameter.getDefaultValue().value())); |
| 820 |
Class<?> beanClass = beanGenerator.createBeanClass(); |
| 821 |
ParameterBean bean = new ParameterBean(beanClass, parameter.getType(), annotations, parameter, jerseyParameter); |
| 822 |
toBeInitializedLater.add(bean); |
| 823 |
event.addBean(bean); |
| 824 |
LOGGER.fine("Added bean for parameter " + parameter + " and qualifier " + theQualifier); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
} |
| 829 |
|
| 830 |
/* |
| 831 |
* Adds a CDI bean for each @Context type we support out of the box |
| 832 |
*/ |
| 833 |
private void addPredefinedContextBeans(AfterBeanDiscovery event) { |
| 834 |
// all standard types first |
| 835 |
|
| 836 |
// @Context Application |
| 837 |
event.addBean(new PredefinedBean<Application>(Application.class, contextAnnotationLiteral)); |
| 838 |
|
| 839 |
// @Context HttpHeaders |
| 840 |
event.addBean(new PredefinedBean<HttpHeaders>(HttpHeaders.class, contextAnnotationLiteral)); |
| 841 |
|
| 842 |
// @Context Providers |
| 843 |
event.addBean(new PredefinedBean<Providers>(Providers.class, contextAnnotationLiteral)); |
| 844 |
|
| 845 |
// @Context Request |
| 846 |
event.addBean(new PredefinedBean<Request>(Request.class, contextAnnotationLiteral)); |
| 847 |
|
| 848 |
// @Context SecurityContext |
| 849 |
event.addBean(new PredefinedBean<SecurityContext>(SecurityContext.class, contextAnnotationLiteral)); |
| 850 |
|
| 851 |
// @Context UriInfo |
| 852 |
event.addBean(new PredefinedBean<UriInfo>(UriInfo.class, contextAnnotationLiteral)); |
| 853 |
|
| 854 |
// now the Jersey extensions |
| 855 |
|
| 856 |
// @Context ExceptionMapperContext |
| 857 |
event.addBean(new PredefinedBean<ExceptionMapperContext>(ExceptionMapperContext.class, contextAnnotationLiteral)); |
| 858 |
|
| 859 |
// @Context ExtendedUriInfo |
| 860 |
event.addBean(new PredefinedBean<ExtendedUriInfo>(ExtendedUriInfo.class, contextAnnotationLiteral)); |
| 861 |
|
| 862 |
// @Context FeaturesAndProperties |
| 863 |
event.addBean(new PredefinedBean<FeaturesAndProperties>(FeaturesAndProperties.class, contextAnnotationLiteral)); |
| 864 |
|
| 865 |
// @Context HttpContext |
| 866 |
event.addBean(new PredefinedBean<HttpContext>(HttpContext.class, contextAnnotationLiteral)); |
| 867 |
|
| 868 |
// @Context HttpRequestContext |
| 869 |
event.addBean(new PredefinedBean<HttpRequestContext>(HttpRequestContext.class, contextAnnotationLiteral)); |
| 870 |
|
| 871 |
// @Context HttpResponseContext |
| 872 |
event.addBean(new PredefinedBean<HttpResponseContext>(HttpResponseContext.class, contextAnnotationLiteral)); |
| 873 |
|
| 874 |
// @Context MessageBodyWorkers |
| 875 |
event.addBean(new PredefinedBean<MessageBodyWorkers>(MessageBodyWorkers.class, contextAnnotationLiteral)); |
| 876 |
|
| 877 |
// @Context ResourceContext |
| 878 |
event.addBean(new PredefinedBean<ResourceContext>(ResourceContext.class, contextAnnotationLiteral)); |
| 879 |
|
| 880 |
// @Context WebApplication |
| 881 |
event.addBean(new ProviderBasedBean<WebApplication>(WebApplication.class, new Provider<WebApplication>() { |
| 882 |
public WebApplication get() { |
| 883 |
return lookupWebApplication(); |
| 884 |
} |
| 885 |
}, contextAnnotationLiteral)); |
| 886 |
|
| 887 |
} |
| 888 |
|
| 889 |
private WebApplication lookupWebApplication() { |
| 890 |
return lookupWebApplication(Thread.currentThread().getContextClassLoader()); |
| 891 |
} |
| 892 |
|
| 893 |
private WebApplication lookupWebApplication(final ClassLoader cl) { |
| 894 |
return webApplications.get(cl); |
| 895 |
} |
| 896 |
|
| 897 |
void setWebApplication(WebApplication wa) { |
| 898 |
webApplications.put(Thread.currentThread().getContextClassLoader(), wa); |
| 899 |
} |
| 900 |
|
| 901 |
WebApplication getWebApplication() { |
| 902 |
return lookupWebApplication(); |
| 903 |
} |
| 904 |
|
| 905 |
void setResourceConfig(ResourceConfig rc) { |
| 906 |
resourceConfig = rc; |
| 907 |
} |
| 908 |
|
| 909 |
ResourceConfig getResourceConfig() { |
| 910 |
return resourceConfig; |
| 911 |
} |
| 912 |
|
| 913 |
/* |
| 914 |
* Called after the WebApplication and ResourceConfig have been set, |
| 915 |
* i.e. when Jersey is in a somewhat initialized state. |
| 916 |
* |
| 917 |
* By contrast, all the CDI driven code earlier in this source file |
| 918 |
* runs before Jersey gets a chance to initialize itself. |
| 919 |
*/ |
| 920 |
void lateInitialize() { |
| 921 |
try { |
| 922 |
for (InitializedLater object : toBeInitializedLater) { |
| 923 |
object.later(); |
| 924 |
} |
| 925 |
} |
| 926 |
finally { |
| 927 |
// clear the JNDI reference as soon as possible |
| 928 |
if (!lookupExtensionInBeanManager) { |
| 929 |
try { |
| 930 |
InitialContext ic = InitialContextHelper.getInitialContext(); |
| 931 |
if (ic != null) { |
| 932 |
lookupJerseyConfigJNDIContext(ic).unbind(JNDI_CDIEXTENSION_NAME); |
| 933 |
} |
| 934 |
} catch (NamingException ex) { |
| 935 |
throw new RuntimeException(ex); |
| 936 |
} |
| 937 |
} |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
/* |
| 942 |
* Constructs an object by delegating to the ServerInjectableProviderFactory of the WebApplication |
| 943 |
*/ |
| 944 |
class PredefinedBean<T> extends AbstractBean<T> { |
| 945 |
|
| 946 |
private Annotation qualifier; |
| 947 |
|
| 948 |
public PredefinedBean(Class<T> klass, Annotation qualifier) { |
| 949 |
super(klass, qualifier); |
| 950 |
this.qualifier = qualifier; |
| 951 |
} |
| 952 |
|
| 953 |
@Override |
| 954 |
public T create(CreationalContext<T> creationalContext) { |
| 955 |
Injectable<T> injectable = lookupWebApplication().getServerInjectableProviderFactory(). |
| 956 |
getInjectable(qualifier.annotationType(), null, qualifier, getBeanClass(), ComponentScope.Singleton); |
| 957 |
if (injectable == null) { |
| 958 |
Errors.error("No injectable for " + getBeanClass().getName()); |
| 959 |
return null; |
| 960 |
} |
| 961 |
return injectable.getValue(); |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
/* |
| 966 |
* Constructs an object by delegating to the Injectable for a Jersey parameter |
| 967 |
*/ |
| 968 |
class ParameterBean<T> extends AbstractBean<T> implements InitializedLater { |
| 969 |
private final DiscoveredParameter discoveredParameter; |
| 970 |
private final Parameter parameter; |
| 971 |
private final Map<ClassLoader, Injectable<T>> injectables = new ConcurrentHashMap<ClassLoader, Injectable<T>>(); |
| 972 |
private final Map<ClassLoader, Boolean> processed = new ConcurrentHashMap<ClassLoader, Boolean>(); |
| 973 |
|
| 974 |
public ParameterBean(Class<?> klass, Type type, Set<Annotation> qualifiers, |
| 975 |
DiscoveredParameter discoveredParameter, Parameter parameter) { |
| 976 |
super(klass, type, qualifiers); |
| 977 |
this.discoveredParameter = discoveredParameter; |
| 978 |
this.parameter = parameter; |
| 979 |
} |
| 980 |
|
| 981 |
@Override |
| 982 |
public void later() { |
| 983 |
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 984 |
if (injectables.containsKey(contextClassLoader)) { |
| 985 |
return; |
| 986 |
} |
| 987 |
if (processed.containsKey(contextClassLoader)) |
| 988 |
return; |
| 989 |
processed.put(contextClassLoader, true); |
| 990 |
|
| 991 |
boolean registered = lookupWebApplication(contextClassLoader).getServerInjectableProviderFactory(). |
| 992 |
isParameterTypeRegistered(parameter); |
| 993 |
if (!registered) { |
| 994 |
Errors.error("Parameter type not registered " + discoveredParameter); |
| 995 |
} |
| 996 |
Injectable<T> injectable = lookupWebApplication(contextClassLoader).getServerInjectableProviderFactory(). |
| 997 |
getInjectable(parameter, ComponentScope.PerRequest); |
| 998 |
if (injectable == null) { |
| 999 |
Errors.error("No injectable for parameter " + discoveredParameter); |
| 1000 |
} else { |
| 1001 |
injectables.put(contextClassLoader, injectable); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
@Override |
| 1006 |
public T create(CreationalContext<T> creationalContext) { |
| 1007 |
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 1008 |
if (!injectables.containsKey(contextClassLoader)) { |
| 1009 |
later(); |
| 1010 |
if (!injectables.containsKey(contextClassLoader)) { |
| 1011 |
return null; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
final Injectable<T> injectable = injectables.get(contextClassLoader); |
| 1015 |
|
| 1016 |
try { |
| 1017 |
return injectable.getValue(); |
| 1018 |
} catch (IllegalStateException e) { |
| 1019 |
if (injectable instanceof AbstractHttpContextInjectable) { |
| 1020 |
return (T)((AbstractHttpContextInjectable)injectables).getValue(lookupWebApplication(contextClassLoader).getThreadLocalHttpContext()); |
| 1021 |
} |
| 1022 |
else { |
| 1023 |
throw e; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} |