Skip to content

Commit bfe17fb

Browse files
committed
Fix #341: Add model classes for java.util.logging
- Created Logger, LogManager, and Level model classes - Bypasses native method calls and complex JDK initialization - Adds test case to verify Logger.getLogger() works in JPF
1 parent 426be89 commit bfe17fb

5 files changed

Lines changed: 195 additions & 11 deletions

File tree

src/classes/modules/java.logging/java/util/logging/FileHandler.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* The Java Pathfinder core (jpf-core) platform is licensed under the
77
* Apache License, Version 2.0 (the "License"); you may not use this file except
88
* in compliance with the License. You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0.
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0.
1111
*
1212
* Unless required by applicable law or agreed to in writing, software
1313
* distributed under the License is distributed on an "AS IS" BASIS,
1414
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
* See the License for the specific language governing permissions and
15+
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
1818

@@ -25,12 +25,12 @@
2525
* simple stub to avoid execptions when using basic logging
2626
*/
2727
public class FileHandler extends StreamHandler {
28-
public FileHandler() throws IOException {
29-
this("log.log");
30-
}
28+
public FileHandler() throws IOException {
29+
this("log.log");
30+
}
3131

32-
public FileHandler(String pattern) throws IOException {
33-
super();
34-
setOutputStream(new FileOutputStream(pattern));
35-
}
36-
}
32+
public FileHandler(String pattern) throws IOException {
33+
super();
34+
setOutputStream(new FileOutputStream(pattern));
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2014, United States Government, as represented by the
3+
* Administrator of the National Aeronautics and Space Administration.
4+
* All rights reserved.
5+
*/
6+
package java.util.logging;
7+
8+
/**
9+
* MJI model class for java.util.logging.Level
10+
*/
11+
public class Level {
12+
private final String name;
13+
private final int value;
14+
15+
protected Level(String name, int value) {
16+
this.name = name;
17+
this.value = value;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public final int intValue() {
25+
return value;
26+
}
27+
28+
public static final Level OFF = new Level("OFF", Integer.MAX_VALUE);
29+
public static final Level SEVERE = new Level("SEVERE", 1000);
30+
public static final Level WARNING = new Level("WARNING", 900);
31+
public static final Level INFO = new Level("INFO", 800);
32+
public static final Level CONFIG = new Level("CONFIG", 700);
33+
public static final Level FINE = new Level("FINE", 500);
34+
public static final Level FINER = new Level("FINER", 400);
35+
public static final Level FINEST = new Level("FINEST", 300);
36+
public static final Level ALL = new Level("ALL", Integer.MIN_VALUE);
37+
38+
public String toString() {
39+
return name;
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2014, United States Government, as represented by the
3+
* Administrator of the National Aeronautics and Space Administration.
4+
* All rights reserved.
5+
*/
6+
package java.util.logging;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* MJI model class for java.util.logging.LogManager
13+
*/
14+
public class LogManager {
15+
16+
private static final LogManager manager = new LogManager();
17+
private final Map<String, Logger> loggers = new HashMap<>();
18+
19+
protected LogManager() {
20+
}
21+
22+
public static LogManager getLogManager() {
23+
return manager;
24+
}
25+
26+
public synchronized Logger getLogger(String name) {
27+
return loggers.get(name);
28+
}
29+
30+
public synchronized boolean addLogger(Logger logger) {
31+
String name = logger.getName();
32+
if (loggers.containsKey(name)) {
33+
return false;
34+
}
35+
loggers.put(name, logger);
36+
return true;
37+
}
38+
39+
Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
40+
Logger result = getLogger(name);
41+
if (result == null) {
42+
Logger newLogger = new Logger(name, resourceBundleName);
43+
addLogger(newLogger);
44+
result = newLogger;
45+
}
46+
return result;
47+
}
48+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2014, United States Government, as represented by the
3+
* Administrator of the National Aeronautics and Space Administration.
4+
* All rights reserved.
5+
*/
6+
package java.util.logging;
7+
8+
/**
9+
* MJI model class for java.util.logging.Logger
10+
*/
11+
public class Logger {
12+
13+
private String name;
14+
private String resourceBundleName;
15+
16+
protected Logger(String name, String resourceBundleName) {
17+
this.name = name;
18+
this.resourceBundleName = resourceBundleName;
19+
}
20+
21+
public static Logger getLogger(String name) {
22+
return getLogger(name, null);
23+
}
24+
25+
public static Logger getLogger(String name, String resourceBundleName) {
26+
LogManager manager = LogManager.getLogManager();
27+
return manager.demandLogger(name, resourceBundleName, null);
28+
}
29+
30+
public static Logger getAnonymousLogger() {
31+
return new Logger("", null);
32+
}
33+
34+
public static Logger getAnonymousLogger(String resourceBundleName) {
35+
return new Logger("", resourceBundleName);
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public String getResourceBundleName() {
43+
return resourceBundleName;
44+
}
45+
46+
public void info(String msg) {
47+
log(Level.INFO, msg);
48+
}
49+
50+
public void warning(String msg) {
51+
log(Level.WARNING, msg);
52+
}
53+
54+
public void severe(String msg) {
55+
log(Level.SEVERE, msg);
56+
}
57+
58+
public void config(String msg) {
59+
log(Level.CONFIG, msg);
60+
}
61+
62+
public void fine(String msg) {
63+
log(Level.FINE, msg);
64+
}
65+
66+
public void finer(String msg) {
67+
log(Level.FINER, msg);
68+
}
69+
70+
public void finest(String msg) {
71+
log(Level.FINEST, msg);
72+
}
73+
74+
public void log(Level level, String msg) {
75+
System.out.println("[" + level.getName() + "] " + name + ": " + msg);
76+
}
77+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package gov.nasa.jpf.test.java.util;
2+
3+
import gov.nasa.jpf.util.test.TestJPF;
4+
import org.junit.Test;
5+
import java.util.logging.Logger;
6+
7+
public class LoggerTest extends TestJPF {
8+
9+
@Test
10+
public void testGetLogger() {
11+
if (verifyNoPropertyViolation()) {
12+
System.out.println("--- calling getLogger");
13+
Logger log = Logger.getLogger("testLogger");
14+
assertNotNull(log);
15+
System.out.println("--- success: " + log.getName());
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)