001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.server.j2ee.ejb;
046:
047: import org.apache.commons.logging.Log;
048: import org.obe.OBERuntimeException;
049: import org.obe.spi.service.ServerConfig;
050:
051: import javax.ejb.CreateException;
052: import javax.ejb.FinderException;
053: import javax.ejb.ObjectNotFoundException;
054:
055: /**
056: * Manages persistent sequences of unique database identifiers.
057: *
058: * @author Adrian Price
059: * @ejb:bean type="CMP"
060: * cmp-version="2.x"
061: * name="Sequence"
062: * display-name="OBE Sequence"
063: * jndi-name="org/obe/ejb/Sequence"
064: * local-jndi-name="org/obe/ejb/SequenceLocal"
065: * primkey-field="name"
066: * reentrant="False"
067: * schema="SEQUENCE"
068: * transaction-type="Container"
069: * view-type="local"
070: * @ejb:home local-class="org.obe.server.j2ee.ejb.SequenceLocalHome"
071: * local-extends="javax.ejb.EJBLocalHome"
072: * @ejb:interface local-class="org.obe.server.j2ee.ejb.SequenceLocal"
073: * local-extends="javax.ejb.EJBLocalObject"
074: * @ejb:persistence table-name="OBESEQUENCE"
075: * @ejb:pk class="java.lang.String"
076: * unchecked="true"
077: * @ejb:permission unchecked="true"
078: * @ejb:transaction type="RequiresNew"
079: * @weblogic:transaction-isolation ${transaction.isolation}
080: * @ejb:finder signature="java.util.Collection findAll()"
081: * query="SELECT OBJECT(s) FROM SEQUENCE AS s"
082: * result-type-mapping="Local"
083: * unchecked="true"
084: * @ejb:resource-ref res-name="jdbc/TxDataSource"
085: * res-type="javax.sql.DataSource"
086: * res-auth="Container"
087: * @jboss:resource-manager res-man-class="javax.sql.DataSource"
088: * res-man-name="jdbc/TxDataSource"
089: * res-man-jndi-name="java:/${xdoclet.DataSource}"
090: * @weblogic:resource-description res-ref-name="jdbc/TxDataSource"
091: * jndi-name="${xdoclet.DataSource}"
092: * @weblogic:data-source-name java:comp/env/jdbc/TxDataSource
093: * @jboss:create-table false
094: * @weblogic:pool max-beans-in-free-pool="10"
095: * initial-beans-in-free-pool="0"
096: * @weblogic:cache max-beans-in-cache="10"
097: * idle-timeout-seconds="0"
098: * concurrency-strategy="Database"
099: * @weblogic:persistence delay-updates-until-end-of-tx="False"
100: * @jboss:tuned-updates tune="false"
101: */
102: public abstract class SequenceEJB extends AbstractEntityEJB {
103: private static final long serialVersionUID = -8051182617737269778L;
104: private static final Log _logger = getLogger(SequenceEJB.class);
105:
106: public SequenceEJB() {
107: }
108:
109: /**
110: * @ejb:create-method
111: */
112: public String ejbCreate(String sequenceName, long maxkey)
113: throws CreateException {
114:
115: if (_logger.isDebugEnabled() && ServerConfig.isVerbose()) {
116: _logger.debug("ejbCreate(" + sequenceName + ", " + maxkey
117: + ')');
118: }
119:
120: setName(sequenceName);
121: setMaxKey(maxkey);
122:
123: return null;
124: }
125:
126: public void ejbPostCreate(String sequenceName, long maxkey) {
127: if (_logger.isDebugEnabled() && ServerConfig.isVerbose()) {
128: _logger.debug("ejbPostCreate(" + sequenceName + ", "
129: + maxkey + ')');
130: }
131: }
132:
133: public void ejbRemove() {
134: if (_logger.isDebugEnabled() && ServerConfig.isVerbose()) {
135: _logger.debug("ejbRemove(pk = '" + getName() + "')");
136: }
137:
138: // Reset the associated in-memory sequence object.
139: Sequence seq = Sequence.get(getName());
140: if (seq != null)
141: seq.reset();
142: }
143:
144: /**
145: * Allocates a new block of unique sequence numbers.
146: *
147: * @param newid Specifies the sequence name and tracks allocations.
148: * @ejb:home-method
149: */
150: public void ejbHomeReallocate(Sequence newid)
151: throws CreateException {
152: String tablename = newid.getSequenceName();
153: SequenceLocalHome seqHome = (SequenceLocalHome) _ctx
154: .getEJBLocalHome();
155: SequenceLocal seq;
156: try {
157: seq = seqHome.findByPrimaryKey(tablename);
158: long maxkey = seq.getMaxKey();
159: newid.setLastkey(maxkey);
160: maxkey += newid.getKeyIncrement();
161: newid.setMaxkey(maxkey);
162: seq.setMaxKey(maxkey);
163: } catch (ObjectNotFoundException e) {
164: // Sequence does not yet exist, so we must create it now.
165: seqHome.create(tablename, newid.getKeyIncrement());
166: newid.setLastkey(0);
167: newid.setMaxkey(newid.getKeyIncrement());
168: } catch (FinderException e) {
169: throw new OBERuntimeException(e);
170: }
171: }
172:
173: /**
174: * @ejb:interface-method
175: * @ejb:persistence column-name="NAME"
176: */
177: public abstract String getName();
178:
179: /**
180: * @ejb:persistence column-name="NAME"
181: */
182: public abstract void setName(String name);
183:
184: /**
185: * @ejb:interface-method
186: * @ejb:persistence column-name="MAXKEY"
187: */
188: public abstract long getMaxKey();
189:
190: /**
191: * @ejb:interface-method
192: * @ejb:persistence column-name="MAXKEY"
193: */
194: public abstract void setMaxKey(long key);
195:
196: protected final Log getLogger() {
197: return _logger;
198: }
199: }
|