blob: 267ff494f6b77e9ab4078d20504978723ec3ce10 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.preferences;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.core.internal.preferences.AbstractScope;
import org.eclipse.core.internal.preferences.InstancePreferences;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.BackingStoreException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ProductInstanceScope extends AbstractScope {
private static final Logger LOGGER = LoggerFactory.getLogger(ProductInstanceScope.class);
public static final String SCOPE = InstanceScope.SCOPE;
private static final String DEFAULT_FILE = "DEFAULTFILE";
private final String productConfigurationFile;
/**
* Create and return a new project scope for the given project. The given
* project must not be <code>null</code>.
*
* @exception IllegalArgumentException if the project is <code>null</code>
*/
public ProductInstanceScope() {
String property = System.getProperty("productConfigurationFile");
productConfigurationFile = property == null ? null : property.replaceAll(DEFAULT_FILE, ProductConfigurationPrefs.PREFERENCES_FULL_PATH);
}
/*
* @see org.eclipse.core.runtime.preferences.IScopeContext#getLocation()
*/
@Override
public IPath getLocation() {
return productConfigurationFile == null ? null : new Path(productConfigurationFile);
}
/*
* @see org.eclipse.core.runtime.IScopeContext#getNode(java.lang.String)
*/
@Override
public IEclipsePreferences getNode(String qualifier) {
if (qualifier == null)
throw new IllegalArgumentException();
IEclipsePreferences preference = super.getNode(qualifier);
if (productConfigurationFile != null) {
try {
Class<?> preferenceClass = preference.getClass();
Method getLoadLevel = preferenceClass.getDeclaredMethod("getLoadLevel");
getLoadLevel.setAccessible(true);
InstancePreferences loadLevelInstance = (InstancePreferences) getLoadLevel.invoke(preference);
Class<?>loadLevelClass = loadLevelInstance.getClass();
Field locationField = loadLevelClass.getDeclaredField("location");
locationField.setAccessible(true);
IPath activeLocation = (IPath) locationField.get(loadLevelInstance);
if ((activeLocation == null) || !activeLocation.toOSString().equals(getLocation().toOSString())) {
locationField.set(loadLevelInstance, getLocation());
preference.sync();
LOGGER.info("set preference file to: "+getLocation().toOSString());
}
preference.sync();
}
catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException | BackingStoreException | NoSuchMethodException | SecurityException | InvocationTargetException x) {
LOGGER.error("exception while trying to check and set preference file", x);
}
}
return preference;
}
/*
* @see org.eclipse.core.runtime.preferences.IScopeContext#getName()
*/
@Override
public String getName() {
return SCOPE;
}
}