use of javax.annotation.Resource in project jetty.project by eclipse.
the class ResourceAnnotationHandler method handleField.
public void handleField(Class<?> clazz, Field field) {
Resource resource = (Resource) field.getAnnotation(Resource.class);
if (resource != null) {
//JavaEE Spec 5.2.3: Field cannot be static
if (Modifier.isStatic(field.getModifiers())) {
LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + field.getName() + ": cannot be static");
return;
}
//JavaEE Spec 5.2.3: Field cannot be final
if (Modifier.isFinal(field.getModifiers())) {
LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + field.getName() + ": cannot be final");
return;
}
//work out default name
String name = clazz.getCanonicalName() + "/" + field.getName();
//allow @Resource name= to override the field name
name = (resource.name() != null && !resource.name().trim().equals("") ? resource.name() : name);
String mappedName = (resource.mappedName() != null && !resource.mappedName().trim().equals("") ? resource.mappedName() : null);
//get the type of the Field
Class<?> type = field.getType();
//Servlet Spec 3.0 p. 76
//If a descriptor has specified at least 1 injection target for this
//resource, then it overrides this annotation
MetaData metaData = _context.getMetaData();
if (metaData.getOriginDescriptor("resource-ref." + name + ".injection") != null) {
//it overrides this annotation
return;
}
//No injections for this resource in any descriptors, so we can add it
//Does the injection already exist?
InjectionCollection injections = (InjectionCollection) _context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
if (injections == null) {
injections = new InjectionCollection();
_context.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
}
Injection injection = injections.getInjection(name, clazz, field);
if (injection == null) {
//No injection has been specified, add it
try {
boolean bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context, name, mappedName);
if (!bound)
bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName);
if (!bound)
bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(null, name, mappedName);
if (!bound) {
//see if there is an env-entry value been bound
try {
InitialContext ic = new InitialContext();
String nameInEnvironment = (mappedName != null ? mappedName : name);
ic.lookup("java:comp/env/" + nameInEnvironment);
bound = true;
} catch (NameNotFoundException e) {
bound = false;
}
}
//Check there is a JNDI entry for this annotation
if (bound) {
LOG.debug("Bound " + (mappedName == null ? name : mappedName) + " as " + name);
// Make the Injection for it if the binding succeeded
injection = new Injection();
injection.setTarget(clazz, field, type);
injection.setJndiName(name);
injection.setMappingName(mappedName);
injections.add(injection);
//TODO - an @Resource is equivalent to a resource-ref, resource-env-ref, message-destination
metaData.setOrigin("resource-ref." + name + ".injection", resource, clazz);
} else if (!isEnvEntryType(type)) {
throw new IllegalStateException("No resource at " + (mappedName == null ? name : mappedName));
}
} catch (NamingException e) {
// JavaEE Spec. sec 5.4.1.3
if (!isEnvEntryType(type))
throw new IllegalStateException(e);
}
}
}
}
use of javax.annotation.Resource in project jetty.project by eclipse.
the class ResourcesAnnotationHandler method doHandle.
public void doHandle(Class<?> clazz) {
Resources resources = (Resources) clazz.getAnnotation(Resources.class);
if (resources != null) {
Resource[] resArray = resources.value();
if (resArray == null || resArray.length == 0) {
LOG.warn("Skipping empty or incorrect Resources annotation on " + clazz.getName());
return;
}
for (int j = 0; j < resArray.length; j++) {
String name = resArray[j].name();
String mappedName = resArray[j].mappedName();
if (name == null || name.trim().equals(""))
throw new IllegalStateException("Class level Resource annotations must contain a name (Common Annotations Spec Section 2.3)");
try {
if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac, name, mappedName))
if (!org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_wac.getServer(), name, mappedName))
LOG.warn("Skipping Resources(Resource) annotation on " + clazz.getName() + " for name " + name + ": No resource bound at " + (mappedName == null ? name : mappedName));
} catch (NamingException e) {
LOG.warn(e);
}
}
}
}
use of javax.annotation.Resource in project wildfly by wildfly.
the class ResourceInjectionUtilities method getResourceName.
public static String getResourceName(InjectionPoint injectionPoint) {
Resource resource = getResourceAnnotated(injectionPoint).getAnnotation(Resource.class);
String mappedName = resource.mappedName();
if (!mappedName.equals("")) {
return mappedName;
}
String name = resource.name();
if (!name.equals("")) {
//see if this is a prefixed name
//and if so just return it
int firstSlash = name.indexOf("/");
int colon = name.indexOf(":");
if (colon != -1) {
if (firstSlash == -1 || colon < firstSlash) {
return name;
}
}
return RESOURCE_LOOKUP_PREFIX + "/" + name;
}
String propertyName;
if (injectionPoint.getMember() instanceof Field) {
propertyName = injectionPoint.getMember().getName();
} else if (injectionPoint.getMember() instanceof Method) {
propertyName = getPropertyName((Method) injectionPoint.getMember());
if (propertyName == null) {
throw WeldLogger.ROOT_LOGGER.injectionPointNotAJavabean((Method) injectionPoint.getMember());
}
} else {
throw WeldLogger.ROOT_LOGGER.cannotInject(injectionPoint);
}
String className = injectionPoint.getMember().getDeclaringClass().getName();
return RESOURCE_LOOKUP_PREFIX + "/" + className + "/" + propertyName;
}
use of javax.annotation.Resource in project redkale by redkale.
the class ResourceFactory method inject.
private <T> boolean inject(final Object src, final T attachment, final BiConsumer<Object, Field> consumer, final List<Object> list) {
if (src == null)
return false;
try {
list.add(src);
Class clazz = src.getClass();
do {
for (Field field : clazz.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()))
continue;
field.setAccessible(true);
final Class classtype = field.getType();
final Type genctype = field.getGenericType();
Resource rc = field.getAnnotation(Resource.class);
if (rc == null) {
// 深度注入
// 是否没有重复
boolean flag = true;
Object ns = field.get(src);
for (Object o : list) {
if (o == ns) {
flag = false;
break;
}
}
if (ns == null)
continue;
if (ns.getClass().isPrimitive() || ns.getClass().isArray() || ns.getClass().getName().startsWith("java.") || ns.getClass().getName().startsWith("javax.") || ns.getClass().getName().startsWith("jdk.") || ns.getClass().getName().startsWith("sun."))
continue;
if (flag)
this.inject(ns, attachment, consumer, list);
continue;
}
if (Modifier.isFinal(field.getModifiers()))
continue;
if (consumer != null)
consumer.accept(src, field);
String tname = rc.name();
if (tname.contains(RESOURCE_PARENT_NAME)) {
Resource res = src.getClass().getAnnotation(Resource.class);
if (res == null) {
if (src instanceof Resourcable) {
tname = tname.replace(RESOURCE_PARENT_NAME, ((Resourcable) src).resourceName());
} else {
logger.log(Level.SEVERE, src.getClass().getName() + " not found @Resource on Class or not implements Resourcable");
}
} else {
tname = tname.replace(RESOURCE_PARENT_NAME, res.name());
}
}
boolean autoregnull = true;
final String rcname = formatResourceName(tname);
Object rs;
if (rcname.startsWith("system.property.")) {
rs = System.getProperty(rcname.substring("system.property.".length()));
} else {
ResourceEntry re = findEntry(rcname, genctype);
if (re == null) {
if (rcname.startsWith("property.")) {
re = findEntry(rcname, String.class);
} else {
re = findEntry(rcname, classtype);
}
}
if (re == null) {
ResourceLoader it = findLoader(genctype, field);
if (it != null) {
it.load(this, src, rcname, field, attachment);
autoregnull = it.autoNone();
re = findEntry(rcname, genctype);
}
}
if (re == null && genctype != classtype) {
re = findEntry(rcname, classtype);
if (re == null) {
if (rcname.startsWith("property.")) {
re = findEntry(rcname, String.class);
} else {
re = findEntry(rcname, classtype);
}
}
if (re == null) {
ResourceLoader it = findLoader(classtype, field);
if (it != null) {
it.load(this, src, rcname, field, attachment);
autoregnull = it.autoNone();
re = findEntry(rcname, classtype);
}
}
}
if (re == null && autoregnull) {
// 自动注入null的值
register(rcname, genctype, null);
re = findEntry(rcname, genctype);
}
if (re == null)
continue;
re.elements.add(new ResourceElement<>(src, field));
rs = re.value;
}
if (rs != null && !rs.getClass().isPrimitive() && classtype.isPrimitive()) {
if (classtype == int.class) {
rs = Integer.decode(rs.toString());
} else if (classtype == long.class) {
rs = Long.decode(rs.toString());
} else if (classtype == short.class) {
rs = Short.decode(rs.toString());
} else if (classtype == boolean.class) {
rs = "true".equalsIgnoreCase(rs.toString());
} else if (classtype == byte.class) {
rs = Byte.decode(rs.toString());
} else if (classtype == float.class) {
rs = Float.parseFloat(rs.toString());
} else if (classtype == double.class) {
rs = Double.parseDouble(rs.toString());
}
}
if (rs != null)
field.set(src, rs);
}
} while ((clazz = clazz.getSuperclass()) != Object.class);
return true;
} catch (Exception ex) {
logger.log(Level.SEVERE, "inject " + src + " error", ex);
return false;
}
}
use of javax.annotation.Resource in project cxf by apache.
the class WSDLManagerImpl method setBus.
@Resource
public final void setBus(Bus b) {
bus = b;
if (null != bus) {
bus.setExtension(this, WSDLManager.class);
ConfiguredBeanLocator loc = bus.getExtension(ConfiguredBeanLocator.class);
if (loc != null) {
loc.getBeansOfType(WSDLExtensionLoader.class);
}
}
}
Aggregations