Search in sources :

Example 31 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class WSS4JInterceptorFactoryBase method getAndDestroyMap.

protected Map<String, Object> getAndDestroyMap() {
    final Map<String, Object> map = new HashMap<String, Object>();
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        map.put(entry.getKey().toString(), entry.getValue());
    }
    // avoid warnings
    final Recipe recipe = ExecutionContext.getContext().getStack().getLast();
    if (ObjectRecipe.class.isInstance(recipe)) {
        final ObjectRecipe or = ObjectRecipe.class.cast(recipe);
        if (or.getUnsetProperties() != null) {
            or.getUnsetProperties().clear();
        }
    }
    return map;
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) HashMap(java.util.HashMap) Recipe(org.apache.xbean.recipe.Recipe) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Map(java.util.Map) HashMap(java.util.HashMap)

Example 32 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class ServiceManager method initServer.

protected ServerService initServer(final String serviceName, final Properties serviceProperties) throws IOException {
    final DiscoveryRegistry registry = SystemInstance.get().getComponent(DiscoveryRegistry.class);
    final OpenEjbConfiguration conf = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
    logger.debug("Processing ServerService(id=" + serviceName + ")");
    overrideProperties(serviceName, serviceProperties);
    serviceProperties.setProperty("name", serviceName);
    if (conf != null && conf.facilities != null) {
        final ServiceInfo info = new ServiceInfo();
        info.className = ((Class) serviceProperties.get(ServerService.class)).getName();
        info.service = "ServerService";
        info.id = serviceName;
        info.properties = serviceProperties;
        conf.facilities.services.add(info);
    }
    final boolean enabled = isEnabled(serviceProperties);
    logger.debug("Found ServerService(id=" + serviceName + ", disabled=" + (!enabled) + ")");
    if (enabled) {
        final Class serviceClass = (Class) serviceProperties.get(ServerService.class);
        logger.info("Creating ServerService(id=" + serviceName + ")");
        // log all properties on debug
        if (logger.isDebugEnabled()) {
            for (final Map.Entry<Object, Object> entry : serviceProperties.entrySet()) {
                logger.debug(entry.getKey() + " = " + entry.getValue());
            }
        }
        try {
            // Create Service
            ServerService service;
            ObjectRecipe recipe = new ObjectRecipe(serviceClass);
            try {
                // Do not import.  This class is not available in xbean-reflect-3.3
                final ReflectionUtil.StaticFactory factory = ReflectionUtil.findStaticFactory(serviceClass, "createServerService", null, null, serviceProperties.stringPropertyNames(), Collections.singleton(Option.NAMED_PARAMETERS));
                if (factory != null) {
                    // can throw an exception so call it before next line
                    recipe.setConstructorArgNames(factory.getParameterNames());
                    recipe.setFactoryMethod("createServerService");
                } else if (ReflectionUtil.findStaticFactory(serviceClass, "createServerService", null, null) != null) {
                    // old behavior, remove when sure previous check is ok
                    recipe.setFactoryMethod("createServerService");
                }
            } catch (final Throwable e) {
            //Ignore
            }
            recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
            recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
            service = (ServerService) recipe.create(serviceClass.getClassLoader());
            if (!(service instanceof SelfManaging)) {
                service = manage(serviceName, serviceProperties, service);
            }
            service.init(serviceProperties);
            if (service instanceof DiscoveryAgent) {
                final DiscoveryAgent agent = (DiscoveryAgent) service;
                registry.addDiscoveryAgent(agent);
            }
            if (LocalMBeanServer.isJMXActive()) {
                final MBeanServer server = LocalMBeanServer.get();
                register(serviceName, service, server);
            }
            return service;
        } catch (Throwable t) {
            t.printStackTrace();
            logger.error("service.instantiation.err", t, serviceClass.getName(), t.getClass().getName(), t.getMessage());
        }
    }
    return null;
}
Also used : ReflectionUtil(org.apache.xbean.recipe.ReflectionUtil) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) ServiceInfo(org.apache.openejb.assembler.classic.ServiceInfo) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Map(java.util.Map) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer)

Example 33 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class Configuration method loadFromProperties.

