private void realRegisterNodes(EffectiveNode node) {
   String key = node.getKey();
   Object value = node.getRealValue();
   // TODO which order for component
   preOrderComponentList.add(new ViewValue(key, value));
   /*
   /// done : suppress that when we get rid of BasicModularSubsystem
   if (value instanceof Module) {
       // was :  this.registerModule((Module) value);
       // block left for maintenance purposes
       // inherited form BasicModularSubSystem this.registerConfigurable(key, (Configurable) value);
   } else if (value instanceof Configurable) {
       //this.registerConfigurable(key, (Configurable) value);
   }
   */
   // end stupid things
   // problem about order of evaluation should proceed from the ground up
   ArrayList<EffectiveNode> children = node.getChildren();
   if (children != null) {
     for (EffectiveNode child : children) {
       realRegisterNodes(child);
       ;
     }
   }
   if (value instanceof Configurable) {
     Configurable configurable = (Configurable) value;
     configurable.setEnvironment(
         new Configurable.Environment(key, configurable, getConfigurationProxy(), lookup));
     /*
     } */
   }
   /*
   } */
 }
Ejemplo n.º 2
0
 public static void testConfigurableForNonPrimitives() {
   Map<String, String> p = new HashMap<String, String>();
   C config = Configurable.createConfigurable(C.class, p);
   assertNull(config.port());
   p.put("port", "10");
   config = Configurable.createConfigurable(C.class, p);
   assertEquals(Integer.valueOf(10), config.port()); // property port is
   // not set
 }
Ejemplo n.º 3
0
  public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.setProperty("seperator", "/");
    IdentifierGenerator gen = new UUIDHexGenerator();
    ((Configurable) gen).configure(Hibernate.STRING, props, null);
    IdentifierGenerator gen2 = new UUIDHexGenerator();
    ((Configurable) gen2).configure(Hibernate.STRING, props, null);

    for (int i = 0; i < 10; i++) {
      String id = (String) gen.generate(null, null);
      System.out.println(id + ": " + id.length());
      String id2 = (String) gen2.generate(null, null);
      System.out.println(id2 + ": " + id2.length());
    }
  }
Ejemplo n.º 4
0
  public static void testEnum() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.build();
    assertEquals(0, b.getErrors().size());
    assertEquals(0, b.getWarnings().size());

    Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$Enums.xml");
    IO.copy(r.openInputStream(), System.err);

    Document d = db.parse(r.openInputStream());
    assertEquals(
        "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI());

    Properties p = new Properties();
    p.setProperty("r", "requireConfiguration");
    p.setProperty("i", "ignoreConfiguration");
    p.setProperty("o", "optionalConfiguration");
    Enums enums = Configurable.createConfigurable(Enums.class, (Map<Object, Object>) p);
    assertEquals(Enums.X.requireConfiguration, enums.r());
    assertEquals(Enums.X.ignoreConfiguration, enums.i());
    assertEquals(Enums.X.optionalConfiguration, enums.o());
  }
Ejemplo n.º 5
0
  /**
   * Same as {@link #setParentConfiguration(Configuration)}.
   *
   * @throws IllegalArgumentException if the argument is {@code null} or not a {@link
   *     Configuration}.
   */
  @Override
  void setParent(Configurable cfg) {
    NullArgumentException.check("cfg", cfg);
    if (!(cfg instanceof Configuration)) {
      throw new IllegalArgumentException(
          "The parent of a TemplateConfigurer can only be a Configuration");
    }

    if (parentConfigurationSet) {
      if (getParent() != cfg) {
        throw new IllegalStateException(
            "This TemplateConfigurer is already associated with a different Configuration instance.");
      }
      return;
    }

    if (((Configuration) cfg).getIncompatibleImprovements().intValue()
            < _TemplateAPI.VERSION_INT_2_3_22
        && hasAnyConfigurableSet()) {
      throw new IllegalStateException(
          "This TemplateConfigurer can't be associated to a Configuration that has incompatibleImprovements "
              + "less than 2.3.22, because it changes non-parser settings.");
    }

    super.setParent(cfg);
    parentConfigurationSet = true;
  }
