@Test
  public void castingStaticFieldInjection() throws Exception {
    final Field field = CastingInjectorTestCase.class.getField("stringField");
    final Injector<String> fieldInjector =
        new FieldInjector<String>(
            Values.<CastingInjectorTestCase>nullValue(), Values.immediateValue(field));
    final Injector<Object> castingInjector =
        new CastingInjector<String>(fieldInjector, String.class);
    castingInjector.inject("injection");
    assertEquals("injection", stringField);
    castingInjector.uninject();
    assertNull(stringField);

    castingInjector.inject(null);
    assertNull(listField);

    castingInjector.inject("");
    assertEquals("", stringField);

    try {
      castingInjector.inject(new StringBuffer());
      fail("InjectionException expected");
    } catch (InjectionException e) {
    }
  }
  @SuppressWarnings("rawtypes")
  @Test
  public void castingFieldInjection() throws Exception {
    final Field field = CastingInjectorTestCase.class.getField("listField");
    Collection<String> collection = new ArrayList<String>();
    final Injector<List> fieldInjector =
        new FieldInjector<List>(Values.immediateValue(this), Values.immediateValue(field));
    final Injector<Object> castingInjector = Injectors.cast(fieldInjector, List.class);
    castingInjector.inject(collection);
    assertSame(collection, listField);
    castingInjector.uninject();
    assertNull(listField);

    castingInjector.inject(null);
    assertNull(listField);

    collection = new LinkedList<String>();
    castingInjector.inject(collection);
    assertSame(collection, listField);

    try {
      castingInjector.inject(new HashMap<Object, Object>());
      fail("InjectionException expected");
    } catch (InjectionException e) {
    }
  }
  @Test
  public void nullCastingInjector() throws Exception {
    final Field field = CastingInjectorTestCase.class.getField("stringField");
    final Injector<String> fieldInjector =
        new FieldInjector<String>(
            Values.<CastingInjectorTestCase>nullValue(), Values.immediateValue(field));
    Injector<Object> castingInjector = new CastingInjector<String>(null, String.class);
    try {
      castingInjector.inject("injection");
      fail("NullPointerException expected");
    } catch (NullPointerException e) {
    }

    castingInjector = new CastingInjector<String>(fieldInjector, null);
    try {
      castingInjector.inject("injection");
      fail("NullPointerException expected");
    } catch (NullPointerException e) {
    }

    castingInjector = new CastingInjector<String>(null, null);
    try {
      castingInjector.inject("injection");
      fail("NullPointerException expected");
    } catch (NullPointerException e) {
    }
  }
 private static Value<?> getValue(
     final Inject injectConfig, final List<ClassReflectionIndex> mBeanClassHierarchy) {
   final String propertyName = injectConfig.getPropertyName();
   Value<?> valueToInject = Values.injectedValue();
   if (propertyName != null) {
     final Method getter = ReflectionUtils.getGetter(mBeanClassHierarchy, propertyName);
     final Value<Method> getterValue = new ImmediateValue<Method>(getter);
     valueToInject =
         cached(new MethodValue<Object>(getterValue, valueToInject, Values.<Object>emptyList()));
   }
   return valueToInject;
 }
  private void addAttributes(
      final JBossServiceAttributeConfig[] attributeConfigs,
      final List<ClassReflectionIndex> mBeanClassHierarchy,
      final MBeanServices mBeanServices,
      final ClassLoader classLoader)
      throws DeploymentUnitProcessingException {
    if (attributeConfigs != null) {
      final Service<Object> createDestroyService = mBeanServices.getCreateDestroyService();
      for (final JBossServiceAttributeConfig attributeConfig : attributeConfigs) {
        final String propertyName = attributeConfig.getName();
        final Inject injectConfig = attributeConfig.getInject();
        final ValueFactory valueFactoryConfig = attributeConfig.getValueFactory();

        if (injectConfig != null) {
          final Value<?> value = getValue(injectConfig, mBeanClassHierarchy);
          final Injector<Object> injector =
              getPropertyInjector(propertyName, mBeanClassHierarchy, createDestroyService, value);
          mBeanServices.addAttribute(injectConfig.getBeanName(), injector);
        } else if (valueFactoryConfig != null) {
          final Value<?> value = getValue(valueFactoryConfig, mBeanClassHierarchy, classLoader);
          final Injector<Object> injector =
              getPropertyInjector(propertyName, mBeanClassHierarchy, createDestroyService, value);
          mBeanServices.addAttribute(valueFactoryConfig.getBeanName(), injector);
        } else {
          final Value<?> value = getValue(attributeConfig, mBeanClassHierarchy);
          final Injector<Object> injector =
              getPropertyInjector(
                  propertyName, mBeanClassHierarchy, createDestroyService, Values.injectedValue());
          mBeanServices.addInjectionValue(injector, value);
        }
      }
    }
  }
 @Test
 public void lookupAndInject1() throws Exception {
   final Injector<Integer> injector =
       new SetMethodInjector<Integer>(
           Values.immediateValue(service), getMethod(AnotherService.class, "setRetry", int.class));
   assertSetRetryInjector(injector);
 }
  /**
   * Add a ContextService for this module.
   *
   * @param phaseContext the deployment unit context
   * @throws org.jboss.as.server.deployment.DeploymentUnitProcessingException
   */
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (isEarDeployment(deploymentUnit)) {
      return;
    }
    final ServiceTarget serviceTarget = phaseContext.getServiceTarget();

    final ServiceName moduleContextServiceName = ContextServiceNameBuilder.module(deploymentUnit);
    final RootContextService contextService = new RootContextService();
    serviceTarget.addService(moduleContextServiceName, contextService).install();

    final BinderService<String> moduleNameBinder =
        new BinderService<String>(
            "ModuleName", Values.immediateValue(getModuleName(deploymentUnit)));
    serviceTarget
        .addService(moduleContextServiceName.append("ModuleName"), moduleNameBinder)
        .addDependency(
            moduleContextServiceName, Context.class, moduleNameBinder.getContextInjector())
        .install();

    final ContextService envContextService = new ContextService("env");
    serviceTarget
        .addService(moduleContextServiceName.append("env"), envContextService)
        .addDependency(
            moduleContextServiceName, Context.class, envContextService.getParentContextInjector())
        .install();

    phaseContext
        .getDeploymentUnit()
        .putAttachment(
            Attachments.MODULE_CONTEXT_CONFIG, new NamingContextConfig(moduleContextServiceName));
  }
 private static Injector<Object> getPropertyInjector(
     final String propertyName,
     final List<ClassReflectionIndex> mBeanClassHierarchy,
     final Service<?> service,
     final Value<?> value) {
   final Method setterMethod = ReflectionUtils.getSetter(mBeanClassHierarchy, propertyName);
   return new MethodInjector<Object>(
       setterMethod, service, Values.nullValue(), Collections.singletonList(value));
 }
 @Test
 public void publicMethodWithExceptionOnUninjection() throws Exception {
   final TargetWrapper<?> targetWrapper = new TargetWrapper<Object>(new StringBuffer());
   Injector<Boolean> injector =
       new SetMethodInjector<Boolean>(
           Values.immediateValue(targetWrapper),
           getMethod(TargetWrapper.class, "readTarget", boolean.class));
   injector.uninject();
 }
 @Test
 public void methodWithException() throws Exception {
   final Injector<Boolean> injector =
       new SetMethodInjector<Boolean>(
           Values.immediateValue(service),
           getMethod(AnotherService.class, "discoverDefinedBy", boolean.class));
   try {
     injector.inject(true);
     fail("InjectionException expected");
   } catch (InjectionException e) {
   }
   injector.uninject(); // no error expected for uninjection
 }
