org.jasig.portal
Class ReferenceSequenceGenerator

java.lang.Object
  extended by org.jasig.portal.ReferenceSequenceGenerator
All Implemented Interfaces:
IOIDGenerator, ISequenceGenerator

public class ReferenceSequenceGenerator
extends java.lang.Object
implements ISequenceGenerator

Version:
$Revision: 1.19.4.1 $
Author:
Dan Ellentuck

Nested Class Summary
private  class ReferenceSequenceGenerator.DataIntegrityException
           
 
Field Summary
private static java.lang.String DEFAULT_COUNTER_NAME
           
private static java.lang.String EQ
           
private static int INITIAL_COUNTER_VALUE
           
private static org.apache.commons.logging.Log log
           
private static java.lang.String NAME_COLUMN
           
private static int NO_COUNTER_VALUE
           
private static java.lang.String QUOTE
           
private  java.util.Random rand
           
private static java.lang.String selectCounterSql
           
private static java.lang.String SEQUENCE_TABLE
           
private static java.lang.String updateCounterForIncrementSql
           
private static java.lang.String updateCounterSql
           
private static java.lang.String VALUE_COLUMN
           
 
Constructor Summary
ReferenceSequenceGenerator()
          ReferenceOIDGenerator constructor comment.
 
Method Summary
 void createCounter(java.lang.String tableName)
           
private  void createCounter(java.lang.String tableName, java.sql.Connection conn)
           
private  java.lang.String getCreateCounterSql(java.lang.String table)
           
private  int getCurrentCounterValue(java.lang.String tableName, java.sql.Connection conn)
           
 java.lang.String getNext()
           
 java.lang.String getNext(java.lang.String table)
           
 int getNextInt()
           
 int getNextInt(java.lang.String tableName)
          Increments the counter and returns the incremented value.
private  java.lang.String getSelectCounterSql()
           
private  java.lang.String getUpdateCounterForIncrementSql()
           
private  java.lang.String getUpdateCounterSql()
           
private  int incrementCounter(java.lang.String tableName, int currentCounterValue, java.sql.Connection conn)
          Try to increment the counter for tableName.
private  void primIncrementCounter(java.lang.String tableName, int currentCounterValue, java.sql.Connection conn)
           
 void setCounter(java.lang.String tableName, int newCounterValue)
           
private  void setCounter(java.lang.String tableName, int newCounterValue, java.sql.Connection conn)
           
private static java.lang.String sqlQuote(java.lang.Object o)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log

rand

private java.util.Random rand

SEQUENCE_TABLE

private static java.lang.String SEQUENCE_TABLE

NAME_COLUMN

private static java.lang.String NAME_COLUMN

VALUE_COLUMN

private static java.lang.String VALUE_COLUMN

INITIAL_COUNTER_VALUE

private static int INITIAL_COUNTER_VALUE

NO_COUNTER_VALUE

private static int NO_COUNTER_VALUE

QUOTE

private static java.lang.String QUOTE

EQ

private static java.lang.String EQ

DEFAULT_COUNTER_NAME

private static java.lang.String DEFAULT_COUNTER_NAME

selectCounterSql

private static java.lang.String selectCounterSql

updateCounterSql

private static java.lang.String updateCounterSql

updateCounterForIncrementSql

private static java.lang.String updateCounterForIncrementSql
Constructor Detail

ReferenceSequenceGenerator

public ReferenceSequenceGenerator()
ReferenceOIDGenerator constructor comment.

Method Detail

createCounter

public void createCounter(java.lang.String tableName)
                   throws java.sql.SQLException
Specified by:
createCounter in interface ISequenceGenerator
Parameters:
tableName - java.lang.String
Throws:
java.sql.SQLException

createCounter

private void createCounter(java.lang.String tableName,
                           java.sql.Connection conn)
                    throws java.sql.SQLException
Parameters:
tableName - java.lang.String
Throws:
java.sql.SQLException

getCreateCounterSql

private java.lang.String getCreateCounterSql(java.lang.String table)
Parameters:
table - java.lang.String
Returns:
java.lang.String

getCurrentCounterValue

private int getCurrentCounterValue(java.lang.String tableName,
                                   java.sql.Connection conn)
                            throws java.sql.SQLException
Parameters:
tableName - java.lang.String
conn - java.sql.Connection
Returns:
int
Throws:
java.sql.SQLException

getNext

public java.lang.String getNext()
                         throws java.lang.Exception
Specified by:
getNext in interface IOIDGenerator
Returns:
java.lang.String
Throws:
java.lang.Exception

getNext

public java.lang.String getNext(java.lang.String table)
                         throws java.lang.Exception
Specified by:
getNext in interface IOIDGenerator
Parameters:
table - String
Returns:
java.lang.String
Throws:
java.lang.Exception

getNextInt

public int getNextInt()
               throws java.lang.Exception
Specified by:
getNextInt in interface ISequenceGenerator
Returns:
int
Throws:
java.lang.Exception

getNextInt

public int getNextInt(java.lang.String tableName)
               throws java.lang.Exception
Increments the counter and returns the incremented value. If the counter does not exist, creates and then increments it to verify that it has been created successfully.

Specified by:
getNextInt in interface ISequenceGenerator
Parameters:
tableName - java.lang.String
Returns:
int
Throws:
java.lang.Exception

getSelectCounterSql

private java.lang.String getSelectCounterSql()
Returns:
java.lang.String

getUpdateCounterForIncrementSql

private java.lang.String getUpdateCounterForIncrementSql()
Returns:
java.lang.String

getUpdateCounterSql

private java.lang.String getUpdateCounterSql()
Returns:
java.lang.String

incrementCounter

private int incrementCounter(java.lang.String tableName,
                             int currentCounterValue,
                             java.sql.Connection conn)
                      throws java.lang.Exception
Try to increment the counter for tableName. If we catch a DataIntegrityException -- which probably means some other process is trying to increment the counter at the same time -- sleep for a while and then try again, up to 20 times.

Parameters:
tableName - java.lang.String
currentCounterValue -
conn - java.sql.Connection
Throws:
java.sql.SQLException
java.lang.Exception

primIncrementCounter

private void primIncrementCounter(java.lang.String tableName,
                                  int currentCounterValue,
                                  java.sql.Connection conn)
                           throws java.sql.SQLException
Parameters:
tableName - java.lang.String
currentCounterValue -
conn - java.sql.Connection
Throws:
java.sql.SQLException

setCounter

public void setCounter(java.lang.String tableName,
                       int newCounterValue)
                throws java.lang.Exception
Specified by:
setCounter in interface ISequenceGenerator
Parameters:
tableName - java.lang.String
newCounterValue - int
Throws:
java.lang.Exception

setCounter

private void setCounter(java.lang.String tableName,
                        int newCounterValue,
                        java.sql.Connection conn)
                 throws java.sql.SQLException
Parameters:
tableName - java.lang.String
newCounterValue -
conn - java.sql.Connection
Throws:
java.sql.SQLException

sqlQuote

private static java.lang.String sqlQuote(java.lang.Object o)
Returns:
java.lang.String