1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.service.impl;
14 import java.io.IOException;
15 import java.io.InputStream;
17 import java.util.Enumeration;
18 import java.util.UUID;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleContext;
25 import org.simantics.databoard.Bindings;
26 import org.simantics.databoard.Datatypes;
27 import org.simantics.databoard.serialization.Serializer;
28 import org.simantics.databoard.type.Datatype;
29 import org.simantics.db.Session;
30 import org.simantics.db.VirtualGraph;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.internal.Activator;
33 import org.simantics.db.service.VirtualGraphSupport;
34 import org.simantics.db.services.ServiceInitializer;
35 import org.simantics.graph.db.ImportAdvisor;
36 import org.simantics.graph.db.TransferableGraphs;
37 import org.simantics.graph.representation.TransferableGraph1;
40 * A database client service initializer which ensures that any <code>.tg</code>
41 * files defined in a bundle folder <code>virtualgraphs</code> are imported into
42 * randomly named in-memory virtualgraphs for the initialized session.
44 * @author Antti Villberg
46 public class VirtualGraphInitializer implements ServiceInitializer {
49 public IStatus initialize(Session s) {
50 MultiStatus result = new MultiStatus(Activator.PLUGIN_ID, 0, "VirtualGraphInitialize errors:", null);
51 BundleContext context = Activator.getInstance().getContext();
52 Serializer datatypeSerializer = Bindings.getSerializerUnchecked( Datatype.class );
53 Datatype tgType = Datatypes.getDatatypeUnchecked(TransferableGraph1.class);
54 Serializer tgSerializer = Bindings.getSerializerUnchecked( TransferableGraph1.class );
55 for (final Bundle b : context.getBundles()) {
56 Enumeration<?> graphs = b.getEntryPaths("virtualgraphs");
57 while(graphs != null && graphs.hasMoreElements()) {
58 String element = (String)graphs.nextElement();
59 if(!element.endsWith(".tg"))
61 URL url = b.getEntry(element);
63 InputStream stream = url.openStream();
64 Datatype dt = (Datatype) datatypeSerializer.deserialize( stream );
65 if(dt.equals(tgType)) {
66 VirtualGraphSupport support = s.getService(VirtualGraphSupport.class);
67 VirtualGraph vg = support.getMemoryPersistent(UUID.randomUUID().toString());
68 TransferableGraph1 tg = (TransferableGraph1) tgSerializer.deserialize(stream);
69 TransferableGraphs.importVirtualGraph(s, vg, tg,
73 } catch (IOException e) {
74 IStatus status = new Status(IStatus.ERROR, context.getBundle().getSymbolicName(), "VirtualGraphInitializer transferable graph initialization failed for '" + url.toString() + "', see exception for details.", e);
76 } catch (DatabaseException e) {
77 IStatus status = new Status(IStatus.ERROR, context.getBundle().getSymbolicName(), "VirtualGraphInitializer transferable graph initialization failed for '" + url.toString() + "', see exception for details.", e);
82 if (!result.isOK()) return result;
83 return Status.OK_STATUS;