예제 #11
0
    @Override
    public void configure(
        final DeploymentPhaseContext context,
        final ComponentConfiguration componentConfiguration,
        final ViewDescription description,
        final ViewConfiguration configuration)
        throws DeploymentUnitProcessingException {

      // Create view bindings
      final List<BindingConfiguration> bindingConfigurations =
          configuration.getBindingConfigurations();
      for (String bindingName : description.getBindingNames()) {
        bindingConfigurations.add(
            new BindingConfiguration(
                bindingName,
                description.createInjectionSource(
                    description.getServiceName(),
                    Values.immediateValue(componentConfiguration.getModuleClassLoader()))));
      }
    }
  /**
   * Process a deployment for KernelDeployment configuration. Will install a {@code MC bean} for
   * each configured bean.
   *
   * @param phaseContext the deployment unit context
   * @throws DeploymentUnitProcessingException
   */
  public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final KernelDeploymentXmlDescriptor kdXmlDescriptor =
        phaseContext
            .getDeploymentUnit()
            .getAttachment(KernelDeploymentXmlDescriptor.ATTACHMENT_KEY);
    if (kdXmlDescriptor == null) return;

    final Module module = phaseContext.getDeploymentUnit().getAttachment(Attachments.MODULE);
    if (module == null)
      throw new DeploymentUnitProcessingException(
          "Failed to get module attachment for " + phaseContext.getDeploymentUnit());

    final ClassLoader classLoader = module.getClassLoader();
    final Value<ClassLoader> classLoaderValue = Values.immediateValue(classLoader);

    final List<BeanMetaDataConfig> beanConfigs = kdXmlDescriptor.getBeans();
    final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
    for (final BeanMetaDataConfig beanConfig : beanConfigs) {
      addBean(serviceTarget, beanConfig, classLoaderValue);
    }
  }
 private static Value<?> getValue(
     final ValueFactory valueFactory,
     final List<ClassReflectionIndex> mBeanClassHierarchy,
     final ClassLoader classLoader)
     throws DeploymentUnitProcessingException {
   final String methodName = valueFactory.getMethodName();
   final ValueFactoryParameter[] parameters = valueFactory.getParameters();
   final List<Class<?>> paramTypes = new ArrayList<Class<?>>(parameters.length);
   final List<Value<?>> paramValues = new ArrayList<Value<?>>(parameters.length);
   for (ValueFactoryParameter parameter : parameters) {
     final Class<?> attributeTypeValue =
         ReflectionUtils.getClass(parameter.getType(), classLoader);
     paramTypes.add(attributeTypeValue);
     paramValues.add(
         new ImmediateValue<Object>(newValue(attributeTypeValue, parameter.getValue())));
   }
   final Value<Method> methodValue =
       new ImmediateValue<Method>(
           ReflectionUtils.getMethod(
               mBeanClassHierarchy, methodName, paramTypes.toArray(new Class<?>[0]), true));
   return cached(new MethodValue<Object>(methodValue, Values.injectedValue(), paramValues));
 }
 private static Injector<Object> getOptionalAttributeInjector(
     final String attributeName,
     final List<ClassReflectionIndex> mBeanClassHierarchy,
     final Service<Object> service) {
   return getPropertyInjector(attributeName, mBeanClassHierarchy, service, Values.injectedValue());
 }
 /** {@inheritDoc} */
 protected synchronized Injector<V> getInjector(final Object target) {
   if (injector == null) {
     injector = new FieldInjector<V>(Values.immediateValue(target), field);
   }
   return injector;
 }
 private static <T> Value<T> cached(final Value<T> value) {
   return Values.cached(value);
 }