blob: a3c2f53f438b6e984dfad05e7718dfb58d6c243b [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro
* 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:
* Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.framework.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
import org.eclipse.viatra2.framework.properties.providers.DummyPropertyProvider;
/**
* Dynamic multiple properties for VPM modeling.
*
* Note: this class has (too) many functionalities. Note2: all getter functions
* implicitly assume that they are queried with registered provider IDs.
*
* @author Istvan Rath
*
*/
public class VPMProperties {
private static final long serialVersionUID = 3838142787694854860L;
/**
* String -> PropertyProvider mapping.
*/
private HashMap<String, IViatraPropertyProvider> multiProperties;
/**
* Container to keep track of listeners which receive a notification when a
* new provider is registered or deregistered.
*/
private Vector<IViatraPropertyProvidersChangedListener> listeners;
public VPMProperties() {
multiProperties = new HashMap<String, IViatraPropertyProvider>();
listeners = new Vector<IViatraPropertyProvidersChangedListener>();
}
public boolean isProviderActive(String providerID) {
return multiProperties.containsKey(providerID)
&& multiProperties.get(providerID).isActive();
}
public void addPropertyProvider(IViatraPropertyProvider provider) {
if (!multiProperties.containsKey(provider)) {
multiProperties.put(provider.getProviderID(), provider);
// FIXME what if there exists a dummy provider for this provider ID?
notifyListeners();
}
}
public void removePropertyProvider(IViatraPropertyProvider provider) {
if (multiProperties.containsKey(provider)) {
multiProperties.remove(provider.getProviderID());
// FIXME what to do with dummy?
notifyListeners();
}
}
public Set<String> getAllProviderIDs() {
return multiProperties.keySet();
}
/**
* Returns a property provider with a given ID.
*
* @param providerID
* provider ID string
* @return property provider object
*/
public IViatraPropertyProvider getProvider(String providerID) {
return multiProperties.get(providerID);
}
/**
* Returns all the Property Keys of the Properties container of this
* IFramework.
*/
public Set<String> getAllRuntimePropertyIDs(String providerID) {
return multiProperties.get(providerID).getAllPropertyIds();
}
/**
* Sets a runtime property of this IFramework instance
*
* @param propid
* the runtime property's unique string id
* @param value
* the desired value
*/
public void setRuntimeProperty(String providerID, String propid,
String value) {
if (multiProperties.get(providerID) != null)
multiProperties.get(providerID).setProperty(propid, value);
}
/**
* Gets the value of a runtime property of this IFramework instance
*
* @param propid
* the runtime property's unique string id
* @return the value of the runtime property, empty string if not found!
*/
public String getRuntimeProperty(String providerID, String propid) {
if (multiProperties.get(providerID) != null)
return multiProperties.get(providerID).getProperty(propid);
else
//return null;
return "";
}
/**
* Gets whether a given runtime property is set for a given provider id.
*
* @param providerID
* @param propid
* @return true if the property is set
*/
public boolean isRuntimePropertySet(String providerID, String propid) {
try {
return multiProperties.get(providerID).getProperty(propid) != null;
} catch (Exception e) {
return false;
}
}
/**
* This method is used to load the multiproperties from a
* java.util.Properties serialization. Its use is discouraged as we should
* load properties from VPML files now.
*
* @param s
* @throws IOException
*/
public void load(InputStream s) throws IOException {
Properties p = new Properties();
p.load(s);
loadFromJavaProperties(p);
}
private static final String ProviderIDPrefixDelimiter = "@";
/**
* Recreate the multi property registry from a simple java.util.Properties
* instance.
*
* Note: this function must be called AFTER all active property providers
* have been registered.
*
* @param p
*/
public void loadFromJavaProperties(Properties p) {
for (Object _key : p.keySet().toArray()) {
String key = _key.toString();
if (key.indexOf(ProviderIDPrefixDelimiter) < 0) {
// System.out.println("Invalid property key: "+key);
continue;
}
String providerID = key.substring(0, key
.indexOf(ProviderIDPrefixDelimiter));
String propid = key.substring(key
.indexOf(ProviderIDPrefixDelimiter)
+ ProviderIDPrefixDelimiter.length());
if (!multiProperties.containsKey(providerID)) {
// no currently active provider
// create dummy provider
DummyPropertyProvider dp = new DummyPropertyProvider(providerID);
addPropertyProvider(dp);
}
setRuntimeProperty(providerID, propid, p.get(key).toString());
}
}
/**
* "Serialize" as a standard java.util.Properties instance. In this
* registry, property keys are prefixed by
* ProviderID+ProProviderIDPrefixDelimiter
*/
public Properties getJavaProperties() {
Properties p = new Properties();
for (String key : multiProperties.keySet()) {
IViatraPropertyProvider prov = multiProperties.get(key);
for (String _key : prov.getAllPropertyIds()) {
p.put(prov.getProviderID() + ProviderIDPrefixDelimiter + _key,
prov.getProperty(_key));
}
}
return p;
}
/**
* Register a new listener for providerset changed events.
*
* @param l
*/
public void addProvideryProvidersChangedListener(
IViatraPropertyProvidersChangedListener l) {
if (!listeners.contains(l))
listeners.add(l);
}
/**
* Deregister a listener for providerset changed events.
*
* @param l
*/
public void removeProvideryProvidersChangedListener(
IViatraPropertyProvidersChangedListener l) {
if (listeners.contains(l)) {
listeners.remove(l);
listeners.trimToSize();
}
}
private void notifyListeners() {
for (IViatraPropertyProvidersChangedListener l : listeners) {
l.propertyProvidersChanged();
}
}
}