/** * Copyright (C) 2010-2017 Structr GmbH * * This file is part of Structr <http://structr.org>. * * Structr is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * Structr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Structr. If not, see <http://www.gnu.org/licenses/>. */ package org.structr.common; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.structr.api.config.Settings; import org.structr.core.Services; import org.structr.core.app.StructrApp; import org.structr.module.StructrModule; /** * Helper class to gather and provide information about the running Structr version. */ public class VersionHelper { private static final String classPath; private static final String instanceName; private static final String instanceStage; private static final Map<String, Map<String, Object>> modules = new HashMap<>(); private static final Map<String, Map<String, String>> components = new HashMap<>(); private static boolean modulesUpdatedAfterSystemInitComplete = false; static { classPath = System.getProperty("java.class.path"); instanceName = Settings.InstanceName.getValue(); instanceStage = Settings.InstanceStage.getValue(); final Pattern outerPattern = Pattern.compile("(structr-.+?(?=.jar))"); final Matcher outerMatcher = outerPattern.matcher(classPath); while (outerMatcher.find()) { final String group = outerMatcher.group(); final Pattern innerPattern = Pattern.compile("(structr-core|structr-rest|structr-ui)-([^-]*(?:-SNAPSHOT){0,1})-{0,1}(?:([0-9]{0,12})\\.{0,1}([0-9a-f]{0,5})).*"); final Matcher innerMatcher = innerPattern.matcher(group); final Map<String, String> module = new HashMap<>(); if (innerMatcher.matches()) { module.put("version", innerMatcher.group(2)); module.put("date", innerMatcher.group(3)); module.put("build", innerMatcher.group(4)); components.put(innerMatcher.group(1), module); } } } public static String getFullVersionInfo() { Map<String, String> structrUiModule = getComponents().get("structr-ui"); if (structrUiModule != null) { return structrUiModule.get("version") + " " + structrUiModule.get("build") + " " + structrUiModule.get("date"); } return "Could not determine version string"; } public static String getClassPath() { return classPath; } public static String getInstanceName() { return instanceName; } public static String getInstanceStage() { return instanceStage; } public static void updateModuleList () { modules.clear(); // collect StructrModules for (final StructrModule module : StructrApp.getConfiguration().getModules().values()) { final Map<String, Object> map = new LinkedHashMap<>(); map.put("source", module.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); if (module.getDependencies() != null) { map.put("dependencies", module.getDependencies()); } if (module.getFeatures() != null) { map.put("features", module.getFeatures()); } modules.put(module.getName(), map); } } public static Map<String, Map<String, Object>> getModules() { if (!modulesUpdatedAfterSystemInitComplete) { updateModuleList(); } modulesUpdatedAfterSystemInitComplete = Services.getInstance().isInitialized(); return modules; } public static Map<String, Map<String, String>> getComponents() { return components; } }