Author: ips Date: Tue Jan 18 11:54:21 2005 New Revision: 125542 URL: http://svn.apache.org/viewcvs?view=rev&rev=125542 Log: an interface for UUID generation
Added: incubator/hermes/trunk/src/java/org/apache/ws/util/ incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/ incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGenerator.java incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGeneratorFactory.java incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/impl/ incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/impl/JugUuidGenerator.java Added: incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGenerator.java Url: http://svn.apache.org/viewcvs/incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGenerator.java?view=auto&rev=125542 ============================================================================== --- (empty file) +++ incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGenerator.java Tue Jan 18 11:54:21 2005 @@ -0,0 +1,36 @@ +/* + * Copyright 2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ws.util.uuid; + +/** + * Provides the ability to generate Universally Unique IDentifiers (UUIDs). Use [EMAIL PROTECTED] + * UuidGeneratorFactory#createUUIdGenerator()} or [EMAIL PROTECTED] UuidGeneratorFactory#createUUIdGenerator(String)} to obtain + * [EMAIL PROTECTED] UuidGenerator} instances. + * + * @author Ian P. Springer <[EMAIL PROTECTED]> + */ +public interface UuidGenerator +{ + + /** + * Generate a Universally Unique IDentifier (UUID). + * + * @return a Universally Unique IDentifier (UUID) + */ + String generateUUId(); + +} Added: incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGeneratorFactory.java Url: http://svn.apache.org/viewcvs/incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGeneratorFactory.java?view=auto&rev=125542 ============================================================================== --- (empty file) +++ incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/UuidGeneratorFactory.java Tue Jan 18 11:54:21 2005 @@ -0,0 +1,63 @@ +/* + * Copyright 2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ws.util.uuid; + +/** + * A [EMAIL PROTECTED] UuidGenerator} factory. + * + * @author Ian P. Springer <[EMAIL PROTECTED]> + */ +public class UuidGeneratorFactory +{ + + /** + * NOTE: Use string instead of Class to avoid dependency on jug.jar. + */ + private static final String IMPL_CLASSNAME_JUG = "org.apache.ws.util.uuid.impl.JugUuidGenerator"; + + public static final String DEFAULT_IMPL_CLASSNAME = IMPL_CLASSNAME_JUG; + + public static UuidGenerator createUUIdGenerator() throws RuntimeException + { + return createUUIdGenerator( DEFAULT_IMPL_CLASSNAME ); + } + + public static UuidGenerator createUUIdGenerator( Class implClass ) throws RuntimeException + { + try + { + return (UuidGenerator) implClass.newInstance(); + } + catch ( Exception e ) + { + throw new RuntimeException( "Failed to instantiate UUID generator " + implClass.getName() + ".", e ); + } + } + + public static UuidGenerator createUUIdGenerator( String implClassname ) throws RuntimeException + { + try + { + return createUUIdGenerator( Class.forName( implClassname ) ); + } + catch ( Exception e ) + { + throw new RuntimeException( "Failed to instantiate UUID generator " + implClassname + ".", e ); + } + } + +} Added: incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/impl/JugUuidGenerator.java Url: http://svn.apache.org/viewcvs/incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/impl/JugUuidGenerator.java?view=auto&rev=125542 ============================================================================== --- (empty file) +++ incubator/hermes/trunk/src/java/org/apache/ws/util/uuid/impl/JugUuidGenerator.java Tue Jan 18 11:54:21 2005 @@ -0,0 +1,94 @@ +/* + * Copyright 2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ws.util.uuid.impl; + +import org.apache.ws.util.uuid.UuidGenerator; + +import javax.xml.rpc.JAXRPCException; +import java.lang.reflect.Method; + +/** + * A [EMAIL PROTECTED] org.apache.ws.util.uuid.UuidGenerator} implementation that uses JUG, an opensource Java UUID generation + * library. Reflection is utilized to avoid a compile-time dependency on jug.jar. + * + * @author Ian P. Springer <[EMAIL PROTECTED]> + */ +public class JugUuidGenerator implements UuidGenerator +{ + + private static final String JUG_UUIDGENERATOR_CLASSNAME = "org.doomdark.uuid.UUIDGenerator"; + private static final String JUG_WEBSITE_URL = "http://www.doomdark.org/doomdark/proj/jug/"; + + private static Method generateUUIdMethod; + private static Method newInstanceMethod; + + static + { + Class jugUuidGenClass = null; + try + { + jugUuidGenClass = Class.forName( JUG_UUIDGENERATOR_CLASSNAME ); + } + catch ( ClassNotFoundException cnfe ) + { + throw new RuntimeException( + "Java UUID Generator (JUG) " + JUG_UUIDGENERATOR_CLASSNAME + + " not found. Please add jug.jar, from " + + JUG_WEBSITE_URL + + ", to your classpath and restart." ); + } + try + { + newInstanceMethod = jugUuidGenClass.getMethod( "getInstance", new Class[0] ); + generateUUIdMethod = jugUuidGenClass.getMethod( "generateTimeBasedUUID", new Class[0] ); + } + catch ( Exception e ) + { + throw new RuntimeException( + "Fatal error initializing Java UUID Generator (JUG) " + JUG_UUIDGENERATOR_CLASSNAME + "." ); + } + } + + private ThreadLocal JUG_UUIDGENERATOR = + new ThreadLocal() + { + protected synchronized Object initialValue() + { + try + { + return newInstanceMethod.invoke( null, new Object[0] ); + } + catch ( Exception e ) + { + throw new RuntimeException( e ); + } + } + }; + + public String generateUUId() + { + try + { + return generateUUIdMethod.invoke( JUG_UUIDGENERATOR.get(), new Object[0] ).toString(); + } + catch ( Exception e ) + { + throw new JAXRPCException( e ); + } + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
