Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.core.stateful;
   18   
   19   import java.io.Serializable;
   20   import java.util.Map;
   21   import java.util.HashMap;
   22   import javax.transaction.Transaction;
   23   import javax.persistence.EntityManagerFactory;
   24   import javax.persistence.EntityManager;
   25   
   26   import org.apache.openejb.util.Index;
   27   
   28   public class BeanEntry implements Serializable {
   29       private static final long serialVersionUID = 5940667199866151048L;
   30   
   31       protected final Object bean;
   32       protected final Object primaryKey;
   33       protected boolean inQueue = false;
   34       private long timeStamp;
   35       protected long timeOutInterval;
   36       protected transient Transaction beanTransaction;
   37       // todo if we keyed by an entity manager factory id we would not have to make this transient and rebuild the index below
   38       // This would require that we crete an id and that we track it
   39       // alternatively, we could use ImmutableArtifact with some read/write replace magic
   40       private transient Map<EntityManagerFactory, EntityManager> entityManagers;
   41       private EntityManager[] entityManagerArray;
   42   
   43       protected BeanEntry(Object beanInstance, Object primKey, long timeOut) {
   44           bean = beanInstance;
   45           primaryKey = primKey;
   46           beanTransaction = null;
   47           timeStamp = System.currentTimeMillis();
   48           timeOutInterval = timeOut;
   49       }
   50   
   51       protected BeanEntry(BeanEntry prototype) {
   52           bean = prototype.bean;
   53           primaryKey = prototype.primaryKey;
   54           beanTransaction = null;
   55           timeStamp = prototype.timeStamp;
   56           timeOutInterval = prototype.timeOutInterval;
   57       }
   58       
   59       protected boolean isTimedOut() {
   60           if (timeOutInterval == 0) {
   61               return false;
   62           }
   63           long now = System.currentTimeMillis();
   64           return (now - timeStamp) > timeOutInterval;
   65       }
   66   
   67       protected void resetTimeOut() {
   68           if (timeOutInterval > 0) {
   69               timeStamp = System.currentTimeMillis();
   70           }
   71       }
   72   
   73       public Map<EntityManagerFactory, EntityManager> getEntityManagers(Index<EntityManagerFactory, Map> factories) {
   74           if (entityManagers == null && entityManagerArray != null) {
   75               entityManagers = new HashMap<EntityManagerFactory, EntityManager>();
   76               for (int i = 0; i < entityManagerArray.length; i++) {
   77                   EntityManagerFactory entityManagerFactory = factories.getKey(i);
   78                   EntityManager entityManager = entityManagerArray[i];
   79                   entityManagers.put(entityManagerFactory, entityManager);
   80               }
   81           }
   82           return entityManagers;
   83       }
   84   
   85       public void setEntityManagers(Index<EntityManagerFactory, EntityManager> entityManagers) {
   86           this.entityManagers = entityManagers;
   87           if (entityManagers != null) {
   88               entityManagerArray = entityManagers.values().toArray(new EntityManager[entityManagers.size()]);
   89           } else {
   90               entityManagerArray = null;
   91           }
   92       }
   93   
   94       public Object getPrimaryKey() {
   95           return primaryKey;
   96       }
   97   
   98       public Object getBean() {
   99           return bean;
  100       }
  101   
  102   }

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