You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ff...@apache.org on 2011/04/15 05:02:02 UTC

svn commit: r1092573 - in /servicemix/smx4/nmr/trunk/nmr/core/src: main/java/org/apache/servicemix/nmr/core/ main/java/org/apache/servicemix/nmr/core/util/ test/java/org/apache/servicemix/nmr/core/util/

Author: ffang
Date: Fri Apr 15 03:02:02 2011
New Revision: 1092573

URL: http://svn.apache.org/viewvc?rev=1092573&view=rev
Log:
[SMX4NMR-270]Use ActiveMQUuidGenerator as the default uuid generator as its faster than the JDK UUID generator

Added:
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/InetAddressUtil.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/UuidGenerator.java
    servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/UuidGeneratorTest.java
Modified:
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ClientChannel.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
    servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/InternalEndpointWrapper.java

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ClientChannel.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ClientChannel.java?rev=1092573&r1=1092572&r2=1092573&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ClientChannel.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ClientChannel.java Fri Apr 15 03:02:02 2011
@@ -16,10 +16,9 @@
  */
 package org.apache.servicemix.nmr.core;
 
-import java.util.UUID;
 import java.util.Map;
 import java.util.Collections;
-import java.util.concurrent.Executors;
+
 
 import org.apache.servicemix.nmr.api.Channel;
 import org.apache.servicemix.nmr.api.Endpoint;
@@ -27,6 +26,7 @@ import org.apache.servicemix.nmr.api.Exc
 import org.apache.servicemix.nmr.api.NMR;
 import org.apache.servicemix.nmr.api.internal.InternalChannel;
 import org.apache.servicemix.nmr.api.internal.InternalEndpoint;
+import org.apache.servicemix.nmr.core.util.UuidGenerator;
 import org.apache.servicemix.executors.Executor;
 
 /**
@@ -43,7 +43,7 @@ public class ClientChannel extends Chann
     protected static class ClientEndpoint implements InternalEndpoint {
 
         private InternalChannel channel;
-        private String id = UUID.randomUUID().toString(); 
+        private String id = UuidGenerator.getUUID(); 
 
         public String getId() {
             return id;

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java?rev=1092573&r1=1092572&r2=1092573&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/ExchangeImpl.java Fri Apr 15 03:02:02 2011
@@ -19,7 +19,6 @@ package org.apache.servicemix.nmr.core;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.UUID;
 import java.util.concurrent.Semaphore;
 
 import javax.xml.namespace.QName;
@@ -34,6 +33,7 @@ import org.apache.servicemix.nmr.api.Typ
 import org.apache.servicemix.nmr.api.internal.InternalEndpoint;
 import org.apache.servicemix.nmr.api.internal.InternalExchange;
 import org.apache.servicemix.nmr.core.util.ExchangeUtils;
+import org.apache.servicemix.nmr.core.util.UuidGenerator;
 
 /**
  * The default {@link Exchange} implementation.
@@ -72,7 +72,7 @@ public class ExchangeImpl implements Int
      * @param pattern the pattern of this exchange
      */
     public ExchangeImpl(Pattern pattern) {
-        this.id = UUID.randomUUID().toString();
+        this.id = UuidGenerator.getUUID();
         this.status = Status.Active;
         this.role = Role.Consumer;
         this.pattern = pattern;

Modified: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/InternalEndpointWrapper.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/InternalEndpointWrapper.java?rev=1092573&r1=1092572&r2=1092573&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/InternalEndpointWrapper.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/InternalEndpointWrapper.java Fri Apr 15 03:02:02 2011
@@ -16,7 +16,6 @@
  */
 package org.apache.servicemix.nmr.core;
 
-import java.util.UUID;
 import java.util.Map;
 import java.util.Collections;
 
@@ -25,6 +24,7 @@ import org.apache.servicemix.nmr.api.End
 import org.apache.servicemix.nmr.api.Exchange;
 import org.apache.servicemix.nmr.api.internal.InternalChannel;
 import org.apache.servicemix.nmr.api.internal.InternalEndpoint;
+import org.apache.servicemix.nmr.core.util.UuidGenerator;
 
 /**
  *
@@ -39,7 +39,7 @@ public class InternalEndpointWrapper imp
     public InternalEndpointWrapper(Endpoint endpoint, Map<String,?> metadata) {
         this.endpoint = endpoint;
         this.metadata = metadata;
-        this.id = UUID.randomUUID().toString();
+        this.id = UuidGenerator.getUUID();
     }
 
     public InternalChannel getChannel() {

Added: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/InetAddressUtil.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/InetAddressUtil.java?rev=1092573&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/InetAddressUtil.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/InetAddressUtil.java Fri Apr 15 03:02:02 2011
@@ -0,0 +1,57 @@
+/*
+ * 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.servicemix.nmr.core.util;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+public class InetAddressUtil {
+	/**
+	 * When using the {@link java.net.InetAddress#getHostName()} method in an 
+	 * environment where neither a proper DNS lookup nor an <tt>/etc/hosts</tt> 
+	 * entry exists for a given host, the following exception will be thrown: 
+	 * <code>
+	 * java.net.UnknownHostException: &lt;hostname&gt;: &lt;hostname&gt;
+     *  at java.net.InetAddress.getLocalHost(InetAddress.java:1425)
+     *   ...
+	 * </code>
+	 * Instead of just throwing an UnknownHostException and giving up, this 
+	 * method grabs a suitable hostname from the exception and prevents the 
+	 * exception from being thrown. If a suitable hostname cannot be acquired
+	 * from the exception, only then is the <tt>UnknownHostException</tt> thrown. 
+	 * 
+	 * @return The hostname 
+	 * @throws UnknownHostException
+	 * @see {@link java.net.InetAddress#getLocalHost()}
+	 * @see {@link java.net.InetAddress#getHostName()}
+	 */
+	public static String getLocalHostName() throws UnknownHostException {
+		try {
+			return (InetAddress.getLocalHost()).getHostName();
+		} catch (UnknownHostException uhe) {
+			String host = uhe.getMessage(); // host = "hostname: hostname"
+			if (host != null) {
+				int colon = host.indexOf(':');
+				if (colon > 0) {
+					return host.substring(0, colon);
+				}
+			}
+			throw uhe;
+		}
+	}
+
+}

