| 1 | package com.hammurapi.eventbus.snapshot.io; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | |
| 6 | import org.eclipse.emf.common.util.EList; |
| 7 | import org.eclipse.emf.common.util.URI; |
| 8 | import org.eclipse.emf.ecore.EObject; |
| 9 | import org.eclipse.emf.ecore.resource.Resource; |
| 10 | import org.eclipse.emf.ecore.resource.ResourceSet; |
| 11 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| 12 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; |
| 13 | |
| 14 | import com.hammurapi.convert.ConvertingService; |
| 15 | import com.hammurapi.eventbus.EventBusException; |
| 16 | import com.hammurapi.extract.Predicate; |
| 17 | |
| 18 | /** |
| 19 | * Class to dump predicates into snapshot model for troubleshooting. |
| 20 | * @author Pavel Vlasov |
| 21 | */ |
| 22 | public class PredicateOutput { |
| 23 | |
| 24 | |
| 25 | public static void output(Predicate<?, ?> predicate, File out) { |
| 26 | |
| 27 | com.hammurapi.eventbus.snapshot.Predicate sp = ConvertingService.convert(predicate, com.hammurapi.eventbus.snapshot.Predicate.class); |
| 28 | |
| 29 | ResourceSet resourceSet = new ResourceSetImpl(); |
| 30 | // Register the appropriate resource factory to handle all file extensions. |
| 31 | // |
| 32 | resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); |
| 33 | |
| 34 | URI uri = URI.createFileURI(out.getAbsolutePath()); |
| 35 | Resource predicateResource = resourceSet.createResource(uri); |
| 36 | EList<EObject> contents = predicateResource.getContents(); |
| 37 | contents.add(sp); |
| 38 | try { |
| 39 | predicateResource.save(null); |
| 40 | } catch (IOException e) { |
| 41 | throw new EventBusException("Could not save snapshot: "+e, e); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | } |