]> gerrit.simantics Code Review - simantics/platform.git/blob
ae7e7c8c5b46339f7620b97085a299c7080c3184
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.service.impl;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.URL;
17 import java.util.Enumeration;
18 import java.util.UUID;
19
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;
38
39 /**
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.
43  * 
44  * @author Antti Villberg
45  */
46 public class VirtualGraphInitializer implements ServiceInitializer {
47
48         @Override
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"))
60                                         continue;
61                                 URL url = b.getEntry(element);
62                                 try {
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, 
70                                                                 new ImportAdvisor());
71                                         }
72                                         stream.close();
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);
75                                         result.add(status);
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);
78                                         result.add(status);
79                                 }
80                         }
81                 }
82                 if (!result.isOK()) return result;
83                 return Status.OK_STATUS;
84         }
85
86 }