Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0602eb3a26a906c3e7ff0b664d7cd3ab33ff27ad (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) 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:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: EModelResolver.java,v 1.9 2009/03/30 07:53:05 mtaal Exp $
 */

package org.eclipse.emf.teneo.ecore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.DataStore;
import org.eclipse.emf.teneo.ERuntime;

/**
 * The EModelResolver allows pluggable access to the underlying ecore model. It maps from
 * eclass/efeature names to java member names or to real eclass names. This default implementation
 * only returns null.
 * 
 * @author <a href="mtaal@elver.org">Martin Taal</a>
 */
public abstract class EModelResolver {

	/** The default instance of an EcoreResolver */
	private static EModelResolver instance = null;

	/** Return the current ecore resolver */
	public static EModelResolver instance() {
		if (instance == null) {
			instance = ERuntime.INSTANCE;
		}
		return instance;
	}

	/** Set an EcoreResolver */
	public static void setInstance(EModelResolver modelResolver) {
		instance = modelResolver;
	}

	private Map<EPackage, List<DataStore>> ePackageRegistration = new HashMap<EPackage, List<DataStore>>();

	/** Clear all internal datastructures */
	public abstract void clear();

	/**
	 * @return the EClass for a java class, if not found then the superclass of the javaclass is tried
	 */
	public abstract EClass getEClass(Class<?> javaClass);

	/** Is the epackage registered */
	public abstract boolean isRegistered(EPackage epackage);

	/** Register the epackages */
	public abstract void register(EPackage[] epacks);

	/** Register that a set of epackages is owned by a datastore */
	public synchronized void registerOwnerShip(DataStore dataStore, EPackage[] epacks) {
		for (EPackage ePackage : epacks) {
			List<DataStore> stores = ePackageRegistration.get(ePackage);
			if (stores == null) {
				stores = new ArrayList<DataStore>();
				ePackageRegistration.put(ePackage, stores);
			}
			if (!stores.contains(dataStore)) {
				stores.add(dataStore);
			}
		}
	}

	/** Unregister an epackage */
	public synchronized void unregisterOwnerShip(DataStore dataStore, EPackage[] epacks) {
		final List<EPackage> toRemove = new ArrayList<EPackage>();
		for (EPackage ePackage : epacks) {
			List<DataStore> stores = ePackageRegistration.get(ePackage);
			if (stores != null) {
				stores.remove(dataStore);
				if (stores.isEmpty()) {
					ePackageRegistration.remove(ePackage);
					toRemove.add(ePackage);
				}
			}
		}
		removeEPackages(toRemove);
	}

	/** Remove an epackage from the internal lists */
	protected void removeEPackages(List<EPackage> ePackages) {

	}

	/** @return all java classes and interfaces */
	public abstract List<Class<?>> getAllClassesAndInterfaces();

	/** @return the java implementation class for an EClass */
	public abstract Class<?> getJavaClass(EClassifier eclassifier);

	/** @return the java interface class for an EClass */
	public abstract Class<?> getJavaInterfaceClass(EClass eclass);

	/** Returns true if the passed EClass has a javaClass representation. */
	public abstract boolean hasImplementationClass(EClassifier eclassifier);

	/** Returns the currently registered epackages */
	public abstract EPackage[] getEPackages();

	/** Returns null */
	public abstract Object create(EClass eclass);

	/** Returns a java instance of an EClass defined by name */
	public Object create(EPackage epackage, String eclassName) {
		final EClass eclass = (EClass) epackage.getEClassifier(eclassName);
		if (eclass == null) {
			throw new IllegalArgumentException("No EClass " + eclassName + " found in epackage "
					+ epackage.getName());
		}
		return create(eclass);
	}

	/**
	 * Returns a java instance of an EClass defined by name, all epackages are searched for this
	 * eclass.
	 */
	public Object create(String eclassName) {
		for (EPackage epackage : getEPackages()) {
			final EClass eclass = (EClass) epackage.getEClassifier(eclassName);
			if (eclass != null) {
				return create(eclass);
			}
		}
		throw new IllegalArgumentException("No EClass " + eclassName + " found.");
	}
}

Back to the top