private void introspectInjectMethod(
      AnnotatedType<X> type, ArrayList<ConfigProgram> injectProgramList) {

    for (AnnotatedMethod method : type.getMethods()) {
      if (method.getAnnotations().size() == 0) continue;

      if (method.isAnnotationPresent(Inject.class)) {
        // boolean isOptional = isQualifierOptional(field);

        List<AnnotatedParameter<?>> params = method.getParameters();

        InjectionPoint[] args = new InjectionPoint[params.size()];

        for (int i = 0; i < args.length; i++) {
          InjectionPoint ij = new InjectionPointImpl(getBeanManager(), this, params.get(i));

          _injectionPointSet.add(ij);

          args[i] = ij;
        }

        injectProgramList.add(new MethodInjectProgram(method.getJavaMember(), args));
      } else {
        InjectionPointHandler handler = getBeanManager().getInjectionPointHandler(method);

        if (handler != null) {
          ConfigProgram program = new MethodHandlerProgram(method, handler);

          injectProgramList.add(program);
        }
      }
    }
  }
  private void introspectDestroy(ArrayList<ConfigProgram> destroyList, AnnotatedType<?> type)
      throws ConfigException {
    if (type == null || type.equals(Object.class)) return;

    if (type.isAnnotationPresent(Interceptor.class)) {
      return;
    }

    for (AnnotatedMethod<?> method : type.getMethods()) {
      if (method.isAnnotationPresent(PreDestroy.class)) {
        Method javaMethod = method.getJavaMember();

        Class<?>[] types = javaMethod.getParameterTypes();

        if (types.length == 0) {
        } else if (types.length == 1 && types[0].equals(InvocationContext.class)) {
          // XXX:
          continue;
        } else
          throw new ConfigException(
              location(javaMethod) + L.l("@PreDestroy is requires zero arguments"));

        PreDestroyInject destroyProgram = new PreDestroyInject(javaMethod);

        if (!destroyList.contains(destroyProgram)) destroyList.add(destroyProgram);
      }
    }
  }
