Author: bfoster
Date: Mon Mar 19 18:40:45 2012
New Revision: 1302592
URL: http://svn.apache.org/viewvc?rev=1302592&view=rev
Log:
Port LoggerOutputStream in CAS-PGE to existing LoggerOutputStream in
oodt-commons io package
----------
OODT-423
Added:
oodt/trunk/commons/src/test/org/apache/oodt/commons/io/TestLoggerOutputStream.java
(with props)
Modified:
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/ExecUtils.java
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/LoggerOutputStream.java
Modified:
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/ExecUtils.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/ExecUtils.java?rev=1302592&r1=1302591&r2=1302592&view=diff
==============================================================================
---
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/ExecUtils.java
(original)
+++
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/exec/ExecUtils.java
Mon Mar 19 18:40:45 2012
@@ -14,8 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-
package org.apache.oodt.commons.exec;
//OODT imports
@@ -29,166 +27,135 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
- * @author mattmann
- * @author bfoster
- * @version $Revision$
- *
- * <p>
* Utilities for executing programs.
- * </p>.
+ *
+ * @author mattmann (Chris Mattmann)
+ * @author bfoster (Brian Foster)
*/
public final class ExecUtils {
- private ExecUtils() throws InstantiationException {
- throw new InstantiationException("Don't construct utility classes!");
- }
-
- public static String printCommandLine(String[] args) {
- StringBuffer cmdLine = new StringBuffer();
-
- if (args != null && args.length > 0) {
- for (int i = 0; i < args.length; i++) {
- cmdLine.append(args[i]);
- cmdLine.append(" ");
- }
- }
-
- return cmdLine.toString();
- }
-
- public static int callProgram(String commandLine, Logger logger)
- throws IOException {
- try {
- return callProgram(commandLine,
- new LoggerOutputStream(logger, Level.INFO),
- new LoggerOutputStream(logger, Level.SEVERE),
- null);
- } catch (Exception e) {
- throw new IOException(e);
- }
- }
-
- public static int callProgram(String commandLine, OutputStream
stdOutStream,
- OutputStream stdErrStream) throws IOException {
- return callProgram(commandLine, stdOutStream, stdErrStream, null);
- }
-
- public static int callProgram(String commandLine, Logger logger,
- File workDir) throws IOException {
- try {
- return callProgram(commandLine,
- new LoggerOutputStream(logger, Level.INFO),
- new LoggerOutputStream(logger, Level.SEVERE),
- workDir);
- } catch (Exception e) {
- throw new IOException(e);
- }
- }
-
- public static int callProgram(String commandLine, OutputStream
stdOutStream,
- OutputStream stdErrStream, File workDir) throws IOException {
- Process progProcess = null;
- StreamGobbler errorGobbler = null, outputGobbler = null;
- int returnVal = -1;
- try {
- progProcess = (workDir == null) ? Runtime.getRuntime().exec(
- commandLine) : Runtime.getRuntime().exec(commandLine, null,
- workDir);
- errorGobbler = new StreamGobbler(progProcess.getErrorStream(),
- "ERROR", stdErrStream);
- outputGobbler = new StreamGobbler(progProcess.getInputStream(),
- "OUTPUT", stdOutStream);
- errorGobbler.start();
- outputGobbler.start();
- returnVal = progProcess.waitFor();
- return returnVal;
- } catch (Exception e) {
- e.printStackTrace();
- throw new IOException("Failed to run '" + commandLine
- + "' -- return val = " + returnVal + " : " +
e.getMessage());
- } finally {
- if (errorGobbler != null)
- errorGobbler.stopGobblingAndDie();
- if (outputGobbler != null)
- outputGobbler.stopGobblingAndDie();
- try {
- progProcess.getErrorStream().close();
- } catch (Exception e) {
- }
- try {
- progProcess.getInputStream().close();
- } catch (Exception e) {
- }
- try {
- progProcess.getOutputStream().close();
- } catch (Exception e) {
- }
- }
- }
-
- public static int callProgram(String commandLine, File workDir)
- throws IOException {
- Process p = Runtime.getRuntime().exec(commandLine, null, workDir);
- return processProgram(p);
- }
-
- public static int callProgram(String[] args, File workDir)
- throws IOException {
- Process p = Runtime.getRuntime().exec(args, null, workDir);
- return processProgram(p);
- }
-
- private static int processProgram(Process p) {
-
- StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),
- "ERROR", System.err);
-
- // any output?
- StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(),
- "OUTPUT", System.out);
-
- errorGobbler.start();
- outputGobbler.start();
- int retVal = -1;
- try {
- retVal = p.waitFor();
- } catch (InterruptedException ignore) {
- } finally {
- // first stop the threads
- if (outputGobbler != null && outputGobbler.isAlive()) {
- outputGobbler.stopGobblingAndDie();
- outputGobbler = null;
- }
-
- if (errorGobbler != null && errorGobbler.isAlive()) {
- errorGobbler.stopGobblingAndDie();
- errorGobbler = null;
- }
-
- if (p != null) {
- if (p.getErrorStream() != null) {
- try {
- p.getErrorStream().close();
- } catch (Exception ignore) {
- }
-
- }
-
- if (p.getOutputStream() != null) {
- try {
- p.getOutputStream().close();
- } catch (Exception ignore) {
- }
- }
-
- if (p.getInputStream() != null) {
- try {
- p.getInputStream().close();
- } catch (Exception ignore) {
- }
- }
- }
- }
- return retVal;
- }
+ private ExecUtils() throws InstantiationException {
+ throw new InstantiationException("Don't construct utility classes!");
+ }
+
+ public static String printCommandLine(String[] args) {
+ StringBuffer cmdLine = new StringBuffer();
+
+ if (args != null && args.length > 0) {
+ for (int i = 0; i < args.length; i++) {
+ cmdLine.append(args[i]);
+ cmdLine.append(" ");
+ }
+ }
+
+ return cmdLine.toString();
+ }
+
+ public static int callProgram(String commandLine, Logger logger)
+ throws IOException {
+ return callProgram(commandLine, logger, null);
+ }
+
+ public static int callProgram(String commandLine, OutputStream stdOutStream,
+ OutputStream stdErrStream) throws IOException {
+ return callProgram(commandLine, stdOutStream, stdErrStream, null);
+ }
+
+ public static int callProgram(String commandLine, Logger logger, File
workDir)
+ throws IOException {
+ LoggerOutputStream loggerInfoStream = null;
+ LoggerOutputStream loggerSevereStream = null;
+ try {
+ return callProgram(
+ commandLine,
+ loggerInfoStream = new LoggerOutputStream(logger, Level.INFO),
+ loggerSevereStream = new LoggerOutputStream(logger,
Level.SEVERE),
+ workDir);
+ } catch (Exception e) {
+ throw new IOException(e);
+ } finally {
+ try { loggerInfoStream.close(); } catch (Exception e) {}
+ try { loggerSevereStream.close(); } catch (Exception e) {}
+ }
+ }
+
+ public static int callProgram(String commandLine, OutputStream stdOutStream,
+ OutputStream stdErrStream, File workDir) throws IOException {
+ Process progProcess = null;
+ StreamGobbler errorGobbler = null, outputGobbler = null;
+ int returnVal = -1;
+ try {
+ progProcess = (workDir == null) ? Runtime.getRuntime().exec(
+ commandLine) : Runtime.getRuntime().exec(commandLine, null,
+ workDir);
+ errorGobbler = new StreamGobbler(progProcess.getErrorStream(),
+ "ERROR", stdErrStream);
+ outputGobbler = new StreamGobbler(progProcess.getInputStream(),
+ "OUTPUT", stdOutStream);
+ errorGobbler.start();
+ outputGobbler.start();
+ returnVal = progProcess.waitFor();
+ return returnVal;
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IOException("Failed to run '" + commandLine
+ + "' -- return val = " + returnVal + " : " + e.getMessage());
+ } finally {
+ if (errorGobbler != null) {
+ errorGobbler.stopGobblingAndDie();
+ }
+ if (outputGobbler != null) {
+ outputGobbler.stopGobblingAndDie();
+ }
+ try { progProcess.getErrorStream().close(); } catch (Exception e) {}
+ try { progProcess.getInputStream().close(); } catch (Exception e) {}
+ try { progProcess.getOutputStream().close(); } catch (Exception e) {}
+ }
+ }
+
+ public static int callProgram(String commandLine, File workDir)
+ throws IOException {
+ Process p = Runtime.getRuntime().exec(commandLine, null, workDir);
+ return processProgram(p);
+ }
+
+ public static int callProgram(String[] args, File workDir)
+ throws IOException {
+ Process p = Runtime.getRuntime().exec(args, null, workDir);
+ return processProgram(p);
+ }
+
+ private static int processProgram(Process p) {
+
+ StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),
+ "ERROR", System.err);
+
+ // any output?
+ StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(),
+ "OUTPUT", System.out);
+
+ errorGobbler.start();
+ outputGobbler.start();
+ int retVal = -1;
+ try {
+ retVal = p.waitFor();
+ } catch (InterruptedException ignore) {
+ } finally {
+ // first stop the threads
+ if (outputGobbler != null && outputGobbler.isAlive()) {
+ outputGobbler.stopGobblingAndDie();
+ outputGobbler = null;
+ }
+
+ if (errorGobbler != null && errorGobbler.isAlive()) {
+ errorGobbler.stopGobblingAndDie();
+ errorGobbler = null;
+ }
+
+ try { p.getErrorStream().close(); } catch (Exception ignore) {}
+ try { p.getOutputStream().close(); } catch (Exception ignore) {}
+ try { p.getInputStream().close(); } catch (Exception ignore) {}
+ }
+ return retVal;
+ }
}
Modified:
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/LoggerOutputStream.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/LoggerOutputStream.java?rev=1302592&r1=1302591&r2=1302592&view=diff
==============================================================================
---
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/LoggerOutputStream.java
(original)
+++
oodt/trunk/commons/src/main/java/org/apache/oodt/commons/io/LoggerOutputStream.java
Mon Mar 19 18:40:45 2012
@@ -14,8 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-
package org.apache.oodt.commons.io;
//JDK imports
@@ -26,59 +24,63 @@ import java.util.logging.Level;
import java.util.logging.Logger;
/**
+ * {@link OutputStream} wrapper around a java {@link Logger}.
*
- * @author bfoster
- * @version $Revision$
- *
- * <p>
- * Describe your class here
- * </p>.
+ * @author bfoster (Brian Foster)
*/
public class LoggerOutputStream extends OutputStream {
- private Logger logger;
-
- private CharBuffer buffer;
-
- private Level logLevel;
-
- public LoggerOutputStream(Logger logger) throws InstantiationException {
- this(logger, Level.INFO);
- }
-
- public LoggerOutputStream(Logger logger, Level logLevel)
- throws InstantiationException {
- this(logger, 512, logLevel);
- }
-
- public LoggerOutputStream(Logger logger, int numOfBytesPerWrite)
- throws InstantiationException {
- this(logger, numOfBytesPerWrite, Level.INFO);
- }
-
- public LoggerOutputStream(Logger logger, int numOfBytesPerWrite,
- Level logLevel) throws InstantiationException {
- this.logger = logger;
- this.buffer = CharBuffer.wrap(new char[numOfBytesPerWrite]);
- this.logLevel = logLevel;
- }
-
- public void write(int b) throws IOException {
- if (this.buffer.hasRemaining()) {
- this.buffer.put((char) b);
- if (!this.buffer.hasRemaining())
- this.flush();
- } else
- this.logger.log(this.logLevel, ((char) b) + "");
- }
-
- public void flush() {
- if (this.buffer.position() > 0) {
- char[] flushContext = new char[this.buffer.position()];
- System.arraycopy(this.buffer.array(), 0, flushContext, 0,
this.buffer.position());
- this.logger.log(this.logLevel, new String(flushContext));
- this.buffer.clear();
- }
- }
-
+ public static final String NUM_BYTES_PER_WRITE_PROPERTY =
+ "org.apache.oodt.commons.io.logger.os.bytes.per.write";
+ private static final int NUM_BYTES_PER_WRITE = Integer.getInteger(
+ NUM_BYTES_PER_WRITE_PROPERTY, 512);
+
+ private Logger logger;
+ private CharBuffer buffer;
+ private Level logLevel;
+
+ public LoggerOutputStream(Logger logger) throws InstantiationException {
+ this(logger, Level.INFO);
+ }
+
+ public LoggerOutputStream(Logger logger, Level logLevel)
+ throws InstantiationException {
+ this(logger, NUM_BYTES_PER_WRITE, logLevel);
+ }
+
+ public LoggerOutputStream(Logger logger, int numOfBytesPerWrite)
+ throws InstantiationException {
+ this(logger, numOfBytesPerWrite, Level.INFO);
+ }
+
+ public LoggerOutputStream(Logger logger, int numOfBytesPerWrite,
+ Level logLevel) throws InstantiationException {
+ this.logger = logger;
+ this.buffer = CharBuffer.wrap(new char[numOfBytesPerWrite]);
+ this.logLevel = logLevel;
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ if (!buffer.hasRemaining()) {
+ flush();
+ }
+ buffer.put((char) b);
+ }
+
+ @Override
+ public void flush() {
+ if (buffer.position() > 0) {
+ char[] flushContext = new char[buffer.position()];
+ System.arraycopy(buffer.array(), 0, flushContext, 0,
buffer.position());
+ logger.log(logLevel, new String(flushContext));
+ buffer.clear();
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ flush();
+ super.close();
+ }
}
Added:
oodt/trunk/commons/src/test/org/apache/oodt/commons/io/TestLoggerOutputStream.java
URL:
http://svn.apache.org/viewvc/oodt/trunk/commons/src/test/org/apache/oodt/commons/io/TestLoggerOutputStream.java?rev=1302592&view=auto
==============================================================================
---
oodt/trunk/commons/src/test/org/apache/oodt/commons/io/TestLoggerOutputStream.java
(added)
+++
oodt/trunk/commons/src/test/org/apache/oodt/commons/io/TestLoggerOutputStream.java
Mon Mar 19 18:40:45 2012
@@ -0,0 +1,59 @@
+/*
+ * 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.oodt.commons.io;
+
+//JDK imports
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+//JUnit imports
+import junit.framework.TestCase;
+
+/**
+ * Test class for {@link LoggerOutputStream}.
+ *
+ * @author bfoster (Brian Foster)
+ */
+public class TestLoggerOutputStream extends TestCase {
+
+ public void testLogging() throws InstantiationException, IOException {
+ final List<LogRecord> records = new ArrayList<LogRecord>();
+ Logger logger = Logger.getLogger(TestLoggerOutputStream.class.getName());
+ logger.addHandler(new Handler() {
+ @Override
+ public void close() throws SecurityException {}
+ @Override
+ public void flush() {}
+ @Override
+ public void publish(LogRecord record) {
+ records.add(record);
+ }
+ });
+ LoggerOutputStream los = new LoggerOutputStream(logger, 10, Level.INFO);
+ los.write("This is a test write to a log file".getBytes());
+ los.close();
+ assertEquals("This is a ", records.get(0).getMessage());
+ assertEquals("test write", records.get(1).getMessage());
+ assertEquals(" to a log ", records.get(2).getMessage());
+ assertEquals("file", records.get(3).getMessage());
+ }
+}
Propchange:
oodt/trunk/commons/src/test/org/apache/oodt/commons/io/TestLoggerOutputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain