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

The following examples show how to use org.apache.xbean.recipe.ObjectRecipe#create() . 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: ClassLoaderUtil.java    From tomee with Apache License 2.0 6 votes vote down vote up
private static ClassLoaderConfigurer createConfigurer(final String key, final String impl) {
    try {
        final ObjectRecipe recipe = new ObjectRecipe(impl);
        for (final Map.Entry<Object, Object> entry : SystemInstance.get().getProperties().entrySet()) {
            final String entryKey = entry.getKey().toString();
            if (entryKey.startsWith(key)) {
                final String newKey = entryKey.substring(key.length());
                if (!"clazz".equals(newKey)) {
                    recipe.setProperty(newKey, entry.getValue());
                }
            }
        }

        final Object instance = recipe.create();
        if (instance instanceof ClassLoaderConfigurer) {
            return (ClassLoaderConfigurer) instance;
        } else {
            logger.error(impl + " is not a classlaoder configurer, using default behavior");
        }
    } catch (final Exception e) {
        logger.error("Can't create classloader configurer " + impl + ", using default behavior");
    }
    return null;
}
 
Example 2
Source File: ServiceInfos.java    From tomee with Apache License 2.0 6 votes vote down vote up
public static Object build(final Collection<ServiceInfo> services, final ServiceInfo info, final ObjectRecipe serviceRecipe) {
    if ("org.apache.openejb.config.sys.MapFactory".equals(info.className)) {
        return info.properties;
    }

    if (!info.properties.containsKey("properties")) {
        info.properties.put("properties", new UnsetPropertiesRecipe());
    }

    // we can't ask for having a setter for existing code
    serviceRecipe.allow(Option.FIELD_INJECTION);
    serviceRecipe.allow(Option.PRIVATE_PROPERTIES);

    setProperties(services, info, serviceRecipe);

    final Object service = serviceRecipe.create();

    SystemInstance.get().addObserver(service); // TODO: remove it? in all case the observer should remove itself when done
    Assembler.logUnusedProperties(serviceRecipe, info);

    return service;
}
 
Example 3
Source File: CdiResourceInjectionService.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public void injectJavaEEResources(final Object managedBeanInstance) {
    if (managedBeanInstance == null) {
        return;
    }

    final Class<?> managedBeanInstanceClass = managedBeanInstance.getClass();
    if (ejbPlugin.isSessionBean(managedBeanInstanceClass)) { // already done
        return;
    }

    final ObjectRecipe receipe = PassthroughFactory.recipe(managedBeanInstance);
    receipe.allow(Option.FIELD_INJECTION);
    receipe.allow(Option.PRIVATE_PROPERTIES);
    receipe.allow(Option.IGNORE_MISSING_PROPERTIES);
    receipe.allow(Option.NAMED_PARAMETERS);

    fillInjectionProperties(receipe, managedBeanInstance);

    receipe.create();
}
 
Example 4
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 5
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 6
Source File: Assembler.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void createSecurityService(final SecurityServiceInfo serviceInfo) throws OpenEJBException {

        Object service = SystemInstance.get().getComponent(SecurityService.class);
        if (service == null) {
            final ObjectRecipe serviceRecipe = createRecipe(Collections.<ServiceInfo>emptyList(), serviceInfo);
            service = serviceRecipe.create();
            logUnusedProperties(serviceRecipe, serviceInfo);
        }

        final Class interfce = serviceInterfaces.get(serviceInfo.service);
        checkImplementation(interfce, service.getClass(), serviceInfo.service, serviceInfo.id);

        try {
            this.containerSystem.getJNDIContext().bind(JAVA_OPENEJB_NAMING_CONTEXT + serviceInfo.service, service);
        } catch (final NamingException e) {
            throw new OpenEJBException("Cannot bind " + serviceInfo.service + " with id " + serviceInfo.id, e);
        }

        setSystemInstanceComponent(interfce, service);

        getContext().put(interfce.getName(), service);

        props.put(interfce.getName(), service);
        props.put(serviceInfo.service, service);
        props.put(serviceInfo.id, service);

        this.securityService = (SecurityService) service;

        // Update the config tree
        config.facilities.securityService = serviceInfo;

        logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);
    }
 
Example 7
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 8
Source File: ListConfigurator.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static <T> List<T> getList(final Properties properties, final String key, final ClassLoader classloader, final Class<T> filter) {
    if (properties == null) {
        return null;
    }

    final String features = properties.getProperty(key);
    if (features == null) {
        return null;
    }

    final List<T> list = new ArrayList<>();
    final String[] split = features.trim().split(",");
    for (final String feature : split) {
        if (feature == null || feature.trim().isEmpty()) {
            continue;
        }

        final String prefix = key + "." + feature + ".";
        final ObjectRecipe recipe = new ObjectRecipe(feature);
        for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
            final String current = entry.getKey().toString();
            if (current.startsWith(prefix)) {
                final String property = current.substring(prefix.length());
                recipe.setProperty(property, entry.getValue());
            }
        }

        final Object instance = recipe.create(classloader);
        if (!filter.isInstance(instance)) {
            throw new OpenEJBRuntimeException(feature + " is not an abstract feature");
        }
        list.add(filter.cast(instance));
    }

    if (list.isEmpty()) {
        return null;
    }
    return list;
}
 
Example 9
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;
}
 
Example 10
Source File: Client.java    From tomee with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Pass the base url as parameter");
        return;
    }

    final ConsoleReader reader = new ConsoleReader(System.in, new OutputStreamWriter(System.out));
    reader.addCompletor(new FileNameCompletor());
    reader.addCompletor(new SimpleCompletor(CommandManager.keys().toArray(new String[CommandManager.size()])));

    String line;
    while ((line = reader.readLine(PROMPT)) != null) {
        if (EXIT_CMD.equals(line)) {
            break;
        }

        Class<?> cmdClass = null;
        for (Map.Entry<String, Class<?>> cmd : CommandManager.getCommands().entrySet()) {
            if (line.startsWith(cmd.getKey())) {
                cmdClass = cmd.getValue();
                break;
            }
        }

        if (cmdClass != null) {
            final ObjectRecipe recipe = new ObjectRecipe(cmdClass);
            recipe.setProperty("url", args[0]);
            recipe.setProperty("command", line);
            recipe.setProperty("commands", CommandManager.getCommands());

            recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
            recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
            recipe.allow(Option.NAMED_PARAMETERS);

            try {
                final AbstractCommand cmdInstance = (AbstractCommand) recipe.create();
                cmdInstance.execute(line);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("sorry i don't understand '" + line + "'");
        }
    }
}
 
Example 11
Source File: Assembler.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void createContainer(final ContainerInfo serviceInfo) throws OpenEJBException {

        final ObjectRecipe serviceRecipe = createRecipe(Collections.<ServiceInfo>emptyList(), serviceInfo);

        serviceRecipe.setProperty("id", serviceInfo.id);
        serviceRecipe.setProperty("transactionManager", props.get(TransactionManager.class.getName()));
        serviceRecipe.setProperty("securityService", props.get(SecurityService.class.getName()));
        serviceRecipe.setProperty("properties", new UnsetPropertiesRecipe());

        // MDB container has a resource adapter string name that
        // must be replaced with the real resource adapter instance
        replaceResourceAdapterProperty(serviceRecipe);

        final Object service = serviceRecipe.create();

        serviceRecipe.getUnsetProperties().remove("id"); // we forced it
        serviceRecipe.getUnsetProperties().remove("securityService"); // we forced it
        logUnusedProperties(serviceRecipe, serviceInfo);

        final Class interfce = serviceInterfaces.get(serviceInfo.service);
        checkImplementation(interfce, service.getClass(), serviceInfo.service, serviceInfo.id);

        bindService(serviceInfo, service);

        setSystemInstanceComponent(interfce, service);

        props.put(interfce.getName(), service);
        props.put(serviceInfo.service, service);
        props.put(serviceInfo.id, service);

        containerSystem.addContainer(serviceInfo.id, (Container) service);

        // Update the config tree
        config.containerSystem.containers.add(serviceInfo);

        logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);

        if (Container.class.isInstance(service) && LocalMBeanServer.isJMXActive()) {
            final ObjectName objectName = ObjectNameBuilder.uniqueName("containers", serviceInfo.id, service);
            try {
                LocalMBeanServer.get().registerMBean(new DynamicMBeanWrapper(new JMXContainer(serviceInfo, (Container) service)), objectName);
                containerObjectNames.add(objectName);
            } catch (final Exception | NoClassDefFoundError e) {
                // no-op
            }
        }
    }
 
