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; ...@@ -45,16 +45,17 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class SequenceManager { public class SequenceManager {
private static final String CREATE_ID =
"INSERT INTO jiveID (id, idType) VALUES (1, ?)";
private static final String LOAD_ID = private static final String LOAD_ID =
"SELECT id FROM jiveID WHERE idType=?"; "SELECT id FROM jiveID WHERE idType=?";
private static final String UPDATE_ID = private static final String UPDATE_ID =
"UPDATE jiveID SET id=? WHERE idType=? AND 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. // 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 { static {
new SequenceManager(JiveConstants.ROSTER, 5); new SequenceManager(JiveConstants.ROSTER, 5);
...@@ -75,28 +76,24 @@ public class SequenceManager { ...@@ -75,28 +76,24 @@ public class SequenceManager {
else { else {
// Verify type is valid from the db, if so create an instance for the type // Verify type is valid from the db, if so create an instance for the type
// And return the next unique id // And return the next unique id
if(isValidType(type)) {
SequenceManager manager = new SequenceManager(type, 1); SequenceManager manager = new SequenceManager(type, 1);
return manager.nextUniqueID(); return manager.nextUniqueID();
} }
throw new IllegalArgumentException("Invalid type");
}
} }
/** /**
* Returns the next id for an object that has defined the annotation {@link JiveID}. * 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. * 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 * The annotation JiveID should contain the id type for the object (the same number you would
* use to call nextID(int type)) * use to call nextID(int type))
* * <p/>
* Example class definition: * Example class definition:
* * <p/>
* <code> * <code>
* \@JiveID(10) * \@JiveID(10)
* public class MyClass { * public class MyClass {
* * <p/>
* } * }
* </code> * </code>
* *
...@@ -107,10 +104,10 @@ public class SequenceManager { ...@@ -107,10 +104,10 @@ public class SequenceManager {
public static long nextID(Object o) { public static long nextID(Object o) {
JiveID id = o.getClass().getAnnotation(JiveID.class); JiveID id = o.getClass().getAnnotation(JiveID.class);
if(id == null) { if (id == null) {
Log.error("Annotation JiveID must be defined in the class "+o.getClass()); Log.error("Annotation JiveID must be defined in the class " + o.getClass());
throw new IllegalArgumentException( 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()); return nextID(id.value());
...@@ -128,12 +125,7 @@ public class SequenceManager { ...@@ -128,12 +125,7 @@ public class SequenceManager {
managers.get(type).blockSize = blockSize; managers.get(type).blockSize = blockSize;
} }
else { else {
// Verify type is valid from the db, if so create an instance for the type
if(isValidType(type)) {
new SequenceManager(type, blockSize); new SequenceManager(type, blockSize);
} else {
throw new IllegalArgumentException("Invalid type");
}
} }
} }
...@@ -197,13 +189,19 @@ public class SequenceManager { ...@@ -197,13 +189,19 @@ public class SequenceManager {
pstmt = con.prepareStatement(LOAD_ID); pstmt = con.prepareStatement(LOAD_ID);
pstmt.setInt(1, type); pstmt.setInt(1, type);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
long currentID = 1;
if (!rs.next()) { if (!rs.next()) {
throw new SQLException("Loading the current ID failed. The " + rs.close();
"jiveID table may not be correctly populated."); pstmt.close();
createNewID(con, type);
} }
long currentID = rs.getLong(1); else {
currentID = rs.getLong(1);
rs.close(); rs.close();
pstmt.close(); pstmt.close();
}
// Increment the id to define our block. // Increment the id to define our block.
long newID = currentID + blockSize; long newID = currentID + blockSize;
...@@ -228,8 +226,14 @@ public class SequenceManager { ...@@ -228,8 +226,14 @@ public class SequenceManager {
abortTransaction = true; abortTransaction = true;
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } try {
catch (Exception e) { Log.error(e); } if (pstmt != null) {
pstmt.close();
}
}
catch (Exception e) {
Log.error(e);
}
DbConnectionManager.closeTransactionConnection(con, abortTransaction); DbConnectionManager.closeTransactionConnection(con, abortTransaction);
} }
...@@ -246,40 +250,20 @@ public class SequenceManager { ...@@ -246,40 +250,20 @@ public class SequenceManager {
} }
} }
private void createNewID(Connection con, int type) throws SQLException {
Log.warn("Autocreating jiveID row for type '" + type + "'");
/** // create new ID row
* 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;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(CREATE_ID);
pstmt = con.prepareStatement(VERIFY_TYPE);
pstmt.setInt(1, type); pstmt.setInt(1, type);
pstmt.execute();
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
isValid = true;
}
}
catch (SQLException sqle) {
Log.error(sqle);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, null);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
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