Java Code Examples for javax.annotation.Resource

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project arquillian-core, under directory /testenrichers/resource/src/main/java/org/jboss/arquillian/testenricher/resource/.

Source file: ResourceInjectionEnricher.java

  22 
vote

protected String getResourceName(Field field){
  Resource resource=field.getAnnotation(Resource.class);
  String resourceName=getResourceName(resource);
  if (resourceName != null) {
    return resourceName;
  }
  String propertyName=field.getName();
  String className=field.getDeclaringClass().getName();
  return RESOURCE_LOOKUP_PREFIX + "/" + className+ "/"+ propertyName;
}
 

Example 2

From project arquillian_deprecated, under directory /testenrichers/resource/src/main/java/org/jboss/arquillian/testenricher/resource/.

Source file: ResourceInjectionEnricher.java

  22 
vote

protected String getResourceName(Field field){
  Resource resource=field.getAnnotation(Resource.class);
  String resourceName=getResourceName(resource);
  if (resourceName != null) {
    return resourceName;
  }
  String propertyName=field.getName();
  String className=field.getDeclaringClass().getName();
  return RESOURCE_LOOKUP_PREFIX + "/" + className+ "/"+ propertyName;
}
 

Example 3

From project api, under directory /weld-spi/src/main/java/org/jboss/weld/injection/spi/helpers/.

Source file: AbstractResourceServices.java

  21 
vote

protected String getResourceName(InjectionPoint injectionPoint){
  Resource resource=injectionPoint.getAnnotated().getAnnotation(Resource.class);
  String mappedName=resource.mappedName();
  if (!mappedName.equals("")) {
    return mappedName;
  }
  String name=resource.name();
  if (!name.equals("")) {
    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 new IllegalArgumentException("Injection point represents a method which doesn't follow " + "JavaBean conventions (unable to determine property name) " + injectionPoint);
    }
  }
 else {
    throw new AssertionError("Unable to inject into " + injectionPoint);
  }
  String className=injectionPoint.getMember().getDeclaringClass().getName();
  return RESOURCE_LOOKUP_PREFIX + "/" + className+ "/"+ propertyName;
}
 

Example 4

From project cipango, under directory /cipango-annotations/src/main/java/org/cipango/annotations/.

Source file: ResourceAnnotationHandler.java

  21 
vote

@Override @SuppressWarnings("rawtypes") public void handleField(Class clazz,Field field){
  Resource resource=(Resource)field.getAnnotation(Resource.class);
  if (resource != null) {
    String jndiName=getSipResourceJndiName(field);
    if (jndiName != null) {
      if (Modifier.isStatic(field.getModifiers())) {
        LOG.warn("Skipping Resource annotation on " + clazz.getName() + "."+ field.getName()+ ": cannot be static");
        return;
      }
      if (Modifier.isFinal(field.getModifiers())) {
        LOG.warn("Skipping Resource annotation on " + clazz.getName() + "."+ field.getName()+ ": cannot be final");
        return;
      }
      InjectionCollection injections=(InjectionCollection)_context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
      if (injections == null) {
        injections=new InjectionCollection();
        _context.setAttribute(InjectionCollection.INJECTION_COLLECTION,injections);
      }
      Injection injection=new Injection();
      injection.setTarget(clazz,field,field.getType());
      injection.setJndiName(jndiName);
      injections.add(injection);
    }
  }
 else   super.handleField(clazz,field);
}
 

Example 5

From project james, under directory /protocols-library/src/test/java/org/apache/james/protocols/lib/mock/.

Source file: MockProtocolHandlerLoader.java

  21 
vote

private void injectResources(Object resource){
  final Method[] methods=resource.getClass().getMethods();
  for (  Method method : methods) {
    final Resource resourceAnnotation=method.getAnnotation(Resource.class);
    if (resourceAnnotation != null) {
      final String name=resourceAnnotation.name();
      if (name == null) {
        throw new UnsupportedOperationException("Resource annotation without name specified is not supported by this implementation");
      }
 else {
        final Object service=getObjectForName(name);
        if (service == null) {
          throw new RuntimeException("Injection failed for object " + resource + " on method "+ method+ " with resource name "+ name+ ", because no mapping was found");
        }
 else {
          try {
            Object[] args={service};
            method.invoke(resource,args);
          }
 catch (          IllegalAccessException e) {
            throw new RuntimeException("Injection failed for object " + resource + " on method "+ method+ " with resource "+ service,e);
          }
catch (          IllegalArgumentException e) {
            throw new RuntimeException("Injection failed for object " + resource + " on method "+ method+ " with resource "+ service,e);
          }
catch (          InvocationTargetException e) {
            throw new RuntimeException("Injection failed for object " + resource + " on method "+ method+ " with resource "+ service,e);
          }
        }
      }
    }
  }
}
 

Example 6

From project openwebbeans, under directory /webbeans-resource/src/main/java/org/apache/webbeans/resource/spi/se/.

Source file: StandaloneResourceInjectionService.java

  21 
vote

public <X,T extends Annotation>X getResourceReference(ResourceReference<X,T> resourceReference){
  if (resourceReference.supports(Resource.class)) {
    Resource resource=resourceReference.getAnnotation(Resource.class);
    return processor.getResource(resource,resourceReference.getResourceType());
  }
  if (resourceReference.supports(WebServiceRef.class)) {
    WebServiceRef resource=resourceReference.getAnnotation(WebServiceRef.class);
    return processor.getWebServiceResource(resource,resourceReference.getResourceType());
  }
  if (resourceReference.supports(PersistenceContext.class)) {
    PersistenceContext persistenceContext=resourceReference.getAnnotation(PersistenceContext.class);
    return processor.getEntityManager(persistenceContext,resourceReference.getResourceType());
  }
  if (resourceReference.supports(PersistenceUnit.class)) {
    PersistenceUnit persistenceUnit=resourceReference.getAnnotation(PersistenceUnit.class);
    return processor.getEntityManagerFactory(persistenceUnit,resourceReference.getResourceType());
  }
  return null;
}
 

Example 7

From project spring-js, under directory /src/main/java/org/springframework/context/annotation/.

Source file: CommonAnnotationBeanPostProcessor.java

  21 
vote

@Override protected void initAnnotation(AnnotatedElement ae){
  Resource resource=ae.getAnnotation(Resource.class);
  String resourceName=resource.name();
  Class resourceType=resource.type();
  this.isDefaultName=!StringUtils.hasLength(resourceName);
  if (this.isDefaultName) {
    resourceName=this.member.getName();
    if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
      resourceName=Introspector.decapitalize(resourceName.substring(3));
    }
  }
 else   if (beanFactory instanceof ConfigurableBeanFactory) {
    resourceName=((ConfigurableBeanFactory)beanFactory).resolveEmbeddedValue(resourceName);
  }
  if (resourceType != null && !Object.class.equals(resourceType)) {
    checkResourceType(resourceType);
  }
 else {
    resourceType=getResourceType();
  }
  this.name=resourceName;
  this.lookupType=resourceType;
  this.mappedName=resource.mappedName();
  this.shareable=resource.shareable();
}
 

Example 8

From project virgo.web, under directory /org.eclipse.virgo.web.enterprise.jsf.support/src/main/java/org/eclipse/virgo/web/enterprise/jsf/support/.

Source file: VirgoJsfInjectionProvider.java

  21 
vote

private void processMethods(Object managedBean,Class currentBeanClass) throws NamingException, IllegalAccessException, InvocationTargetException {
  Method[] methods=currentBeanClass.getDeclaredMethods();
  for (  Method method : methods) {
    if (method.isAnnotationPresent(Resource.class)) {
      Resource annotation=(Resource)method.getAnnotation(Resource.class);
      String lookupName=COMP_ENV + annotation.name();
      if (annotation.lookup() != null && !annotation.lookup().equals("")) {
        lookupName=annotation.lookup();
      }
      lookupMethodResource(namingContext,managedBean,currentBeanClass.getCanonicalName(),method,lookupName);
    }
    if (method.isAnnotationPresent(EJB.class)) {
      EJB annotation=(EJB)method.getAnnotation(EJB.class);
      String lookupName=COMP_ENV + annotation.name();
      if (annotation.lookup() != null && !annotation.lookup().equals("")) {
        lookupName=annotation.lookup();
      }
      lookupMethodResource(namingContext,managedBean,currentBeanClass.getCanonicalName(),method,lookupName);
    }
    if (method.isAnnotationPresent(PersistenceContext.class)) {
      PersistenceContext annotation=(PersistenceContext)method.getAnnotation(PersistenceContext.class);
      lookupMethodResource(namingContext,managedBean,currentBeanClass.getCanonicalName(),method,annotation.name());
    }
    if (method.isAnnotationPresent(PersistenceUnit.class)) {
      PersistenceUnit annotation=(PersistenceUnit)method.getAnnotation(PersistenceUnit.class);
      lookupMethodResource(namingContext,managedBean,currentBeanClass.getCanonicalName(),method,annotation.name());
    }
  }
}
 

