Author: mturk
Date: Tue Apr 21 16:53:23 2009
New Revision: 767217

URL: http://svn.apache.org/viewvc?rev=767217&view=rev
Log:
Add UUID class

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/UUID.java
   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/unix/uuid.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/win32/uuid.c   (with props)
    
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUUID.java 
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/configure
    
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java

Added: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/UUID.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/UUID.java?rev=767217&view=auto
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/UUID.java
 (added)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/UUID.java
 Tue Apr 21 16:53:23 2009
@@ -0,0 +1,75 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.runtime.util;
+
+/**
+ * Generates 128-bit UUID
+ */
+public final class UUID
+{
+    private static native byte[] getb0()
+                                    throws UnsupportedOperationException;
+
+    private byte[] uuid;
+
+    /**
+     * Create new {...@code UUID} instance.
+     */
+    public UUID()
+        throws UnsupportedOperationException
+    {
+        uuid = getb0();
+    }
+
+    private String hex(byte val)
+    {
+        String h = Integer.toHexString(val);
+        if (h.length() == 1)
+            h = "0" + h;
+        return h.substring(h.length() - 2);        
+    }
+
+    public String toString()
+    {
+        // TODO: Make more optimized version
+        // using pre-set char[] array.
+        // 
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
+
+        StringBuffer sb = new StringBuffer();
+        sb.append(hex(uuid[0]));
+        sb.append(hex(uuid[1]));
+        sb.append(hex(uuid[2]));
+        sb.append(hex(uuid[3]));
+        sb.append('-');
+        sb.append(hex(uuid[4]));
+        sb.append(hex(uuid[5]));
+        sb.append('-');
+        sb.append(hex(uuid[6]));
+        sb.append(hex(uuid[7]));
+        sb.append('-');
+        sb.append(hex(uuid[8]));
+        sb.append(hex(uuid[9]));
+        sb.append('-');
+        sb.append(hex(uuid[10]));
+        sb.append(hex(uuid[11]));
+        sb.append(hex(uuid[12]));
+        sb.append(hex(uuid[13]));
+        sb.append(hex(uuid[14]));
+        sb.append(hex(uuid[15]));
+        return sb.toString();
+    }
+}

Propchange: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/UUID.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=767217&r1=767216&r2=767217&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Tue Apr 21 
16:53:23 2009
@@ -79,21 +79,25 @@
 LINUX_OBJS= \
        $(SRCDIR)/os/unix/file.$(OBJ) \
        $(SRCDIR)/os/unix/main.$(OBJ) \
+       $(SRCDIR)/os/unix/uuid.$(OBJ) \
        $(SRCDIR)/os/linux/os.$(OBJ)
 
 SOLARIS_OBJS= \
        $(SRCDIR)/os/unix/file.$(OBJ) \
        $(SRCDIR)/os/unix/main.$(OBJ) \
+       $(SRCDIR)/os/unix/uuid.$(OBJ) \
        $(SRCDIR)/os/solaris/os.$(OBJ)
 
 DARWIN_OBJS= \
        $(SRCDIR)/os/unix/file.$(OBJ) \
        $(SRCDIR)/os/unix/main.$(OBJ) \
+       $(SRCDIR)/os/unix/uuid.$(OBJ) \
        $(SRCDIR)/os/darwin/os.$(OBJ)
 
 HPUX_OBJS= \
        $(SRCDIR)/os/unix/file.$(OBJ) \
        $(SRCDIR)/os/unix/main.$(OBJ) \
+       $(SRCDIR)/os/unix/uuid.$(OBJ) \
        $(SRCDIR)/os/hpux/os.$(OBJ)
 
 TEST_OBJS= \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=767217&r1=767216&r2=767217&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Tue Apr 21 
16:53:23 2009
@@ -75,6 +75,7 @@
        $(SRCDIR)/os/win32/main.$(OBJ) \
        $(SRCDIR)/os/win32/os.$(OBJ) \
        $(SRCDIR)/os/win32/syslog.$(OBJ) \
+       $(SRCDIR)/os/win32/uuid.$(OBJ) \
        $(SRCDIR)/os/win32/main.res
 
 TEST_OBJS= \

Modified: commons/sandbox/runtime/trunk/src/main/native/configure
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/configure?rev=767217&r1=767216&r2=767217&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/configure (original)
+++ commons/sandbox/runtime/trunk/src/main/native/configure Tue Apr 21 16:53:23 
2009
@@ -513,7 +513,7 @@
     have_uuid_uuid=`have_include uuid/uuid`
     if [ ".$have_uuid_uuid" = ".1" ]; then
         ok=`have_library uuid`
-        if [ ".ok" = ".1" ]; then
+        if [ ".$ok" = ".1" ]; then
             varadds ldflags "-luuid"
         fi
     fi

Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/uuid.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/uuid.c?rev=767217&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/uuid.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/uuid.c Tue Apr 21 
16:53:23 2009
@@ -0,0 +1,53 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_error.h"
+
+#if HAVE_UUID_UUID_H
+#include <uuid/uuid.h>
+
+ACR_JNI_EXPORT_DECLARE(jbyteArray, util_UUID, getb0)(ACR_JNISTDARGS)
+{
+    jbyteArray rv;
+    uuid_t id;
+
+    rv = (*_E)->NewByteArray(_E, sizeof(uuid_t));
+    if (rv) {
+        uuid_generate(id);
+        (*_E)->SetByteArrayRegion(_E, rv, 0, sizeof(uuid_t), (jbyte *)id);
+    }
+    return rv;
+}
+
+#else
+
+/* TODO: Find HP-UX equivalent for uuid_generate.
+ * We could use APR's EGD, but that's too complex.
+ * However since we'll depend on openssl, we'll have
+ * a decent random number generator.
+ */
+
+ACR_JNI_EXPORT_DECLARE(jbyteArray, util_UUID, getb0)(ACR_JNISTDARGS)
+{
+    UNREFERENCED_O;
+    ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENOTIMPL, 0);
+    return NULL;
+}
+
+#endif

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/uuid.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/uuid.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/uuid.c?rev=767217&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/uuid.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/uuid.c Tue Apr 21 
16:53:23 2009
@@ -0,0 +1,37 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_error.h"
+
+ACR_JNI_EXPORT_DECLARE(jbyteArray, util_UUID, getb0)(ACR_JNISTDARGS)
+{
+    jbyteArray rv;
+    jbyte id[16];
+
+    if (FAILED(UuidCreate((UUID *)id))) {
+        return NULL;
+    }
+
+    rv = (*_E)->NewByteArray(_E, sizeof(id));
+    if (rv) {
+        (*_E)->SetByteArrayRegion(_E, rv, 0, sizeof(id), id);
+    }
+    return rv;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/uuid.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=767217&r1=767216&r2=767217&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java 
(original)
+++ 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java 
Tue Apr 21 16:53:23 2009
@@ -33,6 +33,7 @@
         // Fundamentals
         suite.addTest(TestProperties.suite());
         suite.addTest(TestOS.suite());
+        suite.addTest(TestUUID.suite());
         suite.addTest(TestDirectByteBuffer.suite());
         suite.addTest(TestPrivate.suite());
         suite.addTest(TestFile.suite());

Added: 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUUID.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUUID.java?rev=767217&view=auto
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUUID.java 
(added)
+++ 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUUID.java 
Tue Apr 21 16:53:23 2009
@@ -0,0 +1,54 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.runtime;
+
+import org.apache.commons.runtime.util.UUID;
+import java.lang.System;
+import junit.framework.*;
+
+/**
+ * UUID Test.
+ *
+ */
+public class TestUUID extends TestCase
+{
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(TestUUID.class);
+        return suite;
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        System.loadLibrary("acr");
+    }
+
+    public void testUUID()
+        throws Exception
+    {
+        try {
+            UUID uuid = new UUID();
+            assertNotNull("UUID" + uuid);
+            System.out.println();
+            System.out.println("UUID       " + uuid);
+        } catch (UnsupportedOperationException uo) {
+            // Skip test.
+        }
+    }
+
+}

Propchange: 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUUID.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to