Skip to main content
summaryrefslogtreecommitdiffstats
blob: bea98d830fa41fc70963539a9d2e67cf65ee30f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Copyright (c) 2008 Arno Haase.
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:
    Arno Haase - initial API and implementation
 */
package org.eclipose.xtend.middleend.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipose.xtend.middleend.plugins.LanguageSpecificMiddleEnd;
import org.eclipose.xtend.middleend.plugins.LanguageSpecificMiddleEndFactory;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.RegistryFactory;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public class Activator implements BundleActivator {
    private static final Log _log = LogFactory.getLog (Activator.class);
    
    private static Activator _instance = null;
    
    public static Activator getInstance () {
        return _instance;
    }

    private final List<LanguageSpecificMiddleEndFactory> _middleEndContributions = new ArrayList<LanguageSpecificMiddleEndFactory> ();
    private boolean _isInitialized = false;

    
    public List<LanguageSpecificMiddleEnd> getFreshMiddleEnds (Map<Class<?>, Object> specificParams) {
        init ();
        
        final List<LanguageSpecificMiddleEnd> result = new ArrayList<LanguageSpecificMiddleEnd>();
        
        for (LanguageSpecificMiddleEndFactory factory: _middleEndContributions) {
            try {
                result.add (factory.create (specificParams.get (factory.getClass())));
            }
            catch (IllegalArgumentException exc) {
                // this is the official way for an implementation to withdraw from the pool for this call
                _log.debug ("middle end implementation " + factory.getName() + " says it is not available: " + exc.getMessage());
            }
        }
        
        return result;
    }
    
    public void start (BundleContext context) throws Exception {
        //TODO Bernd: implement error handling and logging to be both robust and independent of Eclipse
        
        _isInitialized = false;
        _instance = this;
    }
    
    private void init () {
        if (_isInitialized)
            return;
        
        _isInitialized = true;
        _middleEndContributions.clear ();

        try {
            final IConfigurationElement[] confEl = RegistryFactory.getRegistry().getConfigurationElementsFor ("org.eclipse.xtend.middleend.MiddleEnd");

            for (IConfigurationElement curEl: confEl) {
                final Object o = curEl.createExecutableExtension ("class");
                _middleEndContributions.add ((LanguageSpecificMiddleEndFactory) o);
            }
        }
        catch (Exception exc) {
            exc.printStackTrace ();
        }
        
        Collections.sort (_middleEndContributions, new Comparator <LanguageSpecificMiddleEndFactory> () {
            public int compare (LanguageSpecificMiddleEndFactory o1, LanguageSpecificMiddleEndFactory o2) {
                return o1.getPriority() - o2.getPriority();
            }
        });
        
        _log.info ("Activating Eclipse Modeling Middle End - the following middle ends are registered:");
        for (LanguageSpecificMiddleEndFactory factory: _middleEndContributions)
            _log.info ("  " + factory.getName());
    }

    public void stop (BundleContext context) throws Exception {
        _instance = null;
        _middleEndContributions.clear();
    }
}

Back to the top