public void loadFromProperties(final Properties config) {
    // filtering properties with system properties or themself
    final StrSubstitutor strSubstitutor = new StrSubstitutor(new StrLookup<String>() {

        @Override
        public String lookup(final String key) {
            final String property = System.getProperty(key);
            return property == null ? config.getProperty(key) : null;
        }
    });
    for (final String key : config.stringPropertyNames()) {
        final String val = config.getProperty(key);
        if (val == null || val.trim().isEmpty()) {
            continue;
        }
        final String newVal = strSubstitutor.replace(config.getProperty(key));
        if (!val.equals(newVal)) {
            config.setProperty(key, newVal);
        }
    }
    final String http = config.getProperty("http");
    if (http != null) {
        setHttpPort(Integer.parseInt(http));
    }
    final String https = config.getProperty("https");
    if (https != null) {
        setHttpsPort(Integer.parseInt(https));
    }
    final String stop = config.getProperty("stop");
    if (stop != null) {
        setStopPort(Integer.parseInt(stop));
    }
    final String host = config.getProperty("host");
    if (host != null) {
        setHost(host);
    }
    final String dir = config.getProperty("dir");
    if (dir != null) {
        setDir(dir);
    }
    final String serverXml = config.getProperty("serverXml");
    if (serverXml != null) {
        setServerXml(serverXml);
    }
    final String keepServerXmlAsThis = config.getProperty("keepServerXmlAsThis");
    if (keepServerXmlAsThis != null) {
        setKeepServerXmlAsThis(Boolean.parseBoolean(keepServerXmlAsThis));
    }
    final String quickSession = config.getProperty("quickSession");
    if (quickSession != null) {
        setQuickSession(Boolean.parseBoolean(quickSession));
    }
    final String skipHttp = config.getProperty("skipHttp");
    if (skipHttp != null) {
        setSkipHttp(Boolean.parseBoolean(skipHttp));
    }
    final String ssl = config.getProperty("ssl");
    if (ssl != null) {
        setSsl(Boolean.parseBoolean(ssl));
    }
    final String http2 = config.getProperty("http2");
    if (http2 != null) {
        setHttp2(Boolean.parseBoolean(http2));
    }
    final String deleteBaseOnStartup = config.getProperty("deleteBaseOnStartup");
    if (deleteBaseOnStartup != null) {
        setDeleteBaseOnStartup(Boolean.parseBoolean(deleteBaseOnStartup));
    }
    final String webResourceCached = config.getProperty("webResourceCached");
    if (webResourceCached != null) {
        setWebResourceCached(Boolean.parseBoolean(webResourceCached));
    }
    final String withEjbRemote = config.getProperty("withEjbRemote");
    if (withEjbRemote != null) {
        setWithEjbRemote(Boolean.parseBoolean(withEjbRemote));
    }
    final String deployOpenEjbApp = config.getProperty("deployOpenEjbApp");
    if (deployOpenEjbApp != null) {
        setDeployOpenEjbApp(Boolean.parseBoolean(deployOpenEjbApp));
    }
    final String keystoreFile = config.getProperty("keystoreFile");
    if (keystoreFile != null) {
        setKeystoreFile(keystoreFile);
    }
    final String keystorePass = config.getProperty("keystorePass");
    if (keystorePass != null) {
        setKeystorePass(keystorePass);
    }
    final String keystoreType = config.getProperty("keystoreType");
    if (keystoreType != null) {
        setKeystoreType(keystoreType);
    }
    final String clientAuth = config.getProperty("clientAuth");
    if (clientAuth != null) {
        setClientAuth(clientAuth);
    }
    final String keyAlias = config.getProperty("keyAlias");
    if (keyAlias != null) {
        setKeyAlias(keyAlias);
    }
    final String sslProtocol = config.getProperty("sslProtocol");
    if (sslProtocol != null) {
        setSslProtocol(sslProtocol);
    }
    final String webXml = config.getProperty("webXml");
    if (webXml != null) {
        setWebXml(webXml);
    }
    final String tempDir = config.getProperty("tempDir");
    if (tempDir != null) {
        setTempDir(tempDir);
    }
    final String customWebResources = config.getProperty("customWebResources");
    if (customWebResources != null) {
        setCustomWebResources(customWebResources);
    }
    final String classesFilterType = config.getProperty("classesFilter");
    if (classesFilterType != null) {
        try {
            setClassesFilter(Filter.class.cast(Thread.currentThread().getContextClassLoader().loadClass(classesFilterType).newInstance()));
        } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            throw new IllegalArgumentException(e);
        }
    }
    final String conf = config.getProperty("conf");
    if (conf != null) {
        setConf(conf);
    }
    for (final String prop : config.stringPropertyNames()) {
        if (prop.startsWith("properties.")) {
            property(prop.substring("properties.".length()), config.getProperty(prop));
        } else if (prop.startsWith("users.")) {
            user(prop.substring("users.".length()), config.getProperty(prop));
        } else if (prop.startsWith("roles.")) {
            role(prop.substring("roles.".length()), config.getProperty(prop));
        } else if (prop.startsWith("connector.")) {
            // created in container
            property(prop, config.getProperty(prop));
        } else if (prop.equals("realm")) {
            final ObjectRecipe recipe = new ObjectRecipe(config.getProperty(prop));
            for (final String realmConfig : config.stringPropertyNames()) {
                if (realmConfig.startsWith("realm.")) {
                    recipe.setProperty(realmConfig.substring("realm.".length()), config.getProperty(realmConfig));
                }
            }
            setRealm(Realm.class.cast(recipe.create()));
        } else if (prop.equals("login")) {
            final ObjectRecipe recipe = new ObjectRecipe(LoginConfigBuilder.class.getName());
            for (final String nestedConfig : config.stringPropertyNames()) {
                if (nestedConfig.startsWith("login.")) {
                    recipe.setProperty(nestedConfig.substring("login.".length()), config.getProperty(nestedConfig));
                }
            }
            loginConfig(LoginConfigBuilder.class.cast(recipe.create()));
        } else if (prop.equals("securityConstraint")) {
            final ObjectRecipe recipe = new ObjectRecipe(SecurityConstaintBuilder.class.getName());
            for (final String nestedConfig : config.stringPropertyNames()) {
                if (nestedConfig.startsWith("securityConstraint.")) {
                    recipe.setProperty(nestedConfig.substring("securityConstraint.".length()), config.getProperty(nestedConfig));
                }
            }
            securityConstaint(SecurityConstaintBuilder.class.cast(recipe.create()));
        } else if (prop.equals("configurationCustomizer.")) {
            final String next = prop.substring("configurationCustomizer.".length());
            if (next.contains(".")) {
                continue;
            }
            final ObjectRecipe recipe = new ObjectRecipe(properties.getProperty(prop + ".class"));
            for (final String nestedConfig : config.stringPropertyNames()) {
                if (nestedConfig.startsWith(prop) && !prop.endsWith(".class")) {
                    recipe.setProperty(nestedConfig.substring(prop.length() + 1), config.getProperty(nestedConfig));
                }
            }
            addCustomizer(ConfigurationCustomizer.class.cast(recipe.create()));
        }
    }
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Filter(org.apache.xbean.finder.filter.Filter) Realm(org.apache.catalina.Realm)

Example 34 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class LazyRealm method instance.

private Realm instance() {
    if (delegate == null) {
        synchronized (this) {
            if (delegate == null) {
                final Object instance;
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                if (container != null && container.getLoader() != null && container.getLoader().getClassLoader() != null) {
                    cl = container.getLoader().getClassLoader();
                }
                final Class<?> clazz;
                try {
                    clazz = cl.loadClass(realmClass);
                } catch (final ClassNotFoundException e) {
                    throw new TomEERuntimeException(e);
                }
                if (!cdi) {
                    try {
                        final ObjectRecipe recipe = new ObjectRecipe(clazz);
                        recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
                        recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
                        recipe.allow(Option.FIELD_INJECTION);
                        recipe.allow(Option.PRIVATE_PROPERTIES);
                        if (properties != null) {
                            final Properties props = new PropertiesAdapter().unmarshal(properties.trim().replaceAll("\\p{Space}*(\\p{Alnum}*)=", "\n$1="));
                            recipe.setAllProperties(props);
                        }
                        instance = recipe.create();
                    } catch (final Exception e) {
                        throw new TomEERuntimeException(e);
                    }
                } else {
                    final WebBeansContext webBeansContext;
                    try {
                        webBeansContext = WebBeansContext.currentInstance();
                        if (webBeansContext == null) {
                            return null;
                        }
                    } catch (final IllegalStateException ise) {
                        // too early to have a cdi bean, skip these methods - mainly init() but @PostConstruct works then
                        return null;
                    }
                    final BeanManager bm = webBeansContext.getBeanManagerImpl();
                    final Set<Bean<?>> beans = bm.getBeans(clazz);
                    final Bean<?> bean = bm.resolve(beans);
                    if (bean == null) {
                        return null;
                    }
                    creationalContext = bm.createCreationalContext(null);
                    instance = bm.getReference(bean, clazz, creationalContext);
                }
                if (instance == null) {
                    throw new TomEERuntimeException("realm can't be retrieved from cdi");
                }
                if (instance instanceof Realm) {
                    delegate = (Realm) instance;
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                    if (Lifecycle.class.isInstance(delegate)) {
                        if (init) {
                            try {
                                final Lifecycle lifecycle = Lifecycle.class.cast(delegate);
                                lifecycle.init();
                                if (start) {
                                    lifecycle.start();
                                }
                            } catch (final LifecycleException e) {
                            // no-op
                            }
                        }
                    }
                } else {
                    delegate = new LowTypedRealm(instance);
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                }
                for (final PropertyChangeListener listener : support.getPropertyChangeListeners()) {
                    delegate.addPropertyChangeListener(listener);
                }
            }
        }
    }
    return delegate;
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) PropertyChangeListener(java.beans.PropertyChangeListener) Lifecycle(org.apache.catalina.Lifecycle) Properties(java.util.Properties) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) Bean(javax.enterprise.inject.spi.Bean) PropertiesAdapter(org.apache.openejb.config.sys.PropertiesAdapter) WebBeansContext(org.apache.webbeans.config.WebBeansContext) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) BeanManager(javax.enterprise.inject.spi.BeanManager) Realm(org.apache.catalina.Realm)