Added: servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/UuidGenerator.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/UuidGenerator.java?rev=1092573&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/UuidGenerator.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/util/UuidGenerator.java Fri Apr 15 03:02:02 2011
@@ -0,0 +1,141 @@
+/*
+ * 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.servicemix.nmr.core.util;
+
+import java.net.ServerSocket;
+import java.util.concurrent.atomic.AtomicLong;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * {@link org.apache.servicemix.nmr.core.util.UuidGenerator} which is a fast implementation based on
+ * how <a href="http://activemq.apache.org/>Apache ActiveMQ</a> generates its UUID.
+ * <p/>
+ */
+public class UuidGenerator {
+
+	private static final Logger LOG = LoggerFactory.getLogger(UuidGenerator.class);
+    private static final String UNIQUE_STUB;
+    private static int instanceCount;
+    private static String hostName;
+    private String seed;
+    private AtomicLong sequence = new AtomicLong(1);
+    private int length;
+    
+    private static UuidGenerator instance = null;
+    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 = InetAddressUtil.getLocalHostName();
+                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 = stub;
+    }
+
+    /**
+     * Construct an IdGenerator
+     */
+    private UuidGenerator(String prefix) {
+        synchronized (UNIQUE_STUB) {
+        	int hashValue = prefix.hashCode();
+        	if (hashValue < 0) {
+        		hashValue = - hashValue;
+        	}
+            this.seed = hashValue + UNIQUE_STUB + (instanceCount++) + ":";
+            this.seed = generateSanitizedId(this.seed);
+            this.length = this.seed.length() + ("" + Long.MAX_VALUE).length();
+        }
+    }
+
+    private UuidGenerator() {
+        this("ID:" + hostName);
+    }
+
+    /**
+     * As we have to find the hostname as a side-affect of generating a unique
+     * stub, we allow it's easy retrevial here
+     *
+     * @return the local host name
+     */
+
+    public static String getHostName() {
+        return hostName;
+    }
+
+
+    /**
+     * Generate a unqiue id
+     *
+     * @return a unique id
+     */
+
+    private String generateId() {
+        StringBuilder sb = new StringBuilder(length);
+        sb.append(seed);
+        sb.append(sequence.getAndIncrement());
+        return sb.toString();
+    }
+
+    /**
+     * Ensures that the id is friendly for a URL or file system
+     *
+     * @param id the unique id
+     * @return the id as file friendly id
+     */
+    public static String generateSanitizedId(String id) {
+        id = id.replace(':', '-');
+        id = id.replace('_', '-');
+        id = id.replace('.', '-');
+        id = id.replace('/', '-');
+        return id;
+    }
+
+        
+    public static String getUUID() {
+    	return getInstance().generateId();
+    }
+    
+    public static UuidGenerator getInstance() {
+    	if (instance == null) {
+    		instance = new UuidGenerator(); 
+    	}
+    	return instance;
+    }
+  
+}
+

Added: servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/UuidGeneratorTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/UuidGeneratorTest.java?rev=1092573&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/UuidGeneratorTest.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/util/UuidGeneratorTest.java Fri Apr 15 03:02:02 2011
@@ -0,0 +1,29 @@
+/*
+ * 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.servicemix.nmr.core.util;
+
+import junit.framework.TestCase;
+
+public class UuidGeneratorTest extends TestCase {
+	
+	public void testGenerateUUID() throws Exception {
+		String id1 = UuidGenerator.getUUID();
+		String id2 = UuidGenerator.getUUID();
+		assertNotSame(id1, id2);
+	}
+
+}