Created
May 28, 2012 22:03
-
-
Save xseignard/2821402 to your computer and use it in GitHub Desktop.
mongo-emf without OSGI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
// some model | |
Library lib = LibraryFactory.eINSTANCE.createLibrary(); | |
lib.setName("Test"); | |
Book book = LibraryFactory.eINSTANCE.createBook(); | |
book.setTitle("Title"); | |
lib.getBooks().add(book); | |
// provider conf | |
MongoProvider mongoProvider = new MongoProvider(); | |
HashMap<String, Object> properties = new HashMap<String, Object>(); | |
properties.put(IMongoProvider.PROP_URI, "mongodb://ds033087.mongolab.com:33087"); | |
properties.put(IMongoProvider.PROP_USER, "user"); | |
properties.put(IMongoProvider.PROP_PASSWORD, "password"); | |
mongoProvider.configure(properties); | |
// locator conf | |
MongoLocator locator = new MongoLocator(); | |
locator.bindMongoProvider(mongoProvider, properties); | |
// input/output stream factory conf | |
DefaultStreamFactory inputStreamFactory = new DefaultStreamFactory(); | |
inputStreamFactory.bindConverterService(new DefaultConverterService()); | |
DefaultBuilderFactory builderFactory = new DefaultBuilderFactory(); | |
inputStreamFactory.bindDBObjectBuilderFactory(builderFactory); | |
inputStreamFactory.bindEObjectBuilderFactory(builderFactory); | |
inputStreamFactory.bindQueryEngine(new QueryEngine()); | |
// uri handler conf | |
MongoURIHandlerImpl handler = new MongoURIHandlerImpl(locator, inputStreamFactory, inputStreamFactory); | |
IResourceSetFactory resourceSetFactory = new MongoResourceSetFactory(); | |
ResourceSet resourceSet = resourceSetFactory.createResourceSet(); | |
// configures the resourceSet to handle mongodb protocol | |
resourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put("mongodb", new XMIResourceFactoryImpl()); | |
// configures the resourceSet to handle mongodb URIs | |
EList<URIHandler> uriHandlers = resourceSet.getURIConverter().getURIHandlers(); | |
uriHandlers.add(0, handler); | |
// resource creation | |
Resource resource = resourceSet.createResource(URI | |
.createURI("mongodb://ds033087.mongolab.com:33087/mongo-emf/test/1")); | |
resource.getContents().add(lib); | |
// saves it | |
resource.save(null); | |
// loads the resource from mongoDB | |
resource = resourceSet.getResource(URI | |
.createURI("mongodb://ds033087.mongolab.com:33087/mongo-emf/test/1"), true); | |
// gets the library | |
Library libLoadedFromMongoDB = (Library) resource.getContents().get(0); | |
// checks the saved and loaded are the same | |
assertEquals(lib, libLoadedFromMongoDB); | |
} | |
catch (MongoException e1) { | |
e1.printStackTrace(); | |
} | |
catch (UnknownHostException e1) { | |
e1.printStackTrace(); | |
} | |
catch (URISyntaxException e1) { | |
e1.printStackTrace(); | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment