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 */ 017 package org.apache.geronimo.samples.daytrader.ejb; 018 019 import javax.ejb.*; 020 import javax.naming.*; 021 022 import org.apache.geronimo.samples.daytrader.util.*; 023 024 import java.util.Collection; 025 import java.util.Iterator; 026 import java.util.HashMap; 027 028 public class KeySequenceBean implements SessionBean { 029 030 private SessionContext context = null; 031 private LocalKeyGenHome keyGenHome = null; 032 private HashMap keyMap = null; 033 034 /* Business methods */ 035 036 public Integer getNextID(String keyName) 037 { 038 039 // First verify we have allocated a block of keys 040 // for this key name 041 // Then verify the allocated block has not been depleted 042 // allocate a new block if necessary 043 if ( keyMap.containsKey(keyName) == false) 044 allocNewBlock(keyName); 045 Collection block = (Collection) keyMap.get(keyName); 046 Iterator ids = block.iterator(); 047 if ( ids.hasNext() == false ) 048 ids = allocNewBlock(keyName).iterator(); 049 //get and return a new unique key 050 Integer nextID = (Integer) ids.next(); 051 if (Log.doTrace()) Log.trace("KeySequenceBean:getNextID - return new PK ID for Entity type: " + keyName + " ID=" + nextID); 052 return nextID; 053 } 054 055 private Collection allocNewBlock(String keyName) 056 { 057 try 058 { 059 060 LocalKeyGen keyGen = null; 061 try 062 { 063 keyGen = keyGenHome.findByPrimaryKeyForUpdate(keyName); 064 } 065 catch (javax.ejb.ObjectNotFoundException onfe ) 066 { 067 // No keys found for this name - create a new one 068 keyGen = keyGenHome.create(keyName); 069 } 070 Collection block = keyGen.allocBlockOfKeys(); 071 keyMap.put(keyName, block); 072 return block; 073 } 074 catch (Exception e) 075 { 076 Log.error(e, "KeySequence:allocNewBlock - failure to allocate new block of keys for Entity type: "+ keyName); 077 throw new EJBException(e); 078 } 079 } 080 081 082 /* Required javax.ejb.SessionBean interface methods */ 083 084 public KeySequenceBean() { 085 } 086 087 public void ejbCreate() throws CreateException { 088 if ( keyGenHome == null) 089 { 090 String error = "KeySequenceBean:ejbCreate() JNDI lookup of KeyGen Home failed\n" + 091 "\n\t keyGenHome="+keyGenHome; 092 Log.error(error); 093 throw new EJBException(error); 094 } 095 } 096 097 public void ejbRemove() { 098 } 099 public void ejbActivate() { 100 } 101 public void ejbPassivate() { 102 } 103 104 public void setSessionContext(SessionContext sc) { 105 try { 106 context = sc; 107 108 InitialContext ic = new InitialContext(); 109 keyGenHome = (LocalKeyGenHome) ic.lookup("java:comp/env/ejb/KeyGen"); 110 keyMap = new HashMap(); 111 112 } catch (NamingException ne) { 113 Log.error("KeySequenceEJB: Lookup of Local KeyGen Home Failed\n", ne); 114 throw new EJBException("KeySequenceEJB: Lookup of Local KeyGen Home Failed\n", ne); 115 } 116 } 117 }