KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > sequence > SequenceManagerInMemoryImpl


1 package org.apache.ojb.broker.util.sequence;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 import org.apache.ojb.broker.PersistenceBroker;
19 import org.apache.ojb.broker.metadata.FieldDescriptor;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * <p>
26  * Very fast in memory sequence manager implementation, only the first
27  * time an id was requested for a class, the manager query the database
28  * for the max id in requested column - all following request were
29  * performed in memory.
30  * </p>
31  * <b>Limitations:</b>
32  * <ul type="disc">
33  * <li>do not use in clustered environments</li>
34  * <li>do not use if other applications generate id's for objects</li>
35  * </ul>
36  *
37  *
38  *
39  * <p>
40  * Implementation configuration properties:
41  * </p>
42  *
43  * <table cellspacing="2" cellpadding="2" border="3" frame="box">
44  * <tr>
45  * <td><strong>Property Key</strong></td>
46  * <td><strong>Property Values</strong></td>
47  * </tr>
48  * <tr>
49  * <td>seq.start</td>
50  * <td>
51  * Set the start index of used sequences (e.g. set 100000, id generation starts with 100001).
52  * Default start index is <em>1</em>.
53  * </td>
54  * </tr>
55  * <tr>
56  * <td>autoNaming</td>
57  * <td>
58  * Default was 'true'. If set 'true' OJB try to build a
59  * sequence name automatic if none found in field-descriptor
60  * and set this generated name as <code>sequence-name</code>
61  * in field-descriptor. If set 'false' OJB throws an exception
62  * if none sequence name was found in field-descriptor.
63  * </td>
64  * </tr>
65  * <tr>
66  * <td>sequenceStart</td>
67  * <td>
68  * <em>Deprecated, use property 'seq.start'.</em> Set the start index
69  * of used sequences (e.g. set 100000, id generation starts with 100001).
70  * Default start index is <em>1</em>.
71  * </td>
72  * </tr>
73  * </table>
74  *
75  * <br/>
76  * <br/>
77  * <br/>
78  *
79  * @see org.apache.ojb.broker.util.sequence.SequenceManager
80  * @see org.apache.ojb.broker.util.sequence.SequenceManagerFactory
81  * @see org.apache.ojb.broker.util.sequence.SequenceManagerHelper
82  *
83  *
84  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
85  * @version $Id: SequenceManagerInMemoryImpl.java,v 1.14.2.3 2005/12/21 22:28:41 tomdz Exp $
86  */

87 public class SequenceManagerInMemoryImpl extends AbstractSequenceManager
88 {
89     protected static Map JavaDoc sequencesDBMap = new HashMap JavaDoc();
90     private long sequenceStart;
91
92     public SequenceManagerInMemoryImpl(PersistenceBroker broker)
93     {
94         super(broker);
95         Long JavaDoc start = SequenceManagerHelper.getSeqStart(getConfigurationProperties());
96         sequenceStart = start != null ? start.longValue() : 1;
97     }
98
99     protected long getUniqueLong(FieldDescriptor field) throws SequenceManagerException
100     {
101         String JavaDoc seqName = calculateSequenceName(field);
102         // we have to be threadsafe
103
synchronized (SequenceManagerInMemoryImpl.class)
104         {
105             // get id for given seq name
106
Long JavaDoc currentId = getSequence(seqName);
107             // check - first time we search for sequence name
108
if (currentId == null)
109             {
110                 long maxKey = SequenceManagerHelper.getMaxForExtent(getBrokerForClass(), field);
111                 maxKey = sequenceStart > maxKey ? sequenceStart : maxKey;
112                 currentId = new Long JavaDoc(maxKey);
113             }
114             currentId = new Long JavaDoc(currentId.longValue() + 1);
115             // put new id back to map
116
addSequence(seqName, currentId);
117             return currentId.intValue();
118         }
119     }
120
121     /**
122      * Returns last used sequence value or <code>null</code> if no sequence
123      * was add for given sequence name.
124      *
125      * @param sequenceName Name of the sequence.
126      * @return Last used sequence value or <code>null</code>
127      */

128     private Long JavaDoc getSequence(String JavaDoc sequenceName)
129     {
130         Long JavaDoc result = null;
131         // now lookup the sequence map for calling DB
132
Map JavaDoc mapForDB = (Map JavaDoc) sequencesDBMap.get(getBrokerForClass()
133                 .serviceConnectionManager().getConnectionDescriptor().getJcdAlias());
134         if(mapForDB != null)
135         {
136             result = (Long JavaDoc) mapForDB.get(sequenceName);
137         }
138         return result;
139     }
140
141     /**
142      * Add new sequence value for sequence name.
143      * @param sequenceName Name of the sequence.
144      * @param seq The sequence value to add.
145      */

146     private void addSequence(String JavaDoc sequenceName, Long JavaDoc seq)
147     {
148         // lookup the sequence map for calling DB
149
String JavaDoc jcdAlias = getBrokerForClass()
150                 .serviceConnectionManager().getConnectionDescriptor().getJcdAlias();
151         Map JavaDoc mapForDB = (Map JavaDoc) sequencesDBMap.get(jcdAlias);
152         if(mapForDB == null)
153         {
154             mapForDB = new HashMap JavaDoc();
155         }
156         mapForDB.put(sequenceName, seq);
157         sequencesDBMap.put(jcdAlias, mapForDB);
158     }
159
160     /**
161      * Remove the sequence for given sequence name.
162      *
163      * @param sequenceName Name of the sequence to remove.
164      */

165     protected void removeSequence(String JavaDoc sequenceName)
166     {
167         // lookup the sequence map for calling DB
168
Map JavaDoc mapForDB = (Map JavaDoc) sequencesDBMap.get(getBrokerForClass()
169                 .serviceConnectionManager().getConnectionDescriptor().getJcdAlias());
170         if(mapForDB != null)
171         {
172             synchronized(SequenceManagerInMemoryImpl.class)
173             {
174                 mapForDB.remove(sequenceName);
175             }
176         }
177     }
178 }
179
Popular Tags