Example 12
Source File: Assembler.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void createService(final ServiceInfo serviceInfo) throws OpenEJBException {
    final ObjectRecipe serviceRecipe = createRecipe(Collections.<ServiceInfo>emptyList(), serviceInfo);
    serviceRecipe.setProperty("properties", new UnsetPropertiesRecipe());

    final Object service = serviceRecipe.create();
    SystemInstance.get().addObserver(service);

    logUnusedProperties(serviceRecipe, serviceInfo);

    final Class<?> serviceClass = service.getClass();

    getContext().put(serviceClass.getName(), service);

    props.put(serviceClass.getName(), service);
    props.put(serviceInfo.service, service);
    props.put(serviceInfo.id, service);

    config.facilities.services.add(serviceInfo);

    logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);
}
 
Example 13
Source File: Assembler.java    From tomee with Apache License 2.0 3 votes vote down vote up
public void createProxyFactory(final ProxyFactoryInfo serviceInfo) throws OpenEJBException {

        final ObjectRecipe serviceRecipe = createRecipe(Collections.<ServiceInfo>emptyList(), serviceInfo);

        final Object service = serviceRecipe.create();

        logUnusedProperties(serviceRecipe, serviceInfo);

        final Class interfce = serviceInterfaces.get(serviceInfo.service);
        checkImplementation(interfce, service.getClass(), serviceInfo.service, serviceInfo.id);

        ProxyManager.registerFactory(serviceInfo.id, (ProxyFactory) service);
        ProxyManager.setDefaultFactory(serviceInfo.id);

        bindService(serviceInfo, service);

        setSystemInstanceComponent(interfce, service);

        getContext().put(interfce.getName(), service);

        props.put(interfce.getName(), service);
        props.put(serviceInfo.service, service);
        props.put(serviceInfo.id, service);

        // Update the config tree
        config.facilities.intraVmServer = serviceInfo;

        logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);
    }
 
Example 14
Source File: Assembler.java    From tomee with Apache License 2.0 3 votes vote down vote up
public void createConnectionManager(final ConnectionManagerInfo serviceInfo) throws OpenEJBException {

        final ObjectRecipe serviceRecipe = createRecipe(Collections.<ServiceInfo>emptyList(), serviceInfo);

        final Object object = props.get("TransactionManager");
        serviceRecipe.setProperty("transactionManager", object);

        final Object service = serviceRecipe.create();

        logUnusedProperties(serviceRecipe, serviceInfo);

        final Class interfce = serviceInterfaces.get(serviceInfo.service);
        checkImplementation(interfce, service.getClass(), serviceInfo.service, serviceInfo.id);

        bindService(serviceInfo, service);

        setSystemInstanceComponent(interfce, service);

        getContext().put(interfce.getName(), service);

        props.put(interfce.getName(), service);
        props.put(serviceInfo.service, service);
        props.put(serviceInfo.id, service);

        // Update the config tree
        config.facilities.connectionManagers.add(serviceInfo);

        logger.getChildLogger("service").debug("createService.success", serviceInfo.service, serviceInfo.id, serviceInfo.className);
    }