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