Пример #1
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());
    }
  }
Пример #2
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;
  }
Пример #3
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);
      }
    }
Пример #4
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;
  }