/** | |
******************************************************************************** | |
* Copyright (c) 2017 Robert Bosch GmbH 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/ | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
* | |
* Contributors: | |
* Robert Bosch GmbH - initial API and implementation | |
******************************************************************************** | |
*/ | |
package org.eclipse.app4mc.sca.ui.util; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Properties; | |
import java.util.Set; | |
import org.eclipse.app4mc.sca.logging.exception.App4mcLoggerException; | |
import org.eclipse.app4mc.sca.logging.manager.Logmanager; | |
import org.eclipse.app4mc.sca.logging.util.SCALogConstants; | |
import org.eclipse.app4mc.sca.ui.Activator; | |
import org.eclipse.app4mc.sca.ui.preferences.interfaces.IExportPreferences; | |
import org.eclipse.app4mc.sca.util.parsers.OptionsFileParser; | |
import org.eclipse.core.runtime.IConfigurationElement; | |
import org.eclipse.core.runtime.IExtension; | |
import org.eclipse.core.runtime.InvalidRegistryObjectException; | |
import org.eclipse.core.runtime.Platform; | |
import org.eclipse.jface.preference.IPreferenceStore; | |
/** | |
* Utilities for all the SCA ui operations | |
*/ | |
public final class SCAToolsUIUtil { | |
/** | |
* Map that contains all the keys that are to be exported for a given tool | |
*/ | |
private static HashMap<String, List<String>> exportKeyList = null; | |
private static HashMap<String, IPreferenceStore> preferenceStoreMap = new HashMap<String, IPreferenceStore>(); | |
/** | |
* Extension point id of {@code productNameAndVersionDefinition} extension point | |
*/ | |
private static final String EXT_POINT_ID = "org.eclipse.app4mc.ui.exportOptions"; | |
/** | |
* Private constructor | |
*/ | |
private SCAToolsUIUtil() { | |
// Private constructor | |
} | |
/** | |
* Loads the contributed keys that are to be exported as options file | |
*/ | |
private static void loadExportKeyContributions() { | |
try { | |
if ((exportKeyList == null)) { | |
synchronized (SCAToolsUIUtil.class) { | |
exportKeyList = new HashMap<String, List<String>>(); | |
} | |
} | |
IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(EXT_POINT_ID).getExtensions(); | |
for (IExtension extension : extensions) { | |
IConfigurationElement[] configElements = extension.getConfigurationElements(); | |
for (IConfigurationElement configElement : configElements) { | |
String preferenceClass = configElement.getAttribute("preferences"); | |
String toolId = configElement.getAttribute("tool"); | |
exportKeyList.put(toolId, fetchKeysFromClass(preferenceClass, extension, exportKeyList.get(toolId), toolId)); | |
} | |
} | |
} | |
catch (InvalidRegistryObjectException e) { | |
Logmanager.getInstance().logException(SCAToolsUIUtil.class, e, SCALogConstants.PLUGIN_ID); | |
} | |
} | |
/** | |
* Fetches the keys to be exported from the contributed class and returns the list | |
* | |
* @param preferenceClass | |
* @param extension The extension from which | |
* @param keyList The key list. null in case its empty | |
* @return Returns the keyList to be added to the tool | |
*/ | |
@SuppressWarnings("unchecked") | |
private static List<String> fetchKeysFromClass(final String preferenceClass, final IExtension extension, | |
final List<String> keyList, final String toolId) { | |
if (preferenceClass != null) { | |
Class<IExportPreferences> preference; | |
try { | |
preference = (Class<IExportPreferences>) Platform.getBundle(extension.getContributor().getName()) | |
.loadClass(preferenceClass); | |
IExportPreferences preferenceInstance = preference.newInstance(); | |
preferenceStoreMap.put(toolId, preferenceInstance.getPreferenceStore()); | |
if (keyList == null) { | |
ArrayList<String> newList = new ArrayList<String>(); | |
newList.addAll(preferenceInstance.keysToExport()); | |
return newList; | |
} | |
keyList.addAll(preferenceInstance.keysToExport()); | |
} | |
catch (ClassNotFoundException | InvalidRegistryObjectException | InstantiationException | |
| IllegalAccessException e) { | |
Logmanager.getInstance().logException(SCAToolsUIUtil.class, e, SCALogConstants.PLUGIN_ID); | |
} | |
} | |
return keyList; | |
} | |
/** | |
* Uses the properties API to export the preferences to the options file | |
* | |
* @param opFile The options file to which the properties are to be exported | |
* @param store the given preference store | |
* @param toolId The tool for which the preferences are to be exported to the options file | |
*/ | |
public static void exportProperties(final File opFile, final IPreferenceStore store, final String toolId) { | |
List<String> keysToExport = getKeysToExport(toolId); | |
IPreferenceStore prefStore = store; | |
if (prefStore == null) { | |
prefStore = preferenceStoreMap.get(toolId); | |
} | |
try { | |
updateAndSaveProperties(opFile, keysToExport, prefStore); | |
} | |
catch (IOException e) { | |
Logmanager.getInstance().logException(SCAToolsUIUtil.class, e, Activator.PLUGIN_ID); | |
} | |
} | |
/** | |
* Saves the given properties to the given file | |
* | |
* @param prop the properties to be saved | |
* @param opFile The file to which the properties are to be saved | |
* @throws IOException exception in case if something goes wrong | |
*/ | |
private static void saveProperties(final Properties properties, final File opFile) throws IOException { | |
FileOutputStream fr = new FileOutputStream(opFile); | |
properties.store(fr, "Properties"); | |
fr.close(); | |
} | |
/** | |
* Retreives the keys for the given tool id | |
* | |
* @param toolId String | |
* @return {@link String} | |
*/ | |
public static List<String> getKeysToExport(final String toolId) { | |
if ((exportKeyList == null)) { | |
loadExportKeyContributions(); | |
} | |
return exportKeyList.get(toolId); | |
} | |
/** | |
* Appends the given preferences to the provided input file | |
* | |
* @param inputOptFilePath THe input options file to which the preferences are exported | |
* @param toolId The tool for which the preferences are to be exported | |
* @throws IOException Exception in case something goes wrong | |
* @throws App4mcLoggerException if the validation fails | |
*/ | |
public static void exportPropertiesToInputFile(final String inputOptFilePath, final String toolId) | |
throws IOException, App4mcLoggerException { | |
File inputFile = new File(inputOptFilePath); | |
validate(toolId, inputOptFilePath, inputFile); | |
List<String> keysToExport = getKeysToExport(toolId); | |
if (!exportKeyList.containsKey(toolId)) { | |
throw new App4mcLoggerException(SCAToolsPreferenceConstants.INVALID_TOOLID); | |
} | |
IPreferenceStore iPreferenceStore = preferenceStoreMap.get(toolId); | |
removePreferencesPresentInOptionsFile(keysToExport, inputOptFilePath); | |
try { | |
updateAndSaveProperties(inputFile, keysToExport, iPreferenceStore); | |
Logmanager.getInstance().log(SCAToolsPreferenceConstants.OPT_EXPORT_SUCCESS); | |
} | |
catch (IOException e) { | |
Logmanager.getInstance().logException(SCAToolsUIUtil.class, e, Activator.PLUGIN_ID); | |
} | |
} | |
/** | |
* @param inputFile | |
* @param keysToExport | |
* @param iPreferenceStore | |
* @throws IOException | |
* @throws App4mcLoggerException | |
*/ | |
private static void updateAndSaveProperties(final File inputFile, final List<String> keysToExport, | |
final IPreferenceStore iPreferenceStore) throws IOException, App4mcLoggerException { | |
Properties properties = new Properties(); | |
InputStream in = new FileInputStream(inputFile); | |
properties.load(in); | |
for (String key : keysToExport) { | |
String prefValue = iPreferenceStore.getString(key); | |
properties.setProperty(key, prefValue); | |
} | |
saveProperties(properties, inputFile); | |
} | |
/** | |
* Removes the keys that are already part of the options file from the keys that are to be exported. This makes sure | |
* that only the keys that are not there in the options file are written on to the file | |
* | |
* @param keysToExport {@link List} | |
*/ | |
private static void removePreferencesPresentInOptionsFile(final List<String> keysToExport, final String optFilePath) { | |
Map<?, ?> properties = loadOptionsFile(optFilePath); | |
Set<?> keySet = properties.keySet(); | |
for (Object key : keySet) { | |
keysToExport.remove(key); | |
} | |
} | |
/** | |
* Loads the given input options file to the properties map | |
* | |
* @param optFilePath the given options file | |
* @return Returns the properties that contains all the key and values | |
*/ | |
private static Map<?, ?> loadOptionsFile(final String optFilePath) { | |
OptionsFileParser parser = new OptionsFileParser(); | |
return parser.load(optFilePath, null); | |
} | |
/** | |
* Validates the parameters | |
*/ | |
private static void validate(final String toolId, final String inputFilePath, final File inputFile) | |
throws App4mcLoggerException { | |
if ((toolId == null) || toolId.isEmpty() || (inputFilePath == null) || inputFilePath.isEmpty()) { | |
throw new App4mcLoggerException(SCAToolsPreferenceConstants.EMPTY_FILE_OR_TOOL_ID); | |
} | |
if (!inputFile.exists()) { | |
throw new App4mcLoggerException(SCAToolsPreferenceConstants.INVALID_FILE); | |
} | |
else if (!inputFilePath.endsWith(SCAToolsPreferenceConstants.OPTIONS_FILE_EXT)) { | |
throw new App4mcLoggerException(SCAToolsPreferenceConstants.INVALID_EXT); | |
} | |
} | |
/** | |
* @return the exportKeyList | |
*/ | |
public static Map<String, List<String>> getExportKeyList() { | |
return exportKeyList; | |
} | |
/** | |
* @return the preferenceStoreMap | |
*/ | |
public static Map<String, IPreferenceStore> getPreferenceStoreMap() { | |
return preferenceStoreMap; | |
} | |
} |