1 | package de.uka.ipd.sdq.sensorframework; |
2 | |
3 | |
4 | import java.io.DataInputStream; |
5 | import java.io.DataOutputStream; |
6 | import java.io.File; |
7 | import java.io.FileInputStream; |
8 | import java.io.FileOutputStream; |
9 | import java.util.Collection; |
10 | |
11 | import org.eclipse.core.runtime.IConfigurationElement; |
12 | import org.eclipse.core.runtime.IExtensionRegistry; |
13 | import org.eclipse.core.runtime.IStatus; |
14 | import org.eclipse.core.runtime.Platform; |
15 | import org.eclipse.core.runtime.Plugin; |
16 | import org.eclipse.core.runtime.Status; |
17 | import org.osgi.framework.BundleContext; |
18 | |
19 | import de.uka.ipd.sdq.sensorframework.adapter.AdapterRegistry; |
20 | import de.uka.ipd.sdq.sensorframework.adapter.IAdapterFactory; |
21 | import de.uka.ipd.sdq.sensorframework.dao.memory.MemoryDAOFactory; |
22 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
23 | import de.uka.ipd.sdq.sensorframework.filter.FilteredCollectionRegistry; |
24 | import de.uka.ipd.sdq.sensorframework.filter.IFilteredCollectionFactory; |
25 | |
26 | /** |
27 | * The activator class controls the plug-in life cycle |
28 | * @author Roman Andrej |
29 | * @author Steffen Becker |
30 | |
31 | */ |
32 | public class SensorFrameworkPluginActivator extends Plugin { |
33 | |
34 | /** The plug-in ID. */ |
35 | public static final String PLUGIN_ID = "de.uka.ipd.sdq.sensorframework"; |
36 | |
37 | /** EPID - Filter extension Point ID. */ |
38 | private static final String ADAPTER_EPID = "de.uka.ipd.sdq.sensorframework.adapter"; |
39 | |
40 | /** EPID - Filter extension Point ID. */ |
41 | private static final String FILTER_EPID = "de.uka.ipd.sdq.sensorframework.filter"; |
42 | |
43 | /** The shared instance. */ |
44 | private static SensorFrameworkPluginActivator plugin; |
45 | |
46 | /** The constructor. */ |
47 | public SensorFrameworkPluginActivator() { |
48 | } |
49 | |
50 | /* (non-Javadoc) |
51 | * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext) |
52 | */ |
53 | @Override |
54 | public void start(BundleContext context) throws Exception { |
55 | super.start(context); |
56 | plugin = this; |
57 | IExtensionRegistry registry = Platform.getExtensionRegistry(); |
58 | |
59 | for (IConfigurationElement configurationElement : registry |
60 | .getConfigurationElementsFor(ADAPTER_EPID)) { |
61 | |
62 | IAdapterFactory factory = (IAdapterFactory) configurationElement |
63 | .createExecutableExtension("class"); |
64 | AdapterRegistry.singleton().addAdapterFactory(factory); |
65 | } |
66 | |
67 | for (IConfigurationElement configurationElement : registry |
68 | .getConfigurationElementsFor(FILTER_EPID)) { |
69 | |
70 | IFilteredCollectionFactory factory = (IFilteredCollectionFactory) configurationElement |
71 | .createExecutableExtension("class"); |
72 | String filterName = configurationElement |
73 | .getAttribute("displayName"); |
74 | FilteredCollectionRegistry.singleton().addFilteredCollectionFactory(filterName, factory); |
75 | } |
76 | |
77 | try { |
78 | File file = context.getDataFile(PLUGIN_ID); |
79 | FileInputStream fis = new FileInputStream(file); |
80 | DataInputStream dos = new DataInputStream(fis); |
81 | while (fis.available() > 0) { |
82 | long id = dos.readLong(); |
83 | String className = dos.readUTF(); |
84 | String config = dos.readUTF(); |
85 | |
86 | IDAOFactory factory = (IDAOFactory) Class.forName(className) |
87 | .getConstructor(String.class).newInstance(config); |
88 | factory.setID((int) id); |
89 | SensorFrameworkDataset.singleton().addDataSource(factory); |
90 | } |
91 | } catch (Exception e) { |
92 | log(IStatus.ERROR, "Restoring Dataset Configuration failed. Resetting configuration...", e); |
93 | if (SensorFrameworkDataset.singleton().getDataSources().size() == 0) |
94 | SensorFrameworkDataset.singleton().addDataSource( |
95 | new MemoryDAOFactory(0)); |
96 | } |
97 | } |
98 | |
99 | /* (non-Javadoc) |
100 | * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) |
101 | */ |
102 | @Override |
103 | public void stop(BundleContext context) throws Exception { |
104 | plugin = null; |
105 | File f = context.getDataFile("de.uka.ipd.sdq.sensorframework"); |
106 | try { |
107 | f.delete(); |
108 | DataOutputStream dos = new DataOutputStream(new FileOutputStream(f)); |
109 | Collection<IDAOFactory> sources = SensorFrameworkDataset.singleton().getDataSources(); |
110 | for (IDAOFactory source : sources) { |
111 | try { |
112 | source.finalizeAndClose(); |
113 | } catch (Exception e) { |
114 | log(IStatus.ERROR, "Failed to close datasource with ID "+source.getID(),e); |
115 | } |
116 | dos.writeLong(source.getID()); |
117 | dos.writeUTF(source.getClass().getName()); |
118 | dos.writeUTF(source.getPersistendInfo()); |
119 | } |
120 | dos.close(); |
121 | } catch (Exception e) { |
122 | log(IStatus.ERROR, "Saving Dataset Configuration failed.",e); |
123 | f.deleteOnExit(); |
124 | } |
125 | super.stop(context); |
126 | } |
127 | |
128 | /** |
129 | * Returns the shared instance |
130 | * |
131 | * @return the shared instance |
132 | */ |
133 | public static SensorFrameworkPluginActivator getDefault() { |
134 | return plugin; |
135 | } |
136 | |
137 | public static void log(int severity, String message, Throwable t) { |
138 | if (plugin != null) { |
139 | plugin.getLog().log(new Status(severity,PLUGIN_ID,message,t)); |
140 | } |
141 | } |
142 | } |