Ejemplo n.º 6
0
  /** lend an object from the pool. */
  public Object lendObject() {
    checkIsInitialized();

    Object _result = null;

    synchronized (pool_) {
      if (!pool_.isEmpty()) {
        _result = pool_.removeFirst();
      }

      if (_result == null) {
        while (!isCreationAllowed()) {
          poolIsEmpty();
        }
      }
    }

    if (_result == null) {
      _result = createInstance();
    }

    try {
      ((Configurable) _result).configure(this.config_);
    } catch (ClassCastException cce) {
      // no worries, just don't configure
    } catch (ConfigurationException ce) {
      throw new RuntimeException("Could not configure instance");
    }

    doActivateObject(_result);
    active_.add(_result);

    return _result;
  }
Ejemplo n.º 7
0
 public static void testConfigurable() {
   Map<String, Object> ht = new Hashtable<String, Object>();
   DefaultBoolean db = Configurable.createConfigurable(DefaultBoolean.class, ht);
   assertTrue(db.istrue());
   assertFalse(db.isfalse());
   assertFalse(db.isAlsoFalse());
 }
Ejemplo n.º 8
0
    private Codec<?> buildCustomCodec(Element e) {
      String classAttr = e.getAttribute(ATTR_CODEC);
      Class<?> customCodecClass;
      try {
        customCodecClass = Class.forName(classAttr);
        if (CustomCodec.class.isAssignableFrom(customCodecClass)) {
          CustomCodec customCodec = (CustomCodec) customCodecClass.newInstance();

          Map<String, String> params = new HashMap<>();
          List<Element> paramElements = getSubElements(e);
          for (Element paramElement : paramElements) {
            params.put(paramElement.getAttribute(ATTR_KEY), paramElement.getAttribute(ATTR_VALUE));
          }
          if (customCodec instanceof Configurable) {
            ((Configurable) customCodec).configure(params);
          }

          return new CustomCodecAdapter(customCodec, getOptionalInteger(e, ATTR_LENGTH));
        } else {
          throw new ConfigException(format("Invalid custom class %s", classAttr));
        }
      } catch (ClassNotFoundException
          | SecurityException
          | InstantiationException
          | IllegalAccessException
          | IllegalArgumentException ex) {
        throw new ConfigException(ex.getMessage(), ex);
      }
    }
Ejemplo n.º 9
0
 public PropertySheet(
     Configurable configurable,
     String name,
     RawPropertyData rpd,
     ConfigurationManager ConfigurationManager) {
   this(configurable.getClass(), name, ConfigurationManager, rpd);
   owner = configurable;
 }
Ejemplo n.º 10
0
  private void applyConfigurationChange(String propName, Object cmName, Object value)
      throws PropertyException {
    rawProps.put(propName, cmName);
    propValues.put(propName, value != null ? value : cmName);

    if (getInstanceName() != null) cm.fireConfChanged(getInstanceName(), propName);

    if (owner != null) owner.newProperties(this);
  }
Ejemplo n.º 11
0
  static <T> T set(Class<T> interf, Object value) {
    Properties p = new Properties();
    Method ms[] = interf.getMethods();

    for (Method m : ms) {
      p.put(m.getName(), value);
    }
    return Configurable.createConfigurable(interf, (Map<Object, Object>) p);
  }
Ejemplo n.º 12
0
  public static void testSpecialConversions() throws URISyntaxException {
    Properties p = new Properties();
    p.put("enumv", "A");
    p.put("pattern", ".*");
    p.put("clazz", "java.lang.Object");
    p.put("constructor", "http://www.aQute.biz");

    SpecialConversions trt =
        Configurable.createConfigurable(SpecialConversions.class, (Map<Object, Object>) p);
    assertEquals(SpecialConversions.X.A, trt.enumv());
    assertEquals(".*", trt.pattern().pattern());
    assertEquals(Object.class, trt.clazz());
    assertEquals(new URI("http://www.aQute.biz"), trt.constructor());
  }