Example 35 with ObjectRecipe

use of org.apache.xbean.recipe.ObjectRecipe in project tomee by apache.

the class Container method createConnector.

protected Connector createConnector() {
    final Connector connector;
    final Properties properties = configuration.getProperties();
    if (properties != null) {
        final Map<String, String> attributes = new HashMap<>();
        final ObjectRecipe recipe = new ObjectRecipe(Connector.class);
        for (final String key : properties.stringPropertyNames()) {
            if (!key.startsWith("connector.")) {
                continue;
            }
            final String substring = key.substring("connector.".length());
            if (!substring.startsWith("attributes.")) {
                recipe.setProperty(substring, properties.getProperty(key));
            } else {
                attributes.put(substring.substring("attributes.".length()), properties.getProperty(key));
            }
        }
        connector = recipe.getProperties().isEmpty() ? new Connector() : Connector.class.cast(recipe.create());
        for (final Map.Entry<String, String> attr : attributes.entrySet()) {
            connector.setAttribute(attr.getKey(), attr.getValue());
        }
    } else {
        connector = new Connector();
    }
    return connector;
}
Also used : Connector(org.apache.catalina.connector.Connector) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) HashMap(java.util.HashMap) CatalinaProperties(org.apache.catalina.startup.CatalinaProperties) Properties(java.util.Properties) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ObjectRecipe (org.apache.xbean.recipe.ObjectRecipe)35 Map (java.util.Map)14 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 Properties (java.util.Properties)8 NamingException (javax.naming.NamingException)7 OpenEJBException (org.apache.openejb.OpenEJBException)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 SuperProperties (org.apache.openejb.util.SuperProperties)4 MalformedURLException (java.net.MalformedURLException)3 URISyntaxException (java.net.URISyntaxException)3 TreeMap (java.util.TreeMap)3 TimeoutException (java.util.concurrent.TimeoutException)3 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)3 InvalidObjectException (java.io.InvalidObjectException)2 ObjectStreamException (java.io.ObjectStreamException)2 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 DefinitionException (javax.enterprise.inject.spi.DefinitionException)2 DeploymentException (javax.enterprise.inject.spi.DeploymentException)2