Author: davsclaus
Date: Sun Oct 18 10:01:42 2009
New Revision: 826394

URL: http://svn.apache.org/viewvc?rev=826394&view=rev
Log:
CAMEL-2073: Using JDK UUID so Camel can run on Google App Engine. Applied patch 
with thanks to Martin Krasser.

Added:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java   
(with props)
    
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
   (with props)
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/util/UuidGenerator.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/util/UuidGeneratorTest.java
    
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
    
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/DeferredRequestReplyMap.java
    
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/Requestor.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
 Sun Oct 18 10:01:42 2009
@@ -36,6 +36,7 @@
 import org.apache.camel.spi.Language;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.UuidGenerator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -107,7 +108,7 @@
      * none is provided
      */
     public String getGeneratedFileName(Message message) {
-        return UuidGenerator.generateSanitizedId(message.getMessageId());
+        return StringHelper.sanitize(message.getMessageId());
     }
 
     public GenericFileProcessStrategy<T> getGenericFileProcessStrategy() {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java 
Sun Oct 18 10:01:42 2009
@@ -40,7 +40,6 @@
  */
 public final class DefaultExchange implements Exchange {
 
-    private static final UuidGenerator DEFAULT_ID_GENERATOR = new 
UuidGenerator();
     protected final CamelContext context;
     private Map<String, Object> properties;
     private Message in;
@@ -335,7 +334,7 @@
             answer = in.createExchangeId();
         }
         if (answer == null) {
-            answer = DefaultExchange.DEFAULT_ID_GENERATOR.generateId();
+            answer = UuidGenerator.get().generateUuid();
         }
         return answer;
     }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java
 Sun Oct 18 10:01:42 2009
@@ -42,7 +42,6 @@
  */
 public class DefaultUnitOfWork implements TraceableUnitOfWork, Service {
     private static final transient Log LOG = 
LogFactory.getLog(DefaultUnitOfWork.class);
-    private static final UuidGenerator DEFAULT_ID_GENERATOR = new 
UuidGenerator();
 
     private String id;
     private List<Synchronization> synchronizations;
@@ -148,7 +147,7 @@
 
     public String getId() {
         if (id == null) {
-            id = DEFAULT_ID_GENERATOR.generateId();
+            id = UuidGenerator.get().generateUuid();
         }
         return id;
     }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java 
Sun Oct 18 10:01:42 2009
@@ -33,7 +33,6 @@
  * @version $Revision$
  */
 public abstract class MessageSupport implements Message {
-    private static final UuidGenerator DEFALT_ID_GENERATOR = new 
UuidGenerator();
     private Exchange exchange;
     private Object body;
     private String messageId;
@@ -190,6 +189,6 @@
      * Lets allow implementations to auto-create a messageId
      */
     protected String createMessageId() {
-        return DEFALT_ID_GENERATOR.generateId();
+        return UuidGenerator.get().generateUuid();
     }
 }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceFormatter.java
 Sun Oct 18 10:01:42 2009
@@ -259,8 +259,8 @@
         } else if (showExchangeId || showShortExchangeId) {
             id = getBreadCrumbID(exchange).toString();
             if (showShortExchangeId) {
-                // skip hostname for short exchange id
-                id = id.substring(id.indexOf('/') + 1);
+                // only output last part of id
+                id = id.substring(id.lastIndexOf('-') + 1);
             }
         }
 

Added: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java?rev=826394&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java 
(added)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java 
Sun Oct 18 10:01:42 2009
@@ -0,0 +1,48 @@
+/**
+ * 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.camel.util;
+
+/**
+ * Helper methods for working with Strings. 
+ */
+public final class StringHelper {
+
+    /**
+     * Constructor of utility class should be private.
+     */
+    private StringHelper() {
+    }
+    
+    /**
+     * Ensures that <code>s</code> is friendly for a URL or file system.
+     * 
+     * @param s
+     *            String to be sanitized.
+     * @return sanitized version of <code>s</code>.
+     * @throws NullPointerException
+     *             if <code>s</code> is <code>null</code>.
+     */
+    public static String sanitize(String s) {
+        return s
+            .replace(':', '-')
+            .replace('_', '-')
+            .replace('.', '-')
+            .replace('/', '-')
+            .replace('\\', '-');
+    }
+    
+}

Propchange: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/UuidGenerator.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UuidGenerator.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/UuidGenerator.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/UuidGenerator.java 
Sun Oct 18 10:01:42 2009
@@ -16,103 +16,37 @@
  */
 package org.apache.camel.util;
 
-import java.net.InetAddress;
-import java.net.ServerSocket;
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import java.util.UUID;
 
 /**
  * Generator for Globally unique Strings.
  */
 public class UuidGenerator {
 
-    private static final transient Log LOG = 
LogFactory.getLog(UuidGenerator.class); 
-    private static final String UNIQUE_STUB;
-    private static int instanceCount;
-    private static String hostName;
-    private String seed;
-    private AtomicLong sequence = new AtomicLong();
-
-    static {
-        String stub = "";
-        boolean canAccessSystemProps = true;
-        try {
-            SecurityManager sm = System.getSecurityManager();
-            if (sm != null) {
-                sm.checkPropertiesAccess();
-            }
-        } catch (SecurityException se) {
-            canAccessSystemProps = false;
-        }
-
-        if (canAccessSystemProps) {
-            try {
-                hostName = InetAddress.getLocalHost().getHostName();
-                ServerSocket ss = new ServerSocket(0);
-                stub = "/" + ss.getLocalPort() + "-" + 
System.currentTimeMillis() + "/";
-                Thread.sleep(100);
-                ss.close();
-            } catch (Exception ioe) {
-                LOG.warn("Could not generate unique stub", ioe);
-            }
-        } else {
-            hostName = "localhost";
-            stub = "-1-" + System.currentTimeMillis() + "-";
-        }
-        UNIQUE_STUB = generateSanitizedId(stub);
-    }
-
-    public UuidGenerator(String prefix) {
-        synchronized (UNIQUE_STUB) {
-            this.seed = generateSanitizedId(prefix + UNIQUE_STUB + 
(instanceCount++) + "-");
-        }
-    }
-
-    public UuidGenerator() {
-        this("ID-" + hostName);
+    private static UuidGenerator instance = new UuidGenerator();
+    
+    UuidGenerator() {
     }
 
     /**
-     * As we have to find the hostname as a side-affect of generating a unique
-     * stub, we allow it's easy retrevial here
+     * Returns a UUID generator. The instance returned by this method makes use
+     * of {...@link java.util.UUID#randomUUID()} for generating UUIDs. Other
+     * generation strategies are currently not supported (but maybe added in
+     * future versions).
      * 
-     * @return the local host name
-     */
-    public static String getHostName() {
-        return hostName;
-    }
-
-    /**
-     * Generate a unqiue id
+     * @return a UUID generator singleton.
      */
-    public String generateId() {
-        return this.seed + sequence.getAndIncrement();
+    public static UuidGenerator get() {
+        return instance;
     }
 
     /**
-     * Generate a unique ID - that is friendly for a URL or file system
+     * Generates a UUID string representation.  
      * 
-     * @return a unique id
-     */
-    public String generateSanitizedId() {
-        return generateSanitizedId(generateId());
-    }
-
-    /**
-     * Ensures that the id is friendly for a URL or file system
-     *
-     * @param id the unique id
-     * @return the id as file friendly id
+     * @return a UUID string.
      */
-    public static String generateSanitizedId(String id) {
-        id = id.replace(':', '-');
-        id = id.replace('_', '-');
-        id = id.replace('.', '-');
-        id = id.replace('/', '-');
-        id = id.replace('\\', '-');
-        return id;
+    public String generateUuid() {
+        return UUID.randomUUID().toString();
     }
-
+    
 }

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java?rev=826394&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
 Sun Oct 18 10:01:42 2009
@@ -0,0 +1,38 @@
+/**
+ * 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.camel.util;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit test for StringHelper
+ */
+public class StringHelperTest extends TestCase {
+
+    public void testSimpleSanitized() {
+        String out = StringHelper.sanitize("hello");
+        assertTrue("Should not contain : ", out.indexOf(':') == -1);
+        assertTrue("Should not contain . ", out.indexOf('.') == -1);
+    }
+
+    public void testNotFileFriendlySimpleSanitized() {
+        String out = StringHelper.sanitize("c:\\helloworld");
+        assertTrue("Should not contain : ", out.indexOf(':') == -1);
+        assertTrue("Should not contain . ", out.indexOf('.') == -1);
+    }
+
+}

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/UuidGeneratorTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/UuidGeneratorTest.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/UuidGeneratorTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/UuidGeneratorTest.java
 Sun Oct 18 10:01:42 2009
@@ -23,34 +23,8 @@
  */
 public class UuidGeneratorTest extends TestCase {
 
-    private UuidGenerator generator;
-
-    protected void setUp() throws Exception {
-        generator = new UuidGenerator("unittest");
-    }
-
-    public void testUniqueId() {
-        assertNotSame("Should generate unique ids", generator.generateId(), 
generator.generateId());
-    }
-
-    public void testHostName() {
-        assertNotNull(UuidGenerator.getHostName());
-    }
-
-    public void testSimpleSanitizedId() {
-        String out = UuidGenerator.generateSanitizedId("hello");
-        assertTrue("Should not contain : ", out.indexOf(':') == -1);
-        assertTrue("Should not contain . ", out.indexOf('.') == -1);
-    }
-
-    public void testNotFileFriendlySimpleSanitizedId() {
-        String out = UuidGenerator.generateSanitizedId("c:\\helloworld");
-        assertTrue("Should not contain : ", out.indexOf(':') == -1);
-        assertTrue("Should not contain . ", out.indexOf('.') == -1);
-    }
-
-    public void testSanitizedId() {
-        String out = generator.generateSanitizedId();
+    public void testSanitized() {
+        String out = UuidGenerator.get().generateUuid();
         assertTrue("Should not contain : ", out.indexOf(':') == -1);
         assertTrue("Should not contain . ", out.indexOf('.') == -1);
     }

Modified: 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
 (original)
+++ 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
 Sun Oct 18 10:01:42 2009
@@ -186,7 +186,7 @@
         String correlationId = in.getHeader("JMSCorrelationID", String.class);
 
         if (correlationId == null && !msgIdAsCorrId) {
-            in.setHeader("JMSCorrelationID", getUuidGenerator().generateId());
+            in.setHeader("JMSCorrelationID", 
getUuidGenerator().generateUuid());
         }
 
         final ValueHolder<FutureTask> futureHolder = new 
ValueHolder<FutureTask>();
@@ -420,7 +420,7 @@
 
     public UuidGenerator getUuidGenerator() {
         if (uuidGenerator == null) {
-            uuidGenerator = new UuidGenerator();
+            uuidGenerator = UuidGenerator.get();
         }
         return uuidGenerator;
     }

Modified: 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/DeferredRequestReplyMap.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/DeferredRequestReplyMap.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/DeferredRequestReplyMap.java
 (original)
+++ 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/DeferredRequestReplyMap.java
 Sun Oct 18 10:01:42 2009
@@ -42,7 +42,7 @@
         private Object monitor;
 
         public DeferredMessageSentCallback(DeferredRequestReplyMap map, 
UuidGenerator uuidGenerator, Object monitor) {
-            transitionalID = uuidGenerator.generateId();
+            transitionalID = uuidGenerator.generateUuid();
             this.map = map;
             this.monitor = monitor;
         }

Modified: 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/Requestor.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/Requestor.java?rev=826394&r1=826393&r2=826394&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/Requestor.java
 (original)
+++ 
camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/requestor/Requestor.java
 Sun Oct 18 10:01:42 2009
@@ -261,7 +261,7 @@
 
     public static synchronized UuidGenerator getUuidGenerator() {
         if (uuidGenerator == null) {
-            uuidGenerator = new UuidGenerator();
+            uuidGenerator = UuidGenerator.get();
         }
         return uuidGenerator;
     }


Reply via email to