001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 committers of openArchitectureWare and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * committers of openArchitectureWare - initial API and implementation
010: *******************************************************************************/package model;
011:
012: import java.io.File;
013: import java.io.FileFilter;
014: import java.io.IOException;
015: import java.lang.reflect.Field;
016: import java.util.List;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.eclipse.emf.common.util.URI;
021: import org.eclipse.emf.ecore.EObject;
022: import org.eclipse.emf.ecore.EPackage;
023: import org.eclipse.emf.ecore.EcorePackage;
024: import org.eclipse.emf.ecore.EPackage.Registry;
025: import org.eclipse.emf.ecore.plugin.EcorePlugin;
026: import org.eclipse.emf.ecore.resource.Resource;
027: import org.eclipse.emf.ecore.resource.ResourceSet;
028: import org.eclipse.emf.ecore.resource.URIConverter;
029: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
030: import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
031: import org.openarchitectureware.workflow.ConfigurationException;
032: import org.openarchitectureware.workflow.util.ResourceLoaderFactory;
033:
034: /**
035: * Initializes EMF support. Allows to register additional Packages.
036: */
037: @SuppressWarnings("unchecked")
038: public class StandaloneSetup {
039:
040: private static String platformRootPath = null;
041:
042: public static String getPlatformRootPath() {
043: return platformRootPath;
044: }
045:
046: private Log log = LogFactory.getLog(getClass());
047: static {
048: Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap()
049: .put(Resource.Factory.Registry.DEFAULT_EXTENSION,
050: new XMIResourceFactoryImpl());
051: EPackage.Registry.INSTANCE.put(EcorePackage.eINSTANCE
052: .getNsURI(), EcorePackage.eINSTANCE);
053: }
054:
055: /**
056: * sets the platform uri for standalone execution
057: *
058: * @param pathToPlatform
059: */
060: @SuppressWarnings("unchecked")
061: public void setPlatformUri(String pathToPlatform) {
062: File f = new File(pathToPlatform);
063: if (!f.exists()) {
064: log.error("The platformUri location '" + pathToPlatform
065: + "' does not exist");
066: throw new ConfigurationException(
067: "The platformUri location '" + pathToPlatform
068: + "' does not exist");
069: }
070: if (!f.isDirectory()) {
071: log
072: .error("The platformUri location must point to a directory");
073: throw new ConfigurationException(
074: "The platformUri location must point to a directory");
075: }
076: String path = f.getAbsolutePath();
077: try {
078: path = f.getCanonicalPath();
079: } catch (IOException e) {
080: log.error("Error when registering platform location", e);
081: }
082: if (platformRootPath == null || !platformRootPath.equals(path)) {
083: platformRootPath = path;
084: log.info("Registering platform uri '" + path + "'");
085: if (f.exists()) {
086: for (File subdir : f.listFiles(new FileFilter() {
087: public boolean accept(File arg0) {
088: return arg0.exists() && arg0.isDirectory()
089: && !arg0.getName().startsWith(".");
090: }
091: })) {
092: String s = subdir.getName();
093: try {
094: URI uri = URI.createFileURI(subdir
095: .getCanonicalPath()
096: + "/");
097: EcorePlugin.getPlatformResourceMap()
098: .put(s, uri);
099: log.debug("Registering project " + s + " at '"
100: + subdir.getCanonicalPath() + "'");
101: } catch (IOException e) {
102: log
103: .error(
104: "Error when registering platform location",
105: e);
106: throw new ConfigurationException(
107: "Error when registering platform location",
108: e);
109: }
110: }
111: }
112: }
113: }
114:
115: /**
116: *
117: * @param uriMap
118: */
119: @SuppressWarnings("unchecked")
120: public void addUriMap(final Mapping uriMap) {
121: log.info("Adding URI mapping from '" + uriMap.getFrom()
122: + "' to '" + uriMap.getTo() + "'");
123: final URI baseUri = URI.createURI(uriMap.getFrom());
124: final URI mappedUri = URI.createURI(uriMap.getTo());
125: if (mappedUri == null) {
126: log.error("cannot make URI out of " + uriMap.getTo());
127: throw new ConfigurationException("cannot make URI out of "
128: + uriMap.getTo());
129: } else {
130: URIConverter.URI_MAP.put(baseUri, mappedUri);
131: }
132: }
133:
134: /**
135: * Adds an extension
136: *
137: * @param m
138: * <tt>from</tt>: extension name, <tt>to</tt> factory
139: * classname
140: * @throws ConfigurationException
141: * <ul>
142: * <li> The factory class for the extension cannot be found
143: * <li> The inner factory class for the extension cannot be
144: * found
145: * </ul>
146: */
147: @SuppressWarnings("unchecked")
148: public void addExtensionMap(final Mapping m)
149: throws ConfigurationException {
150: log.info("Adding Extension mapping from '" + m.getFrom()
151: + "' to '" + m.getTo() + "'");
152: try {
153: // locate the factory class of the extension
154: Class factoryClass = ResourceLoaderFactory
155: .createResourceLoader().loadClass(m.getTo());
156: if (factoryClass == null) {
157: log.error("cannot find class " + m.getTo()
158: + " for extension " + m.getFrom());
159: throw new ConfigurationException("cannot find class "
160: + m.getTo() + " for extension " + m.getFrom());
161: }
162: Object factoryInstance = null;
163: if (factoryClass.isInterface()) {
164: final Class[] innerClasses = factoryClass
165: .getDeclaredClasses();
166: factoryClass = null;
167: for (int j = 0; j < innerClasses.length; j++) {
168: if (Resource.Factory.class
169: .isAssignableFrom(innerClasses[j])) {
170: factoryClass = innerClasses[j];
171: }
172: }
173: if (factoryClass == null) {
174: log.error("cannot find inner factory class "
175: + m.getTo() + " for extension "
176: + m.getFrom());
177: throw new ConfigurationException(
178: "cannot find inner factory class "
179: + m.getTo() + " for extension "
180: + m.getFrom());
181: }
182: final Field instanceField = factoryClass
183: .getField("INSTANCE");
184: factoryInstance = instanceField.get(null);
185: } else {
186: factoryInstance = factoryClass.newInstance();
187: }
188: Resource.Factory.Registry.INSTANCE
189: .getExtensionToFactoryMap().put(m.getFrom(),
190: factoryInstance);
191: } catch (final Exception e) {
192: log.error("", e);
193: throw new ConfigurationException(e);
194: }
195: }
196:
197: public void addRegisterGeneratedEPackage(String interfacename) {
198: Class clazz = ResourceLoaderFactory.createResourceLoader()
199: .loadClass(interfacename);
200: if (clazz == null) {
201: log.error("Couldn't find an interface " + interfacename);
202: throw new ConfigurationException(
203: "Couldn't find an interface " + interfacename);
204: }
205: try {
206: EPackage pack = (EPackage) clazz.getDeclaredField(
207: "eINSTANCE").get(null);
208: registry.put(pack.getNsURI(), pack);
209: log.info("Adding generated EPackage '" + interfacename
210: + "'");
211:
212: } catch (Exception e) {
213: log.error("Couldn't register " + interfacename
214: + ". Is it the generated EPackage interface? : "
215: + e.getMessage());
216: throw new ConfigurationException("Couldn't register "
217: + interfacename
218: + ". Is it the generated EPackage interface? : "
219: + e.getMessage());
220: }
221: }
222:
223: protected ResourceSet resourceSet = new ResourceSetImpl();
224: protected Registry registry = EPackage.Registry.INSTANCE;
225:
226: public void setResourceSet(ResourceSet resourceSet) {
227: log
228: .info("Using resourceSet registry. The registered Packages will not be registered in the global EPackage.Registry.INSTANCE!");
229: this .resourceSet = resourceSet;
230: this .registry = resourceSet.getPackageRegistry();
231: }
232:
233: public void setResourceSetImpl(ResourceSetImpl resourceSet) {
234: this .setResourceSet(resourceSet);
235: }
236:
237: public void addRegisterEcoreFile(String fileName)
238: throws IllegalArgumentException, SecurityException,
239: IllegalAccessException, NoSuchFieldException {
240: Resource res = resourceSet.getResource(URI.createURI(fileName),
241: true);
242: if (res == null) {
243: log.error("Couldn't find resource under " + fileName);
244: throw new ConfigurationException(
245: "Couldn't find resource under " + fileName);
246: }
247: if (!res.isLoaded()) {
248: try {
249: res.load(null);
250: } catch (IOException e) {
251: log.error("Couldn't load resource under " + fileName
252: + " : " + e.getMessage());
253: throw new ConfigurationException(
254: "Couldn't load resource under " + fileName
255: + " : " + e.getMessage());
256: }
257: }
258: List<EObject> result = res.getContents();
259: for (EObject object : result) {
260: if (object instanceof EPackage) {
261: String nsUri = ((EPackage) object).getNsURI();
262: if (registry.get(nsUri) == null) {
263: registry.put(nsUri, object);
264: log.info("Adding dynamic EPackage '" + nsUri
265: + "' from '" + fileName + "'");
266: } else {
267: log.debug("Dynamic EPackage '" + nsUri + "' from '"
268: + fileName + "' already in the registry!");
269: }
270: }
271: }
272: }
273:
274: public EPackage getPackage(String nsUri) {
275: return (EPackage) registry.get(nsUri);
276: }
277:
278: }
|