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