use of com.github.zafarkhaja.semver.Version in project DiscordSRV by Scarsz.
the class ConfigUtil method migrate.
// When I wrote this, only God and I understood what I was doing. Now, God only knows.
// If this code works, it was written by Scarsz. If not, I don’t know who wrote it.
public static void migrate() {
Version configVersion = DiscordSRV.config().getString("ConfigVersion").split("\\.").length == 3 ? Version.valueOf(DiscordSRV.config().getString("ConfigVersion")) : Version.valueOf("1." + DiscordSRV.config().getString("ConfigVersion"));
Version pluginVersion = Version.valueOf(DiscordSRV.getPlugin().getDescription().getVersion());
// no migration necessary
if (configVersion.equals(pluginVersion))
return;
if (configVersion.greaterThan(pluginVersion)) {
DiscordSRV.warning("You're attempting to use a higher config version than the plugin. Things might not work correctly.");
return;
}
DiscordSRV.info("Your DiscordSRV config file was outdated; attempting migration...");
try {
if (configVersion.greaterThanOrEqualTo(Version.forIntegers(1, 13, 0))) {
// messages
File messagesFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "messages.yml-build." + configVersion + ".old");
File messagesTo = DiscordSRV.getPlugin().getMessagesFile();
FileUtils.moveFile(messagesTo, messagesFrom);
LangUtil.saveMessages();
copyYmlValues(messagesFrom, messagesTo);
LangUtil.reloadMessages();
// config
File configFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "config.yml-build." + configVersion + ".old");
File configTo = DiscordSRV.getPlugin().getConfigFile();
FileUtils.moveFile(configTo, configFrom);
LangUtil.saveConfig();
copyYmlValues(configFrom, configTo);
DiscordSRV.getPlugin().reloadConfig();
} else {
// messages
File messagesFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "config.yml");
File messagesTo = DiscordSRV.getPlugin().getMessagesFile();
LangUtil.saveMessages();
copyYmlValues(messagesFrom, messagesTo);
LangUtil.reloadMessages();
// config
File configFrom = new File(DiscordSRV.getPlugin().getDataFolder(), "config.yml-build." + configVersion + ".old");
File configTo = DiscordSRV.getPlugin().getConfigFile();
FileUtils.moveFile(configTo, configFrom);
LangUtil.saveConfig();
copyYmlValues(configFrom, configTo);
DiscordSRV.getPlugin().reloadConfig();
// channels
File channelsFile = new File(DiscordSRV.getPlugin().getDataFolder(), "channels.json");
if (channelsFile.exists()) {
List<Map<String, String>> channels = new ArrayList<>();
JsonArray jsonElements = DiscordSRV.getPlugin().getGson().fromJson(FileUtils.readFileToString(channelsFile, Charset.forName("UTF-8")), JsonArray.class);
for (JsonElement jsonElement : jsonElements) {
channels.add(new HashMap<String, String>() {
{
put(jsonElement.getAsJsonObject().get("channelname").getAsString(), jsonElement.getAsJsonObject().get("channelid").getAsString());
}
});
}
String channelsString = "{" + channels.stream().map(stringStringMap -> "\"" + stringStringMap.keySet().iterator().next() + "\": \"" + stringStringMap.values().iterator().next() + "\"").collect(Collectors.joining(", ")) + "}";
FileUtils.writeStringToFile(channelsFile, "Channels: " + channelsString, Charset.forName("UTF-8"));
copyYmlValues(channelsFile, configTo);
channelsFile.delete();
}
// colors
File colorsFile = new File(DiscordSRV.getPlugin().getDataFolder(), "colors.json");
FileUtils.moveFile(colorsFile, new File(colorsFile.getParent(), "colors.json.old"));
}
DiscordSRV.info("Successfully migrated configuration files to version " + DiscordSRV.config().getString("ConfigVersion"));
} catch (Exception e) {
DiscordSRV.error("Failed migrating configs: " + e.getMessage());
}
}
use of com.github.zafarkhaja.semver.Version in project incubator-pulsar by apache.
the class BrokerVersionFilter method getLatestVersionNumber.
/**
* Get the most recent broker version number from the load reports of all the running brokers. The version
* number is from the build artifact in the pom and got added to the package when it was built by Maven
*
* @param brokers
* The brokers to choose the latest version string from.
* @param loadData
* The load data from the leader broker (contains the load reports which in turn contain the version string).
* @return The most recent broker version
* @throws BrokerFilterBadVersionException
* If the most recent version is undefined (e.g., a bad broker version was encountered or a broker
* does not have a version string in its load report.
*/
public Version getLatestVersionNumber(Set<String> brokers, LoadData loadData) throws BrokerFilterBadVersionException {
if (null == brokers) {
throw new BrokerFilterBadVersionException("Unable to determine latest version since broker set was null");
}
if (brokers.size() == 0) {
throw new BrokerFilterBadVersionException("Unable to determine latest version since broker set was empty");
}
if (null == loadData) {
throw new BrokerFilterBadVersionException("Unable to determine latest version since loadData was null");
}
Version latestVersion = null;
for (String broker : brokers) {
BrokerData data = loadData.getBrokerData().get(broker);
if (null == data) {
LOG.warn("No broker data for broker [{}]; disabling PreferLaterVersions feature", broker);
// trigger the ModularLoadManager to reset all the brokers to the original set
throw new BrokerFilterBadVersionException("No broker data for broker \"" + broker + "\"");
}
String brokerVersion = data.getLocalData().getBrokerVersionString();
if (null == brokerVersion || brokerVersion.length() == 0) {
LOG.warn("No version string in load report for broker [{}]; disabling PreferLaterVersions feature", broker);
// trigger the ModularLoadManager to reset all the brokers to the original set
throw new BrokerFilterBadVersionException("No version string in load report for broker \"" + broker + "\"");
}
Version brokerVersionVersion = null;
try {
brokerVersionVersion = Version.valueOf(brokerVersion);
} catch (Exception x) {
LOG.warn("Invalid version string in load report for broker [{}]: [{}]; disabling PreferLaterVersions feature", broker, brokerVersion);
// trigger the ModularLoadManager to reset all the brokers to the original set
throw new BrokerFilterBadVersionException("Invalid version string in load report for broker \"" + broker + "\": \"" + brokerVersion + "\")");
}
if (null == latestVersion) {
latestVersion = brokerVersionVersion;
} else if (Version.BUILD_AWARE_ORDER.compare(latestVersion, brokerVersionVersion) < 0) {
latestVersion = brokerVersionVersion;
}
}
if (null == latestVersion) {
throw new BrokerFilterBadVersionException("Unable to determine latest broker version");
}
return latestVersion;
}
use of com.github.zafarkhaja.semver.Version in project ma-core-public by infiniteautomation.
the class ModulesDwr method startDownloads.
@DwrPermission(admin = true)
public static String startDownloads(List<StringStringPair> modules, boolean backup, boolean restart) {
synchronized (UPGRADE_DOWNLOADER_LOCK) {
if (UPGRADE_DOWNLOADER != null)
return Common.translate("modules.versionCheck.occupied");
}
// Check if the selected modules will result in a version-consistent system.
try {
// Create the request
Map<String, Object> json = new HashMap<>();
Map<String, String> jsonModules = new HashMap<>();
json.put("modules", jsonModules);
Version coreVersion = Common.getVersion();
jsonModules.put("core", coreVersion.toString());
for (StringStringPair module : modules) jsonModules.put(module.getKey(), module.getValue());
StringWriter stringWriter = new StringWriter();
new JsonWriter(Common.JSON_CONTEXT, stringWriter).writeObject(json);
String requestData = stringWriter.toString();
// Send the request
String baseUrl = Common.envProps.getString("store.url");
baseUrl += "/servlet/consistencyCheck";
HttpPost post = new HttpPost(baseUrl);
post.setEntity(new StringEntity(requestData));
String responseData = HttpUtils4.getTextContent(Common.getHttpClient(), post, 1);
// Parse the response
JsonTypeReader jsonReader = new JsonTypeReader(responseData);
String result = jsonReader.read().toString();
if (!"ok".equals(result))
return result;
} catch (Exception e) {
LOG.error("", e);
return e.getMessage();
}
synchronized (UPGRADE_DOWNLOADER_LOCK) {
// Ensure that 2 downloads cannot start at the same time.
if (UPGRADE_DOWNLOADER == null) {
UPGRADE_DOWNLOADER = new UpgradeDownloader(modules, backup, restart, Common.getHttpUser());
// Clear out common info
resetUpgradeStatus();
Common.backgroundProcessing.execute(UPGRADE_DOWNLOADER);
} else
return Common.translate("modules.versionCheck.occupied");
}
return null;
}
use of com.github.zafarkhaja.semver.Version in project ma-core-public by infiniteautomation.
the class ModulesDwr method getAvailableUpgrades.
public static JsonValue getAvailableUpgrades() throws JsonException, IOException, HttpException {
// Create the request
List<Module> modules = ModuleRegistry.getModules();
Module.sortByName(modules);
Map<String, Object> json = new HashMap<>();
json.put("guid", Providers.get(ICoreLicense.class).getGuid());
json.put("description", SystemSettingsDao.getValue(SystemSettingsDao.INSTANCE_DESCRIPTION));
json.put("distributor", Common.envProps.getString("distributor"));
json.put("upgradeVersionState", SystemSettingsDao.getIntValue(SystemSettingsDao.UPGRADE_VERSION_STATE));
Properties props = new Properties();
File propFile = new File(Common.MA_HOME + File.separator + "release.properties");
int versionState = UpgradeVersionState.DEVELOPMENT;
if (propFile.exists()) {
InputStream in = new FileInputStream(propFile);
try {
props.load(in);
} finally {
in.close();
}
String currentVersionState = props.getProperty("versionState");
try {
if (currentVersionState != null)
versionState = Integer.valueOf(currentVersionState);
} catch (NumberFormatException e) {
}
}
json.put("currentVersionState", versionState);
Map<String, String> jsonModules = new HashMap<>();
json.put("modules", jsonModules);
Version coreVersion = Common.getVersion();
jsonModules.put("core", coreVersion.toString());
for (Module module : modules) if (!module.isMarkedForDeletion())
jsonModules.put(module.getName(), module.getVersion().toString());
// Add in the unloaded modules so we don't re-download them if we don't have to
for (Module module : ModuleRegistry.getUnloadedModules()) if (!module.isMarkedForDeletion())
jsonModules.put(module.getName(), module.getVersion().toString());
StringWriter stringWriter = new StringWriter();
new JsonWriter(Common.JSON_CONTEXT, stringWriter).writeObject(json);
String requestData = stringWriter.toString();
// Send the request
String baseUrl = Common.envProps.getString("store.url");
baseUrl += "/servlet/versionCheck";
HttpPost post = new HttpPost(baseUrl);
post.setEntity(new StringEntity(requestData));
String responseData = HttpUtils4.getTextContent(Common.getHttpClient(), post, 1);
// Parse the response
JsonTypeReader jsonReader = new JsonTypeReader(responseData);
return jsonReader.read();
}
use of com.github.zafarkhaja.semver.Version in project graylog2-server by Graylog2.
the class VersionDeserializerTest method successfullyDeserializesInteger.
@Test
public void successfullyDeserializesInteger() throws IOException {
final Version version = objectMapper.readValue("5", Version.class);
assertThat(version).isEqualTo(Version.forIntegers(5));
}
Aggregations