Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.core.stateful;
   19   
   20   import java.util.Properties;
   21   import java.util.Map.Entry;
   22   
   23   import org.apache.openejb.spi.SecurityService;
   24   import org.apache.xbean.recipe.ObjectRecipe;
   25   import org.apache.xbean.recipe.Option;
   26   
   27   public class StatefulContainerFactory {
   28       private Object id;
   29       private SecurityService securityService;
   30       private Cache<Object, Instance> cache;
   31       private Properties properties = new Properties();
   32   
   33       public Object getId() {
   34           return id;
   35       }
   36   
   37       public void setId(Object id) {
   38           this.id = id;
   39       }
   40   
   41       public SecurityService getSecurityService() {
   42           return securityService;
   43       }
   44   
   45       public void setSecurityService(SecurityService securityService) {
   46           this.securityService = securityService;
   47       }
   48   
   49       public Cache<Object, Instance> getCache() {
   50           return cache;
   51       }
   52   
   53       public void setCache(Cache<Object, Instance> cache) {
   54           this.cache = cache;
   55       }
   56   
   57       public void setCache(String s) {
   58           properties.put("Cache", s);
   59       }
   60   
   61       public void setPassivator(String s) {
   62           properties.put("Passivator", s);
   63       }
   64   
   65       public void setTimeOut(String s) {
   66           properties.put("TimeOut", s);
   67       }
   68   
   69       public void setCapacity(String s) {
   70           properties.put("Capacity", s);
   71       }
   72   
   73       public void setBulkPassivate(String s) {
   74           properties.put("BulkPassivate", s);
   75       }
   76   
   77       public Properties getProperties() {
   78           return properties;
   79       }
   80   
   81       public void setProperties(Properties properties) {
   82           this.properties.putAll(properties);
   83       }
   84   
   85       public StatefulContainer create() throws Exception {
   86           // if a live cache was not assigned, build one
   87           if (cache == null) {
   88               buildCache();
   89           }
   90           return new StatefulContainer(id, securityService, cache);
   91       }
   92   
   93       private void buildCache() throws Exception {
   94           if (properties == null) {
   95               throw new IllegalArgumentException("No cache defined for StatefulContainer " + id);
   96           }
   97   
   98           // get the cache property
   99           Object cache = getProperty("Cache");
  100           if (cache == null) {
  101               throw new IllegalArgumentException("No cache defined for StatefulContainer " + id);
  102           }
  103   
  104           // if property contains a live cache instance, just use it
  105           if (cache instanceof Cache) {
  106               this.cache = (Cache<Object, Instance>) cache;
  107               return;
  108           }
  109   
  110           // build the object recipe
  111           ObjectRecipe serviceRecipe = new ObjectRecipe((String) cache);
  112           serviceRecipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
  113           serviceRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
  114           serviceRecipe.allow(Option.NAMED_PARAMETERS);
  115           serviceRecipe.setAllProperties(properties);
  116   
  117           // invoke recipe
  118           ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  119           if (classLoader == null) getClass().getClassLoader();
  120           cache = serviceRecipe.create(classLoader);
  121   
  122           // assign value
  123           this.cache = (Cache<Object, Instance>) cache;
  124       }
  125   
  126       private Object getProperty(String name) {
  127           for (Entry<Object, Object> entry : properties.entrySet()) {
  128               Object key = entry.getKey();
  129               if (key instanceof String && name.equalsIgnoreCase((String) key)) {
  130                   return entry.getValue();
  131               }
  132           }
  133           return null;
  134       }
  135   }

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [javadoc | source]