Metadata.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package griffon.core.env;
019 
020 import org.slf4j.Logger;
021 import org.slf4j.LoggerFactory;
022 
023 import javax.annotation.Nonnull;
024 import java.io.File;
025 import java.io.FileInputStream;
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.net.URL;
029 import java.util.Collections;
030 import java.util.LinkedHashSet;
031 import java.util.Properties;
032 import java.util.Set;
033 
034 import static griffon.util.GriffonNameUtils.requireNonBlank;
035 import static java.util.Objects.requireNonNull;
036 
037 /**
038  * Represents the application Metadata
039  *
040  @author Andres Almiray
041  @since 2.0.0
042  */
043 public class Metadata {
044     public static final String FILENAME = "application.properties";
045     public static final String APPLICATION_VERSION = "application.version";
046     public static final String APPLICATION_NAME = "application.name";
047     private static final Logger LOG = LoggerFactory.getLogger(Metadata.class);
048 
049     private final Properties properties = new Properties();
050 
051     public Metadata(@Nonnull String path) {
052         requireNonBlank(path, "Argument 'path' must not be blank");
053         loadProperties(path);
054     }
055 
056     public Metadata(@Nonnull File file) {
057         requireNonNull(file, "Argument 'file' must not be null");
058         loadProperties(file);
059     }
060 
061     public Metadata(@Nonnull InputStream in) {
062         requireNonNull(in, "Argument 'in' must not be null");
063         try {
064             loadProperties(in);
065         catch (IOException e) {
066             LOG.warn("Could not load application metadata from inpustream.", e);
067         }
068     }
069 
070     private void loadProperties(String path) {
071         URL url = Metadata.class.getClassLoader().getResource(path);
072         if (url != null) {
073             try {
074                 loadProperties(url.openStream());
075             catch (IOException e) {
076                 LOG.warn("Could not load application metadata from " + path, e);
077             }
078         }
079     }
080 
081     private void loadProperties(File file) {
082         try {
083             loadProperties(new FileInputStream(file));
084         catch (IOException e) {
085             LOG.warn("Could not load application metadata from " + file.getAbsolutePath(), e);
086         }
087     }
088 
089     private void loadProperties(InputStream inthrows IOException {
090         properties.load(in);
091     }
092 
093     /**
094      @return The application version
095      */
096     public String getApplicationVersion() {
097         return get(APPLICATION_VERSION);
098     }
099 
100     /**
101      @return The environment the application expects to run in
102      */
103     public String getEnvironment() {
104         return get(Environment.KEY);
105     }
106 
107     public String getRunMode() {
108         return get(RunMode.KEY);
109     }
110 
111     /**
112      @return The application name
113      */
114     public String getApplicationName() {
115         return get(APPLICATION_NAME);
116     }
117 
118     public String get(@Nonnull String key) {
119         return properties.getProperty(requireNonBlank(key, "Argument 'key' must not be blank"));
120     }
121 
122     @Nonnull
123     public Set<String> keySet() {
124         Set<String> keys = new LinkedHashSet<>();
125         for (Object key : properties.keySet()) {
126             keys.add(String.valueOf(key));
127         }
128         return Collections.unmodifiableSet(keys);
129     }
130 }