All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openejb.core.stateful.StatefulContainerFactory Maven / Gradle / Ivy

There is a newer version: 4.7.5
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.openejb.core.stateful;

import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.Map.Entry;

import org.apache.openejb.spi.SecurityService;
import org.apache.openejb.util.Duration;
import org.apache.xbean.recipe.ObjectRecipe;
import org.apache.xbean.recipe.Option;

public class StatefulContainerFactory {
    private Object id;
    private SecurityService securityService;
    private Cache cache;
    private Properties properties = new Properties();
    private Duration accessTimeout = new Duration(0, TimeUnit.MILLISECONDS);

    public Object getId() {
        return id;
    }

    public void setId(Object id) {
        this.id = id;
    }

    public SecurityService getSecurityService() {
        return securityService;
    }

    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }

    public Duration getAccessTimeout() {
        return accessTimeout;
    }

    public void setAccessTimeout(Duration accessTimeout) {
        this.accessTimeout = accessTimeout;
    }

    public Cache getCache() {
        return cache;
    }

    public void setCache(Cache cache) {
        this.cache = cache;
    }

    public void setCache(String s) {
        properties.put("Cache", s);
    }

    public void setPassivator(String s) {
        properties.put("Passivator", s);
    }

    public void setTimeOut(String s) {
        properties.put("TimeOut", s);
    }

    public void setCapacity(String s) {
        properties.put("Capacity", s);
    }

    public void setBulkPassivate(String s) {
        properties.put("BulkPassivate", s);
    }

    public void setFrequency(String s) {
        properties.put("Frequency", s);
    }
    
    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties.putAll(properties);
    }

    public StatefulContainer create() throws Exception {
        // if a live cache was not assigned, build one
        if (cache == null) {
            buildCache();
        }
        cache.init();
        return new StatefulContainer(id, securityService, cache, accessTimeout);
    }

    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) cache;
            return;
        }

        // build the object recipe
        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
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader == null) getClass().getClassLoader();
        cache = serviceRecipe.create(classLoader);

        // assign value
        this.cache = (Cache) cache;
    }

    private Object getProperty(String name) {
        for (Entry entry : properties.entrySet()) {
            Object key = entry.getKey();
            if (key instanceof String && name.equalsIgnoreCase((String) key)) {
                return entry.getValue();
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy