Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4012a7b5b44cdff96354e3f5ca6b4fc12a46d544 (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
/***************************************************************************************************
 * Copyright (c) 2003, 2006 IBM Corporation and others. 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: IBM Corporation - initial API and implementation
 **************************************************************************************************/
package org.eclipse.wst.common.internal.emf.utilities;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.wst.common.internal.emf.resource.Translator;
import org.eclipse.wst.common.internal.emf.utilities.TranslatorManager.TranslatorDescriptor;

/**
 * This is the service class to deliver API to use to retrieve Translator extensions from
 * the TranslatorManager and its RegistryReader.
 */
public class TranslatorService {

	/**
	 * Static Key value pair of descriptors as keys and Translator instances as values
	 */
	private static final Map translators = new HashMap();
	
	/**
	 * Static empty array used when no extensions found
	 */
	// never used
	//private static final Translator[] NO_TRANSLATORS = new Translator[0];
	
	/**
	 * Singleton instance of the Translator service
	 */
	private static final TranslatorService INSTANCE = new TranslatorService();
	
	/**
	 * Default constructor
	 */
	private TranslatorService() {
		super();
	}
	
	public static TranslatorService getInstance() {
		return INSTANCE;
	}
	
	/**
	 * This will return the associated extension point TranslatorDescriptor objects from the manager
	 * @return TranslatorDescriptor[]
	 */
	public TranslatorDescriptor[] getTranslatorDescriptors() {
		return TranslatorManager.getInstance().findTranslators();
	}
	
	/**
     * This retrieves the extended Translators using the extension point manager and descriptors
     * @return Translator[] (Note, the return value may contain null entries.)
     */
	public Translator[] getTranslators() {
		TranslatorDescriptor[] descriptors = getTranslatorDescriptors();
		Translator[] result = new Translator[descriptors.length];
		//The result index could differ from the descriptors index.
		int resultIndex = 0;
		for (int i=0; i<descriptors.length; i++) {
			Translator instance = getTranslator(descriptors[i]);
			if (instance!=null) {
				result[resultIndex] = instance;
				resultIndex++;
			}
		}
		return result;  
    }
	
	/**
	 * Retrieve the existing associated Translator instance for the descriptor, or create a new
	 * one and cache on the Set.
	 * 
	 * @param translatorDescriptor
	 * @return Translator associated with the descriptor
	 */
	public Translator getTranslator(TranslatorDescriptor translatorDescriptor) {
		Translator translator = (Translator) translators.get(translatorDescriptor);
		if (translator != null)
			return translator;
		
		synchronized (translators) {
			translator = translatorDescriptor.createTranslator();
			if (translator != null)
				translators.put(translatorDescriptor, translator);
		}
		return translator;
	}
}

Back to the top