]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/extension/internal/EvaluatorFactoryExtensionManager.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / extension / internal / EvaluatorFactoryExtensionManager.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common.extension.internal;
13
14 import java.util.Arrays;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtension;
21 import org.eclipse.core.runtime.IExtensionPoint;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
25 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
26 import org.eclipse.core.runtime.dynamichelpers.IFilter;
27 import org.simantics.browsing.ui.common.Activator;
28 import org.simantics.browsing.ui.common.ErrorLogger;
29 import org.simantics.browsing.ui.common.extension.EvaluatorFactory;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class EvaluatorFactoryExtensionManager implements IExtensionChangeHandler {
35
36     private final static String                NAMESPACE    = Activator.PLUGIN_ID;
37
38     private final static String                EP_NAME      = "evaluator";
39
40     private ExtensionTracker                   tracker;
41
42     private IEvaluatorFactoryExtension[]            extensions   = new IEvaluatorFactoryExtension[0];
43
44     private static EvaluatorFactoryExtensionManager INSTANCE;
45
46
47     public static synchronized EvaluatorFactoryExtensionManager getInstance() {
48         if (INSTANCE == null)
49             INSTANCE = new EvaluatorFactoryExtensionManager();
50         return INSTANCE;
51     }
52
53     public static synchronized void dispose() {
54         if (INSTANCE != null) {
55             INSTANCE.close();
56             INSTANCE = null;
57         }
58     }
59
60
61     private EvaluatorFactoryExtensionManager() {
62         tracker = new ExtensionTracker();
63
64         // Cache defined actions
65         IExtensionPoint expt = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE, EP_NAME);
66         loadExtensions(expt.getConfigurationElements());
67
68         // Start tracking for new and removed extensions
69         IFilter filter = ExtensionTracker.createExtensionPointFilter(expt);
70         tracker.registerHandler(this, filter);
71     }
72
73     private void close() {
74         tracker.close();
75         tracker = null;
76         extensions = new IEvaluatorFactoryExtension[0];
77     }
78
79     public IEvaluatorFactoryExtension[] getExtensions() {
80         return extensions;
81     }
82
83     private void loadExtensions(IConfigurationElement[] elements) {
84
85         Set<IEvaluatorFactoryExtension> newExtensions = new HashSet<IEvaluatorFactoryExtension>(Arrays.asList(extensions));
86
87         for (IConfigurationElement el : elements) {
88             try {
89
90                 EvaluatorFactory factory = (EvaluatorFactory)el.createExecutableExtension("class");
91                 IEvaluatorFactoryExtension ext = new EvaluatorFactoryExtension(factory);
92
93                 // Start tracking the new extension object, its removal will be notified of
94                 // with removeExtension(extension, Object[]).
95                 tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);
96
97                 newExtensions.add(ext);
98             } catch (CoreException e) {
99                 ErrorLogger.defaultLogError("Failed to initialize " + EP_NAME + " extension \"" + el.getName()
100                         + "\" with name \"" + el.getAttribute("name") + "\": "
101                         + e.getMessage(), e);
102             }
103         }
104
105         // Atomic assignment
106         this.extensions = newExtensions.toArray(new IEvaluatorFactoryExtension[newExtensions.size()]);
107     }
108
109     @Override
110     public void addExtension(IExtensionTracker tracker, IExtension extension) {
111         loadExtensions(extension.getConfigurationElements());
112     }
113
114     @Override
115     public void removeExtension(IExtension extension, Object[] objects) {
116         Set<IEvaluatorFactoryExtension> newExtensions = new HashSet<IEvaluatorFactoryExtension>(Arrays.asList(extensions));
117
118         for (Object o : objects) {
119             tracker.unregisterObject(extension, o);
120             newExtensions.remove(o);
121         }
122
123         // Atomic assignment
124         this.extensions = newExtensions.toArray(new IEvaluatorFactoryExtension[newExtensions.size()]);
125     }
126
127     public static EvaluatorFactory getFactory(String id) {
128         for(IEvaluatorFactoryExtension ext : getInstance().extensions) {
129             if(ext.factory().getClass().getCanonicalName().equals(id)) return ext.factory();
130         }
131         return null;
132     }
133
134 }