GriffonEnvironment.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 java.net.URL;
024 import java.util.Properties;
025 
026 import static griffon.util.GriffonNameUtils.isBlank;
027 
028 /**
029  @author Andres Almiray
030  */
031 public class GriffonEnvironment {
032     private static final Logger LOG = LoggerFactory.getLogger(GriffonEnvironment.class);
033 
034     private static final String BUILD_DATE;
035     private static final String BUILD_TIME;
036     private static final String BUILD_REVISION;
037     private static final String GRIFFON_VERSION;
038 
039     static {
040         String buildDate = null;
041         String buildTime = null;
042         String buildRevision = null;
043         String version = null;
044 
045         try {
046             Properties griffonProperties = new Properties();
047             URL griffonPropertiesResource = GriffonEnvironment.class.getClassLoader().getResource("META-INF/griffon-core.properties");
048 
049             if (griffonPropertiesResource != null) {
050                 griffonProperties.load(griffonPropertiesResource.openStream());
051                 buildDate = griffonProperties.getProperty("build.date");
052                 buildTime = griffonProperties.getProperty("build.time");
053                 buildRevision = griffonProperties.getProperty("build.revision");
054                 version = griffonProperties.getProperty("griffon.version");
055             }
056 
057             if (isBlank(buildDate|| isBlank(buildTime|| isBlank(version)) {
058                 LOG.error("Unable to read Griffon version from META-INF/griffon-core.properties. Are you sure the griffon-core jar is in the classpath?");
059                 buildDate = buildTime = buildRevision = version = "";
060             }
061         catch (Exception e) {
062             LOG.error("Unable to read Griffon version from META-INF/griffon-core.properties. Are you sure the griffon-core jar is in the classpath? " + e.getMessage(), e);
063             buildDate = buildTime = buildRevision = version = "";
064         }
065 
066         BUILD_DATE = buildDate;
067         BUILD_TIME = buildTime;
068         BUILD_REVISION = buildRevision;
069         GRIFFON_VERSION = version;
070     }
071 
072     private GriffonEnvironment() {
073         // disable instantiation
074     }
075 
076     public static String getGriffonVersion() {
077         return GRIFFON_VERSION;
078     }
079 
080     public static String getJvmVersion() {
081         StringBuilder sb = new StringBuilder();
082         sb.append(System.getProperty("java.version"))
083             .append(" (")
084             .append(System.getProperty("java.vendor"))
085             .append(" ")
086             .append(System.getProperty("java.vm.version"))
087             .append(")");
088         return sb.toString();
089     }
090 
091     public static String getOsVersion() {
092         StringBuilder sb = new StringBuilder();
093         sb.append(System.getProperty("os.name"))
094             .append(" ")
095             .append(System.getProperty("os.version"))
096             .append(" ")
097             .append(System.getProperty("os.arch"));
098         return sb.toString();
099     }
100 
101     public static String getBuildDateTime() {
102         return BUILD_DATE + "T" + BUILD_TIME;
103     }
104 
105     public static String getBuildDate() {
106         return BUILD_DATE;
107     }
108 
109     public static String getBuildTime() {
110         return BUILD_TIME;
111     }
112 
113     public static String getBuildRevision() {
114         return BUILD_REVISION;
115     }
116 
117     public static String prettyPrint() {
118         padLeft("Griffon"8" ");
119 
120         final StringBuilder sb = new StringBuilder();
121         sb.append("\n------------------------------------------------------------\n")
122             .append(padLeft("Griffon"9" "))
123             .append(" ")
124             .append(getGriffonVersion())
125             .append("\n------------------------------------------------------------\n\n");
126         entry("Build", getBuildDateTime(), sb);
127         entry("Revision", getBuildRevision(), sb);
128         entry("JVM", getJvmVersion(), sb);
129         entry("OS", getOsVersion(), sb);
130         return sb.toString();
131     }
132 
133     private static void entry(String label, String version, StringBuilder sb) {
134         sb.append(padLeft(label, 8" "))
135             .append(": ")
136             .append(version)
137             .append("\n");
138     }
139 
140     private static String padLeft(String self, Number numberOfChars, String padding) {
141         int numChars = numberOfChars.intValue();
142         if (numChars <= self.length()) {
143             return self;
144         else {
145             return getPadding(padding, numChars - self.length()) + self;
146         }
147     }
148 
149     private static String getPadding(String padding, int length) {
150         if (padding.length() < length) {
151             return multiply(padding, length / padding.length() 1).substring(0, length);
152         else {
153             return padding.substring(0, length);
154         }
155     }
156 
157     private static String multiply(String self, Number factor) {
158         int size = factor.intValue();
159         if (size == 0)
160             return "";
161         else if (size < 0) {
162             throw new IllegalArgumentException("multiply() should be called with a number of 0 or greater not: " + size);
163         }
164         StringBuilder answer = new StringBuilder(self);
165         for (int i = 1; i < size; i++) {
166             answer.append(self);
167         }
168         return answer.toString();
169     }
170 }