From 665c5f62ff9b6f497063ca9a3e30c2cff8fcbb8c Mon Sep 17 00:00:00 2001
From: Nils Christian Ehmke <nie@informatik.uni-kiel.de>
Date: Thu, 7 Jun 2012 12:54:46 +0200
Subject: [PATCH] Code modifications for the quality tools.

---
 .../beans/application/ThemeSwitcherBean.java  |  2 +-
 .../CurrentAnalysisControllerProjectBean.java |  2 +-
 .../beans/session/CurrentThemeBean.java       |  8 +++--
 .../session/CurrentWorkSpaceProjectBean.java  | 34 +++++++++++++++----
 .../java/kieker/webgui/common/FSManager.java  | 25 ++++++++++----
 .../kieker/webgui/common/PluginFinder.java    |  2 +-
 6 files changed, 54 insertions(+), 19 deletions(-)

diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/application/ThemeSwitcherBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/application/ThemeSwitcherBean.java
index dbf06cc3..e85f9764 100644
--- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/application/ThemeSwitcherBean.java
+++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/application/ThemeSwitcherBean.java
@@ -41,7 +41,7 @@ public final class ThemeSwitcherBean {
 	/**
 	 * A map containing all available themes.
 	 */
-	private final Map<String, String> themes = new TreeMap<String, String>();
+	private final Map<String, String> themes = new TreeMap<String, String>(); // NOPMD (No concurrent access)
 
 	/**
 	 * Creates a new instance of this class.
diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentAnalysisControllerProjectBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentAnalysisControllerProjectBean.java
index 48ea2be7..67a3aaae 100644
--- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentAnalysisControllerProjectBean.java
+++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentAnalysisControllerProjectBean.java
@@ -131,7 +131,7 @@ public class CurrentAnalysisControllerProjectBean {
 	public void instantiateAnalysis() {
 		try {
 			ACManager.getInstance().instantiateAnalysisController(this.projectName);
-		} catch (final NullPointerException e) {
+		} catch (final NullPointerException e) { // NOPMD (Exception is explicitly thrown)
 			CurrentAnalysisControllerProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An error occurred while instantiating the analysis.");
 		} catch (final AnalysisConfigurationException e) {
 			CurrentAnalysisControllerProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An error occurred while instantiating the analysis.");
diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentThemeBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentThemeBean.java
index 99e7a72a..87dcf569 100644
--- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentThemeBean.java
+++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentThemeBean.java
@@ -66,7 +66,7 @@ public final class CurrentThemeBean {
 	 */
 	public CurrentThemeBean() {
 		// Get the parameters within the current context.
-		final Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
+		final Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); // NOPMD (No concurrent access)
 		// Try to find the default theme within the parameters.
 		if (params.containsKey(CurrentThemeBean.KEY_THEME)) {
 			this.theme = params.get(CurrentThemeBean.KEY_THEME);
@@ -76,7 +76,7 @@ public final class CurrentThemeBean {
 		}
 
 		// Try to find the cookie for the theme.
-		final Map<String, Object> cookies = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
+		final Map<String, Object> cookies = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap(); // NOPMD (No concurrent access)
 
 		if (cookies.containsKey(CurrentThemeBean.KEY_COOKIE_THEME)) {
 			this.theme = ((Cookie) cookies.get(CurrentThemeBean.KEY_COOKIE_THEME)).getValue();
@@ -89,7 +89,9 @@ public final class CurrentThemeBean {
 	 * @return The currently used theme.
 	 */
 	public final String getTheme() {
-		return this.theme;
+		synchronized (this) {
+			return this.theme;
+		}
 	}
 
 	/**
diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentWorkSpaceProjectBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentWorkSpaceProjectBean.java
index 327b487d..aea64a1a 100644
--- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentWorkSpaceProjectBean.java
+++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/CurrentWorkSpaceProjectBean.java
@@ -21,6 +21,7 @@
 package kieker.webgui.beans.session;
 
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Modifier;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
@@ -163,7 +164,9 @@ public final class CurrentWorkSpaceProjectBean {
 	 * @return The project for this user.
 	 */
 	public MIProject getProject() {
-		return this.project;
+		synchronized (this) {
+			return this.project;
+		}
 	}
 
 	/**
@@ -524,8 +527,13 @@ public final class CurrentWorkSpaceProjectBean {
 					plugin.getRepositories().add(mConnector);
 				}
 
-			} catch (final Exception ex) {
-				// Inform the user about the fail!
+			} catch (final InstantiationException e) {
+				CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the ports of the plugin.");
+			} catch (final IllegalAccessException e) {
+				CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the ports of the plugin.");
+			} catch (final InvocationTargetException e) {
+				CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the ports of the plugin.");
+			} catch (final NoSuchMethodException e) {
 				CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the ports of the plugin.");
 			}
 		}
@@ -548,8 +556,14 @@ public final class CurrentWorkSpaceProjectBean {
 			final Configuration configuration = repositoryInstance.getCurrentConfiguration();
 
 			repository.getProperties().addAll(this.extractProperties(configuration));
-		} catch (final Exception ex) {
-			// Inform the user about the fail!
+
+		} catch (final InstantiationException e) {
+			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the repository.");
+		} catch (final IllegalAccessException e) {
+			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the repository.");
+		} catch (final InvocationTargetException e) {
+			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the repository.");
+		} catch (final NoSuchMethodException e) {
 			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the repository.");
 		}
 	}
@@ -571,8 +585,14 @@ public final class CurrentWorkSpaceProjectBean {
 			final Configuration configuration = pluginInstance.getCurrentConfiguration();
 
 			plugin.getProperties().addAll(this.extractProperties(configuration));
-		} catch (final Exception ex) {
-			// Inform the user about the fail!
+
+		} catch (final InstantiationException e) {
+			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the plugin.");
+		} catch (final IllegalAccessException e) {
+			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the plugin.");
+		} catch (final InvocationTargetException e) {
+			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the plugin.");
+		} catch (final NoSuchMethodException e) {
 			CurrentWorkSpaceProjectBean.showMessage(FacesMessage.SEVERITY_ERROR, "An errcor occured while loading the properties of the plugin.");
 		}
 	}
diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/common/FSManager.java b/Kieker.WebGUI/src/main/java/kieker/webgui/common/FSManager.java
index 541671ae..248f2f72 100644
--- a/Kieker.WebGUI/src/main/java/kieker/webgui/common/FSManager.java
+++ b/Kieker.WebGUI/src/main/java/kieker/webgui/common/FSManager.java
@@ -170,7 +170,7 @@ public final class FSManager {
 			// Now the empty project file
 			try {
 				AnalysisController.saveToFile(projectFile, emptyProject);
-			} catch (final IOException ex) {
+			} catch (final IOException ex) { // NOPMD (Rethrow to clean the directories)
 				// Something went wrong. Remove the directories and files!
 				final boolean viewDirDeleted = viewDir.delete();
 				final boolean libDirDeleted = libDir.delete();
@@ -478,7 +478,7 @@ public final class FSManager {
 
 			BufferedInputStream in = null;
 			BufferedOutputStream out = null;
-
+			IOException occException = null;
 			try {
 				// Get the streams.
 				in = new BufferedInputStream(srcFile.getInputstream());
@@ -494,13 +494,26 @@ public final class FSManager {
 				}
 			} finally {
 				// Try to make sure that the streams will be closed.
-				if (in != null) {
-					in.close();
+				try {
+					if (in != null) {
+						in.close();
+					}
+				} catch (final IOException ex) {
+					occException = ex;
 				}
-				if (out != null) {
-					out.close();
+
+				try {
+					if (out != null) {
+						out.close();
+					}
+				} catch (final IOException ex) {
+					occException = ex;
 				}
 			}
+			// If something went wrong, rethrow the exception
+			if (occException != null) {
+				throw occException;
+			}
 		}
 
 		final MIDependency dependency = this.factory.createDependency();
diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/common/PluginFinder.java b/Kieker.WebGUI/src/main/java/kieker/webgui/common/PluginFinder.java
index 6e04c94f..a41c6e83 100644
--- a/Kieker.WebGUI/src/main/java/kieker/webgui/common/PluginFinder.java
+++ b/Kieker.WebGUI/src/main/java/kieker/webgui/common/PluginFinder.java
@@ -124,7 +124,7 @@ public final class PluginFinder {
 					// Try to find a class with the same name and put it into our list
 					final Class<?> c = classLoader.loadClass(name);
 					result.add(c);
-				} catch (final Throwable ex) { // NOCS (IllegalCatchCheck)
+				} catch (final Throwable ex) { // NOPMD (Generic throwable and empty catch block) NOCS (IllegalCatchCheck)
 					// Ignore error.
 				}
 			}
-- 
GitLab