Ejemplo n.º 1
0
  @Override
  public String getStringSilent(Object bean, String param, String defaultValue) {

    String beanValue = defaultValue;

    if (bean != null) {
      try {
        Object value = BeanUtil.getProperty(bean, param);

        beanValue = Convert.toString(value, defaultValue);
      } catch (Exception e) {
      }
    }

    return beanValue;
  }
Ejemplo n.º 2
0
  /** Creates new bean instance and performs constructor injection. */
  protected Object newBeanInstance(BeanDefinition def, Map<String, Object> acquiredBeans) {
    if (def.ctor == null) {
      def.ctor = petiteResolvers.resolveCtorInjectionPoint(def.type);
    }

    // other ctors
    if (def.name != null) {
      acquiredBeans.put(def.name, Void.TYPE); // puts a dummy marker for cyclic dependency check
    }

    int paramNo = def.ctor.references.length;
    Object[] args = new Object[paramNo];

    // wiring
    if (def.wiringMode != WiringMode.NONE) {
      for (int i = 0; i < paramNo; i++) {
        args[i] = getBean(def.ctor.references[i], acquiredBeans);
        if (args[i] == null) {
          if ((def.wiringMode == WiringMode.STRICT)) {
            throw new PetiteException(
                "Wiring constructor failed. References '"
                    + Convert.toString(def.ctor.references[i])
                    + "' not found for constructor: "
                    + def.ctor.constructor);
          }
        }
      }
    }

    // create instance
    Object bean;
    try {
      bean = def.ctor.constructor.newInstance(args);
    } catch (Exception ex) {
      throw new PetiteException(
          "Failed to create new bean instance '"
              + def.type.getName()
              + "' using constructor: "
              + def.ctor.constructor,
          ex);
    }

    if (def.name != null) {
      acquiredBeans.put(def.name, bean);
    }
    return bean;
  }
Ejemplo n.º 3
0
  /** Wires methods. */
  protected void wireMethods(Object bean, BeanDefinition def, Map<String, Object> acquiredBeans) {
    if (def.methods == null) {
      def.methods = petiteResolvers.resolveMethodInjectionPoint(def.type);
    }
    for (MethodInjectionPoint methodRef : def.methods) {
      String[][] refNames = methodRef.references;
      Object[] args = new Object[refNames.length];
      for (int i = 0; i < refNames.length; i++) {
        String[] refName = refNames[i];
        Object value = null;

        boolean mixing = petiteConfig.wireScopedProxy || petiteConfig.detectMixedScopes;

        if (mixing) {
          BeanDefinition refBeanDefinition = lookupBeanDefinitions(refName);

          if (refBeanDefinition != null) {
            value = scopedProxyManager.lookupValue(this, def, refBeanDefinition);
          }
        }

        if (value == null) {
          value = getBean(refName, acquiredBeans);
        }

        args[i] = value;
        if (value == null) {
          if ((def.wiringMode == WiringMode.STRICT)) {
            throw new PetiteException(
                "Wiring failed. Beans references: '"
                    + Convert.toString(refName)
                    + "' not found for method: "
                    + def.type.getName()
                    + '#'
                    + methodRef.method.getName());
          }
        }
      }

      try {
        methodRef.method.invoke(bean, args);
      } catch (Exception ex) {
        throw new PetiteException(ex);
      }
    }
  }
Ejemplo n.º 4
0
  /** Wires properties. */
  protected void wireProperties(
      Object bean, BeanDefinition def, Map<String, Object> acquiredBeans) {
    if (def.properties == null) {
      def.properties =
          petiteResolvers.resolvePropertyInjectionPoint(
              def.type, def.wiringMode == WiringMode.AUTOWIRE);
    }

    boolean mixing = petiteConfig.wireScopedProxy || petiteConfig.detectMixedScopes;

    for (PropertyInjectionPoint pip : def.properties) {
      String[] refNames = pip.references;

      Object value = null;

      if (mixing) {
        BeanDefinition refBeanDefinition = lookupBeanDefinitions(refNames);

        if (refBeanDefinition != null) {
          value = scopedProxyManager.lookupValue(this, def, refBeanDefinition);
        }
      }

      if (value == null) {
        value = getBean(refNames, acquiredBeans);
      }

      if (value == null) {
        if ((def.wiringMode == WiringMode.STRICT)) {
          throw new PetiteException(
              "Wiring failed. Beans references: '"
                  + Convert.toString(refNames)
                  + "' not found for property: "
                  + def.type.getName()
                  + '#'
                  + pip.propertyDescriptor.getName());
        }
        continue;
      }

      // BeanUtil.setDeclaredProperty(bean, pip.propertyDescriptor.getName(), value);

      Setter setter = pip.propertyDescriptor.getSetter(true);
      try {
        setter.invokeSetter(bean, value);
      } catch (Exception ex) {
        throw new PetiteException("Wiring failed", ex);
      }
    }

    // sets
    if (def.sets == null) {
      def.sets =
          petiteResolvers.resolveSetInjectionPoint(def.type, def.wiringMode == WiringMode.AUTOWIRE);
    }
    for (SetInjectionPoint sip : def.sets) {

      String[] beanNames = resolveBeanNamesForType(sip.targetClass);

      Collection beans = sip.createSet(beanNames.length);

      for (String beanName : beanNames) {
        if (beanName.equals(def.name) == false) {
          Object value = getBean(beanName, acquiredBeans);
          beans.add(value);
        }
      }

      // BeanUtil.setDeclaredProperty(bean, sip.field.getName(), beans);

      Setter setter = sip.propertyDescriptor.getSetter(true);
      try {
        setter.invokeSetter(bean, beans);
      } catch (Exception ex) {
        throw new PetiteException("Wiring failed", ex);
      }
    }
  }