blob: 65bb0248b7e54f18b2b423394f9ddaf0fce57a18 [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.ui.data;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.eclipse.core.internal.resources.ProjectPreferences;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.osgi.service.prefs.BackingStoreException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.osbp.preferences.ProductConfigurationPrefs;
public final class ProductProjectScope implements IScopeContext {
private static final Logger LOGGER = LoggerFactory.getLogger(ProductProjectScope.class);
/**
* String constant (value of <code>"project"</code>) used for the
* scope name for this preference scope.
*/
public static final String SCOPE = "project"; //$NON-NLS-1$
private IProject context;
/**
* Create and return a new project scope for the given project. The given
* project must not be <code>null</code>.
*
* @param context the project
* @exception IllegalArgumentException if the project is <code>null</code>
*/
public ProductProjectScope(IProject context) {
super();
if (context == null)
throw new IllegalArgumentException();
this.context = context;
}
/*
* @see org.eclipse.core.runtime.preferences.IScopeContext#getLocation()
*/
public IPath getLocation() {
IProject project = ((IResource) context).getProject();
IResource resource = ProductConfigurationPrefs.getPrefsFile(project);
if (resource == null) {
IPath location = project.getLocation();
return location == null ? null : location.append(ProductConfigurationPrefs.PREFERENCES_FULL_PATH);
}
else {
return resource.getLocation();
}
}
/*
* @see org.eclipse.core.runtime.IScopeContext#getNode(java.lang.String)
*/
public IEclipsePreferences getNode(String qualifier) {
IEclipsePreferences preference = null;
if (qualifier == null)
throw new IllegalArgumentException();
preference = (IEclipsePreferences) Platform.getPreferencesService().getRootNode().node(SCOPE).node(context.getName()).node(qualifier);
IFile prefsFile = null;
if (context instanceof IProject) {
IResource resource = ProductConfigurationPrefs.getPrefsFile((IProject)context);
if (resource instanceof IFile) {
prefsFile = (IFile) resource;
}
}
if (prefsFile == null) {
prefsFile = context.getFile(new Path(ProductConfigurationPrefs.PREFERENCES_FULL_PATH));
}
if (prefsFile instanceof IFile) {
try {
Class<?> preferenceClass = preference.getClass();
Method getLoadLevel = preferenceClass.getDeclaredMethod("getLoadLevel");
getLoadLevel.setAccessible(true);
ProjectPreferences loadLevelInstance = (ProjectPreferences) getLoadLevel.invoke(preference);
Class<?>loadLevelClass = loadLevelInstance.getClass();
Field fileField = loadLevelClass.getDeclaredField("file");
fileField.setAccessible(true);
IFile activeFile = (IFile) fileField.get(loadLevelInstance);
if ((activeFile == null) || !activeFile.getFullPath().toOSString().equals(prefsFile.getFullPath().toOSString())) {
fileField.set(loadLevelInstance, prefsFile);
preference.sync();
LOGGER.info("set preference file to: "+prefsFile.getFullPath().toOSString());
}
}
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()
*/
public String getName() {
return SCOPE;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (!(obj instanceof ProductProjectScope))
return false;
ProductProjectScope other = (ProductProjectScope) obj;
return context.equals(other.context);
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return super.hashCode() + context.getFullPath().hashCode();
}
}