Java Code Examples for org.apache.xbean.recipe.ObjectRecipe#setAllProperties()

The following examples show how to use org.apache.xbean.recipe.ObjectRecipe#setAllProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PoolDataSourceCreator.java    From tomee with Apache License 2.0 5 votes vote down vote up
protected <T> T build(final Class<T> clazz, final Properties properties) {
    final ObjectRecipe serviceRecipe = new ObjectRecipe(clazz);
    recipeOptions(serviceRecipe);
    serviceRecipe.setAllProperties(properties);
    final T value = (T) serviceRecipe.create();
    if (trackRecipeFor(value)) { // avoid to keep config objects
        recipes.put(value, serviceRecipe);
    }
    return value;
}
 
Example 2
Source File: PoolDataSourceCreator.java    From tomee with Apache License 2.0 5 votes vote down vote up
protected <T> T build(final Class<T> clazz, final Object instance, final Properties properties) {
    final ObjectRecipe recipe = PassthroughFactory.recipe(instance);
    recipeOptions(recipe);
    recipe.setAllProperties(properties);
    final T value = (T) recipe.create();
    recipes.put(value, recipe);
    return value;
}
 
Example 3
Source File: FlushableDataSourceHandler.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void createANewDelegate() {
    final CommonDataSource old = delegate.get();
    try {
        final ObjectRecipe recipe = new ObjectRecipe(DataSourceFactory.class.getName(), "create", FACTORY_ARGS);
        recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
        recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
        recipe.allow(Option.NAMED_PARAMETERS);
        recipe.allow(Option.PRIVATE_PROPERTIES);
        recipe.setAllProperties(config.properties);

        recipe.setProperty("resettableHandler", resettableHandler);
        recipe.setProperty("flushableHandler", this);

        updateDataSource(CommonDataSource.class.cast(recipe.create()));
    } catch (final Exception e) {
        LOGGER.error("Can't recreate the datasource, keeping old one", e);
        return;
    }

    if (DataSourceFactory.knows(old)) {
        try {
            DataSourceFactory.destroy(old);
        } catch (final Throwable t) {
            //Ignore
        }
    }
}
 
Example 4
Source File: ObjectRecipeHelper.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static Object createMeFromSystemProps(final String prefix, final String suffix, final Class<?> clazz) {
    final Properties props = SystemInstance.get().getProperties();
    final Map<String, Object> usedOnes = new HashMap<>();

    for (final Map.Entry<Object, Object> entry : props.entrySet()) {
        final String key = entry.getKey().toString();
        if (prefix != null && !key.startsWith(prefix)) {
            continue;
        }
        if (suffix != null && !key.endsWith(suffix)) {
            continue;
        }

        String newKey = key;
        if (prefix != null) {
            newKey = newKey.substring(prefix.length());
        }
        if (suffix != null) {
            newKey = newKey.substring(0, newKey.length() - suffix.length());
        }
        usedOnes.put(newKey, entry.getValue());
    }

    final ObjectRecipe recipe = new ObjectRecipe(clazz);
    recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
    recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
    recipe.allow(Option.PRIVATE_PROPERTIES);
    recipe.allow(Option.FIELD_INJECTION);
    recipe.allow(Option.NAMED_PARAMETERS);
    recipe.setAllProperties(usedOnes);
    return recipe.create();
}
 
Example 5
Source File: StatefulContainerFactory.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void buildCache() throws Exception {
    if (properties == null) {
        throw new IllegalArgumentException("No cache defined for StatefulContainer " + id);
    }

    // get the cache property
    Object cache = getProperty("Cache");
    if (cache == null) {
        throw new IllegalArgumentException("No cache defined for StatefulContainer " + id);
    }

    // if property contains a live cache instance, just use it
    if (cache instanceof Cache) {
        this.cache = (Cache<Object, Instance>) cache;
        return;
    }

    // build the object recipe
    final ObjectRecipe serviceRecipe = new ObjectRecipe((String) cache);
    serviceRecipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
    serviceRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
    serviceRecipe.allow(Option.NAMED_PARAMETERS);
    serviceRecipe.setAllProperties(properties);

    // invoke recipe
    /* the cache should be created with container loader to avoid memory leaks
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) getClass().getClassLoader();
    */
    ClassLoader classLoader = StatefulContainerFactory.class.getClassLoader();
    if (!((String) cache).startsWith("org.apache.tomee")) { // user impl?
        classLoader = Thread.currentThread().getContextClassLoader();
    }
    cache = serviceRecipe.create(classLoader);

    // assign value
    this.cache = (Cache<Object, Instance>) cache;
}