001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.core.stateful;
017:
018: import java.io.Serializable;
019: import java.util.Map;
020: import java.util.HashMap;
021: import javax.transaction.Transaction;
022: import javax.persistence.EntityManagerFactory;
023: import javax.persistence.EntityManager;
024:
025: import org.apache.openejb.util.Index;
026:
027: public class BeanEntry implements Serializable {
028: private static final long serialVersionUID = 5940667199866151048L;
029:
030: protected final Object bean;
031: protected final Object primaryKey;
032: protected boolean inQueue = false;
033: private long timeStamp;
034: protected long timeOutInterval;
035: protected transient Transaction beanTransaction;
036: // todo if we keyed by an entity manager factory id we would not have to make this transient and rebuild the index below
037: // This would require that we crete an id and that we track it
038: // alternatively, we could use ImmutableArtifact with some read/write replace magic
039: private transient Map<EntityManagerFactory, EntityManager> entityManagers;
040: private EntityManager[] entityManagerArray;
041:
042: protected BeanEntry(Object beanInstance, Object primKey,
043: long timeOut) {
044: bean = beanInstance;
045: primaryKey = primKey;
046: beanTransaction = null;
047: timeStamp = System.currentTimeMillis();
048: timeOutInterval = timeOut;
049: }
050:
051: protected BeanEntry(BeanEntry prototype) {
052: bean = prototype.bean;
053: primaryKey = prototype.primaryKey;
054: beanTransaction = null;
055: timeStamp = prototype.timeStamp;
056: timeOutInterval = prototype.timeOutInterval;
057: }
058:
059: protected boolean isTimedOut() {
060: if (timeOutInterval == 0) {
061: return false;
062: }
063: long now = System.currentTimeMillis();
064: return (now - timeStamp) > timeOutInterval;
065: }
066:
067: protected void resetTimeOut() {
068: if (timeOutInterval > 0) {
069: timeStamp = System.currentTimeMillis();
070: }
071: }
072:
073: public Map<EntityManagerFactory, EntityManager> getEntityManagers(
074: Index<EntityManagerFactory, Map> factories) {
075: if (entityManagers == null && entityManagerArray != null) {
076: entityManagers = new HashMap<EntityManagerFactory, EntityManager>();
077: for (int i = 0; i < entityManagerArray.length; i++) {
078: EntityManagerFactory entityManagerFactory = factories
079: .getKey(i);
080: EntityManager entityManager = entityManagerArray[i];
081: entityManagers.put(entityManagerFactory, entityManager);
082: }
083: }
084: return entityManagers;
085: }
086:
087: public void setEntityManagers(
088: Index<EntityManagerFactory, EntityManager> entityManagers) {
089: this .entityManagers = entityManagers;
090: if (entityManagers != null) {
091: entityManagerArray = entityManagers.values().toArray(
092: new EntityManager[entityManagers.size()]);
093: } else {
094: entityManagerArray = null;
095: }
096: }
097:
098: public Object getPrimaryKey() {
099: return primaryKey;
100: }
101:
102: public Object getBean() {
103: return bean;
104: }
105:
106: }
|