Commit 1d67d414 authored by Andrew Wright's avatar Andrew Wright Committed by andrew

Added automatic sequence creation

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@3182 b35dd754-fafc-0310-a699-88a17e54d16e
parent 2f059492
......@@ -45,16 +45,17 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class SequenceManager {
private static final String CREATE_ID =
"INSERT INTO jiveID (id, idType) VALUES (1, ?)";
private static final String LOAD_ID =
"SELECT id FROM jiveID WHERE idType=?";
private static final String UPDATE_ID =
"UPDATE jiveID SET id=? WHERE idType=? AND id=?";
private static final String VERIFY_TYPE = "SELECT 1 FROM jiveID WHERE idType = ?";
// Statically startup a sequence manager for each of the sequence counters.
private static Map<Integer,SequenceManager> managers = new ConcurrentHashMap<Integer,SequenceManager>();
private static Map<Integer, SequenceManager> managers = new ConcurrentHashMap<Integer, SequenceManager>();
static {
new SequenceManager(JiveConstants.ROSTER, 5);
......@@ -75,28 +76,24 @@ public class SequenceManager {
else {
// Verify type is valid from the db, if so create an instance for the type
// And return the next unique id
if(isValidType(type)) {
SequenceManager manager = new SequenceManager(type, 1);
return manager.nextUniqueID();
}
throw new IllegalArgumentException("Invalid type");
SequenceManager manager = new SequenceManager(type, 1);
return manager.nextUniqueID();
}
}
/**
* Returns the next id for an object that has defined the annotation {@link JiveID}.
* The JiveID annotation value is the synonymous for the type integer.
*
* <p/>
* The annotation JiveID should contain the id type for the object (the same number you would
* use to call nextID(int type))
*
* <p/>
* Example class definition:
*
* <p/>
* <code>
* \@JiveID(10)
* public class MyClass {
*
* <p/>
* }
* </code>
*
......@@ -107,10 +104,10 @@ public class SequenceManager {
public static long nextID(Object o) {
JiveID id = o.getClass().getAnnotation(JiveID.class);
if(id == null) {
Log.error("Annotation JiveID must be defined in the class "+o.getClass());
if (id == null) {
Log.error("Annotation JiveID must be defined in the class " + o.getClass());
throw new IllegalArgumentException(
"Annotation JiveID must be defined in the class "+o.getClass());
"Annotation JiveID must be defined in the class " + o.getClass());
}
return nextID(id.value());
......@@ -120,7 +117,7 @@ public class SequenceManager {
* Used to set the blocksize of a given SequenceManager. If no SequenceManager has been registered
* for the type we will verify the type is valid and then create a new sequence manager for it.
*
* @param type the type of unique id
* @param type the type of unique id
* @param blockSize how many blocks of ids we should
*/
public static void setBlockSize(int type, int blockSize) {
......@@ -128,12 +125,7 @@ public class SequenceManager {
managers.get(type).blockSize = blockSize;
}
else {
// Verify type is valid from the db, if so create an instance for the type
if(isValidType(type)) {
new SequenceManager(type, blockSize);
} else {
throw new IllegalArgumentException("Invalid type");
}
new SequenceManager(type, blockSize);
}
}
......@@ -197,13 +189,19 @@ public class SequenceManager {
pstmt = con.prepareStatement(LOAD_ID);
pstmt.setInt(1, type);
ResultSet rs = pstmt.executeQuery();
long currentID = 1;
if (!rs.next()) {
throw new SQLException("Loading the current ID failed. The " +
"jiveID table may not be correctly populated.");
rs.close();
pstmt.close();
createNewID(con, type);
}
else {
currentID = rs.getLong(1);
rs.close();
pstmt.close();
}
long currentID = rs.getLong(1);
rs.close();
pstmt.close();
// Increment the id to define our block.
long newID = currentID + blockSize;
......@@ -228,8 +226,14 @@ public class SequenceManager {
abortTransaction = true;
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try {
if (pstmt != null) {
pstmt.close();
}
}
catch (Exception e) {
Log.error(e);
}
DbConnectionManager.closeTransactionConnection(con, abortTransaction);
}
......@@ -246,40 +250,20 @@ public class SequenceManager {
}
}
private void createNewID(Connection con, int type) throws SQLException {
Log.warn("Autocreating jiveID row for type '" + type + "'");
/**
* Checks to seed if the jiveID type listed is valid
*
* @param type jiveID type to check
* @return true if it is valid
*/
private static boolean isValidType(int type) {
boolean isValid = false;
Connection con = null;
// create new ID row
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(VERIFY_TYPE);
pstmt = con.prepareStatement(CREATE_ID);
pstmt.setInt(1, type);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
isValid = true;
}
}
catch (SQLException sqle) {
Log.error(sqle);
pstmt.execute();
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
DbConnectionManager.closeConnection(pstmt, null);
}
return isValid;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment