You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@creadur.apache.org by co...@google.com on 2009/07/20 23:57:34 UTC

[apache-rat-pd] r41 commited - It is possible to pause checking process and resume it after that.

Revision: 41
Author: maka82
Date: Mon Jul 20 14:56:49 2009
Log: It is possible to pause checking process and resume it after that.
http://code.google.com/p/apache-rat-pd/source/detail?r=41

Added:
  /trunk/src/main/java/org/apache/rat/pd/core/PauseListener.java
Modified:
  /trunk/src/main/java/org/apache/rat/pd/core/SourceCodeAnalyser.java

=======================================
--- /dev/null
+++ /trunk/src/main/java/org/apache/rat/pd/core/PauseListener.java	Mon Jul  
20 14:56:49 2009
@@ -0,0 +1,83 @@
+/*
+ *
+ * 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.rat.pd.core;
+
+import java.io.IOException;
+
+/**
+ *
+ * This is the way to implement something like KeyListener in console
+ * applications. This Thread will listen keystroke and if enter is pressed  
it
+ * will call proper methods.
+ *
+ * To use this class it is needed to override and implement it's methods  
onPause
+ * and onResume in derived class (hook design pattern).
+ *
+ *
+ * @author maka
+ *
+ */
+public class PauseListener extends Thread {
+
+	private boolean paused = false;
+
+	@Override
+	public void run() {
+		while (true) {
+			try {
+				sleep(500);
+				// read all input characters and expect enter
+				while (System.in.available() > 0) {
+					System.in.read();
+					// only for last one character we do something
+					if (System.in.available() == 0) {
+						paused = !paused;
+						if (paused)
+							onPause();
+						else
+							onResume();
+					}
+				}
+
+			} catch (IOException e) {
+				// swallow exception
+			} catch (InterruptedException e) {
+				// swallow exception
+			}
+		}
+	}
+
+	/**
+	 * override this function if want to do something when pause event is
+	 * happened
+	 */
+	public void onPause() {
+		// override and implement me
+	}
+
+	/**
+	 * override this function if want to do something when resume event is
+	 * happened
+	 */
+	public void onResume() {
+		// override and implement me
+	}
+}
=======================================
--- /trunk/src/main/java/org/apache/rat/pd/core/SourceCodeAnalyser.java	Sun  
Jul 12 17:28:44 2009
+++ /trunk/src/main/java/org/apache/rat/pd/core/SourceCodeAnalyser.java	Mon  
Jul 20 14:56:49 2009
@@ -48,6 +48,8 @@
  	private final List<IHeuristicChecker> algorithmsForChecking;
  	private final ReportDocument reportDocument;

+	private PauseListener pauseListener;
+	private volatile boolean paused = false;
  	private int progressMessageLength;

  	public SourceCodeAnalyser(List<ISearchEngine> searchEngines,
@@ -56,6 +58,44 @@
  		this.searchEngines = searchEngines;
  		this.algorithmsForChecking = algorithmsForChecking;
  		this.reportDocument = reportDocument;
+
+		initializeKeyListener();
+	}
+
+	/**
+	 * initialize key listener for pause/resume
+	 */
+	private void initializeKeyListener(){
+		this.pauseListener = new PauseListener() {
+			@Override
+			public void onPause() {
+				System.out.println("apache-rat-pd is paused...");
+				System.out
+						.println("to resume application, pres Enter");
+				SourceCodeAnalyser.this.paused = true;
+			}
+
+			@Override
+			public void onResume() {
+				System.out.println("apache-rat-pd resumed...");
+				SourceCodeAnalyser.this.paused = false;
+			}
+
+		};
+		pauseListener.start();
+
+	}
+
+	/**
+	 * this method will make algorithm to sleep until flag paused == true
+	 */
+	private void pauseIfNeeded() {
+		while (paused)
+			try {
+				Thread.currentThread().sleep(1000);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
  	}

  	@Override
@@ -105,6 +145,9 @@

  		while (i < tokens.length) {
  			while (j < tokens.length) {
+				// this method will make application to sleep if it is needed
+				pauseIfNeeded();
+
  				StringBuffer toCheck = combineTokens(tokens, i, j);
  				j++;
  				printProgress(tokens.length, j);