[Remoterengine-commits] r198 - in pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common: . utils

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Oct 2 09:02:35 CEST 2009


Author: ian_long
Date: 2009-10-02 09:02:34 +0200 (Fri, 02 Oct 2009)
New Revision: 198

Added:
   pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/
   pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/FileParser.java
   pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/PIDReporter.java
   pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/ThreadLogger.java
Log:
Utilities refactored out from RemoteREngine_Server

Added: pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/FileParser.java
===================================================================
--- pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/FileParser.java	                        (rev 0)
+++ pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/FileParser.java	2009-10-02 07:02:34 UTC (rev 198)
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2009, Ian Long <ilong at stoatsoftware.com>
+ *
+ * This file is part of the RemoteREngine project
+ *
+ * The RemoteREngine project 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 of the License, or
+ * (at your option) any later version.
+ *
+ * The RemoteREngine project 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 the RemoteREngine project. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.rosuda.REngine.remote.common.utils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.rmi.AccessException;
+import java.util.Vector;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to return the contents of a file as a String
+ * @author $Author$
+ * @version $Rev$ as of $Date$
+ * <p>URL : $HeadURL$
+ */
+public class FileParser {
+	private final Logger logger = LoggerFactory.getLogger(org.rosuda.REngine.remote.common.utils.FileParser.class);
+	
+	/**
+	 * Read the content of the init script as a single string
+	 * @param filePath Path to a local file on the server
+	 * @return String containing the contents of the file
+	 * @throws AccessException
+	 */
+	public String readFile(String filePath) throws AccessException{
+		String[] lines = readLines(filePath);
+		StringBuilder completeFile = new StringBuilder();
+		for (String line : lines) {
+			completeFile.append(line);
+		}
+		return completeFile.toString();
+	}
+	
+	/**
+	 * Read the content of the init script as an array of Strings
+	 * @param filePath Path to a local file on the server
+	 * @return The contents of the file
+	 * @throws AccessException
+	 */
+	public String[] readLines(String filePath) throws AccessException {
+		File file = new File(filePath);
+		logger.debug("Executing: {}",file.getAbsolutePath());
+		InputStream in = null;
+		Vector<String> lines = new Vector<String>();
+		try {
+			BufferedReader reader = new BufferedReader(new FileReader(file));
+			String line = "";
+			while ((line=reader.readLine()) != null) {
+				lines.add(line);
+			}
+			return lines.toArray(new String[0]);
+		} catch (FileNotFoundException e) {
+			String msg = "Init script '"+filePath+"' not found.";
+			logger.error(msg,e);
+			throw new AccessException(msg, e);
+		} catch (IOException e) {
+			String msg = "Error in reading Init script '"+filePath+"', "+e.getMessage();
+			logger.error(msg,e);
+			throw new AccessException(msg, e);
+		} finally{
+			closeInputStream(in);
+		}
+	}	
+	
+	/**
+	 * Close the input stream silently.
+	 * 
+	 * @param in Stream to be closed
+	 */
+	private void closeInputStream(InputStream in) {
+		if (in == null){
+			return;
+		}
+		try {
+			in.close();
+		} catch (IOException e) {
+			logger.error("Error in close inputstream :"+e.getMessage(),e);
+		} finally {
+			in = null;
+		}
+	}
+
+}


Property changes on: pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/FileParser.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Date Revision Author HeadURL Id

Added: pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/PIDReporter.java
===================================================================
--- pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/PIDReporter.java	                        (rev 0)
+++ pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/PIDReporter.java	2009-10-02 07:02:34 UTC (rev 198)
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2009, Ian Long <ilong at stoatsoftware.com>
+ *
+ * This file is part of the RemoteREngine project
+ *
+ * The RemoteREngine project 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 of the License, or
+ * (at your option) any later version.
+ *
+ * The RemoteREngine project 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 the RemoteREngine project. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.rosuda.REngine.remote.common.utils;
+
+import java.lang.management.ManagementFactory;
+import java.util.StringTokenizer;
+
+/**
+ * Class to return an estimate of the JVM's process Id. This implementation is not guaranteed to work
+ * on all OSs - no Java PID solution is, however it appears to work on most common ones.
+ * @author $Author$
+ * @version $Rev$ as of $Date$
+ * <p>URL : $HeadURL$
+ */
+public class PIDReporter {
+	/**
+	 * Method to return an estimate of the JVM's process Id. This implementation is not guaranteed to work
+	 * on all OSs - no Java PID solution is, however it appears to work on most common ones.
+	 * @return ProcessId of the JVM
+	 */
+	public static String getPID() {
+		try {
+			String name = ManagementFactory.getRuntimeMXBean().getName();
+			if (name.indexOf("@") > 0) {
+				StringTokenizer tok = new StringTokenizer(name,"@");
+				return tok.nextToken();
+			} else  {
+				return name;
+			}
+		} catch (Throwable t) {
+			return t.getClass().getName() + ":" + (t.getMessage() != null ? t.getMessage() : "");
+		}
+	}
+}


