001: package org.andromda.repositories.emf.uml2;
002:
003: import java.util.Map;
004:
005: import org.andromda.core.common.ComponentContainer;
006: import org.andromda.core.metafacade.ModelAccessFacade;
007: import org.andromda.core.repository.RepositoryFacadeException;
008: import org.andromda.metafacades.emf.uml2.UMLModelAccessFacade;
009: import org.andromda.repositories.emf.EMFRepositoryFacade;
010: import org.apache.log4j.Logger;
011: import org.eclipse.emf.ecore.EObject;
012: import org.eclipse.emf.ecore.EcorePackage;
013: import org.eclipse.emf.ecore.resource.Resource;
014: import org.eclipse.emf.ecore.resource.ResourceSet;
015: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
016: import org.eclipse.emf.ecore.util.EcoreUtil;
017: import org.eclipse.emf.ecore.xmi.XMLResource;
018: import org.eclipse.uml2.Model;
019: import org.eclipse.uml2.UML2Package;
020: import org.eclipse.uml2.util.UML2Resource;
021:
022: /**
023: * Implements an AndroMDA object model repository by using the <a href="http://www.eclipse.org/uml2/">Eclipse
024: * UML2 API set </a>.
025: *
026: * @author Steve Jerman
027: * @author Chad Brandon
028: */
029: public class EMFUML2RepositoryFacade extends EMFRepositoryFacade {
030: /**
031: * The logger instance.
032: */
033: private static Logger logger = Logger
034: .getLogger(EMFUML2RepositoryFacade.class);
035:
036: /**
037: * Perform required registrations for EMF/UML2.
038: *
039: * @see org.andromda.core.repository.RepositoryFacade#open()
040: */
041: protected ResourceSet createNewResourceSet() {
042: if (logger.isDebugEnabled()) {
043: logger.debug("Registering resource factories");
044: }
045:
046: final ResourceSet resourceSet = new ResourceSetImpl();
047: final Map extensionToFactoryMap = resourceSet
048: .getResourceFactoryRegistry()
049: .getExtensionToFactoryMap();
050:
051: // - we need to perform these registrations in order to load a UML2 model into EMF
052: // see: http://dev.eclipse.org/viewcvs/indextools.cgi/%7Echeckout%7E/uml2-home/faq.html#6
053: resourceSet.getPackageRegistry().put(UML2Package.eNS_URI,
054: UML2Package.eINSTANCE);
055: extensionToFactoryMap.put(
056: Resource.Factory.Registry.DEFAULT_EXTENSION,
057: UML2Resource.Factory.INSTANCE);
058:
059: // - populate the load options
060: final Map loadOptions = this .getLoadOptions();
061: loadOptions.put(UML2Resource.OPTION_DISABLE_NOTIFY,
062: Boolean.FALSE);
063: loadOptions.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
064: Boolean.TRUE);
065:
066: return resourceSet;
067: }
068:
069: /**
070: * Overrridden to check that the model is of the correct type.
071: *
072: * @see org.andromda.repositories.emf.EMFRepositoryFacade#readModel(java.lang.String)
073: */
074: protected void readModel(final String uri) {
075: super .readModel(uri);
076: // Just to be sure there is a valid "model" inside
077: EObject modelPackage = (EObject) EcoreUtil.getObjectByType(
078: model.getContents(), EcorePackage.eINSTANCE
079: .getEObject());
080: if (!(modelPackage instanceof Model)) {
081: throw new RepositoryFacadeException("Model '" + uri
082: + "' is not a valid EMF UML2 model");
083: }
084: }
085:
086: /**
087: * @see org.andromda.core.repository.RepositoryFacade#getModel()
088: */
089: public ModelAccessFacade getModel() {
090: if (this .modelFacade == null) {
091: try {
092: this .modelFacade = (ModelAccessFacade) ComponentContainer
093: .instance().newComponent(
094: UMLModelAccessFacade.class,
095: ModelAccessFacade.class);
096: } catch (final Throwable throwable) {
097: throw new RepositoryFacadeException(throwable);
098: }
099: }
100: if (this.model != null) {
101: this.modelFacade.setModel(this.model);
102: } else {
103: this.modelFacade = null;
104: }
105: return this.modelFacade;
106: }
107: }
|