Example 9

From project arquillian-rusheye, under directory /rusheye-impl/src/main/java/org/jboss/rusheye/parser/.

Source file: RetriverInjector.java

  19 
vote

@Override public void afterUnmarshal(Object target,Object parent){
  final Class<?> targetClass=target.getClass();
  List<Field> fields;
  if (target instanceof Test) {
    Test test=(Test)target;
    afterUnmarshal(test.getSample(),test);
  }
  if (!fieldMap.containsKey(target.getClass())) {
    fields=new LinkedList<Field>();
    fieldMap.put(targetClass,fields);
    for (    Field field : targetClass.getDeclaredFields()) {
      if (field.isAnnotationPresent(Resource.class)) {
        fields.add(field);
      }
    }
  }
 else {
    fields=fieldMap.get(targetClass);
  }
  for (  Field field : fields) {
    boolean accessible=field.isAccessible();
    if (!accessible) {
      field.setAccessible(true);
    }
    Retriever retriever=getRetriever(field.getType());
    try {
      field.set(target,retriever);
    }
 catch (    IllegalAccessException e) {
      throw new IllegalStateException(e);
    }
    if (!accessible) {
      field.setAccessible(accessible);
    }
  }
}
 

Example 10

From project Blitz, under directory /src/com/laxser/blitz/web/impl/module/.

Source file: ModulesBuilderImpl.java

  19 
vote

private void registerBeanDefinitions(XmlWebApplicationContext context,List<Class<?>> classes){
  DefaultListableBeanFactory bf=(DefaultListableBeanFactory)context.getBeanFactory();
  String[] definedClasses=new String[bf.getBeanDefinitionCount()];
  String[] definitionNames=bf.getBeanDefinitionNames();
  for (int i=0; i < definedClasses.length; i++) {
    String name=definitionNames[i];
    definedClasses[i]=bf.getBeanDefinition(name).getBeanClassName();
  }
  for (  Class<?> clazz : classes) {
    if (!isCandidate(clazz)) {
      continue;
    }
    String clazzName=clazz.getName();
    if (ArrayUtils.contains(definedClasses,clazzName)) {
      if (logger.isDebugEnabled()) {
        logger.debug("Ignores bean definition because it has been exist in context: " + clazz.getName());
      }
      continue;
    }
    String beanName=null;
    if (StringUtils.isEmpty(beanName) && clazz.isAnnotationPresent(Component.class)) {
      beanName=clazz.getAnnotation(Component.class).value();
    }
    if (StringUtils.isEmpty(beanName) && clazz.isAnnotationPresent(Resource.class)) {
      beanName=clazz.getAnnotation(Resource.class).name();
    }
    if (StringUtils.isEmpty(beanName) && clazz.isAnnotationPresent(Service.class)) {
      beanName=clazz.getAnnotation(Service.class).value();
    }
    if (StringUtils.isEmpty(beanName)) {
      beanName=AUTO_BEAN_NAME_PREFIX + clazz.getName();
    }
    bf.registerBeanDefinition(beanName,new AnnotatedGenericBeanDefinition(clazz));
  }
}
 

Example 11

From project CalendarPortlet, under directory /src/main/java/org/jasig/portlet/calendar/mvc/.

Source file: ViewSelectorDefaultImpl.java

  19 
vote

/** 
 * Set a list of regex patterns for user agents which should be considered to be mobile devices.
 * @param patterns
 */
@Resource(name="mobileDeviceRegexes") public void setMobileDeviceRegexes(List<String> patterns){
  this.mobileDeviceRegexes=new ArrayList<Pattern>();
  for (  String pattern : patterns) {
    this.mobileDeviceRegexes.add(Pattern.compile(pattern));
  }
}
 

Example 12

From project dci-examples, under directory /frontloading/java/ant-kutschera/lib/maxant-dci-tools-1.1.0/ch/maxant/dci/util/.

Source file: BehaviourInjector.java

  19 
vote

/** 
 * @see #injectResources(Object)
 */
@SuppressWarnings("unchecked") private void injectResources(Object roleInstance,Class clazz){
  Field[] declaredFields=clazz.getDeclaredFields();
  Field[] otherFields=clazz.getFields();
  Field[] allFields=new Field[declaredFields.length + otherFields.length];
  System.arraycopy(declaredFields,0,allFields,0,declaredFields.length);
  System.arraycopy(otherFields,0,allFields,declaredFields.length,otherFields.length);
  for (  Field field : allFields) {
    Annotation a=field.getAnnotation(Resource.class);
    if (a != null) {
      String name=field.getName();
      Object resource=resources.get(name);
      if (resource == null) {
        String aName=((Resource)a).name();
        if (aName != null) {
          name=aName;
          resource=resources.get(name);
        }
      }
      if (resource != null) {
        field.setAccessible(true);
        try {
          field.set(roleInstance,resource);
        }
 catch (        Exception e) {
          throw new RuntimeException("unable to set resource " + name + " in class "+ roleInstance.getClass().getName(),e);
        }
      }
    }
  }
  if (clazz.getSuperclass() != null) {
    injectResources(roleInstance,clazz.getSuperclass());
  }
}
 

Example 13

From project email-preview, under directory /src/main/java/org/jasig/portlet/emailpreview/service/auth/.

Source file: AuthenticationServiceRegistryImpl.java

  19 
vote

@Resource(name="authServices") @Required public void setServices(Collection<IAuthenticationService> services){
  serviceMap.clear();
  for (  IAuthenticationService service : services) {
    registerService(service);
  }
}
 

Example 14

From project jboss-ejb3-tx2, under directory /antediluvian/src/test/java/org/jboss/ejb3/test/tx/bmt/.

Source file: StatefulBMTBean.java

  19 
vote

/** 
 * Note that the call to this method is hard-coded!
 * @param ctx
 */
@Resource public void setSessionContext(SessionContext ctx){
  this.ctx=ctx;
  try {
    ctx.getUserTransaction();
    isGetUserTransactionAllowed=true;
  }
 catch (  IllegalStateException e) {
    isGetUserTransactionAllowed=false;
  }
}
 

Example 15

From project OpenTripPlanner, under directory /opentripplanner-analyst/src/main/java/org/opentripplanner/analyst/batch/.

Source file: BatchProcessor.java

  19 
vote

public static void main(String[] args) throws IOException {
  org.springframework.core.io.Resource appContextResource;
  if (args.length == 0) {
    LOG.warn("no configuration XML file specified; using example on classpath");
    appContextResource=new ClassPathResource(EXAMPLE_CONTEXT);
  }
 else {
    String configFile=args[0];
    appContextResource=new FileSystemResource(configFile);
  }
  GenericApplicationContext ctx=new GenericApplicationContext();
  XmlBeanDefinitionReader xmlReader=new XmlBeanDefinitionReader(ctx);
  xmlReader.loadBeanDefinitions(appContextResource);
  ctx.refresh();
  ctx.registerShutdownHook();
  BatchProcessor processor=ctx.getBean(BatchProcessor.class);
  if (processor == null)   LOG.error("No BatchProcessor bean was defined.");
 else   processor.run();
}
 

Example 16

From project trugger, under directory /src/test/java/org/atatec/trugger/test/mock/.

Source file: AnnotationMockTest.java

  19 
vote

@Before public void initialize(){
  resource=new AnnotationMock<Resource>(){
{
      map("name").to(annotation.name());
      map(false).to(annotation.shareable());
    }
  }
.createMock();
  AnnotationMock<Resource> builder=annotation(Resource.class);
  resource2=builder.annotation();
  builder.map("name2").to(resource2.name());
  builder.map(true).to(resource2.shareable());
  builder.map(AuthenticationType.APPLICATION).to(resource2.authenticationType());
  builder.createMock();
}