Author: alessio.soldano(a)jboss.com
Date: 2010-06-10 12:20:53 -0400 (Thu, 10 Jun 2010)
New Revision: 12459
Added:
spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4JUtil.java
spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4jOutputStream.java
Modified:
spi/trunk/pom.xml
spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSConsume.java
spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSProvide.java
Log:
[JBWS-3063] Make wsconsume/wsprovide command line tools use log4j when configured
Modified: spi/trunk/pom.xml
===================================================================
--- spi/trunk/pom.xml 2010-06-10 16:14:13 UTC (rev 12458)
+++ spi/trunk/pom.xml 2010-06-10 16:20:53 UTC (rev 12459)
@@ -34,6 +34,7 @@
<jboss.microcontainer.version>2.0.8.GA</jboss.microcontainer.version>
<jboss.web.version>2.1.3.GA</jboss.web.version>
<junit.version>3.8.2</junit.version>
+ <log4j.version>1.2.14</log4j.version>
</properties>
<!-- Dependencies -->
@@ -70,6 +71,11 @@
<version>${jboss.microcontainer.version}</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>${log4j.version}</version>
+ </dependency>
<!-- transitive dependencies -->
<dependency>
Modified: spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSConsume.java
===================================================================
--- spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSConsume.java 2010-06-10 16:14:13
UTC (rev 12458)
+++ spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSConsume.java 2010-06-10 16:20:53
UTC (rev 12459)
@@ -23,7 +23,13 @@
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.Level;
+
import org.jboss.wsf.spi.tools.WSContractConsumer;
+import org.jboss.wsf.spi.util.Log4JUtil;
+import org.jboss.wsf.spi.util.Log4jOutputStream;
import java.io.File;
import java.io.PrintStream;
@@ -215,7 +221,19 @@
consumer.setSourceDirectory(sourceDir);
if (! quiet)
- consumer.setMessageStream(System.out);
+ {
+ PrintStream ps;
+ if (Log4JUtil.isLog4jConfigurationAvailable())
+ {
+ ps = new PrintStream(new
Log4jOutputStream(Logger.getLogger("WSConsume"), Level.INFO));
+ }
+ else
+ {
+ ps = System.out;
+ ps.println("Could not find log4j.xml configuration, logging to
console.\n");
+ }
+ consumer.setMessageStream(ps);
+ }
if (catalog != null)
consumer.setCatalog(catalog);
Modified: spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSProvide.java
===================================================================
--- spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSProvide.java 2010-06-10 16:14:13
UTC (rev 12458)
+++ spi/trunk/src/main/java/org/jboss/wsf/spi/tools/cmd/WSProvide.java 2010-06-10 16:20:53
UTC (rev 12459)
@@ -32,7 +32,12 @@
import java.util.ArrayList;
import java.util.List;
+import org.apache.log4j.Logger;
+import org.apache.log4j.Level;
+
import org.jboss.wsf.spi.tools.WSContractProvider;
+import org.jboss.wsf.spi.util.Log4JUtil;
+import org.jboss.wsf.spi.util.Log4jOutputStream;
/**
* WSProvideTask is a cmd line tool that generates portable JAX-WS artifacts
@@ -184,7 +189,19 @@
gen.setSourceDirectory(sourceDir);
if (! quiet)
- gen.setMessageStream(System.out);
+ {
+ PrintStream ps;
+ if (Log4JUtil.isLog4jConfigurationAvailable())
+ {
+ ps = new PrintStream(new
Log4jOutputStream(Logger.getLogger("WSProvide"), Level.INFO));
+ }
+ else
+ {
+ ps = System.out;
+ ps.println("Could not find log4j.xml configuration, logging to
console.\n");
+ }
+ gen.setMessageStream(ps);
+ }
try
{
Added: spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4JUtil.java
===================================================================
--- spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4JUtil.java
(rev 0)
+++ spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4JUtil.java 2010-06-10 16:20:53 UTC
(rev 12459)
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.spi.util;
+
+import java.net.URL;
+
+import org.apache.log4j.helpers.Loader;
+
+/**
+ *
+ * @author alessio.soldano(a)jboss.com
+ * @since 10-Jun-2010
+ *
+ */
+public class Log4JUtil
+{
+ public static final String LOG4J_CONFIGURATION = "log4j.configuration";
+ public static final String LOG4J_PROPERTIES = "log4j.properties";
+
+ /**
+ * Returns true if a log4j configuration can be found given the current environment.
+ * See
http://logging.apache.org/log4j/1.2/manual.html (Default Initialization
Procedure)
+ * @return
+ */
+ public static boolean isLog4jConfigurationAvailable()
+ {
+ String log4jConfiguration = System.getProperty(LOG4J_CONFIGURATION);
+ String resource = log4jConfiguration != null ? log4jConfiguration :
LOG4J_PROPERTIES;
+ URL url = null;
+ try
+ {
+ url = new URL(resource);
+ }
+ catch (Exception e1)
+ {
+ try
+ {
+ url = Loader.getResource(resource);
+ }
+ catch (Exception e2)
+ {
+ //ignore
+ }
+ }
+ return url != null;
+ }
+}
Property changes on: spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4JUtil.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added: spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4jOutputStream.java
===================================================================
--- spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4jOutputStream.java
(rev 0)
+++ spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4jOutputStream.java 2010-06-10
16:20:53 UTC (rev 12459)
@@ -0,0 +1,220 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.wsf.spi.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import org.apache.log4j.Category;
+import org.apache.log4j.Priority;
+
+/**
+ * An OutputStream that flushes out to a Category.<p>
+ * A simple port of <a href="mailto://Jim.Moore@rocketmail.com">Jim
Moore</a>'s
+ * LoggingOutputStream contribution to log4j.
+ *
+ * Note that no data is written out to the Category until the stream is
+ * flushed or closed.<p>
+ *
+ * Example:<pre>
+ * // make sure everything sent to System.err is logged
+ * System.setErr(new PrintStream(new LoggingOutputStream(Category.getRoot(),
Priority.WARN), true));
+ *
+ * // make sure everything sent to System.out is also logged
+ * System.setOut(new PrintStream(new LoggingOutputStream(Category.getRoot(),
Priority.INFO), true));
+ * </pre>
+ *
+ */
+public class Log4jOutputStream extends OutputStream
+{
+ protected static final String LINE_SEPERATOR =
System.getProperty("line.separator");
+
+ /**
+ * Used to maintain the contract of {@link #close()}.
+ */
+ protected boolean hasBeenClosed = false;
+
+ /**
+ * The internal buffer where data is stored.
+ */
+ protected byte[] buf;
+
+ /**
+ * The number of valid bytes in the buffer. This value is always
+ * in the range <tt>0</tt> through <tt>buf.length</tt>;
elements
+ * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain
valid
+ * byte data.
+ */
+ protected int count;
+
+ /**
+ * Remembers the size of the buffer for speed.
+ */
+ private int bufLength;
+
+ /**
+ * The default number of bytes in the buffer. =2048
+ */
+ public static final int DEFAULT_BUFFER_LENGTH = 2048;
+
+ /**
+ * The category to write to.
+ */
+ protected Category category;
+
+ /**
+ * The priority to use when writing to the Category.
+ */
+ protected Priority priority;
+
+ @SuppressWarnings("unused")
+ private Log4jOutputStream()
+ {
+ // illegal
+ }
+
+ /**
+ * Creates the LoggingOutputStream to flush to the given Category.
+ *
+ * @param cat the Category to write to
+ *
+ * @param priority the Priority to use when writing to the Category
+ *
+ * @exception IllegalArgumentException
+ * if cat == null or priority == null
+ */
+ public Log4jOutputStream(Category cat, Priority priority) throws
IllegalArgumentException
+ {
+ if (cat == null)
+ {
+ throw new IllegalArgumentException("cat == null");
+ }
+ if (priority == null)
+ {
+ throw new IllegalArgumentException("priority == null");
+ }
+
+ this.priority = priority;
+ category = cat;
+ bufLength = DEFAULT_BUFFER_LENGTH;
+ buf = new byte[DEFAULT_BUFFER_LENGTH];
+ count = 0;
+ }
+
+ /**
+ * Closes this output stream and releases any system resources
+ * associated with this stream. The general contract of
<code>close</code>
+ * is that it closes the output stream. A closed stream cannot perform
+ * output operations and cannot be reopened.
+ */
+ public void close()
+ {
+ flush();
+ hasBeenClosed = true;
+ }
+
+ /**
+ * Writes the specified byte to this output stream. The general
+ * contract for <code>write</code> is that one byte is written
+ * to the output stream. The byte to be written is the eight
+ * low-order bits of the argument <code>b</code>. The 24
+ * high-order bits of <code>b</code> are ignored.
+ *
+ * @param b the <code>byte</code> to write
+ *
+ * @exception IOException
+ * if an I/O error occurs. In particular,
+ * an <code>IOException</code> may be thrown if the
+ * output stream has been closed.
+ */
+ public void write(final int b) throws IOException
+ {
+ if (hasBeenClosed)
+ {
+ throw new IOException("The stream has been closed.");
+ }
+
+ // don't log nulls
+ if (b == 0)
+ {
+ return;
+ }
+
+ // would this be writing past the buffer?
+ if (count == bufLength)
+ {
+ // grow the buffer
+ final int newBufLength = bufLength + DEFAULT_BUFFER_LENGTH;
+ final byte[] newBuf = new byte[newBufLength];
+
+ System.arraycopy(buf, 0, newBuf, 0, bufLength);
+
+ buf = newBuf;
+ bufLength = newBufLength;
+ }
+
+ buf[count] = (byte)b;
+ count++;
+ }
+
+ /**
+ * Flushes this output stream and forces any buffered output bytes
+ * to be written out. The general contract of <code>flush</code> is
+ * that calling it is an indication that, if any bytes previously
+ * written have been buffered by the implementation of the output
+ * stream, such bytes should immediately be written to their
+ * intended destination.
+ */
+ public void flush()
+ {
+ if (count == 0)
+ {
+ return;
+ }
+
+ // don't print out blank lines; flushing from PrintStream puts out these
+ if (count == LINE_SEPERATOR.length())
+ {
+ if (((char)buf[0]) == LINE_SEPERATOR.charAt(0) && ((count == 1) || //
<- Unix & Mac, -> Windows
+ ((count == 2) && ((char)buf[1]) == LINE_SEPERATOR.charAt(1))))
+ {
+ reset();
+ return;
+ }
+ }
+
+ final byte[] theBytes = new byte[count];
+
+ System.arraycopy(buf, 0, theBytes, 0, count);
+
+ category.log(priority, new String(theBytes));
+
+ reset();
+ }
+
+ private void reset()
+ {
+ // not resetting the buffer -- assuming that if it grew that it
+ // will likely grow similarly again
+ count = 0;
+ }
+
+}
Property changes on:
spi/trunk/src/main/java/org/jboss/wsf/spi/util/Log4jOutputStream.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF