Skip to content

Commit

Permalink
1025: migrate from Joda Time to java.time (#1361)
Browse files Browse the repository at this point in the history
* 1025: migrate from Joda Time to java.time; use java.time.ZonedDateTime directly instead of custom Timestamp class

Signed-off-by: Denis N. Antonioli <[email protected]>

* 1025: adding tests for the LogOutputSpec

Signed-off-by: Denis N. Antonioli <[email protected]>

Co-authored-by: Roland Huß <[email protected]>
  • Loading branch information
denisa and rhuss authored Aug 30, 2020
1 parent 02822a7 commit 7a74393
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 188 deletions.
2 changes: 2 additions & 0 deletions doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* **0.33-SNAPSHOT**
- Support `ARG` in `FROM` (#859)
- Handle authentication tokens returned from credential helpers ([#1348](https://github.com/fabric8io/docker-maven-plugin/issues/1348))
- Migrate from joda-time to java.time ([#1025](https://github.com/fabric8io/docker-maven-plugin/issues/1025))
The handling of Y changes when the week straddle the New year ([Stack Overflow](https://stackoverflow.com/questions/26431882/difference-between-year-of-era-and-week-based-year))

* **0.33.0** (2020-01-21)
- Update to jnr-unixsocket 0.25 to solve concurrency issues ([#552](https://github.com/fabric8io/docker-maven-plugin/issues/552))
Expand Down
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.4</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions src/main/asciidoc/inc/start/_logging.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ a| Dateformat to use for log timestamps. If `<date>` is not given no timestamp w
(`-Ddocker.logDate=NONE`) for switching off otherwise enabled
logging.
* `DEFAULT` A default format in the form `HH:mm:ss.SSS`
* `MEDIUM` Joda medium date time format
* `SHORT` Joda short date time format
* `LONG` Joda long date time format
* `MEDIUM` java.time medium date time format
* `SHORT` java.time short date time format
* `LONG` java.time long date time format
* `ISO8601` Full ISO-8601 formatted date time with milliseconds
As an alternative a date-time format string as recognized by
http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html[JodaTime] is possible. In order to set a consistent date format, the global configuration parameter `logDate` can be used.
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html[java.time] is possible. In order to set a consistent date format, the global configuration parameter `logDate` can be used.

| *color*
| Color used for coloring the prefix when coloring is enabled (i.e. if running in a console and `useColor` is set). The available colors are `YELLOW`, `CYAN`, `MAGENTA`, `GREEN`, `RED`, `BLUE`. If coloring is enabled and now color is provided a color is picked for you.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.JsonFactory;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.Timestamp;
import io.fabric8.maven.docker.util.TimestampFactory;

/**
* Implementation using <a href="http://hc.apache.org/">Apache HttpComponents</a>
Expand Down Expand Up @@ -178,7 +178,7 @@ public Object handleResponse(HttpResponse response) throws IOException {
try {
callback.open();
while ( (line = reader.readLine()) != null) {
callback.log(1, new Timestamp(), line);
callback.log(1, TimestampFactory.createTimestamp(), line);
}
} catch (LogCallback.DoneException e) {
// Ok, we stop here ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
*/

import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.concurrent.CancellationException;

import io.fabric8.maven.docker.util.Timestamp;

/**
* Interface called for each log line received from the docker host when asynchronous
* log fetching is used.
Expand All @@ -36,7 +35,7 @@ public interface LogCallback {
* @param txt log output
* @throws CancellationException if thrown will stop the logging.
*/
void log(int type, Timestamp timestamp, String txt) throws DoneException;
void log(int type, ZonedDateTime timestamp, String txt) throws DoneException;

/**
* Method called in case on an error when reading the logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.time.ZonedDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -28,7 +29,7 @@
import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.access.UrlBuilder;
import io.fabric8.maven.docker.access.util.RequestUtil;
import io.fabric8.maven.docker.util.Timestamp;
import io.fabric8.maven.docker.util.TimestampFactory;
import org.apache.commons.codec.binary.Hex;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
Expand Down Expand Up @@ -196,7 +197,7 @@ private void callLogCallback(int type, String txt) throws LogCallback.DoneExcept
txt,(int) (txt.toCharArray())[0],(int) (txt.toCharArray())[1]));
throw new LogCallback.DoneException();
}
Timestamp ts = new Timestamp(matcher.group("timestamp"));
ZonedDateTime ts = TimestampFactory.createTimestamp(matcher.group("timestamp"));
String logTxt = matcher.group("entry");
callback.log(type, ts, logTxt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/

import java.io.*;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;

import com.google.common.io.Files;
import io.fabric8.maven.docker.access.log.LogCallback;
import io.fabric8.maven.docker.util.Timestamp;

/**
* @author roland
Expand Down Expand Up @@ -77,7 +77,7 @@ private PrintStream ps() {
}

@Override
public void log(int type, Timestamp timestamp, String txt) {
public void log(int type, ZonedDateTime timestamp, String txt) {
addLogEntry(ps(), new LogEntry(type, timestamp, txt));
}

Expand All @@ -101,10 +101,10 @@ private void addLogEntry(PrintStream ps, LogEntry logEntry) {
// A single log-entry
private static class LogEntry implements Comparable<LogEntry> {
private final int type;
private final Timestamp timestamp;
private final ZonedDateTime timestamp;
private final String text;

public LogEntry(int type, Timestamp timestamp, String text) {
public LogEntry(int type, ZonedDateTime timestamp, String text) {
this.type = type;
this.timestamp = timestamp;
this.text = text;
Expand All @@ -114,7 +114,7 @@ public int getType() {
return type;
}

public Timestamp getTimestamp() {
public ZonedDateTime getTimestamp() {
return timestamp;
}

Expand Down
28 changes: 15 additions & 13 deletions src/main/java/io/fabric8/maven/docker/log/LogOutputSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* limitations under the License.
*/

import io.fabric8.maven.docker.util.Timestamp;
import org.fusesource.jansi.Ansi;
import org.joda.time.format.*;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

import static org.fusesource.jansi.Ansi.Color.*;
import static org.fusesource.jansi.Ansi.ansi;
Expand Down Expand Up @@ -62,19 +64,19 @@ public boolean isLogStdout() {
return logStdout;
}

public String getPrompt(boolean withColor,Timestamp timestamp) {
public String getPrompt(boolean withColor, ZonedDateTime timestamp) {
return formatTimestamp(timestamp,withColor) + formatPrefix(prefix, withColor);
}

public String getFile(){
return file;
}

private String formatTimestamp(Timestamp timestamp,boolean withColor) {
private String formatTimestamp(ZonedDateTime timestamp, boolean withColor) {
if (timeFormatter == null) {
return "";
}
String date = timeFormatter.print(timestamp.getDate());
String date = timeFormatter.format(timestamp);
return (withColor ?
ansi().fgBright(BLACK).a(date).reset().toString() :
date) + " ";
Expand Down Expand Up @@ -138,24 +140,24 @@ public Builder timeFormatter(String formatOrConstant) {
|| formatOrConstant.equalsIgnoreCase("FALSE")) {
timeFormatter = null;
} else if (formatOrConstant.length() == 0 || formatOrConstant.equalsIgnoreCase("DEFAULT")) {
timeFormatter = DateTimeFormat.forPattern("HH:mm:ss.SSS");
timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
} else if (formatOrConstant.equalsIgnoreCase("ISO8601")) {
timeFormatter = ISODateTimeFormat.dateTime();
timeFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
} else if (formatOrConstant.equalsIgnoreCase("SHORT")) {
timeFormatter = DateTimeFormat.shortDateTime();
timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
} else if (formatOrConstant.equalsIgnoreCase("MEDIUM")) {
timeFormatter = DateTimeFormat.mediumDateTime();
timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
} else if (formatOrConstant.equalsIgnoreCase("LONG")) {
timeFormatter = DateTimeFormat.longDateTime();
timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
} else {
try {
timeFormatter = DateTimeFormat.forPattern(formatOrConstant);
timeFormatter = DateTimeFormatter.ofPattern(formatOrConstant);
} catch (IllegalArgumentException exp) {
throw new IllegalArgumentException(
"Cannot parse log date specification '" + formatOrConstant + "'." +
"Must be either DEFAULT, NONE, ISO8601, SHORT, MEDIUM, LONG or a " +
"format string parseable by DateTimeFormat. See " +
"http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html");
"format string parseable by DateTimeFormatter. See " +
"https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html");
}
}
return this;
Expand Down
103 changes: 0 additions & 103 deletions src/main/java/io/fabric8/maven/docker/util/Timestamp.java

This file was deleted.

48 changes: 48 additions & 0 deletions src/main/java/io/fabric8/maven/docker/util/TimestampFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.fabric8.maven.docker.util;/*
*
* Copyright 2014 Roland Huss
*
* 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.
*/

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

/**
* Factory class for timestamp, eg {@link ZonedDateTime}.
*
* @author roland
* @since 25/11/14
*/
public class TimestampFactory {
private TimestampFactory() {
// Empty Constructor
}

/**
* Create a timestamp for *now*
*/
public static ZonedDateTime createTimestamp() {
return ZonedDateTime.now();
}

/**
* Create a timestamp by parsing the given string representation which must be in an extended ISO 8601 Format
* with Nanoseconds since this is the format used by Docker for logging (e.g. "2014-11-24T22:34:00.761764812Z")
*
* @param spec date specification to parse
*/
public static ZonedDateTime createTimestamp(String spec) {
return ZonedDateTime.parse(spec).truncatedTo(ChronoUnit.MILLIS);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.fabric8.maven.docker.wait;

import java.time.ZonedDateTime;
import java.util.regex.Pattern;

import io.fabric8.maven.docker.access.log.LogCallback;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.Timestamp;

class LogMatchCallback implements LogCallback {

Expand All @@ -21,7 +21,7 @@ class LogMatchCallback implements LogCallback {
}

@Override
public void log(int type, Timestamp timestamp, String txt) throws DoneException {
public void log(int type, ZonedDateTime timestamp, String txt) throws DoneException {
logger.debug("LogWaitChecker: Trying to match '%s' [Pattern: %s] [thread: %d]",
txt, pattern.pattern(), Thread.currentThread().getId());

Expand Down
Loading

0 comments on commit 7a74393

Please sign in to comment.