Skip to content

Commit 52488ca

Browse files
committed
ongoing work on LOGBACK-108
1 parent a50baab commit 52488ca

19 files changed

Lines changed: 171 additions & 85 deletions

File tree

logback-access/src/test/java/ch/qos/logback/access/db/DBAppenderIntegrationTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import ch.qos.logback.access.spi.IAccessEvent;
2121
import ch.qos.logback.core.status.StatusChecker;
22+
import ch.qos.logback.core.testUtil.EnvUtilForTests;
23+
import ch.qos.logback.core.util.EnvUtil;
2224
import org.junit.After;
2325
import org.junit.AfterClass;
2426
import org.junit.Before;
@@ -32,12 +34,11 @@
3234
import ch.qos.logback.core.Appender;
3335
import ch.qos.logback.core.joran.spi.JoranException;
3436
import ch.qos.logback.core.status.Status;
35-
import ch.qos.logback.core.testUtil.Env;
3637
import ch.qos.logback.core.util.StatusPrinter;
3738

3839
public class DBAppenderIntegrationTest {
3940

40-
static String LOCAL_HOST_NAME = Env.getLocalHostName();
41+
static String LOCAL_HOST_NAME = EnvUtilForTests.getLocalHostName();
4142
static String[] CONFORMING_HOST_LIST = new String[] { "Orion" };
4243

4344
int diff = new Random(System.nanoTime()).nextInt(10000);
@@ -81,10 +82,10 @@ public void doTest(String configFile) throws JoranException {
8182
}
8283

8384
static boolean isConformingHostAndJDK16OrHigher() {
84-
if(!Env.isJDK6OrHigher()) {
85+
if(!EnvUtil.isJDK6OrHigher()) {
8586
return false;
8687
}
87-
return Env.isLocalHostNameInList(CONFORMING_HOST_LIST);
88+
return EnvUtilForTests.isLocalHostNameInList(CONFORMING_HOST_LIST);
8889
}
8990

9091
@Test

logback-classic/src/test/java/ch/qos/logback/classic/LoggerPerfTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static org.junit.Assert.assertTrue;
1717

18+
import ch.qos.logback.core.testUtil.EnvUtilForTests;
1819
import org.junit.Before;
1920
import org.junit.Ignore;
2021
import org.junit.Test;
@@ -25,7 +26,6 @@
2526
import ch.qos.logback.core.CoreConstants;
2627
import ch.qos.logback.core.UnsynchronizedAppenderBase;
2728
import ch.qos.logback.core.helpers.NOPAppender;
28-
import ch.qos.logback.core.testUtil.Env;
2929

3030

3131
@Ignore
@@ -149,7 +149,7 @@ public void durationOfDisabledLog_1_Parameter() {
149149

150150
@Test
151151
public void durationOfEnabledLog() {
152-
if (Env.isLinux()) {
152+
if (EnvUtilForTests.isLinux()) {
153153
// the JIT on Linux behaves very differently
154154
return;
155155
}

logback-classic/src/test/java/ch/qos/logback/classic/db/DBAppenderIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.HashMap;
2626
import java.util.Map;
2727

28+
import ch.qos.logback.core.testUtil.EnvUtilForTests;
29+
import ch.qos.logback.core.util.EnvUtil;
2830
import org.junit.After;
2931
import org.junit.AfterClass;
3032
import org.junit.Before;
@@ -39,7 +41,6 @@
3941
import ch.qos.logback.core.db.DriverManagerConnectionSource;
4042
import ch.qos.logback.core.joran.spi.JoranException;
4143
import ch.qos.logback.core.status.StatusChecker;
42-
import ch.qos.logback.core.testUtil.Env;
4344
import ch.qos.logback.core.testUtil.RandomUtil;
4445
import ch.qos.logback.core.util.StatusPrinter;
4546

@@ -187,7 +188,7 @@ Throwable getCause() {
187188
}
188189

189190
static boolean isConformingHostAndJDK16OrHigher(String[] conformingHostList) {
190-
if (!Env.isJDK6OrHigher()) {
191+
if (!EnvUtil.isJDK6OrHigher()) {
191192
return false;
192193
}
193194
for (String conformingHost : conformingHostList) {

logback-classic/src/test/java/ch/qos/logback/classic/spi/LoggingEventSerializationPerfTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.ObjectOutputStream;
2121

22+
import ch.qos.logback.core.testUtil.EnvUtilForTests;
2223
import org.junit.After;
2324
import org.junit.Before;
2425
import org.junit.Test;
@@ -30,7 +31,6 @@
3031
import ch.qos.logback.classic.net.testObjectBuilders.LoggingEventWithParametersBuilder;
3132
import ch.qos.logback.classic.net.testObjectBuilders.TrivialLoggingEventBuilder;
3233
import ch.qos.logback.core.CoreConstants;
33-
import ch.qos.logback.core.testUtil.Env;
3434

3535
// As of logback 0.9.15,
3636
// average time per logging event: 3979 nanoseconds
@@ -86,7 +86,7 @@ public void tearDown() throws Exception {
8686

8787
@Test
8888
public void testPerformance() {
89-
if (Env.isLinux()) {
89+
if (EnvUtilForTests.isLinux()) {
9090
return;
9191
}
9292
TrivialLoggingEventBuilder builder = new TrivialLoggingEventBuilder();
@@ -114,7 +114,7 @@ public void testPerformance() {
114114

115115
@Test
116116
public void testPerformanceWithParameters() {
117-
if (Env.isLinux()) {
117+
if (EnvUtilForTests.isLinux()) {
118118
return;
119119
}
120120
LoggingEventWithParametersBuilder builder = new LoggingEventWithParametersBuilder();

logback-classic/src/test/java/ch/qos/logback/classic/turbo/ReconfigureOnChangeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import ch.qos.logback.core.status.InfoStatus;
3232
import ch.qos.logback.core.status.Status;
3333
import ch.qos.logback.core.status.StatusChecker;
34-
import ch.qos.logback.core.testUtil.Env;
34+
import ch.qos.logback.core.testUtil.EnvUtilForTests;
3535
import ch.qos.logback.core.testUtil.FileTestUtil;
3636
import ch.qos.logback.core.testUtil.RandomUtil;
3737
import ch.qos.logback.core.util.CoreTestConstants;
@@ -311,7 +311,7 @@ ReconfigureOnChangeFilter initROCF() throws MalformedURLException {
311311
@Test
312312
@Ignore
313313
public void directPerfTest() throws MalformedURLException {
314-
if (Env.isLinux()) {
314+
if (EnvUtilForTests.isLinux()) {
315315
// for some reason this test does not pass on Linux (AMD 64 bit,
316316
// Dual Core Opteron 170)
317317
return;
@@ -339,7 +339,7 @@ public double directLoop(ReconfigureOnChangeFilter rocf) {
339339
@Ignore
340340
@Test
341341
public void indirectPerfTest() throws MalformedURLException {
342-
if (Env.isLinux()) {
342+
if (EnvUtilForTests.isLinux()) {
343343
// for some reason this test does not pass on Linux (AMD 64 bit,
344344
// Dual Core
345345
// Opteron 170)

logback-classic/src/test/java/ch/qos/logback/classic/turbo/ReconfigurePerf.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.File;
1717
import java.io.IOException;
1818

19+
import ch.qos.logback.core.testUtil.EnvUtilForTests;
1920
import org.junit.Before;
2021
import org.junit.Ignore;
2122
import org.junit.Test;
@@ -28,7 +29,6 @@
2829
import ch.qos.logback.core.contention.MultiThreadedHarness;
2930
import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
3031
import ch.qos.logback.core.joran.spi.JoranException;
31-
import ch.qos.logback.core.testUtil.Env;
3232

3333
@Ignore
3434
public class ReconfigurePerf {
@@ -57,7 +57,7 @@ public class ReconfigurePerf {
5757
@Before
5858
public void setUp() {
5959
// take into account propagation latency occurs on Linux
60-
if (Env.isLinux()) {
60+
if (EnvUtilForTests.isLinux()) {
6161
sleepBetweenUpdates = 850;
6262
totalTestDuration = sleepBetweenUpdates * numberOfCycles;
6363
} else {

logback-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<url>http://logback.qos.ch</url>
1919

2020
<properties>
21-
<scala.version>2.10.1</scala.version>
21+
<scala.version>2.9.1</scala.version>
2222
</properties>
2323

2424
<licenses>

logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileStoreUtil.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
package ch.qos.logback.core.rolling.helper;
1515

1616
import java.io.File;
17-
import java.io.IOException;
18-
import java.nio.file.FileStore;
19-
import java.nio.file.Files;
20-
import java.nio.file.Path;
17+
import java.lang.reflect.Method;
2118

2219
/**
2320
* A utility class using functionality available since JDK 1.7.
@@ -27,13 +24,32 @@
2724
*/
2825
public class FileStoreUtil {
2926

30-
static boolean areOnSameFileStore(File a, File b) throws IOException {
31-
Path pathA = a.toPath();
32-
Path pathB = b.toPath();
27+
static final String PATH_CLASS_STR = "java.nio.file.Path";
28+
static final String FILES_CLASS_STR = "java.nio.file.Files";
3329

34-
FileStore fileStoreA = Files.getFileStore(pathA);
35-
FileStore fileStoreB = Files.getFileStore(pathB);
30+
static public boolean areOnSameFileStore(File a, File b) throws Exception {
3631

32+
// Implement the following by reflection
33+
// Path pathA = a.toPath();
34+
// Path pathB = b.toPath();
35+
//
36+
// FileStore fileStoreA = Files.getFileStore(pathA);
37+
// FileStore fileStoreB = Files.getFileStore(pathB);
38+
//
39+
// return fileStoreA.equals(fileStoreB);
40+
41+
Class pathClass = Class.forName(PATH_CLASS_STR);
42+
Class filesClass = Class.forName(FILES_CLASS_STR);
43+
44+
Method toPath = File.class.getMethod("toPath");
45+
Method getFileStoreMethod = filesClass.getMethod("getFileStore", pathClass);
46+
47+
48+
Object pathA = toPath.invoke(a);
49+
Object pathB = b.toPath();
50+
51+
Object fileStoreA = getFileStoreMethod.invoke(null, pathA);
52+
Object fileStoreB = getFileStoreMethod.invoke(null, pathB);
3753
return fileStoreA.equals(fileStoreB);
3854
}
3955
}

logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RenameUtil.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import ch.qos.logback.core.rolling.RollingFileAppender;
2626
import ch.qos.logback.core.rolling.RolloverFailure;
2727
import ch.qos.logback.core.spi.ContextAwareBase;
28+
import ch.qos.logback.core.util.EnvUtil;
2829
import ch.qos.logback.core.util.FileUtil;
2930

3031

@@ -64,15 +65,39 @@ public void rename(String from, String to) throws RolloverFailure {
6465

6566
if (!result) {
6667
addWarn("Failed to rename file [" + fromFile + "] to [" + toFile + "].");
68+
if(attemptVolumeDetermination(fromFile, toFile)) {
69+
renameByCopying(from, to);
70+
}
6771
addWarn("Please consider leaving the [file] option of "+ RollingFileAppender.class.getSimpleName()+" empty.");
6872
addWarn("See also "+ RENAMING_ERROR_URL);
69-
7073
}
7174
} else {
7275
throw new RolloverFailure("File [" + from + "] does not exist.");
7376
}
7477
}
7578

79+
80+
/**
81+
* Attempts tp determine whether both files are on different volumes. Returns true if we could determine that
82+
* the files are on different volumes. Returns false otherwise or if an error occurred while doing the check.
83+
*
84+
* @param fromFile
85+
* @param toFile
86+
* @return true if on different volumes, false otherwise or if an error occurred
87+
*/
88+
static boolean attemptVolumeDetermination(File fromFile, File toFile) {
89+
if(!EnvUtil.isJDK7OrHigher())
90+
return false;
91+
92+
try {
93+
boolean result = FileStoreUtil.areOnSameFileStore(fromFile, toFile);
94+
return result;
95+
} catch(Exception e) {
96+
return false;
97+
}
98+
}
99+
100+
76101
static final int BUF_SIZE = 32 * 1024;
77102

78103
public void renameByCopying(String from, String to)

logback-core/src/main/java/ch/qos/logback/core/util/EnvUtil.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,46 @@
1313
*/
1414
package ch.qos.logback.core.util;
1515

16+
import java.util.ArrayList;
17+
import java.util.List;
18+
1619
/**
1720
* @author Ceki G&uuml;c&uuml;
1821
*/
1922
public class EnvUtil {
2023

2124

25+
static private boolean isJDK_N_OrHigher(int n) {
26+
List<String> versionList = new ArrayList<String>();
27+
// this code should work at least until JDK 10 (assuming n parameter is
28+
// always 6 or more)
29+
for (int i = 0; i < 5; i++) {
30+
versionList.add("1." + (n + i));
31+
}
32+
33+
String javaVersion = System.getProperty("java.version");
34+
if (javaVersion == null) {
35+
return false;
36+
}
37+
for (String v : versionList) {
38+
if (javaVersion.startsWith(v))
39+
return true;
40+
}
41+
return false;
42+
}
43+
2244
static public boolean isJDK5() {
23-
String javaVersion = System.getProperty("java.version");
24-
if (javaVersion == null) {
25-
return false;
26-
}
27-
if (javaVersion.startsWith("1.5")) {
28-
return true;
29-
} else {
30-
return false;
31-
}
45+
return isJDK_N_OrHigher(5);
3246
}
3347

48+
static public boolean isJDK6OrHigher() {
49+
return isJDK_N_OrHigher(6);
50+
}
51+
52+
static public boolean isJDK7OrHigher() {
53+
return isJDK_N_OrHigher(7);
54+
}
55+
3456
static public boolean isJaninoAvailable() {
3557
ClassLoader classLoader = EnvUtil.class.getClassLoader();
3658
try {

0 commit comments

Comments
 (0)