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