Repository: incubator-tamaya Updated Branches: refs/heads/master ad1562867 -> 29a83d9e0
[TAMAYA-164] Logging configurable. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/29a83d9e Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/29a83d9e Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/29a83d9e Branch: refs/heads/master Commit: 29a83d9e01761a91040a24e51737a422ea0eae96 Parents: ad15628 Author: Oliver B. Fischer <[email protected]> Authored: Sat Nov 19 19:52:04 2016 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Sat Nov 19 19:52:04 2016 +0100 ---------------------------------------------------------------------- .../apache/tamaya/ConfigurationProvider.java | 35 +++--------- .../tamaya/banner/AbstractBannerPrinter.java | 59 ++++++++++++++++++++ .../org/apache/tamaya/banner/BannerManager.java | 55 ++++++++++++++++++ .../org/apache/tamaya/banner/BannerPrinter.java | 31 ++++++++++ .../tamaya/banner/ConsoleBannerPrinter.java | 29 ++++++++++ .../tamaya/banner/LoggingBannerPrinter.java | 33 +++++++++++ .../tamaya/banner/SilentBannerPrinter.java | 28 ++++++++++ 7 files changed, 244 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/29a83d9e/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java index 9aa54b7..7c65da0 100644 --- a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java +++ b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java @@ -18,18 +18,12 @@ */ package org.apache.tamaya; +import org.apache.tamaya.banner.BannerManager; import org.apache.tamaya.spi.ConfigurationContext; import org.apache.tamaya.spi.ConfigurationContextBuilder; import org.apache.tamaya.spi.ConfigurationProviderSpi; import org.apache.tamaya.spi.ServiceContextManager; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; - /** * Static access to the {@link Configuration} for the very application. */ @@ -37,32 +31,21 @@ public final class ConfigurationProvider { private static final ConfigurationProviderSpi PROVIDER_SPI = loadSpi(); + static { + String bannerConfig = PROVIDER_SPI.getConfiguration().getOrDefault("tamaya.banner", "OFF"); + + BannerManager bm = new BannerManager(bannerConfig); + bm.outputBanner(); + } + private static ConfigurationProviderSpi loadSpi() { ConfigurationProviderSpi spi = ServiceContextManager.getServiceContext() .getService(ConfigurationProviderSpi.class); if(spi==null){ throw new IllegalStateException("ConfigurationProviderSpi not available."); } - showBanner(); - return spi; - } - private static void showBanner() { - try{ - URL url = ConfigurationProvider.class.getResource("/tamaya-banner.txt"); - - if (url!=null) { - Path path = Paths.get(url.toURI()); - List<String> content = Files.readAllLines(path, StandardCharsets.UTF_8); - - for (String line : content) { - System.out.println(line); - } - } - } - catch (Exception e){ - System.out.println("************ TAMAYA CONFIG ************"); - } + return spi; } private ConfigurationProvider() { http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/29a83d9e/code/api/src/main/java/org/apache/tamaya/banner/AbstractBannerPrinter.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/banner/AbstractBannerPrinter.java b/code/api/src/main/java/org/apache/tamaya/banner/AbstractBannerPrinter.java new file mode 100644 index 0000000..1cab9d9 --- /dev/null +++ b/code/api/src/main/java/org/apache/tamaya/banner/AbstractBannerPrinter.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tamaya.banner; + +import org.apache.tamaya.ConfigurationProvider; + +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Abstract base class for an implementation of the + * {@link BannerPrinter} providing the common functionality + * of all implementations. + */ +abstract class AbstractBannerPrinter implements BannerPrinter { + private static final Logger log = Logger.getLogger(AbstractBannerPrinter.class.getName()); + + @Override + public void outputBanner() { + try { + URL url = ConfigurationProvider.class.getResource("/tamaya-banner.txt"); + + if (url != null) { + Path path = Paths.get(url.toURI()); + List<String> content = Files.readAllLines(path, StandardCharsets.UTF_8); + + for (String line : content) { + outputSingleLine(line); + } + } + } catch (Exception e) { + log.log(Level.FINE, "Failed to output the banner of tamaya.", e); + } + } + + abstract void outputSingleLine(CharSequence line); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/29a83d9e/code/api/src/main/java/org/apache/tamaya/banner/BannerManager.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/banner/BannerManager.java b/code/api/src/main/java/org/apache/tamaya/banner/BannerManager.java new file mode 100644 index 0000000..a63b7c2 --- /dev/null +++ b/code/api/src/main/java/org/apache/tamaya/banner/BannerManager.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tamaya.banner; + + +public class BannerManager { + enum BannerTarget { + OFF, CONSOLE, LOGGER + } + + private BannerTarget bannerTarget; + + public BannerManager(String value) { + try { + bannerTarget = BannerTarget.valueOf(value); + } catch (NullPointerException | IllegalArgumentException e) { + bannerTarget = BannerTarget.OFF; + } + } + + public void outputBanner() { + BannerPrinter bp = new SilentBannerPrinter(); + + switch (bannerTarget) { + case CONSOLE: + bp = new ConsoleBannerPrinter(); + break; + case LOGGER: + bp = new LoggingBannerPrinter(); + break; + case OFF: + default: + break; + } + + bp.outputBanner(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/29a83d9e/code/api/src/main/java/org/apache/tamaya/banner/BannerPrinter.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/banner/BannerPrinter.java b/code/api/src/main/java/org/apache/tamaya/banner/BannerPrinter.java new file mode 100644 index 0000000..96b2aad --- /dev/null +++ b/code/api/src/main/java/org/apache/tamaya/banner/BannerPrinter.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tamaya.banner; + +/** + * Outputs the Tamaya banner to an implementation specific output channel + * as STDOUT or the logging system. + */ +public interface BannerPrinter { + /** + * Outputs the banner to the output channel + * used by the implementation. + */ + void outputBanner(); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/29a83d9e/code/api/src/main/java/org/apache/tamaya/banner/ConsoleBannerPrinter.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/banner/ConsoleBannerPrinter.java b/code/api/src/main/java/org/apache/tamaya/banner/ConsoleBannerPrinter.java new file mode 100644 index 0000000..07ec961 --- /dev/null +++ b/code/api/src/main/java/org/apache/tamaya/banner/ConsoleBannerPrinter.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tamaya.banner; + +/** + * Prints the banner to the console. + */ +public class ConsoleBannerPrinter extends AbstractBannerPrinter { + @Override + void outputSingleLine(CharSequence line) { + System.out.println(line); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/29a83d9e/code/api/src/main/java/org/apache/tamaya/banner/LoggingBannerPrinter.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/banner/LoggingBannerPrinter.java b/code/api/src/main/java/org/apache/tamaya/banner/LoggingBannerPrinter.java new file mode 100644 index 0000000..eb2d79b --- /dev/null +++ b/code/api/src/main/java/org/apache/tamaya/banner/LoggingBannerPrinter.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tamaya.banner; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Logs the banner via JUL at level {@link java.util.logging.Level#INFO}. + */ +public class LoggingBannerPrinter extends AbstractBannerPrinter { + private static final Logger log = Logger.getLogger(LoggingBannerPrinter.class.getName()); + @Override + void outputSingleLine(CharSequence line) { + log.log(Level.INFO, line.toString()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/29a83d9e/code/api/src/main/java/org/apache/tamaya/banner/SilentBannerPrinter.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/banner/SilentBannerPrinter.java b/code/api/src/main/java/org/apache/tamaya/banner/SilentBannerPrinter.java new file mode 100644 index 0000000..8669ad4 --- /dev/null +++ b/code/api/src/main/java/org/apache/tamaya/banner/SilentBannerPrinter.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tamaya.banner; + +/** + * Silent implementation of a {@link BannerPrinter}. + */ +public class SilentBannerPrinter implements BannerPrinter { + @Override + public void outputBanner() { + } +}
