JSEC-117 - initial logging framework support

git-svn-id: https://svn.apache.org/repos/asf/incubator/jsecurity/trunk@710965 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/org/jsecurity/logging/FormattedLog.java b/src/org/jsecurity/logging/FormattedLog.java
new file mode 100644
index 0000000..29898b8
--- /dev/null
+++ b/src/org/jsecurity/logging/FormattedLog.java
@@ -0,0 +1,157 @@
+/*

+ * 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.jsecurity.logging;

+

+import java.util.regex.Pattern;

+

+/**

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public abstract class FormattedLog implements Log {

+

+    private static final Pattern TOKEN_PATTERN = Pattern.compile("\\{\\}");

+    private String name;

+

+    public FormattedLog() {

+    }

+

+    public FormattedLog(String name) {

+        setName(name);

+    }

+

+    public String getName() {

+        return this.name;

+    }

+

+    protected void setName(String name) {

+        this.name = name;

+    }

+

+    protected String format(String msg, Object... args) {

+        if (args != null && args.length > 0) {

+            for (Object o : args) {

+                msg = TOKEN_PATTERN.matcher(msg).replaceFirst(o.toString());

+            }

+            return msg;

+        } else {

+            return msg;

+        }

+    }

+

+    public void trace(String msg) {

+        if (isTraceEnabled()) {

+            doTrace(msg);

+        }

+    }

+

+    public void trace(String format, Object arg) {

+        if (isTraceEnabled()) {

+            doTrace(format(format, arg));

+        }

+    }

+

+    public void trace(String format, Object... args) {

+        if (isTraceEnabled()) {

+            doTrace(format(format, args));

+        }

+    }

+

+    protected abstract void doTrace(String msg);

+

+    public void debug(String msg) {

+        if (isDebugEnabled()) {

+            doDebug(msg);

+        }

+    }

+

+    public void debug(String format, Object arg) {

+        if (isDebugEnabled()) {

+            doDebug(format(format, arg));

+        }

+    }

+

+    public void debug(String format, Object... args) {

+        if (isDebugEnabled()) {

+            debug(format(format, args));

+        }

+    }

+

+    protected abstract void doDebug(String msg);

+

+    public void info(String msg) {

+        if (isInfoEnabled()) {

+            doInfo(msg);

+        }

+    }

+

+    public void info(String format, Object arg) {

+        if (isInfoEnabled()) {

+            doInfo(format(format, arg));

+        }

+    }

+

+    public void info(String format, Object... args) {

+        if (isInfoEnabled()) {

+            doInfo(format(format, args));

+        }

+    }

+

+    protected abstract void doInfo(String msg);

+

+    public void warn(String msg) {

+        if (isWarnEnabled()) {

+            doWarn(msg);

+        }

+    }

+

+    public void warn(String format, Object arg) {

+        if (isWarnEnabled()) {

+            doWarn(format(format, arg));

+        }

+    }

+

+    public void warn(String format, Object... args) {

+        if (isWarnEnabled()) {

+            doWarn(format(format, args));

+        }

+    }

+

+    protected abstract void doWarn(String msg);

+

+    public void error(String msg) {

+        if (isErrorEnabled()) {

+            doError(msg);

+        }

+    }

+

+    public void error(String format, Object arg) {

+        if (isErrorEnabled()) {

+            doError(format(format, arg));

+        }

+    }

+

+    public void error(String format, Object... args) {

+        if (isErrorEnabled()) {

+            doError(format(format, args));

+        }

+    }

+

+    protected abstract void doError(String msg);

+}

diff --git a/src/org/jsecurity/logging/JSecurityLogFactory.java b/src/org/jsecurity/logging/JSecurityLogFactory.java
new file mode 100644
index 0000000..4060573
--- /dev/null
+++ b/src/org/jsecurity/logging/JSecurityLogFactory.java
@@ -0,0 +1,53 @@
+/*

+ * 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.jsecurity.logging;

+

+import org.jsecurity.logging.console.ConsoleLogFactory;

+import org.jsecurity.logging.jdk.JdkLogFactory;

+import org.jsecurity.util.JavaEnvironment;

+

+/**

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public final class JSecurityLogFactory {

+

+    transient static LogFactory instance;

+

+    static {

+        if (JavaEnvironment.isAtLeastVersion14()) {

+            instance = new JdkLogFactory();

+        } else {

+            instance = new ConsoleLogFactory();

+        }

+    }

+

+    public static void setLogFactory(LogFactory instance) {

+        JSecurityLogFactory.instance = instance;

+    }

+

+    public static LogFactory getLogFactory() {

+        return instance;

+    }

+

+    public static Log getLog(String name) {

+        return instance.getLog(name);

+    }

+

+}

diff --git a/src/org/jsecurity/logging/Log.java b/src/org/jsecurity/logging/Log.java
new file mode 100644
index 0000000..eecf404
--- /dev/null
+++ b/src/org/jsecurity/logging/Log.java
@@ -0,0 +1,85 @@
+/*

+ * 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.jsecurity.logging;

+

+/**

+ * A very simple Log instance to record useful information during runtime.

+ *

+ * <p>JSecurity doesn't implement a Logging mechanism itself, since that is outside the core competency of a

+ * Security framework.  Instead, this interface provides an abstraction (wrapper) API on top of an underlying

+ * logging framework's Log instance (e.g. JDK, SLF4J, Commons Logging, company proprietary mechansim, etc), allowing

+ * a JSecurity user to configure any logging mechanism they choose.

+ *

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public interface Log {

+

+    String getName();

+

+    boolean isTraceEnabled();

+

+    void trace(String msg);

+

+    void trace(String format, Object arg);

+

+    void trace(String format, Object... args);

+

+    void trace(String msg, Throwable t);

+

+    boolean isDebugEnabled();

+

+    void debug(String msg);

+

+    void debug(String format, Object arg);

+

+    void debug(String format, Object... args);

+

+    void debug(String msg, Throwable t);

+

+    boolean isInfoEnabled();

+

+    void info(String msg);

+

+    void info(String format, Object arg);

+

+    void info(String format, Object... args);

+

+    void info(String msg, Throwable t);

+

+    boolean isWarnEnabled();

+

+    void warn(String msg);

+

+    void warn(String format, Object arg);

+

+    void warn(String format, Object... args);

+

+    void warn(String msg, Throwable t);

+

+    boolean isErrorEnabled();

+

+    void error(String msg);

+

+    void error(String format, Object arg);

+

+    void error(String format, Object... args);

+

+    void error(String msg, Throwable t);

+}

diff --git a/src/org/jsecurity/logging/LogFactory.java b/src/org/jsecurity/logging/LogFactory.java
new file mode 100644
index 0000000..6fd39ee
--- /dev/null
+++ b/src/org/jsecurity/logging/LogFactory.java
@@ -0,0 +1,28 @@
+/*

+ * 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.jsecurity.logging;

+

+/**

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public interface LogFactory {

+

+    Log getLog(String name);

+}

diff --git a/src/org/jsecurity/logging/console/ConsoleLog.java b/src/org/jsecurity/logging/console/ConsoleLog.java
new file mode 100644
index 0000000..50d803e
--- /dev/null
+++ b/src/org/jsecurity/logging/console/ConsoleLog.java
@@ -0,0 +1,110 @@
+/*

+ * 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.jsecurity.logging.console;

+

+import org.jsecurity.logging.FormattedLog;

+

+/**

+ * Simple implementation that always prints to <code>System.out</code>

+ *

+ * <p>This implementation does not support log levels therefore <em>all</em> messages are printed out always.

+ *

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public class ConsoleLog extends FormattedLog {

+

+    private static final String name = "ConsoleLog";

+

+    public ConsoleLog() {

+    }

+

+    public String getName() {

+        return name;

+    }

+

+    public boolean isTraceEnabled() {

+        return true;

+    }

+

+    public boolean isDebugEnabled() {

+        return true;

+    }

+

+    public boolean isInfoEnabled() {

+        return true;

+    }

+

+    public boolean isWarnEnabled() {

+        return true;

+    }

+

+    public boolean isErrorEnabled() {

+        return true;

+    }

+

+    protected void out(String msg) {

+        System.out.println(msg);

+    }

+

+    protected void doTrace(String msg) {

+        out(msg);

+    }

+

+    protected void doDebug(String msg) {

+        out(msg);

+    }

+

+    protected void doInfo(String msg) {

+        out(msg);

+    }

+

+    protected void doWarn(String msg) {

+        out(msg);

+    }

+

+    protected void doError(String msg) {

+        out(msg);

+    }

+

+    protected void tout(String msg, Throwable t) {

+        out(msg);

+        t.printStackTrace(System.out);

+    }

+

+    public void trace(String msg, Throwable t) {

+        tout(msg, t);

+    }

+

+    public void debug(String msg, Throwable t) {

+        tout(msg, t);

+    }

+

+    public void info(String msg, Throwable t) {

+        tout(msg, t);

+    }

+

+    public void warn(String msg, Throwable t) {

+        tout(msg, t);

+    }

+

+    public void error(String msg, Throwable t) {

+        tout(msg, t);

+    }

+}

diff --git a/src/org/jsecurity/logging/console/ConsoleLogFactory.java b/src/org/jsecurity/logging/console/ConsoleLogFactory.java
new file mode 100644
index 0000000..827f984
--- /dev/null
+++ b/src/org/jsecurity/logging/console/ConsoleLogFactory.java
@@ -0,0 +1,33 @@
+/*

+ * 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.jsecurity.logging.console;

+

+import org.jsecurity.logging.Log;

+import org.jsecurity.logging.LogFactory;

+

+/**

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public class ConsoleLogFactory implements LogFactory {

+

+    public Log getLog(String name) {

+        return new ConsoleLog();

+    }

+}

diff --git a/src/org/jsecurity/logging/jdk/JdkLog.java b/src/org/jsecurity/logging/jdk/JdkLog.java
new file mode 100644
index 0000000..7cd448d
--- /dev/null
+++ b/src/org/jsecurity/logging/jdk/JdkLog.java
@@ -0,0 +1,141 @@
+/*

+ * 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.jsecurity.logging.jdk;

+

+import org.jsecurity.logging.Log;

+

+import java.util.logging.Level;

+import java.util.logging.Logger;

+

+/**

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public class JdkLog implements Log {

+

+    private final Logger logger;

+

+    public JdkLog(Logger logger) {

+        this.logger = logger;

+    }

+

+    public String getName() {

+        return logger.getName();

+    }

+

+    public boolean isTraceEnabled() {

+        return logger.getLevel().intValue() < Level.FINE.intValue();

+    }

+

+    public void trace(String msg) {

+        logger.log(Level.FINEST, msg);

+    }

+

+    public void trace(String format, Object arg) {

+        logger.log(Level.FINEST, format, arg);

+    }

+

+    public void trace(String format, Object... args) {

+        logger.log(Level.FINEST, format, args);

+    }

+

+    public void trace(String msg, Throwable t) {

+        logger.log(Level.FINEST, msg, t);

+    }

+

+    public boolean isDebugEnabled() {

+        return logger.getLevel().intValue() >= Level.FINE.intValue();

+    }

+

+    public void debug(String msg) {

+        logger.log(Level.FINE, msg);

+    }

+

+    public void debug(String format, Object arg) {

+        logger.log(Level.FINE, format, arg);

+    }

+

+    public void debug(String format, Object... args) {

+        logger.log(Level.FINE, format, args);

+    }

+

+    public void debug(String msg, Throwable t) {

+        logger.log(Level.FINE, msg, t);

+    }

+

+    public boolean isInfoEnabled() {

+        return logger.getLevel().intValue() >= Level.INFO.intValue();

+    }

+

+    public void info(String msg) {

+        logger.log(Level.INFO, msg);

+    }

+

+    public void info(String format, Object arg) {

+        logger.log(Level.INFO, format, arg);

+    }

+

+    public void info(String format, Object... args) {

+        logger.log(Level.INFO, format, args);

+    }

+

+    public void info(String msg, Throwable t) {

+        logger.log(Level.INFO, msg, t);

+    }

+

+    public boolean isWarnEnabled() {

+        return logger.getLevel().intValue() >= Level.WARNING.intValue();

+    }

+

+    public void warn(String msg) {

+        logger.log(Level.WARNING, msg);

+    }

+

+    public void warn(String format, Object arg) {

+        logger.log(Level.WARNING, format, arg);

+    }

+

+    public void warn(String format, Object... args) {

+        logger.log(Level.WARNING, format, args);

+    }

+

+    public void warn(String msg, Throwable t) {

+        logger.log(Level.WARNING, msg, t);

+    }

+

+    public boolean isErrorEnabled() {

+        return logger.getLevel().intValue() >= Level.SEVERE.intValue();

+    }

+

+    public void error(String msg) {

+        logger.log(Level.SEVERE, msg);

+    }

+

+    public void error(String format, Object arg) {

+        logger.log(Level.SEVERE, format, arg);

+    }

+

+    public void error(String format, Object... args) {

+        logger.log(Level.SEVERE, format, args);

+    }

+

+    public void error(String msg, Throwable t) {

+        logger.log(Level.SEVERE, msg, t);

+    }

+}

diff --git a/src/org/jsecurity/logging/jdk/JdkLogFactory.java b/src/org/jsecurity/logging/jdk/JdkLogFactory.java
new file mode 100644
index 0000000..5e31a37
--- /dev/null
+++ b/src/org/jsecurity/logging/jdk/JdkLogFactory.java
@@ -0,0 +1,35 @@
+/*

+ * 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.jsecurity.logging.jdk;

+

+import org.jsecurity.logging.Log;

+import org.jsecurity.logging.LogFactory;

+

+import java.util.logging.Logger;

+

+/**

+ * @author Les Hazlewood

+ * @since 0.9

+ */

+public class JdkLogFactory implements LogFactory {

+

+    public Log getLog(String name) {

+        return new JdkLog(Logger.getLogger(name));

+    }

+}