Ejemplo n.º 13
0
 @Override
 public void componentAdded(Configurable component) {
   super.componentAdded(component);
   if (log.isLoggable(Level.CONFIG)) {
     log.config(" component: " + component.getName());
   }
   ServiceEntity item = config_list.findNode(component.getName());
   if (item == null) {
     item = new ServiceEntity(getName(), component.getName(), "Component: " + component.getName());
     item.addFeatures(CMD_FEATURES);
     item.addIdentities(
         new ServiceIdentity[] {
           new ServiceIdentity("automation", "command-node", "Component: " + component.getName())
         });
     config_list.addItems(new ServiceEntity[] {item});
   }
   if (config_set.findNode(component.getName()) == null) {
     config_set.addItems(new ServiceEntity[] {item});
   }
   if (component.getClass().getName().equals(ROUTER_COMP_CLASS_NAME)) {
     routerCompName = component.getName();
   } // end of if (component.getClass().getName().equals())
 }
Ejemplo n.º 14
0
  /**
   * Get a parameter value which is an instance of a class identified by the parameter. It is
   * assumed that the class identified by the parameter has a default parameter-less constructor.
   *
   * <p>If a non-{@code null} value is returned, it will automatically be checked if it is an
   * instance of {@link org.logisticPlanning.utils.config.Configurable Configurable} first. If so,
   * its {@link org.logisticPlanning.utils.config.Configurable#configure(Configuration) configure}
   * method will be invoked with this {@link org.logisticPlanning.utils.config.Configuration
   * configuration} as parameter. This also holds if it is decided to return {@code defInstance} .
   *
   * @param key the key
   * @param defInstance the default instance, or {@code null}
   * @param defClass the default class, or {@code null}
   * @param baseClass the base class of the instance to return, used to check type consistency
   * @return the instance
   * @param <T> the type of object to return
   */
  @SuppressWarnings("unchecked")
  public final <T> T getInstance(
      final String key,
      final Class<T> baseClass,
      final Class<? extends T> defClass,
      final T defInstance) {
    final Class<?> c;
    final Object o;
    final T ret;

    c = this.getClass(key, baseClass, defClass);
    if (c == null) {
      ret = defInstance;
    } else {

      try {
        o = c.newInstance();
      } catch (final Throwable tt) {
        throw new IllegalArgumentException(key, tt);
      }

      if (o == null) {
        ret = defInstance;
      } else {
        if (baseClass != null) {
          ret = baseClass.cast(o);
        } else {
          ret = ((T) o);
        }
      }
    }

    if (ret != null) {
      if (ret instanceof Configurable) {
        ((Configurable) ret).configure(this);
      }
    }
    return ret;
  }
Ejemplo n.º 15
0
  /**
   * @return the owner of this property sheet. In most cases this will be the configurable instance
   *     which was instrumented by this property sheet.
   */
  public synchronized Configurable getOwner() {
    try {

      if (!isInstanciated()) {
        // ensure that all mandatory properties are set before instantiating the component
        Collection<String> undefProps = getUndefinedMandatoryProps();
        if (!undefProps.isEmpty()) {
          throw new InternalConfigurationException(
              getInstanceName(), undefProps.toString(), "not all mandatory properties are defined");
        }

        owner = ownerClass.newInstance();
        owner.newProperties(this);
      }
    } catch (IllegalAccessException e) {
      throw new InternalConfigurationException(
          e, getInstanceName(), null, "Can't access class " + ownerClass);
    } catch (InstantiationException e) {
      throw new InternalConfigurationException(
          e, getInstanceName(), null, "Can't instantiate class " + ownerClass);
    }

    return owner;
  }
 /** invokes the <TT>start()</TT> methods on all components (starting from the top main Module) */
 @Override
 public void doStart() {
   super.doStart();
   mainObject.proceduralWalk(Configurable::start, null);
 }
 /**
  * invokes the <TT>shutdownNow()</TT> methods on all components (starting from the top main
  * Module)
  */
 @Override
 public void doShutdown() {
   log.fine("Subsystem " + getName() + " shutdown starting");
   mainObject.proceduralWalk(Configurable::shutdownNow, null);
 }
 private void callPostStart(Configurable goal) throws HardwareException {
   goal.postStart();
   for (Configurable configurable : goal.listChildren()) {
     callPostStart(configurable);
   }
 }
Ejemplo n.º 19
0
  public static void testNaming() throws Exception {
    Map<String, Object> map = Create.map();

    map.put("_secret", "_secret");
    map.put("_secret", "_secret");
    map.put(".secret", ".secret");
    map.put("$new", "$new");
    map.put("new", "new");
    map.put("secret", "secret");
    map.put("a_b_c", "a_b_c");
    map.put("a.b.c", "a.b.c");
    map.put(".a_b", ".a_b");
    map.put("$$$$a_b", "$$$$a_b");
    map.put("$$$$a.b", "$$$$a.b");
    map.put("a", "a");
    map.put("a$", "a$");
    map.put("a$$", "a$$");
    map.put("a$.$", "a$.$");
    map.put("a$_$", "a$_$");
    map.put("a..", "a..");
    map.put("noid", "noid");
    map.put("nullid", "nullid");

    Naming trt = Configurable.createConfigurable(Naming.class, map);

    // By name
    assertEquals("secret", trt.secret());
    assertEquals("_secret", trt.__secret());
    assertEquals(".secret", trt._secret());
    assertEquals("new", trt.$new());
    assertEquals("$new", trt.$$new());
    assertEquals("a.b.c", trt.a_b_c());
    assertEquals("a_b_c", trt.a__b__c());
    assertEquals(".a_b", trt._a__b());
    assertEquals("$$$$a.b", trt.$$$$$$$$a_b());
    assertEquals("$$$$a_b", trt.$$$$$$$$a__b());
    assertEquals("a", trt.a$());
    assertEquals("a$", trt.a$$());
    assertEquals("a$", trt.a$$$());
    assertEquals("a$.$", trt.a$$_$$());
    assertEquals("a$_$", trt.a$$__$$());
    assertEquals("a..", trt.a_$_());
    assertEquals("noid", trt.noid());
    assertEquals("nullid", trt.nullid());

    // By AD
    assertEquals("secret", trt.xsecret());
    assertEquals("_secret", trt.x__secret());
    assertEquals(".secret", trt.x_secret());
    assertEquals("new", trt.x$new());
    assertEquals("$new", trt.x$$new());
    assertEquals("a.b.c", trt.xa_b_c());
    assertEquals("a_b_c", trt.xa__b__c());
    assertEquals(".a_b", trt.x_a__b());
    assertEquals("$$$$a.b", trt.x$$$$$$$$a_b());
    assertEquals("$$$$a_b", trt.x$$$$$$$$a__b());
    assertEquals("a", trt.xa$());
    assertEquals("a$", trt.xa$$());
    assertEquals("a$", trt.xa$$$());
    assertEquals("a$.$", trt.xa$$_$$());

    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setProperty("Export-Package", "test.metatype");
    b.setProperty("-metatype", "*");
    b.build();
    assertEquals(0, b.getErrors().size());
    assertEquals(0, b.getWarnings().size());

    Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$Naming.xml");
    IO.copy(r.openInputStream(), System.err);
    Document d = db.parse(r.openInputStream(), "UTF-8");
    assertEquals(
        "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI());
  }
 private void doInitComponents() {
   mainObject.proceduralWalk(Configurable::init, null);
   /*
    */
 }