use of javax.annotation.Resource in project cxf by apache.
the class PolicyEngineImpl method setBus.
@Resource
public final void setBus(Bus b) {
if (this.bus == b) {
// avoid bus init twice through injection
return;
}
bus = b;
addBusInterceptors();
FactoryBeanListenerManager fblm = bus.getExtension(FactoryBeanListenerManager.class);
if (fblm != null) {
for (FactoryBeanListener l : fblm.getListeners()) {
if (l instanceof PolicyAnnotationListener) {
return;
}
}
fblm.addListener(new PolicyAnnotationListener(bus));
}
}
use of javax.annotation.Resource in project Payara by payara.
the class ResourcesHandler method processAnnotation.
/**
* This entry point is used both for a single @EJB and iteratively
* from a compound @EJBs processor.
*/
protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts) throws AnnotationProcessorException {
Resources resourcesAn = (Resources) ainfo.getAnnotation();
Resource[] resourceAns = resourcesAn.value();
List<HandlerProcessingResult> results = new ArrayList<HandlerProcessingResult>();
for (Resource res : resourceAns) {
results.add(processResource(ainfo, rcContexts, res));
}
return getOverallProcessingResult(results);
}
use of javax.annotation.Resource in project Payara by payara.
the class InjectionServicesImpl method getLookupName.
/**
* Returns the JNDI name to lookup
* <p>
* This calls getJndiName with the appropriate arguments for the annotation type
* @param annotatedClass
* @param annotatedField
* @param injectionResources
* @return
*/
private String getLookupName(Class annotatedClass, AnnotatedField annotatedField, List<InjectionCapable> injectionResources) {
String lookupName = null;
if (annotatedField.isAnnotationPresent(Resource.class)) {
Resource resource = annotatedField.getAnnotation(Resource.class);
lookupName = getJndiName(resource.lookup(), resource.mappedName(), resource.name());
} else if (annotatedField.isAnnotationPresent(EJB.class)) {
EJB ejb = annotatedField.getAnnotation(EJB.class);
lookupName = getJndiName(ejb.lookup(), ejb.mappedName(), ejb.name());
} else if (annotatedField.isAnnotationPresent(WebServiceRef.class)) {
WebServiceRef webServiceRef = annotatedField.getAnnotation(WebServiceRef.class);
lookupName = getJndiName(webServiceRef.lookup(), webServiceRef.mappedName(), webServiceRef.name());
} else if (annotatedField.isAnnotationPresent(PersistenceUnit.class)) {
PersistenceUnit persistenceUnit = annotatedField.getAnnotation(PersistenceUnit.class);
lookupName = getJndiName(persistenceUnit.unitName(), null, persistenceUnit.name());
} else if (annotatedField.isAnnotationPresent(PersistenceContext.class)) {
PersistenceContext persistenceContext = annotatedField.getAnnotation(PersistenceContext.class);
lookupName = getJndiName(persistenceContext.unitName(), null, persistenceContext.name());
}
if (lookupName == null || lookupName.trim().length() == 0) {
lookupName = getComponentEnvName(annotatedClass, annotatedField.getJavaMember().getName(), injectionResources);
}
return lookupName;
}
use of javax.annotation.Resource in project tomcat70 by apache.
the class DefaultInstanceManager method populateAnnotationsCache.
/**
* Make sure that the annotations cache has been populated for the provided
* class.
*
* @param clazz clazz to populate annotations for
* @param injections map of injections for this class from xml deployment
* descriptor
* @throws IllegalAccessException if injection target is inaccessible
* @throws javax.naming.NamingException if value cannot be looked up in jndi
* @throws java.lang.reflect.InvocationTargetException
* if injection fails
*/
protected void populateAnnotationsCache(Class<?> clazz, Map<String, String> injections) throws IllegalAccessException, InvocationTargetException, NamingException {
List<AnnotationCacheEntry> annotations = null;
while (clazz != null) {
AnnotationCacheEntry[] annotationsArray = annotationCache.get(clazz);
if (annotationsArray == null) {
if (annotations == null) {
annotations = new ArrayList<AnnotationCacheEntry>();
} else {
annotations.clear();
}
if (context != null) {
// Initialize fields annotations for resource injection if
// JNDI is enabled
Field[] fields = Introspection.getDeclaredFields(clazz);
for (Field field : fields) {
Resource resourceAnnotation;
Annotation ejbAnnotation;
Annotation webServiceRefAnnotation;
Annotation persistenceContextAnnotation;
Annotation persistenceUnitAnnotation;
if (injections != null && injections.containsKey(field.getName())) {
annotations.add(new AnnotationCacheEntry(field.getName(), null, injections.get(field.getName()), AnnotationCacheEntryType.FIELD));
} else if ((resourceAnnotation = field.getAnnotation(Resource.class)) != null) {
annotations.add(new AnnotationCacheEntry(field.getName(), null, resourceAnnotation.name(), AnnotationCacheEntryType.FIELD));
} else if (EJB_PRESENT && (ejbAnnotation = field.getAnnotation(EJB.class)) != null) {
annotations.add(new AnnotationCacheEntry(field.getName(), null, ((EJB) ejbAnnotation).name(), AnnotationCacheEntryType.FIELD));
} else if (WS_PRESENT && (webServiceRefAnnotation = field.getAnnotation(WebServiceRef.class)) != null) {
annotations.add(new AnnotationCacheEntry(field.getName(), null, ((WebServiceRef) webServiceRefAnnotation).name(), AnnotationCacheEntryType.FIELD));
} else if (JPA_PRESENT && (persistenceContextAnnotation = field.getAnnotation(PersistenceContext.class)) != null) {
annotations.add(new AnnotationCacheEntry(field.getName(), null, ((PersistenceContext) persistenceContextAnnotation).name(), AnnotationCacheEntryType.FIELD));
} else if (JPA_PRESENT && (persistenceUnitAnnotation = field.getAnnotation(PersistenceUnit.class)) != null) {
annotations.add(new AnnotationCacheEntry(field.getName(), null, ((PersistenceUnit) persistenceUnitAnnotation).name(), AnnotationCacheEntryType.FIELD));
}
}
}
// Initialize methods annotations
Method[] methods = Introspection.getDeclaredMethods(clazz);
Method postConstruct = null;
String postConstructFromXml = postConstructMethods.get(clazz.getName());
Method preDestroy = null;
String preDestroyFromXml = preDestroyMethods.get(clazz.getName());
for (Method method : methods) {
if (context != null) {
// Resource injection only if JNDI is enabled
if (injections != null && Introspection.isValidSetter(method)) {
String fieldName = Introspection.getPropertyName(method);
if (injections.containsKey(fieldName)) {
annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), injections.get(fieldName), AnnotationCacheEntryType.SETTER));
continue;
}
}
Resource resourceAnnotation;
Annotation ejbAnnotation;
Annotation webServiceRefAnnotation;
Annotation persistenceContextAnnotation;
Annotation persistenceUnitAnnotation;
if ((resourceAnnotation = method.getAnnotation(Resource.class)) != null) {
annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), resourceAnnotation.name(), AnnotationCacheEntryType.SETTER));
} else if (EJB_PRESENT && (ejbAnnotation = method.getAnnotation(EJB.class)) != null) {
annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((EJB) ejbAnnotation).name(), AnnotationCacheEntryType.SETTER));
} else if (WS_PRESENT && (webServiceRefAnnotation = method.getAnnotation(WebServiceRef.class)) != null) {
annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((WebServiceRef) webServiceRefAnnotation).name(), AnnotationCacheEntryType.SETTER));
} else if (JPA_PRESENT && (persistenceContextAnnotation = method.getAnnotation(PersistenceContext.class)) != null) {
annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((PersistenceContext) persistenceContextAnnotation).name(), AnnotationCacheEntryType.SETTER));
} else if (JPA_PRESENT && (persistenceUnitAnnotation = method.getAnnotation(PersistenceUnit.class)) != null) {
annotations.add(new AnnotationCacheEntry(method.getName(), method.getParameterTypes(), ((PersistenceUnit) persistenceUnitAnnotation).name(), AnnotationCacheEntryType.SETTER));
}
}
postConstruct = findPostConstruct(postConstruct, postConstructFromXml, method);
preDestroy = findPreDestroy(preDestroy, preDestroyFromXml, method);
}
if (postConstruct != null) {
annotations.add(new AnnotationCacheEntry(postConstruct.getName(), postConstruct.getParameterTypes(), null, AnnotationCacheEntryType.POST_CONSTRUCT));
} else if (postConstructFromXml != null) {
throw new IllegalArgumentException("Post construct method " + postConstructFromXml + " for class " + clazz.getName() + " is declared in deployment descriptor but cannot be found.");
}
if (preDestroy != null) {
annotations.add(new AnnotationCacheEntry(preDestroy.getName(), preDestroy.getParameterTypes(), null, AnnotationCacheEntryType.PRE_DESTROY));
} else if (preDestroyFromXml != null) {
throw new IllegalArgumentException("Pre destroy method " + preDestroyFromXml + " for class " + clazz.getName() + " is declared in deployment descriptor but cannot be found.");
}
if (annotations.isEmpty()) {
// Use common object to save memory
annotationsArray = ANNOTATIONS_EMPTY;
} else {
annotationsArray = annotations.toArray(new AnnotationCacheEntry[annotations.size()]);
}
synchronized (annotationCache) {
annotationCache.put(clazz, annotationsArray);
}
}
clazz = clazz.getSuperclass();
}
}
use of javax.annotation.Resource in project tomcat70 by apache.
the class WebAnnotationSet method loadMethodsAnnotation.
protected static void loadMethodsAnnotation(Context context, Class<?> clazz) {
// Initialize the annotations
Method[] methods = Introspection.getDeclaredMethods(clazz);
if (methods != null && methods.length > 0) {
for (Method method : methods) {
Resource annotation = method.getAnnotation(Resource.class);
if (annotation != null) {
if (!Introspection.isValidSetter(method)) {
throw new IllegalArgumentException(sm.getString("webAnnotationSet.invalidInjection"));
}
String defaultName = clazz.getName() + SEPARATOR + Introspection.getPropertyName(method);
Class<?> defaultType = (method.getParameterTypes()[0]);
addResource(context, annotation, defaultName, defaultType);
}
}
}
}
Aggregations