001 package toolbus;
002
003 import java.io.File;
004 import java.io.IOException;
005 import java.net.URL;
006
007 import org.eclipse.core.runtime.FileLocator;
008 import org.eclipse.core.runtime.IConfigurationElement;
009 import org.eclipse.core.runtime.IExtension;
010 import org.eclipse.core.runtime.IExtensionPoint;
011 import org.eclipse.core.runtime.IExtensionRegistry;
012 import org.eclipse.core.runtime.Platform;
013 import org.eclipse.core.runtime.Plugin;
014 import org.eclipse.ui.IStartup;
015 import org.osgi.framework.Bundle;
016
017 import aterm.pure.PureFactory;
018
019 public class ToolBusEclipsePlugin extends Plugin implements IStartup{
020 private static final String pluginId = "toolbus";
021
022 private static final String toolbusConfig = "config";
023
024 private static class SingletonToolBus{
025 private final ToolBus toolbus;
026
027 private SingletonToolBus(){
028 super();
029 String configFile = getConfigFile();
030
031 if (configFile != null) {
032 toolbus = new ToolBus(new String[]{"-properties", getConfigFile()});
033 }
034 else {
035 throw new RuntimeException("There was no proper toolbus extension found, so the ToolBus will not function");
036 }
037 }
038
039 public ToolBus getToolBus(){
040 return toolbus;
041 }
042 }
043
044 private static class InstanceKeeper{
045 private static SingletonToolBus instance;
046 static{
047 instance = new SingletonToolBus();
048 runToolBus(instance.getToolBus());
049 }
050 }
051
052 /**
053 * This constructor should only be called by the Eclipse Workbench.
054 */
055 public ToolBusEclipsePlugin(){
056 super();
057 }
058
059 public void earlyStartup(){
060 getInstance(); // Initialize this thing.
061 }
062
063 /**
064 * The plugin activator is a singleton. Use this method to obtain the
065 * instance.
066 */
067 private static SingletonToolBus getInstance(){
068 return InstanceKeeper.instance;
069 }
070
071 /**
072 * This method is needed for other plugins to connect to the Bus.
073 *
074 * @return The port number to connect to using the adapter.
075 */
076 public static int getPort(){
077 return getInstance().getToolBus().getPort();
078 }
079
080 public static PureFactory getFactory(){
081 return getInstance().getToolBus().getTBTermFactory();
082 }
083
084 public static ToolBus getToolBus(){
085 return getInstance().getToolBus();
086 }
087
088 private static void runToolBus(final ToolBus toolbus){
089 try{
090 toolbus.parsecup();
091 toolbus.prepare();
092 Thread thread = new Thread(new Runnable(){
093 public void run(){
094 toolbus.execute();
095 }
096 });
097 thread.setName(pluginId);
098 thread.start();
099
100 DebugConsole debugConsole = new DebugConsole(toolbus);
101 debugConsole.show();
102 }catch(Exception rex){
103 rex.printStackTrace();
104 throw new RuntimeException(rex);
105 }
106 }
107
108 private static String getConfigFile(){
109 IExtensionRegistry registry = Platform.getExtensionRegistry();
110 IExtensionPoint point = registry.getExtensionPoint(pluginId, toolbusConfig);
111
112 IExtension extensions[] = point.getExtensions();
113
114 if(extensions.length == 1){
115 IConfigurationElement[] configElements = extensions[0].getConfigurationElements();
116 for(int i = configElements.length - 1; i >= 0; i--){
117 IConfigurationElement ce = configElements[i];
118 if(ce.getName().equals("toolbus")){
119 String configPath = ce.getAttribute("config");
120
121 Bundle bundle = Platform.getBundle(ce.getContributor().getName());
122 File file;
123
124 try{
125 file = getFile(bundle, configPath);
126
127 if (file != null) {
128 return file.getAbsolutePath();
129 }
130 }catch(IOException e){
131 System.err.println("Failed to get config file: " + configPath);
132 e.printStackTrace();
133 }
134 }
135 }
136 }
137
138 return null;
139 }
140
141 /**
142 * This method finds the file pointing to by resourcePath in bundle, and returns its file handle.
143 *
144 * @param bundle
145 * @param resourcePath
146 * @return
147 * @throws IOException
148 */
149 public static File getFile(Bundle bundle, String resourcePath) throws IOException {
150 URL url = bundle.getEntry(resourcePath);
151
152 File file = null;
153
154 if(url != null){
155 file = new File(FileLocator.toFileURL(url).getPath());
156 }
157
158 return file;
159 }
160
161 }