You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-commits@ws.apache.org by da...@apache.org on 2007/02/12 16:49:15 UTC

svn commit: r506504 - in /webservices/muse/trunk/modules/muse-util-logging: ./ src/ src/java/ src/java/util/ src/java/util/logging/

Author: danj
Date: Mon Feb 12 07:49:14 2007
New Revision: 506504

URL: http://svn.apache.org/viewvc?view=rev&rev=506504
Log:
Implementation of JDK Logging API from Barry Atkins. This code provides basic java.util.logging.* 
support for J2ME at both compile and runtime. The OSGi isolation layer contains an additional 
Logger that forwards all messages to the OSGi logging service.

This code is not part of the normal build - in fact, it will be used by the build to determine 
J2ME compliance each night. It will also be used by J2ME users in order to satisfy runtime 
requirements.

Added:
    webservices/muse/trunk/modules/muse-util-logging/
    webservices/muse/trunk/modules/muse-util-logging/src/
    webservices/muse/trunk/modules/muse-util-logging/src/java/
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/ConsoleHandler.java
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/FileHandler.java
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Formatter.java
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Handler.java
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Level.java
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/LogRecord.java
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Logger.java
    webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/SimpleFormatter.java

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/ConsoleHandler.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/ConsoleHandler.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/ConsoleHandler.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/ConsoleHandler.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,36 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+/**
+ * 
+ * @author Barry Atkins
+ *
+ */
+
+public class ConsoleHandler extends Handler
+{
+    public void close()
+    {
+        System.out.flush();
+    }
+    
+    public void publish(LogRecord record)
+    {
+        System.out.println(record);
+    }
+}

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/FileHandler.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/FileHandler.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/FileHandler.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/FileHandler.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,71 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Date;
+
+/**
+ * 
+ * @author Barry Atkins
+ *
+ */
+
+public class FileHandler extends Handler
+{
+    private FileWriter _writer = null;
+    
+    public FileHandler(String fileName)
+        throws IOException
+    {
+        _writer = new FileWriter(fileName);
+    }
+    
+    public void close()
+    {
+        try
+        {
+            _writer.flush();
+            _writer.close();
+        }
+        
+        catch (IOException error)
+        {
+            error.printStackTrace();
+        }
+    }
+
+    public void publish(LogRecord record)
+    {
+        try
+        {
+            Date time = new Date(record.getMillis());
+            
+            _writer.write("[ ");
+            _writer.write(time.toString());
+            _writer.write(" ] ");
+            _writer.write(record.getMessage());
+            _writer.write('\n');
+        }
+        
+        catch (IOException error)
+        {
+            error.printStackTrace();
+        }
+    }
+}

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Formatter.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Formatter.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Formatter.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Formatter.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,28 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+/**
+ * 
+ * @author Barry Atkins
+ *
+ */
+
+public abstract class Formatter
+{
+    public abstract String format(LogRecord record);
+}

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Handler.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Handler.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Handler.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Handler.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,54 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+/**
+ * 
+ * @author Barry Atkins
+ * 
+ */
+
+public abstract class Handler
+{
+    private Formatter _formatter = null;
+    
+    private Level _level = null;
+    
+    public Formatter getFormatter()
+    {
+        return _formatter;
+    }
+    
+    public Level getLevel()
+    {
+        return _level;
+    }
+
+    public void setFormatter(Formatter formatter)
+    {
+        _formatter = formatter;
+    }
+    
+    public void setLevel(Level level)
+    {
+        _level = level;
+    }
+    
+    public abstract void close();
+    
+    public abstract void publish(LogRecord record);
+}

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Level.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Level.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Level.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Level.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,97 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 
+ * @author Barry Atkins
+ * 
+ */
+
+public class Level
+{
+    public static final Level OFF = new Level("OFF", 0);
+    
+    public static final Level SEVERE = new Level("SEVERE", 1);
+    
+    public static final Level WARNING = new Level("WARNING", 2);
+    
+    public static final Level INFO = new Level("INFO", 3);
+    
+    public static final Level CONFIG = new Level("CONFIG", 4);
+    
+    public static final Level FINE = new Level("FINE", 5);
+    
+    public static final Level FINER = new Level("FINER", 6);
+    
+    public static final Level FINEST = new Level("FINEST", 7);
+    
+    public static final Level ALL = new Level("ALL", 8);
+    
+    private static final Map _LEVELS_BY_NAME = new HashMap();
+    
+    static
+    {
+        _LEVELS_BY_NAME.put(OFF.getName(), OFF);
+        _LEVELS_BY_NAME.put(SEVERE.getName(), SEVERE);
+        _LEVELS_BY_NAME.put(WARNING.getName(), WARNING);
+        _LEVELS_BY_NAME.put(INFO.getName(), INFO);
+        _LEVELS_BY_NAME.put(CONFIG.getName(), CONFIG);
+        _LEVELS_BY_NAME.put(FINE.getName(), FINE);
+        _LEVELS_BY_NAME.put(FINER.getName(), FINER);
+        _LEVELS_BY_NAME.put(FINEST.getName(), FINEST);
+        _LEVELS_BY_NAME.put(ALL.getName(), ALL);
+    }
+    
+    private String _name = null;
+    
+    private int _value;
+    
+    private Level(String name, int value)
+    {
+        _name = name;
+        _value = value;
+    }
+    
+    public String getName()
+    {
+        return _name;
+    }
+    
+    public int intValue()
+    {
+        return _value;
+    }
+    
+    public static Level parse(String name)
+    {
+        Level level = (Level)_LEVELS_BY_NAME.get(name);
+        
+        if (level == null)
+            throw new IllegalArgumentException("Invalid logging level: " + name);
+        
+        return level;
+    }
+    
+    public String toString()
+    {
+        return getName();
+    }
+}

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/LogRecord.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/LogRecord.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/LogRecord.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/LogRecord.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,60 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+import java.util.Date;
+
+/**
+ * 
+ * @author Barry Atkins
+ * 
+ */
+
+public class LogRecord
+{
+    private Level _level = null;
+    
+    private String _message = null;
+    
+    private Date _time = new Date();
+    
+    public LogRecord(Level level, String message)
+    {
+        _level = level;
+        _message = message;
+    }
+    
+    public Level getLevel()
+    {
+        return _level;
+    }
+    
+    public String getMessage()
+    {
+        return _message;
+    }
+    
+    public long getMillis()
+    {
+        return _time.getTime();
+    }
+    
+    public String toString()
+    {
+        return getMessage();
+    }
+}

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Logger.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Logger.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Logger.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/Logger.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,142 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+import java.util.*;
+
+/**
+ * 
+ * @author Barry Atkins
+ * 
+ */
+
+public class Logger
+{
+    private static final Map _LOGGERS = new HashMap();
+    
+    private List _handlers = new LinkedList();
+    
+    private Level _level = Level.ALL;
+    
+    private String _name = null;
+    
+    private Logger(String name)
+    {
+        _name = name;
+    }
+    
+    public void addHandler(Handler handler)
+    {
+        handler.setLevel(getLevel());
+        _handlers.add(handler);
+    }
+    
+    public void config(String message)
+    {
+        log(Level.CONFIG, message);
+    }
+    
+    public void fine(String message)
+    {
+        log(Level.FINE, message);
+    }
+    
+    public void finer(String message)
+    {
+        log(Level.FINER, message);
+    }
+    
+    public void finest(String message)
+    {
+        log(Level.FINEST, message);
+    }
+    
+    public Handler[] getHandlers()
+    {
+        Handler[] array = new Handler[_handlers.size()];
+        return (Handler[])_handlers.toArray(array);
+    }
+    
+    public Level getLevel()
+    {
+        return _level;
+    }
+    
+    public static Logger getLogger(String name)
+    {
+        if (!_LOGGERS.containsKey(name))
+            _LOGGERS.put(name, new Logger(name));
+        
+        return (Logger)_LOGGERS.get(name);
+    }
+        
+    public String getName()
+    {
+        return _name;
+    }
+    
+    public void info(String message)
+    {
+        log(Level.INFO, message);
+    }
+    
+    public boolean isLoggable(LogRecord record)
+    {
+        return getLevel().intValue() >= record.getLevel().intValue();
+    }
+    
+    public void log(Level level, String message)
+    {
+        LogRecord record = new LogRecord(level, message);
+        
+        if (!isLoggable(record))
+            return;
+        
+        Iterator i = _handlers.iterator();
+        
+        while (i.hasNext())
+        {
+            Handler next = (Handler)i.next();
+            next.publish(record);
+        }
+    }
+    
+    public void removeHandler(Handler handler)
+    {
+        _handlers.remove(handler);
+    }
+    
+    public void setLevel(Level level)
+    {
+        _level = level;
+    }
+    
+    public void severe(String message)
+    {
+        log(Level.SEVERE, message);
+    }
+    
+    public String toString()
+    {
+        return getName();
+    }
+    
+    public void warning(String message)
+    {
+        log(Level.WARNING, message);
+    }
+}

Added: webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/SimpleFormatter.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/SimpleFormatter.java?view=auto&rev=506504
==============================================================================
--- webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/SimpleFormatter.java (added)
+++ webservices/muse/trunk/modules/muse-util-logging/src/java/util/logging/SimpleFormatter.java Mon Feb 12 07:49:14 2007
@@ -0,0 +1,31 @@
+/*=============================================================================*
+ *  Copyright 2007 The Apache Software Foundation
+ *
+ *  Licensed 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 java.util.logging;
+
+/**
+ * 
+ * @author Barry Atkins
+ *
+ */
+
+public class SimpleFormatter extends Formatter
+{
+    public String format(LogRecord record)
+    {
+        return record.getMessage();
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: muse-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: muse-commits-help@ws.apache.org