KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > autonumber > AutoNumberFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.autonumber;
23
24 import javax.naming.InitialContext JavaDoc;
25
26 import org.jboss.logging.Logger;
27
28 /**
29  * AutoNumberFactory can persistently auto number items.
30  *
31  * @author <a HREF="mailto:michel.anke@wolmail.nl">Michel de Groot</a>
32  * @version $Revision: 37459 $
33  */

34 public class AutoNumberFactory
35 {
36    private static final Logger log = Logger.getLogger(AutoNumberFactory.class);
37
38    private static AutoNumberHome autoNumberHome;
39
40    /**
41     * Gets the next key for the given collection.
42     * Note 1: you must deploy EJB AutoNumber
43     * Note 2: the keys are persistent in your database, independent of
44     * the actual table
45     * Note 3: you can only add instances to the collection which have a
46     * key generated by this method, otherwise the keys are not guaranteed
47     * to be unique
48     * Note 4: key values are >= 0
49     * @param collectionName the name of the collection for which you want an autonumber
50     * @throws ArrayIndexOutOfBoundsException if no more numbers are available
51     */

52    public static synchronized Integer JavaDoc getNextInteger(String JavaDoc collectionName)
53       throws ArrayIndexOutOfBoundsException JavaDoc
54    {
55       Integer JavaDoc value = null;
56       AutoNumber autoNumber = null;
57       if (autoNumberHome == null)
58       {
59          try
60          {
61             autoNumberHome = (AutoNumberHome) new InitialContext JavaDoc().lookup("JBossUtilAutoNumber");
62          }
63          catch (javax.naming.NamingException JavaDoc e)
64          {
65             log.error("operation failed", e);
66          }
67       }
68
69       try
70       {
71          autoNumber = autoNumberHome.findByPrimaryKey(collectionName);
72       }
73       catch (javax.ejb.FinderException JavaDoc e)
74       {
75          // autonumber does not exist yet, create one at value 0
76
try
77          {
78             autoNumber = autoNumberHome.create(collectionName);
79          }
80          catch (javax.ejb.CreateException JavaDoc x)
81          {
82             log.error("operation failed", x);
83          }
84          catch (java.rmi.RemoteException JavaDoc x)
85          {
86             log.error("operation failed", x);
87          }
88
89          try
90          {
91             autoNumber.setValue(new Integer JavaDoc(0));
92          }
93          catch (java.rmi.RemoteException JavaDoc x)
94          {
95             log.error("operation failed", x);
96          }
97       }
98       catch (java.rmi.RemoteException JavaDoc e)
99       {
100          log.error("operation failed", e);
101       }
102
103       try
104       {
105          value = autoNumber.getValue();
106          autoNumber.setValue(new Integer JavaDoc(value.intValue() + 1));
107       }
108       catch (java.rmi.RemoteException JavaDoc e)
109       {
110          log.error("operation failed", e);
111       }
112
113       return value;
114    }
115
116    /**
117     * Resets the given autonumber to zero.
118     * Use with extreme care!
119     */

120    public static synchronized void resetAutoNumber(String JavaDoc collectionName)
121    {
122       setAutoNumber(collectionName, new Integer JavaDoc(0));
123    }
124
125    /**
126     * Sets the given autonumber to the given value so that it starts
127     * counting at the given value.
128     * Use with extreme care!
129     */

130    public static synchronized void setAutoNumber(String JavaDoc collectionName, Integer JavaDoc value)
131    {
132       AutoNumber autoNumber = null;
133       if (autoNumberHome == null)
134       {
135          try
136          {
137             autoNumberHome = (AutoNumberHome)
138                new InitialContext JavaDoc().lookup("JBossUtilAutoNumber");
139          }
140          catch (javax.naming.NamingException JavaDoc e)
141          {
142             log.error("operation failed", e);
143          }
144       }
145
146       try
147       {
148          autoNumber = autoNumberHome.findByPrimaryKey(collectionName);
149       }
150       catch (javax.ejb.FinderException JavaDoc e)
151       {
152          // autonumber does not exist yet, create one
153

154          try
155          {
156             autoNumber = autoNumberHome.create(collectionName);
157          }
158          catch (javax.ejb.CreateException JavaDoc x)
159          {
160             log.error("operation failed", x);
161          }
162          catch (java.rmi.RemoteException JavaDoc x)
163          {
164             log.error("operation failed", x);
165          }
166       }
167       catch (java.rmi.RemoteException JavaDoc e)
168       {
169          log.error("operation failed", e);
170       }
171
172       try
173       {
174          autoNumber.setValue(value);
175       }
176       catch (java.rmi.RemoteException JavaDoc e)
177       {
178          log.error("operation failed", e);
179       }
180    }
181 }
182
183
Popular Tags