1 22 package org.jboss.varia.autonumber; 23 24 import javax.naming.InitialContext ; 25 26 import org.jboss.logging.Logger; 27 28 34 public class AutoNumberFactory 35 { 36 private static final Logger log = Logger.getLogger(AutoNumberFactory.class); 37 38 private static AutoNumberHome autoNumberHome; 39 40 52 public static synchronized Integer getNextInteger(String collectionName) 53 throws ArrayIndexOutOfBoundsException 54 { 55 Integer value = null; 56 AutoNumber autoNumber = null; 57 if (autoNumberHome == null) 58 { 59 try 60 { 61 autoNumberHome = (AutoNumberHome) new InitialContext ().lookup("JBossUtilAutoNumber"); 62 } 63 catch (javax.naming.NamingException 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 e) 74 { 75 try 77 { 78 autoNumber = autoNumberHome.create(collectionName); 79 } 80 catch (javax.ejb.CreateException x) 81 { 82 log.error("operation failed", x); 83 } 84 catch (java.rmi.RemoteException x) 85 { 86 log.error("operation failed", x); 87 } 88 89 try 90 { 91 autoNumber.setValue(new Integer (0)); 92 } 93 catch (java.rmi.RemoteException x) 94 { 95 log.error("operation failed", x); 96 } 97 } 98 catch (java.rmi.RemoteException e) 99 { 100 log.error("operation failed", e); 101 } 102 103 try 104 { 105 value = autoNumber.getValue(); 106 autoNumber.setValue(new Integer (value.intValue() + 1)); 107 } 108 catch (java.rmi.RemoteException e) 109 { 110 log.error("operation failed", e); 111 } 112 113 return value; 114 } 115 116 120 public static synchronized void resetAutoNumber(String collectionName) 121 { 122 setAutoNumber(collectionName, new Integer (0)); 123 } 124 125 130 public static synchronized void setAutoNumber(String collectionName, Integer value) 131 { 132 AutoNumber autoNumber = null; 133 if (autoNumberHome == null) 134 { 135 try 136 { 137 autoNumberHome = (AutoNumberHome) 138 new InitialContext ().lookup("JBossUtilAutoNumber"); 139 } 140 catch (javax.naming.NamingException 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 e) 151 { 152 154 try 155 { 156 autoNumber = autoNumberHome.create(collectionName); 157 } 158 catch (javax.ejb.CreateException x) 159 { 160 log.error("operation failed", x); 161 } 162 catch (java.rmi.RemoteException x) 163 { 164 log.error("operation failed", x); 165 } 166 } 167 catch (java.rmi.RemoteException e) 168 { 169 log.error("operation failed", e); 170 } 171 172 try 173 { 174 autoNumber.setValue(value); 175 } 176 catch (java.rmi.RemoteException e) 177 { 178 log.error("operation failed", e); 179 } 180 } 181 } 182 183 | Popular Tags |