You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2003/12/30 23:36:04 UTC
cvs commit: jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid IdentifierUtils.java
psteitz 2003/12/30 14:36:04
Modified: uid/src/java/org/apache/commons/uid IdentifierUtils.java
Log:
Added initialization methods to avoid class initialization exceptions.
Revision Changes Path
1.2 +100 -12 jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierUtils.java
Index: IdentifierUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/uid/src/java/org/apache/commons/uid/IdentifierUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- IdentifierUtils.java 30 Dec 2003 07:29:17 -0000 1.1
+++ IdentifierUtils.java 30 Dec 2003 22:36:04 -0000 1.2
@@ -60,7 +60,13 @@
* identifiers using the identifier generators provided by commons-uid.</p>
*
* <p>This class maintains a static instance of each generator and uses this instance
- * to generate identifiers in response to its corresponding nextXxx() method.</p>
+ * to generate identifiers via its corresponding nextXxx() method.</p>
+ *
+ * <p>When this class is loaded, static instances are initialized using the currently
+ * configured <code>IdentifierGeneratorFactory.</code> If initialization fails,
+ * the static instances will be <code>null</code> and the static identifier generation
+ * methods will throw null pointer exceptions. This will not happen unless a custom
+ * configuration is used and the specified factory class fails to load.</p>
*
* @author Commons-Uid team
* @version $Id$
@@ -70,8 +76,7 @@
/**
* Static {@link IdentifierGeneratorFactory} used to create generator instances
*/
- private static IdentifierGeneratorFactory factory =
- IdentifierGeneratorFactory.newInstance();
+ private static IdentifierGeneratorFactory factory = createFactory();
/**
* <p>Singleton instance of the
@@ -89,7 +94,8 @@
* </ul>
*/
public static final LongIdentifierGenerator LONG_IDENTIFIER_GENERATOR =
- factory.longGenerator();
+ createLongIdentifierGenerator();
+
/**
* <p>Singleton instance of the <code>StringNumericIdentifierFactory</code>.
* </p>
@@ -105,9 +111,9 @@
* <li>...</li>
* </ul>
*/
-
public static final StringIdentifierGenerator STRING_NUMERIC_IDENTIFIER_GENERATOR =
- factory.numericGenerator();
+ createNumericIdentifierGenerator();
+
/**
* <p>Singleton instance of the
* <code>StringAlphanumericIdentifierFactory</code>.</p>
@@ -132,9 +138,9 @@
* <li>...</li>
* </ul>
*/
-
public static final StringIdentifierGenerator STRING_ALPHANUMERIC_IDENTIFIER_GENERATOR =
- factory.alphanumericGenerator();
+ createAlphanumericGenerator();
+
/**
* <p>Singleton instance of the
* <code>StringSessionIdentifierFactory</code>.</p>
@@ -144,9 +150,8 @@
*
* <p>The objects returned are 10 or more base-36 digits.</p>
*/
-
public static final StringIdentifierGenerator STRING_SESSION_IDENTIFIER_GENERATOR =
- factory.sessionIdGenerator();
+ createSessionIdGenerator();
//---------------------------------------------------------------------------------
@@ -217,4 +222,87 @@
return STRING_NUMERIC_IDENTIFIER_GENERATOR.nextStringIdentifier();
}
+ /**
+ * <p>Gets an IdentifierGeneratorFactory instance to use for
+ * initializing the static factory field.</p>
+ *
+ * <p>Returns null if factory creation fails.</p>
+ *
+ * @return a new IdentifierGeneratorFactory
+ */
+ private static IdentifierGeneratorFactory createFactory() {
+ try {
+ return IdentifierGeneratorFactory.newInstance();
+ } catch (Exception ex) {
+ return null;
+ }
+ }
+
+ /**
+ * <p>Gets a LongIdentifierGenerator to initialize
+ * LONG_IDENTIFIER_GENERATOR with.</p>
+ *
+ * <p>Returns null if instance creation fails.</p>
+ *
+ * @return a new LongIdentifierGenerator
+ *
+ */
+ private static LongIdentifierGenerator createLongIdentifierGenerator() {
+ try {
+ return factory.longGenerator(); // will toss NPE if factory is null
+ } catch (Exception ex) {
+ return null;
+ }
+ }
+
+ /**
+ * <p>Gets a StringIdentifierGenerator to initialize
+ * STRING_NUMERIC_IDENTIFIER_GENERATOR with.</p>
+ *
+ * <p>Returns null if instance creation fails.</p>
+ *
+ * @return a new NumericGenerator
+ *
+ */
+ private static StringIdentifierGenerator createNumericIdentifierGenerator() {
+ try {
+ return factory.numericGenerator(); // will toss NPE if factory is null
+ } catch (Exception ex) {
+ return null;
+ }
+ }
+
+ /**
+ * <p>Gets a StringIdentifierGenerator to initialize
+ * STRING_ALPHANUMERIC_IDENTIFIER_GENERATOR with.</p>
+ *
+ * <p>Returns null if instance creation fails.</p>
+ *
+ * @return a new AlphaumericGenerator
+ *
+ */
+ private static StringIdentifierGenerator createAlphanumericGenerator() {
+ try {
+ return factory.alphanumericGenerator(); // will toss NPE if factory is null
+ } catch (Exception ex) {
+ return null;
+ }
+ }
+
+ /**
+ * <p>Gets a StringIdentifierGenerator to initialize
+ * STRING_SESSION_IDENTIFIER_GENERATOR with.</p>
+ *
+ * <p>Returns null if instance creation fails.</p>
+ *
+ * @return a new SessionIdGenerator
+ *
+ */
+ private static StringIdentifierGenerator createSessionIdGenerator() {
+ try {
+ return factory.sessionIdGenerator(); // will toss NPE if factory is null
+ } catch (Exception ex) {
+ return null;
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org