【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-CloudscapeIDGenerator.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
/* -*- mode: Java; fill-column: 72 -*-
* The contents of this file are subject to the ClickBlocks Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.clickblocks.org
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied, including, but not limited to, the implied warranties of
* merchantability, fitness for a particular purpose and
* non-infringement. See the License for the specific language
* governing rights and limitations under the License.
*
* ClickBlocks, the ClickBlocks logo and combinations thereof are
* trademarks of ClickBlocks, LLC in the United States and other
* countries.
*
* The Initial Developer of the Original Code is ClickBlocks, LLC.
* Portions created by ClickBlocks, LLC are Copyright (C) 2000.
* All Rights Reserved.
*/
/**
* $Log: CloudscapeIDGenerator.java,v $
* Revision 1.3 2000/11/14 12:26:04 mgrand
* Moved to clickblocks packages.
*
* Revision 1.2 2000/11/03 10:26:00 mgrand
* moved to clickblocks package structure.
*
* Revision 1.1.1.1 2000/10/30 01:37:45 mgrand
* reestablished local repository
*/
package org.clickblocks.util;
import org.clickblocks.persistence.JDBCPersistenceManager;
import org.clickblocks.persistence.JDBCTransaction;
import org.clickblocks.persistence.PersistenceManagerFactory;
import org.clickblocks.persistence.PersistenceManagerIF;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
/**
* Cloudscape-specific class to generate unique IDs.
* @author Mark Grand
* @stereotype moment-interval
*/
class CloudscapeIDGenerator extends IDGenerator {
private HashMap sequences = new HashMap();
private JDBCPersistenceManager persistenceManager;
/**
* Each time this goes out to the database, reserve this many id
* numbers.
*/
private static final int SEQUENCE_SIZE = 500;
/**
* Constructor.
* @exception CBSystemException
* If there is a problem.
*/
public CloudscapeIDGenerator() throws CBSystemException {
PersistenceManagerFactory persistFactory;
persistFactory = PersistenceManagerFactory.getInstance();
persistenceManager
= (JDBCPersistenceManager)persistFactory.getPersistenceManager();
// Since this class is specific to cloudScape, we are justified
// to insist that the persistenceManager be the kind for JDBC.
if (!(persistenceManager instanceof JDBCPersistenceManager)) {
String msg = "Cannot use "
+ getClass().getName()
+ " with a persistence manager other than "
+ JDBCPersistenceManager.class.getName();
throw new CBInternalException(toString(), msg);
} // if
} // constructor()
/**
* Return a unique id number.
* @param connection A Database connection to use.
* @param sequenceName The object that this method returns generates
* id numbers that are unique for the given
* sequenceName.
* @exeception CBSystemException
* If there is a problem.
*/
public synchronized long nextID(String sequenceName)
throws CBSystemException {
NumberSequence seq = (NumberSequence)sequences.get(sequenceName);
if (seq==null) {
seq = new NumberSequence();
seq.maxValue = Long.MIN_VALUE;
sequences.put(sequenceName, seq);
} // if null
if (seq.maxValue<=seq.nextValue) {
reserveNewSequence(seq, sequenceName);
} // if seq.maxValue
long nextValue = seq.nextValue;
seq.nextValue++;
return nextValue;
} // nextID(Connection, String)
/**
* Reserve a range of numbers.
*/
private void reserveNewSequence(NumberSeq