Beispiel #3
0
 private RequestMappingInfo findMethodRequestMapping(FacesContext context, Bean<?> bean) {
   RequestMappingInfo result = null;
   Class clazz = bean.getBeanClass();
   AnnotatedType annotatedType = beanManager.createAnnotatedType(clazz);
   Set<AnnotatedMethod> annotatedMethodSet = annotatedType.getMethods();
   for (AnnotatedMethod method : annotatedMethodSet) {
     if (method.isAnnotationPresent(RequestMapping.class)) {
       RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
       String[] mappings = requestMapping.value();
       String mapping = null;
       for (String current : mappings) {
         String pathInfo = context.getExternalContext().getRequestPathInfo();
         if (pathInfo.equals(current)) {
           result = new RequestMappingInfo();
           result.setBean(bean);
           result.setMethod(method.getJavaMember());
           result.setRequestMapping(mapping);
           result.setMappingType(RequestMappingInfo.MappingType.EXACT);
           break;
         } else if (current.endsWith("*")) {
           current = current.substring(0, current.length() - 1);
           if (pathInfo.startsWith(current)) {
             if (result == null) {
               result = new RequestMappingInfo();
               result.setBean(bean);
               result.setMethod(method.getJavaMember());
               result.setRequestMapping(current);
               result.setMappingType(RequestMappingInfo.MappingType.PREFIX);
             } else if (current.length() > result.getLength()) {
               result.setBean(bean);
               result.setMethod(method.getJavaMember());
               result.setRequestMapping(current);
               result.setMappingType(RequestMappingInfo.MappingType.PREFIX);
             }
           }
         } else if (current.startsWith("*")) {
           current = current.substring(1);
           if (pathInfo.endsWith(current)) {
             result = new RequestMappingInfo();
             result.setBean(bean);
             result.setMethod(method.getJavaMember());
             result.setRequestMapping(current);
             result.setMappingType(RequestMappingInfo.MappingType.EXTENSION);
             break;
           }
         }
       }
     }
     if (result != null
         && (result.getMappingType().equals(RequestMappingInfo.MappingType.EXACT)
             || (result.getMappingType().equals(RequestMappingInfo.MappingType.EXTENSION)))) {
       break;
     }
   }
   return result;
 }
 <X> void registerGenericBean(@Observes ProcessManagedBean<X> event) {
   AnnotatedType<X> type = event.getAnnotatedBeanClass();
   if (type.isAnnotationPresent(GenericConfiguration.class)) {
     Class<? extends Annotation> genericType =
         type.getAnnotation(GenericConfiguration.class).value();
     genericBeans.put(
         genericType, new BeanHolder<X>(event.getAnnotatedBeanClass(), event.getBean()));
     for (AnnotatedMethod<? super X> m : event.getAnnotatedBeanClass().getMethods()) {
       if (m.isAnnotationPresent(Unwraps.class)) {
         unwrapsMethods.put(genericType, m);
       }
     }
   }
 }
    @Override
    public void run() {
      if (System.currentTimeMillis() > expiryTime) {
        scheduledExecutorService.shutdown();
        throw new RuntimeException(
            "failed to discover beans: " + managedTypes.getServiceEndpoints());
      }

      if (registered.isEmpty()) {
        scheduledExecutorService.shutdown();
        log.info("all services registered successfully");
        return;
      }

      for (final AnnotatedType<?> type : managedTypes.getServiceEndpoints()) {
        if (!registered.contains(type) || beanManager.getBeans(type.getJavaClass()).size() == 0) {
          continue;
        }

        final MessageCallback callback =
            (MessageCallback) CDIServerUtil.lookupBean(beanManager, type.getJavaClass());

        registered.remove(type);

        // Discriminate on @Command
        final Map<String, Method> commandPoints = new HashMap<String, Method>();
        for (final AnnotatedMethod method : type.getMethods()) {
          if (method.isAnnotationPresent(Command.class)) {
            final Command command = method.getAnnotation(Command.class);
            for (String cmdName : command.value()) {
              if (cmdName.equals("")) cmdName = method.getJavaMember().getName();
              commandPoints.put(cmdName, method.getJavaMember());
            }
          }
        }

        final String subjectName = CDIServerUtil.resolveServiceName(type.getJavaClass());

        if (commandPoints.isEmpty()) {
          bus.subscribe(subjectName, callback);
        } else {
          bus.subscribeLocal(
              subjectName, new CommandBindingsCallback(commandPoints, callback, bus));
        }
      }
    }
  public static void introspectInit(ArrayList<ConfigProgram> initList, AnnotatedType<?> type)
      throws ConfigException {
    for (AnnotatedMethod<?> annMethod : type.getMethods()) {
      Method method = annMethod.getJavaMember();

      if (!annMethod.isAnnotationPresent(PostConstruct.class)) {
        // && ! isAnnotationPresent(annList, Inject.class)) {
        continue;
      }

      if (method.getParameterTypes().length == 1
          && InvocationContext.class.equals(method.getParameterTypes()[0])) continue;

      if (method.isAnnotationPresent(PostConstruct.class)
          && method.getParameterTypes().length != 0) {
        throw new ConfigException(
            location(method) + L.l("{0}: @PostConstruct is requires zero arguments"));
      }

      PostConstructProgram initProgram = new PostConstructProgram(method);

      if (!initList.contains(initProgram)) initList.add(initProgram);
    }
  }
  /**
   * Register managed beans as Errai services
   *
   * @param event -
   * @param <T> -
   */
  @SuppressWarnings("UnusedDeclaration")
  public <T> void observeResources(@Observes final ProcessAnnotatedType<T> event) {
    final AnnotatedType<T> type = event.getAnnotatedType();

    for (final Annotation a : type.getJavaClass().getAnnotations()) {
      if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
        beanQualifiers.put(a.annotationType().getName(), a);
      }
    }

    // services
    if (type.isAnnotationPresent(Service.class)) {
      log.debug("discovered Errai annotation on type: " + type);
      boolean isRpc = false;

      final Class<T> javaClass = type.getJavaClass();
      for (final Class<?> intf : javaClass.getInterfaces()) {
        isRpc = intf.isAnnotationPresent(Remote.class);

        if (isRpc) {
          if (!managedTypes.getRemoteInterfaces().contains(intf)) {
            managedTypes.addRemoteInterface(intf);
          }
        }
      }

      if (!isRpc) {
        managedTypes.addServiceEndpoint(type);
      }
    } else {
      for (final AnnotatedMethod method : type.getMethods()) {
        if (method.isAnnotationPresent(Service.class)) {
          managedTypes.addServiceMethod(type, method);
        }
      }
    }

    // veto on client side implementations that contain CDI annotations
    // (i.e. @Observes) Otherwise Weld might try to invoke on them
    if (vetoClasses.contains(type.getJavaClass().getName())
        || (type.getJavaClass().getPackage().getName().contains("client")
            && !type.getJavaClass().isInterface())) {
      event.veto();
    }
    /** We must scan for Event consumer injection points to build the tables */
    final Class clazz = type.getJavaClass();

    for (final Field f : clazz.getDeclaredFields()) {
      if (f.isAnnotationPresent(Inject.class) && f.isAnnotationPresent(ObserverModel.class)) {
        processEventInjector(f.getType(), f.getGenericType(), f.getAnnotations());
      }
    }
    for (final Method m : clazz.getDeclaredMethods()) {
      if (m.isAnnotationPresent(Inject.class) && m.isAnnotationPresent(ObserverModel.class)) {
        final Class<?>[] parameterTypes = m.getParameterTypes();
        for (int i = 0, parameterTypesLength = parameterTypes.length;
            i < parameterTypesLength;
            i++) {
          final Class<?> parmType = parameterTypes[i];
          processEventInjector(
              parmType, m.getGenericParameterTypes()[i], m.getParameterAnnotations()[i]);
        }
      }
    }
    for (final Constructor c : clazz.getDeclaredConstructors()) {
      if (c.isAnnotationPresent(Inject.class) && c.isAnnotationPresent(ObserverModel.class)) {
        final Class<?>[] parameterTypes = c.getParameterTypes();
        for (int i = 0, parameterTypesLength = parameterTypes.length;
            i < parameterTypesLength;
            i++) {
          final Class<?> parmType = parameterTypes[i];
          processEventInjector(
              parmType, c.getGenericParameterTypes()[i], c.getParameterAnnotations()[i]);
        }
      }
    }
  }
  private ProducesMethodBean(
      InjectManager manager,
      Bean<X> producerBean,
      AnnotatedMethod<? super X> producesMethod,
      Arg<?>[] producesArgs,
      AnnotatedMethod<? super X> disposesMethod,
      Arg<?>[] disposesArgs) {
    super(manager, producesMethod.getBaseType(), producesMethod);

    _producerBean = producerBean;
    _producesMethod = producesMethod;
    _producesArgs = producesArgs;

    if (producesMethod == null) throw new NullPointerException();

    if (producesArgs == null) throw new NullPointerException();

    producesMethod.getJavaMember().setAccessible(true);

    if (disposesMethod != null) {
      _disposesProducer =
          new DisposesProducer<T, X>(
              manager, producerBean,
              disposesMethod, disposesArgs);

      for (AnnotatedParameter<? super X> param : disposesMethod.getParameters()) {
        if (param.isAnnotationPresent(Disposes.class)) _disposedParam = param;
      }
    }

    introspectInjectionPoints();

    _methodProducer = new MethodProducer();
    _producer = _methodProducer;

    Method javaMethod = producesMethod.getJavaMember();
    int modifiers = javaMethod.getModifiers();

    if (producesMethod.isAnnotationPresent(Specializes.class)) {
      if (Modifier.isStatic(modifiers)) {
        throw new ConfigException(
            L.l(
                "{0}.{1} is an invalid @Specializes @Producer because the method is static.",
                javaMethod.getDeclaringClass().getName(), javaMethod.getName()));
      }

      Method parentMethod = getSpecializedMethod(javaMethod);

      if (parentMethod == null) {
        throw new ConfigException(
            L.l(
                "{0}.{1} is an invalid @Specializes @Producer because it does not directly specialize a parent method",
                javaMethod.getDeclaringClass().getName(), javaMethod.getName()));
      }

      if (producesMethod.getJavaMember().isAnnotationPresent(Named.class)
          && parentMethod.isAnnotationPresent(Named.class)) {
        throw new ConfigException(
            L.l(
                "{0}.{1} is an invalid @Specializes @Producer because both it and its parent defines @Named",
                javaMethod.getDeclaringClass().getName(), javaMethod.getName()));
      }

      for (Annotation ann : parentMethod.getAnnotations()) {
        if (ann.annotationType().isAnnotationPresent(Qualifier.class)) {
          // ioc/07a5
          if (producesMethod instanceof AnnotatedElementImpl) {
            ((AnnotatedElementImpl) producesMethod).addAnnotation(ann);
          }
        }
      }
    }
  }