1 package org.andromda.repositories.emf;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Map;
12 import org.andromda.core.common.ResourceFinder;
13 import org.andromda.core.engine.ModelProcessor;
14 import org.andromda.core.metafacade.ModelAccessFacade;
15 import org.andromda.core.repository.RepositoryFacade;
16 import org.andromda.core.repository.RepositoryFacadeException;
17 import org.apache.log4j.Logger;
18 import org.eclipse.emf.common.util.EList;
19 import org.eclipse.emf.common.util.URI;
20 import org.eclipse.emf.ecore.EObject;
21 import org.eclipse.emf.ecore.resource.Resource;
22 import org.eclipse.emf.ecore.resource.ResourceSet;
23 import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
24
25
26
27
28
29
30
31
32 public abstract class EMFRepositoryFacade
33 implements RepositoryFacade
34 {
35
36
37
38 private static final Logger logger = Logger.getLogger(EMFRepositoryFacade.class);
39
40
41
42
43 protected ResourceSet resourceSet;
44
45
46
47
48 protected ModelAccessFacade modelFacade;
49
50
51
52
53
54
55 protected List<Resource> model;
56
57
58
59
60 private Map<String, Object> loadOptions = new HashMap<String, Object>();
61
62
63
64
65
66
67 protected Map<String, Object> getLoadOptions()
68 {
69 return this.loadOptions;
70 }
71
72
73
74
75
76
77 public void readModel(final String uri)
78 {
79 try
80 {
81 if (this.model==null)
82 {
83 this.model = new ArrayList<Resource>();
84 }
85 Resource modelResource = this.resourceSet.createResource(EMFRepositoryFacadeUtils.createUri(uri));
86 if (modelResource == null)
87 {
88 throw new RepositoryFacadeException('\'' + uri + "' is an invalid model");
89 }
90 modelResource.load(this.getLoadOptions());
91
92 EList<Diagnostic> errors = modelResource.getErrors();
93 if (errors!=null && !errors.isEmpty())
94 {
95 logger.info(errors);
96 }
97 EList<Diagnostic> warnings = modelResource.getWarnings();
98 if (warnings!=null && !warnings.isEmpty())
99 {
100 logger.info(warnings);
101 }
102
103 if (ModelProcessor.getModelValidation())
104 {
105 try {
106
107
108
109
110 for (Iterator<EObject> i = modelResource.getAllContents(); i.hasNext(); )
111 {
112
113 EObject eObject = i.next();
114
115 for (Iterator<EObject> crossRefIterator = eObject.eCrossReferences().iterator(); crossRefIterator.hasNext(); )
116 {
117 try
118 {
119
120
121 crossRefIterator.next();
122
123
124 }
125 catch (Exception ex)
126 {
127 logger.error("EMFRepositoryFacade.readModel.resolveAll on " + eObject + ": " + ex);
128 }
129 }
130 }
131 } catch (RuntimeException e) {
132 logger.error("EMFRepositoryFacade.readModel.resolveAll :" + e);
133 }
134 }
135 this.model.add(modelResource);
136 }
137 catch (final Exception exception)
138 {
139 throw new RepositoryFacadeException(exception);
140 }
141 }
142
143
144
145
146 public void open()
147 {
148 this.resourceSet = this.createNewResourceSet();
149 }
150
151
152
153
154
155
156
157
158
159 public abstract ResourceSet createNewResourceSet();
160
161
162
163
164
165 public abstract void close();
166
167
168
169
170 private static final String MODULES_PATH = "META-INF/emf/modules";
171
172
173
174
175 public void readModel(
176 String[] modelUris,
177 String[] moduleSearchPaths)
178 {
179 if (modelUris == null || modelUris.length == 0)
180 {
181 throw new RepositoryFacadeException("No model specified.");
182 }
183 final List<String> moduleSearchPathList = new ArrayList<String>();
184 if (moduleSearchPaths != null)
185 {
186 moduleSearchPathList.addAll(Arrays.asList(moduleSearchPaths));
187 }
188
189
190 URL[] classpathSearchPaths = ResourceFinder.findResources(MODULES_PATH);
191 if (classpathSearchPaths != null)
192 {
193 classpathSearchPaths = ResourceFinder.findResources("profiles");
194 }
195 if (classpathSearchPaths != null)
196 {
197 final int numberOfClasspathSearchPaths = classpathSearchPaths.length;
198 for (int ctr = 0; ctr < numberOfClasspathSearchPaths; ctr++)
199 {
200 final URL classpathSearchPath = classpathSearchPaths[ctr];
201 if (classpathSearchPath != null)
202 {
203 moduleSearchPathList.add(classpathSearchPath.toString());
204 }
205 }
206 }
207 logger.debug("ModuleSearchPaths: " + moduleSearchPathList);
208
209
210 this.resourceSet.setURIConverter(new EMFURIConverter(moduleSearchPathList,
211 this.resourceSet.getURIConverter().getURIMap()));
212 if (modelUris.length > 0)
213 {
214 final int numberOfModelUris = modelUris.length;
215 for (int ctr = 0; ctr < numberOfModelUris; ctr++)
216 {
217 this.readModel(modelUris[ctr]);
218 }
219 }
220 }
221
222
223
224
225 public void readModel(
226 InputStream[] stream,
227 String[] modelUri,
228 String[] moduleSearchPaths)
229 {
230 this.readModel(
231 modelUri,
232 moduleSearchPaths);
233 }
234
235
236
237
238 public void writeModel(
239 Object modelIn,
240 String location,
241 String version,
242 String encoding)
243 {
244 this.writeModel(
245 modelIn,
246 location,
247 "");
248 }
249
250
251
252
253 public void writeModel(
254 Object modelIn,
255 String location,
256 String version)
257 {
258 final org.eclipse.emf.ecore.EModelElement element = (org.eclipse.emf.ecore.EModelElement)modelIn;
259 final Resource resource = element.eResource();
260 final URI uri = URI.createURI(location);
261 resource.setURI(uri);
262 try
263 {
264 resource.save(null);
265 }
266 catch (IOException exception)
267 {
268 throw new RepositoryFacadeException("Could not save model", exception);
269 }
270 }
271
272
273
274
275 public void clear()
276 {
277 this.model = null;
278 this.resourceSet = this.createNewResourceSet();
279 }
280 }