| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.runtime.memento; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.nio.file.Path; |
| import java.util.ArrayList; |
| |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
| import org.eclipse.osbp.runtime.common.memento.IMementoManager; |
| import org.eclipse.osbp.runtime.common.validation.IStatus; |
| import org.eclipse.osbp.runtime.common.validation.Status; |
| import org.osgi.service.component.ComponentContext; |
| import org.osgi.service.component.annotations.Activate; |
| import org.osgi.service.component.annotations.Component; |
| import org.osgi.service.component.annotations.Deactivate; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| @Component(immediate = true) |
| public class MementoManager implements IMementoManager { |
| |
| private static final Logger LOGGER = LoggerFactory |
| .getLogger(MementoManager.class); |
| |
| private ResourceSet rs; |
| private Path basePath; |
| |
| @Override |
| public Object loadMemento(String userId, String mementoId) { |
| |
| URI uri = createURI(userId, mementoId); |
| File file = new File(uri.toFileString()); |
| if (!file.exists()) { |
| LOGGER.debug("Memento does not exists: " + uri.toFileString()); |
| return null; |
| } |
| |
| Resource resource = rs.getResource(createURI(userId, mementoId), true); |
| return resource.getContents().size() > 0 ? resource.getContents() |
| .get(0) : null; |
| } |
| |
| @Override |
| public IStatus saveMemento(String userId, String mementoId, Object memento) { |
| try { |
| URI uri = createURI(userId, mementoId); |
| |
| Resource resource = null; |
| try { |
| resource = rs.getResource(uri, true); |
| } catch (Exception ex) { |
| LOGGER.debug("Resource not available yet. A new one is created. URI = " |
| + uri); |
| } |
| |
| if (resource == null) { |
| resource = rs.getResource(uri, false); |
| } |
| |
| resource.getContents().clear(); |
| resource.getContents().add((EObject) memento); |
| resource.save(null); |
| } catch (IOException e) { |
| return Status.createErrorStatus(e); |
| } |
| |
| return IStatus.OK; |
| } |
| |
| protected URI createURI(String userId, String mementoId) { |
| URI result = URI.createFileURI(basePath.toString()); |
| result = result.appendSegment(userId); |
| result = result.appendSegment(mementoId); |
| result = result.appendFileExtension("memento"); |
| return result; |
| } |
| |
| @Activate |
| protected void activate(ComponentContext context) { |
| rs = new ResourceSetImpl(); |
| |
| File baseFolder = context.getBundleContext().getDataFile(""); |
| basePath = baseFolder.toPath(); |
| } |
| |
| @Deactivate |
| protected void deactivate() { |
| |
| // unload all resources |
| for (Resource resource : new ArrayList<Resource>(rs.getResources())) { |
| resource.unload(); |
| } |
| |
| rs = null; |
| basePath = null; |
| } |
| |
| } |