blob: 9b1c32b45a12ef657c1831be36e6e073ac373be8 [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;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
/**
* This class manages all instances of VPM Frameworks.
*
* @authorh Andras Schmidt
*/
public class FrameworkManager {
Map<String, Framework> frameworks = new TreeMap<String, Framework>();
ArrayList<IFrameworkListChangedListener> listeners = new ArrayList<IFrameworkListChangedListener>();
static FrameworkManager inst = null;
/**
* Get the frameworkmanager instance. Before using first the
* extensionprovider must be set. (<code>setExtensionProvider</code>)
*
* @return the frameworkmanager instance
*/
public static FrameworkManager getInstance() {
if (inst == null) {
inst = new FrameworkManager();
}
return inst;
}
private FrameworkManager() {
}
/**
* Get the framework with the given id.
*
* @param id
* @return the framework with the given id or null if no framework with
* given id is in memory.
*/
public Framework getFramework(String id) {
return frameworks.get(id);
}
private ExtensionProvider defExprovider;
/**
* Set the extension provider for this framework manager instance.
*
* @param exProvider
* extension provider which searches all extensions for the VPM
* modeling system
*/
public void setExtensionProvider(ExtensionProvider exProvider) {
defExprovider = exProvider;
}
/**
* Create a framework with loading the given file into the model.
*
* @param fileName
* file to be loaded
* @return the created framework
* @throws Exception
*/
public IFramework createFramework(String fileName)
throws FrameworkManagerException {
return createFramework(fileName, getExtensionProvider());
}
/**
* Create a framework with loading the given file into the model.
*
* @param file
* file to be loaded
* @param name
* name of the framework to be created
* @return the created framework
* @throws Exception
*/
public IFramework createFramework(InputStream file, String name)
throws FrameworkManagerException {
return createFramework(file, getExtensionProvider(), name);
}
private ExtensionProvider getExtensionProvider() {
/*
* ExtensionProvider exProvider; if(ViatraPlugin.getDefault()!=null) {
* exProvider=new EclipseExtensionProvider(); } else {
* exProvider=defExprovider; }
*/
return defExprovider;
}
public IFramework createFramework(String fileName,
ExtensionProvider exProvider) throws FrameworkManagerException {
/*
* if(exProvider==null) throw new
* Exception("Extension provider required."); String id=genId();
* Framework newFramework=new Framework();
* newFramework.init(fileName,id,exProvider); //
* newFramework.addImporterPlugins(nat_imp); // adding the importer
* factory information to the framework instance.
*
* frameworks.put(id,newFramework);
* notifyFrameworkListChangedListeners(); return newFramework;
*/
try {
return createFramework(new FileInputStream(fileName), exProvider,
fileName);
} catch (FileNotFoundException e) {
throw new FrameworkManagerException("file not found", e);
}
}
public IFramework createFramework(InputStream fileStream,
ExtensionProvider exProvider, String name)
throws FrameworkManagerException {
if (exProvider == null)
throw new FrameworkManagerException("Extension provider required.");
String id = genId();
Framework newFramework = new Framework();
try {
newFramework.init(fileStream, id, exProvider, name);
} catch (FrameworkException e) {
throw new FrameworkManagerException("framework init", e);
}
// newFramework.addImporterPlugins(nat_imp); // adding the importer
// factory information to the framework instance.
frameworks.put(id, newFramework);
for (IFrameworkListChangedListener l : new ArrayList<IFrameworkListChangedListener>(
listeners)) {
l.frameworkAdded(newFramework);
}
// notifyFrameworkListChangedListeners();
return newFramework;
}
public IFramework createFramework() throws FrameworkException// throws
// Exception
{
ExtensionProvider exProvider = getExtensionProvider();
String id = genId();
Framework newFramework = new Framework();
newFramework.init(null, id, exProvider, "null");
// newFramework.addImporterPlugins(nat_imp); // adding the importer
// factory information to the framework instance.
frameworks.put(id, newFramework);
// notifyFrameworkListChangedListeners();
for (IFrameworkListChangedListener l : new ArrayList<IFrameworkListChangedListener>(
listeners)) {
l.frameworkAdded(newFramework);
}
return newFramework;
}
/**
* Store the available native importers that are discovered by the Plugin.
*
* @param v
* The vector of importer factories.
*/
// Vector nat_imp = null;
/*
* public void addImporterPlugins(Vector v) { nat_imp = v; }
*/
/**
* Dispose the framework with the given id.
*
* @param id
*/
public void disposeFramework(String id) throws FrameworkManagerException {
if (frameworks.containsKey(id)) {
Framework fw = frameworks.get(id);
fw.dispose();
frameworks.remove(id);
for (IFrameworkListChangedListener l : new ArrayList<IFrameworkListChangedListener>(
listeners)) {
l.frameworkRemoved(fw);
}
// notifyFrameworkListChangedListeners();
}
// else
// throw new FrameworkManagerException("Couldn't dispose framework!");
}
/**
* Get the ids of all frameworks in memory.
*
* @return ids of frameworks in memory
*/
public String[] getAllFrameWorks() {
return frameworks.keySet().toArray(new String[0]);
}
int counter = 0;
/**
* Generate a new id for a new framework.
*
* @return new id
*/
private String genId() {
return "modelSpace" + counter++;
}
public void addFrameworkListChangedListener(IFrameworkListChangedListener l) {
if (!listeners.contains(l))
listeners.add(l);
}
public void removeFrameworkListChangedListener(
IFrameworkListChangedListener l) {
if (listeners.contains(l))
listeners.remove(l);
}
public static File convertURLtoFile(URL url) throws IllegalArgumentException {
URI uri;
try
{
uri = url.toURI();
}
catch (URISyntaxException e) {
try
{
uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
} catch (URISyntaxException e1) {
throw new IllegalArgumentException("The URL cannot be converted: " + url);
}
}
return new File(uri);
}
public static File getFileFromBundle(String bundleId, String path) {
// ugly hack: switch /->\ on windoze
if ("\\".equals(System.getProperty("file.separator"))) {
// switch / to \ on windoze
path = path.replace('/', '\\');
} else if ("/".equals(System.getProperty("file.separator"))) {
path = path.replace('\\', '/');
}
File f = null;
// if the bundle is not ready then there is no file, however we may
// assume its perfectly ready
Bundle bundle = Platform.getBundle(bundleId);
if (bundle == null)// || !BundleUtility.isReady(bundle))
return null;
// look for the image (this will check both the plugin and fragment
// folders
java.net.URL fullPathString = null;
try {
// fullPathString = BundleUtility.find(bundle, path);
fullPathString = FileLocator.toFileURL(FileLocator.find(bundle,
new Path(path), null));
f = convertURLtoFile(fullPathString);
} catch (IllegalArgumentException e) {
f = new File(fullPathString.getPath());
} // impossible
catch (IOException e) {
e.printStackTrace();
}
return f;
}
}