blob: c841a4297509922c3d26b6592dd469550260e3e3 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2013, 2020 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.internal.rj.servi;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.management.OperationsException;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.rj.RjInitFailedException;
import org.eclipse.statet.rj.server.util.RJContext;
import org.eclipse.statet.rj.servi.node.PropertiesBean;
import org.eclipse.statet.rj.servi.node.PropertiesBean.ValidationMessage;
@NonNullByDefault
public class MXUtils {
public static boolean validate(final PropertiesBean bean) throws OperationsException {
final List<ValidationMessage> messages= new ArrayList<>();
if (bean.validate(messages)) {
return true;
}
final StringBuilder sb= new StringBuilder();
sb.append(MessageFormat.format("The configuration ''{0}'' has invalid values:\n", bean.getBeanId()));
for (final ValidationMessage message : messages) {
if (message.getPropertyId() != null) {
sb.append("{");
sb.append(message.getPropertyId());
sb.append("}: ");
}
sb.append(message);
sb.append('\n');
}
sb.deleteCharAt(sb.length() - 1);
throw new OperationsException(sb.toString());
}
public static PropertiesBean loadInit(final PropertiesBean bean, final RJContext context) throws RjInitFailedException {
try {
final Properties map= context.loadProperties(bean.getBeanId());
if (map == null) {
Utils.logInfo(MessageFormat.format("The configuration ''{0}'' could not be found. Default values are used.", bean.getBeanId()));
}
else {
bean.load(map);
bean.validate(null);
}
return bean;
}
catch (final IOException e) {
throw new RjInitFailedException(MessageFormat.format("Failed to load configuration ''{0}''.", bean.getBeanId()), e);
}
}
public static PropertiesBean load(final PropertiesBean bean, final RJContext context) throws OperationsException {
try {
final Properties map= context.loadProperties(bean.getBeanId());
if (map != null) {
synchronized (bean) {
bean.load(map);
validate(bean);
}
}
return bean;
}
catch (final IOException e) {
throw new OperationsException(MessageFormat.format("Failed to load configuration ''{0}'': {1}", bean.getBeanId(), e.getMessage()));
}
}
public static void save(final PropertiesBean bean, final RJContext context) throws OperationsException {
try {
final Properties map= new Properties();
synchronized (bean) {
if (!validate(bean)) {
return;
}
bean.save(map);
}
context.saveProperties(bean.getBeanId(), map);
return;
}
catch (final IOException e) {
throw new OperationsException(MessageFormat.format("Failed to save configuration ''{0}'': {1}", bean.getBeanId(), e.getMessage()));
}
}
}