changeset 1772:b0969b897c25

Change MXBeanConnectionPool to throw MXBeanConnectionException Change MXBeanConnectionPool class to throw a custom MXBeanConnectionException instead of a general Exception. Removed chain of ApplicationExceptions being thrown by com.redhat.thermostat.utils.management.internal.AgentProxyClient.ProcessCreator. PR2044 Reviewed-by: jerboaa, omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-June/014217.html
author Anirudhan Mukundan <amukunda@redhat.com>
date Thu, 25 Jun 2015 11:06:17 -0400
parents 71e3a9005923
children 4fe63570c611
files agent/core/src/main/java/com/redhat/thermostat/agent/utils/management/MXBeanConnectionException.java agent/core/src/main/java/com/redhat/thermostat/agent/utils/management/MXBeanConnectionPool.java agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/AgentProxyClient.java agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/MXBeanConnectionPoolImpl.java agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/MXBeanConnector.java thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/Harvester.java vm-gc/remote-collector-command/src/main/java/com/redhat/thermostat/gc/remote/command/internal/GC.java vm-jmx/agent/src/main/java/com/redhat/thermostat/vm/jmx/agent/internal/JmxBackend.java
diffstat 8 files changed, 92 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/utils/management/MXBeanConnectionException.java	Thu Jun 25 11:06:17 2015 -0400
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2012-2015 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.agent.utils.management;
+
+public class MXBeanConnectionException extends Exception {
+
+    public MXBeanConnectionException() {
+        super();
+    }
+
+    public MXBeanConnectionException(String message) {
+        super(message);
+    }
+
+    public MXBeanConnectionException(Throwable cause) {
+        super(cause);
+    }
+
+    public MXBeanConnectionException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/utils/management/MXBeanConnectionPool.java	Thu Jun 25 15:47:36 2015 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/utils/management/MXBeanConnectionPool.java	Thu Jun 25 11:06:17 2015 -0400
@@ -44,8 +44,8 @@
 @Service
 public interface MXBeanConnectionPool {
 
-    MXBeanConnection acquire(int pid) throws Exception;
+    MXBeanConnection acquire(int pid) throws MXBeanConnectionException;
 
-    void release(int pid, MXBeanConnection connection) throws Exception;
+    void release(int pid, MXBeanConnection connection) throws MXBeanConnectionException;
 }
 
--- a/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/AgentProxyClient.java	Thu Jun 25 15:47:36 2015 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/AgentProxyClient.java	Thu Jun 25 11:06:17 2015 -0400
@@ -44,7 +44,6 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.redhat.thermostat.common.tools.ApplicationException;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
 class AgentProxyClient {
@@ -68,7 +67,7 @@
         this.username = user;
     }
 
-    String getJMXServiceURL() throws IOException, ApplicationException {
+    String getJMXServiceURL() throws IOException {
         // Start the agent proxy
         Process proxy = null;
         Thread errReaderThread = null;
@@ -128,13 +127,13 @@
         }
     }
 
-    private Process startProcess() throws IOException, ApplicationException {
+    private Process startProcess() throws IOException {
         String serverPath = binPath + File.separator + SERVER_NAME;
         return procCreator.createAndRunProcess(new String[] { serverPath, String.valueOf(pid), username });
     }
     
     static class ProcessCreator {
-        Process createAndRunProcess(String[] args) throws IOException, ApplicationException {
+        Process createAndRunProcess(String[] args) throws IOException {
             ProcessBuilder process = new ProcessBuilder(args);
             return process.start();
         }
--- a/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/MXBeanConnectionPoolImpl.java	Thu Jun 25 15:47:36 2015 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/MXBeanConnectionPoolImpl.java	Thu Jun 25 11:06:17 2015 -0400
@@ -43,10 +43,10 @@
 
 import com.redhat.thermostat.agent.utils.ProcDataSource;
 import com.redhat.thermostat.agent.utils.management.MXBeanConnection;
+import com.redhat.thermostat.agent.utils.management.MXBeanConnectionException;
 import com.redhat.thermostat.agent.utils.management.MXBeanConnectionPool;
 import com.redhat.thermostat.agent.utils.username.UserNameUtil;
 import com.redhat.thermostat.common.Pair;
-import com.redhat.thermostat.common.tools.ApplicationException;
 import com.redhat.thermostat.utils.management.internal.ProcessUserInfoBuilder.ProcessUserInfo;
 
 public class MXBeanConnectionPoolImpl implements MXBeanConnectionPool {
@@ -69,18 +69,22 @@
     }
 
     @Override
-    public synchronized MXBeanConnection acquire(int pid) throws Exception {
+    public synchronized MXBeanConnection acquire(int pid) throws MXBeanConnectionException {
         Pair<Integer, MXBeanConnectionImpl> data = pool.get(pid);
         if (data == null) {
             MXBeanConnector connector = null;
             ProcessUserInfo info = userInfoBuilder.build(pid);
             String username = info.getUsername();
             if (username == null) {
-                throw new IOException("Unable to determine owner of " + pid);
+                throw new MXBeanConnectionException("Unable to determine owner of " + pid);
             }
-            connector = creator.create(pid, username, binPath);
-            MXBeanConnectionImpl connection = connector.connect();
-            data = new Pair<Integer, MXBeanConnectionImpl>(1, connection);
+            try {
+                connector = creator.create(pid, username, binPath);
+                MXBeanConnectionImpl connection = connector.connect();
+                data = new Pair<Integer, MXBeanConnectionImpl>(1, connection);
+            } catch (IOException e) {
+                throw new MXBeanConnectionException(e);
+            }
         } else {
             data = new Pair<>(data.getFirst() + 1, data.getSecond());
         }
@@ -89,13 +93,17 @@
     }
 
     @Override
-    public synchronized void release(int pid, MXBeanConnection toRelese) throws Exception {
+    public synchronized void release(int pid, MXBeanConnection toRelease) throws MXBeanConnectionException {
         Pair<Integer, MXBeanConnectionImpl> data = pool.get(pid);
         MXBeanConnectionImpl connection = data.getSecond();
         int usageCount = data.getFirst();
         usageCount--;
         if (usageCount == 0) {
-            connection.close();
+            try {
+                connection.close();
+            } catch (IOException e) {
+                throw new MXBeanConnectionException(e);
+            }
             pool.remove(pid);
         } else {
             data = new Pair<>(usageCount, connection);
@@ -104,7 +112,7 @@
     }
     
     static class ConnectorCreator {
-        public MXBeanConnector create(int pid, String user, File binPath) throws IOException, ApplicationException {
+        public MXBeanConnector create(int pid, String user, File binPath) throws IOException {
             MXBeanConnector connector = new MXBeanConnector(pid, user, binPath);
             return connector;
         }
--- a/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/MXBeanConnector.java	Thu Jun 25 15:47:36 2015 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/utils/management/internal/MXBeanConnector.java	Thu Jun 25 11:06:17 2015 -0400
@@ -44,18 +44,16 @@
 import javax.management.remote.JMXConnectorFactory;
 import javax.management.remote.JMXServiceURL;
 
-import com.redhat.thermostat.common.tools.ApplicationException;
-
 class MXBeanConnector {
     
     private final JMXConnectionCreator jmxCreator;
     private final String serviceURL;
     
-    public MXBeanConnector(int pid, String user, File binPath) throws IOException, ApplicationException {
+    public MXBeanConnector(int pid, String user, File binPath) throws IOException {
         this(new AgentProxyClient(pid, user, binPath), new JMXConnectionCreator());
     }
     
-    MXBeanConnector(AgentProxyClient client, JMXConnectionCreator jmxCreator) throws IOException, ApplicationException {
+    MXBeanConnector(AgentProxyClient client, JMXConnectionCreator jmxCreator) throws IOException {
         this.jmxCreator = jmxCreator;
         this.serviceURL = client.getJMXServiceURL();
     }
--- a/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/Harvester.java	Thu Jun 25 15:47:36 2015 -0400
+++ b/thread/harvester/src/main/java/com/redhat/thermostat/thread/harvester/Harvester.java	Thu Jun 25 11:06:17 2015 -0400
@@ -37,6 +37,7 @@
 package com.redhat.thermostat.thread.harvester;
 
 import com.redhat.thermostat.agent.utils.management.MXBeanConnection;
+import com.redhat.thermostat.agent.utils.management.MXBeanConnectionException;
 import com.redhat.thermostat.agent.utils.management.MXBeanConnectionPool;
 import com.redhat.thermostat.common.SystemClock;
 import com.redhat.thermostat.common.utils.LoggingUtils;
@@ -114,11 +115,11 @@
     private synchronized boolean connect() {
         try {
             connection = connectionPool.acquire(pid);
-        } catch (Exception ex) {
+        } catch (MXBeanConnectionException ex) {
             logger.log(Level.SEVERE, "can't connect", ex);
             return false;
         }
-        
+
         setConnected(true);
         return true;
     }
@@ -154,7 +155,7 @@
 
         try {
             connectionPool.release(pid, connection);
-        } catch (Exception ex) {
+        } catch (MXBeanConnectionException ex) {
             logger.log(Level.SEVERE, "can't disconnect", ex);
             return false;
         }
--- a/vm-gc/remote-collector-command/src/main/java/com/redhat/thermostat/gc/remote/command/internal/GC.java	Thu Jun 25 15:47:36 2015 -0400
+++ b/vm-gc/remote-collector-command/src/main/java/com/redhat/thermostat/gc/remote/command/internal/GC.java	Thu Jun 25 11:06:17 2015 -0400
@@ -42,9 +42,12 @@
 import java.util.logging.Logger;
 
 import com.redhat.thermostat.agent.utils.management.MXBeanConnection;
+import com.redhat.thermostat.agent.utils.management.MXBeanConnectionException;
 import com.redhat.thermostat.agent.utils.management.MXBeanConnectionPool;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
+import javax.management.MalformedObjectNameException;
+
 public class GC {
 
     private static final Logger logger = LoggingUtils.getLogger(GC.class);
@@ -66,15 +69,14 @@
             try {
                 MemoryMXBean bean = connection.createProxy(ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
                 bean.gc();
-
-            } catch (Exception ex) {
+            } catch (MalformedObjectNameException ex) {
                 exceptionInGc = ex;
                 logger.log(Level.SEVERE, "can't get MXBeanConnection connection", ex);
             } finally {
                 pool.release(vmId, connection);
             }
-        } catch (Exception ioe) {
-            exceptionInGc = ioe;
+        } catch (MXBeanConnectionException ex) {
+            exceptionInGc = ex;
         }
 
         if (exceptionInGc != null) {
--- a/vm-jmx/agent/src/main/java/com/redhat/thermostat/vm/jmx/agent/internal/JmxBackend.java	Thu Jun 25 15:47:36 2015 -0400
+++ b/vm-jmx/agent/src/main/java/com/redhat/thermostat/vm/jmx/agent/internal/JmxBackend.java	Thu Jun 25 11:06:17 2015 -0400
@@ -56,6 +56,7 @@
 import com.redhat.thermostat.agent.command.ReceiverRegistry;
 import com.redhat.thermostat.agent.command.RequestReceiver;
 import com.redhat.thermostat.agent.utils.management.MXBeanConnection;
+import com.redhat.thermostat.agent.utils.management.MXBeanConnectionException;
 import com.redhat.thermostat.agent.utils.management.MXBeanConnectionPool;
 import com.redhat.thermostat.backend.BaseBackend;
 import com.redhat.thermostat.common.Clock;
@@ -190,7 +191,7 @@
 
         try {
             pool.release(pid, connection);
-        } catch (Exception e) {
+        } catch (MXBeanConnectionException e) {
             logger.warning("Unable to release mx bean connection");
         }
     }