fix typos and some Java improvements in ClusterManager
[controller.git] / opendaylight / clustering / services_implementation / src / main / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusterManager.java
index 122063abba0e3c92ad38c2af28a6a9e72f1dcf86..83db4144007963a94e503a33345b1e641eadc2a0 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Properties;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
 
 import javax.transaction.HeuristicMixedException;
 import javax.transaction.HeuristicRollbackException;
@@ -67,7 +68,10 @@ public class ClusterManager implements IClusterServices {
     private HashSet<IListenRoleChange> roleChangeListeners;
     private ViewChangedListener cacheManagerListener;
 
-    private static String loopbackAddress = "127.0.0.1";
+    private static String loopbackAddress = InetAddress.getLoopbackAddress().getHostAddress();
+    private static final int gossipRouterPortDefault = 12001;
+    // defaultTransactionTimeout is 60 seconds
+    private static int DEFAULT_TRANSACTION_TIMEOUT = 60;
 
     /**
      * Start a JGroups GossipRouter if we are a supernode. The
@@ -87,12 +91,11 @@ public class ClusterManager implements IClusterServices {
      */
     private GossipRouter startGossiper() {
         boolean amIGossipRouter = false;
-        Integer gossipRouterPortDefault = 12001;
         Integer gossipRouterPort = gossipRouterPortDefault;
         InetAddress gossipRouterAddress = null;
         String supernodes_list = System.getProperty("supernodes",
                 loopbackAddress);
-        StringBuffer sanitized_supernodes_list = new StringBuffer();
+        StringBuilder sanitized_supernodes_list = new StringBuilder();
         List<InetAddress> myAddresses = new ArrayList<InetAddress>();
 
         StringTokenizer supernodes = new StringTokenizer(supernodes_list, ":");
@@ -131,7 +134,7 @@ public class ClusterManager implements IClusterServices {
             try {
                 hostAddr = InetAddress.getByName(host);
             } catch (UnknownHostException ue) {
-                logger.error("Host not known");
+                logger.error("Host {} is not known", host);
                 continue;
             }
             if (host_port.hasMoreTokens()) {
@@ -139,13 +142,12 @@ public class ClusterManager implements IClusterServices {
                 try {
                     port_num = Integer.valueOf(port);
                 } catch (NumberFormatException ne) {
-                    logger
-                            .error("Supplied supernode gossiepr port is not recognized, using standard gossipport");
+                    logger.error("Supplied supernode gossip port is not recognized, using default gossip port {}",
+                                 gossipRouterPortDefault);
                     port_num = gossipRouterPortDefault;
                 }
                 if ((port_num > 65535) || (port_num < 0)) {
-                    logger
-                            .error("Supplied supernode gossip port is outside a valid TCP port range");
+                    logger.error("Supplied supernode gossip port is outside a valid TCP port range");
                     port_num = gossipRouterPortDefault;
                 }
             }
@@ -164,8 +166,7 @@ public class ClusterManager implements IClusterServices {
             if (!sanitized_supernodes_list.toString().equals("")) {
                 sanitized_supernodes_list.append(",");
             }
-            sanitized_supernodes_list.append(hostAddr.getHostAddress() + "["
-                    + port_num + "]");
+            sanitized_supernodes_list.append(hostAddr.getHostAddress()).append("[").append(port_num).append("]");
         }
 
         if (amIGossipRouter) {
@@ -236,6 +237,17 @@ public class ClusterManager implements IClusterServices {
         return res;
     }
 
+    private void exitOnSecurityException(Exception ioe) {
+        Throwable cause = ioe.getCause();
+        while (cause != null) {
+            if (cause instanceof java.lang.SecurityException) {
+                logger.error("Failed Cluster authentication. Stopping Controller...");
+                System.exit(0);
+            }
+            cause = cause.getCause();
+        }
+    }
+
     public void start() {
         this.gossiper = startGossiper();
         if (this.gossiper != null) {
@@ -272,6 +284,7 @@ public class ClusterManager implements IClusterServices {
             logger.error("Stack Trace that raised th exception");
             logger.error("",ioe);
             this.cm = null;
+            exitOnSecurityException(ioe);
             this.stop();
         }
         logger.debug("Cache Manager has value {}", this.cm);
@@ -386,10 +399,12 @@ public class ClusterManager implements IClusterServices {
     @Override
     public boolean existCache(String containerName, String cacheName) {
         EmbeddedCacheManager manager = this.cm;
-        String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
+
         if (manager == null) {
             return false;
         }
+
+        String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
         return manager.cacheExists(realCacheName);
     }
 
@@ -511,6 +526,12 @@ public class ClusterManager implements IClusterServices {
 
     @Override
     public void tbegin() throws NotSupportedException, SystemException {
+        // call tbegin with the default timeout
+        tbegin(DEFAULT_TRANSACTION_TIMEOUT, TimeUnit.SECONDS);
+    }
+
+    @Override
+    public void tbegin(long timeout, TimeUnit unit) throws NotSupportedException, SystemException {
         EmbeddedCacheManager manager = this.cm;
         if (manager == null) {
             throw new IllegalStateException();
@@ -520,6 +541,15 @@ public class ClusterManager implements IClusterServices {
         if (tm == null) {
             throw new IllegalStateException();
         }
+        long timeoutSec = unit.toSeconds(timeout);
+        if((timeoutSec > Integer.MAX_VALUE) || (timeoutSec <= 0)) {
+            // fall back to the default timeout
+            tm.setTransactionTimeout(DEFAULT_TRANSACTION_TIMEOUT);
+        } else {
+            // cast is ok here
+            // as here we are sure that timeoutSec < = Integer.MAX_VALUE.
+            tm.setTransactionTimeout((int) timeoutSec);
+        }
         tm.begin();
     }