Property changes on: pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/PIDReporter.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Date Revision Author HeadURL Id

Added: pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/ThreadLogger.java
===================================================================
--- pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/ThreadLogger.java	                        (rev 0)
+++ pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/ThreadLogger.java	2009-10-02 07:02:34 UTC (rev 198)
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2009, Ian Long <ilong at stoatsoftware.com>
+ *
+ * This file is part of the RemoteREngine project
+ *
+ * The RemoteREngine project 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 of the License, or
+ * (at your option) any later version.
+ *
+ * The RemoteREngine project 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 the RemoteREngine project. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.rosuda.REngine.remote.common.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to write out information about all the threads within the JVM to a logger
+ * @author $Author$
+ * @version $Rev$ as of $Date$
+ * <p>URL : $HeadURL$
+ */
+public class ThreadLogger {
+	private final Logger logger = LoggerFactory.getLogger(org.rosuda.REngine.remote.common.utils.ThreadLogger.class);
+
+	/**
+	 * Log information about all the current threads
+	 * @param eventDetails Message to be written into the log prior to the thread information
+	 */
+	public void logAllThreads(String eventDetails) {
+		if (!logger.isDebugEnabled()) return; // All details written to debug so don't execute if not going to be logged
+		logger.debug(eventDetails);
+		ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
+		while (root.getParent() != null) {
+			root = root.getParent();
+		}
+		visit(root, 0);
+	}
+
+	/**
+	 * Loop over all the threads within the thread group and sub groups and write out
+	 * information about them
+	 * @param group Thread group to be reported
+	 * @param level Current level of the thread group
+	 */
+	private void visit(ThreadGroup group, int level) {
+		logger.debug("ThreadGroup: {}",Integer.toString(level));
+		int numThreads = group.activeCount();
+		Thread[] threads = new Thread[numThreads*2];	// Allow head room on the number of threads returned
+		numThreads = group.enumerate(threads, false);
+		while (numThreads >= threads.length) {
+			threads = new Thread[threads.length + 5];
+			numThreads = group.enumerate(threads, false);
+		}
+		
+		for (int i=0; i<numThreads; i++) {
+			// Get thread
+			Thread thread = threads[i];
+			logger.debug(reportThread(thread));
+		}
+		
+		int numGroups = group.activeGroupCount();
+		ThreadGroup[] groups = new ThreadGroup[numGroups*2];
+		numGroups = group.enumerate(groups, false);
+		while (numGroups >= groups.length) {
+			groups = new ThreadGroup[groups.length + 5];
+			numGroups = group.enumerate(groups, false);
+		}
+		for (int i=0; i<numGroups; i++) {
+			visit(groups[i], level+1);
+		}
+	}
+
+	/**
+	 * Helper method to return information about a thread. Returns the following information
+	 * <p>
+	 * <ul>
+	 * <li>Name</li>
+	 * <li>Alive</li>
+	 * <li>State</li>
+	 * <li>isDaemon</li>
+	 * <li>Class.Method(line number)</li>
+	 * <ul>
+	 * @param thread
+	 * @return
+	 */
+	private String reportThread(Thread thread) {
+		StringBuilder details = new StringBuilder(thread.getName());
+		details.append(": Alive? " + Boolean.toString(thread.isAlive()));
+		details.append(" State: " + thread.getState().toString());
+		details.append(" isDaemon? " + Boolean.toString(thread.isDaemon()));
+		StackTraceElement[] stack = thread.getStackTrace();
+		if (stack.length > 0) {
+			details.append(" Location: ");
+			details.append(stack[0].getClassName());
+			details.append(".");
+			details.append(stack[0].getMethodName());
+			details.append("(" + stack[0].getLineNumber() + ")");
+		}
+		return details.toString();
+	}
+
+}


Property changes on: pkg/RemoteREngine/inst/java_src/src/common/org/rosuda/REngine/remote/common/utils/ThreadLogger.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Date Revision Author HeadURL Id



More information about the Remoterengine-commits mailing list