-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add output registry for custom outputs
- Loading branch information
Showing
4 changed files
with
83 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/src/main/java/io/jstach/rainbowgum/LogOutputRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.jstach.rainbowgum; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Register output providers by URI scheme. | ||
*/ | ||
public interface LogOutputRegistry extends LogOutputProvider { | ||
|
||
/** | ||
* Register a provider by {@link URI#getScheme() scheme}. | ||
* @param scheme URI scheme to match for. | ||
* @param provider provider for scheme. | ||
*/ | ||
public void put(String scheme, LogOutputProvider provider); | ||
|
||
/** | ||
* Default output provider. | ||
* @return default output provider. | ||
*/ | ||
public static LogOutputRegistry of() { | ||
return new DefaultOutputRegistry(); | ||
} | ||
|
||
} | ||
|
||
class DefaultOutputRegistry implements LogOutputRegistry { | ||
|
||
private final Map<String, LogOutputProvider> providers = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void put(String scheme, LogOutputProvider provider) { | ||
providers.put(scheme, provider); | ||
} | ||
|
||
@Override | ||
public LogOutput output(URI uri) throws IOException { | ||
String scheme = uri.getScheme(); | ||
String path = uri.getPath(); | ||
LogOutputProvider customProvider; | ||
if (scheme == null && path != null) { | ||
@SuppressWarnings("resource") | ||
FileOutputStream fos = new FileOutputStream(path); | ||
return LogOutput.of(uri, fos.getChannel()); | ||
} | ||
else if (LogOutput.STDOUT_SCHEME.equals(scheme)) { | ||
return LogOutput.ofStandardOut(); | ||
} | ||
else if (LogOutput.STDERR_SCHEME.equals(scheme)) { | ||
return LogOutput.ofStandardErr(); | ||
} | ||
else if ((customProvider = providers.get(scheme)) != null) { | ||
return customProvider.output(uri); | ||
} | ||
else { | ||
var p = Paths.get(uri); | ||
var channel = FileChannel.open(p, StandardOpenOption.APPEND, StandardOpenOption.CREATE); | ||
channel.close(); | ||
return LogOutput.of(uri, channel); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters