X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2FSpreadsheetSessionManager.java;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2FSpreadsheetSessionManager.java;h=659eddf7458bbf9648ec2d8321cbd453104c4003;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=ff247b73a76f30b51d78d79aa6760a0a706d7e54;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java index ff247b73a..659eddf74 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/SpreadsheetSessionManager.java @@ -1,203 +1,203 @@ -package org.simantics.spreadsheet.graph; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.io.ObjectStreamClass; -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.StandardRealm; -import org.simantics.db.layer0.StandardSessionManager; -import org.simantics.db.layer0.variable.ProxyVariables; -import org.simantics.db.layer0.variable.Variable; -import org.simantics.db.layer0.variable.Variables; -import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler; -import org.simantics.spreadsheet.resource.SpreadsheetResource; -import org.simantics.structural.synchronization.Synchronizer; - -public class SpreadsheetSessionManager extends StandardSessionManager { - - private static SpreadsheetSessionManager INSTANCE; - - public static SpreadsheetSessionManager getInstance() { - if(INSTANCE == null) { - INSTANCE = new SpreadsheetSessionManager(); - } - return INSTANCE; - } - - public class ClassLoaderObjectInputStream extends ObjectInputStream{ - - private ClassLoader classLoader; - - public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException { - super(in); - this.classLoader = classLoader; - } - - @Override - protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{ - - try{ - String name = desc.getName(); - return Class.forName(name, false, classLoader); - } - catch(ClassNotFoundException e){ - return super.resolveClass(desc); - } - } - } - - private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException { - SpreadsheetResource SR = SpreadsheetResource.getInstance(graph); - Collection ics = graph.getObjects(context, SR.HasInitialCondition); - - Resource found = null; - Set founds = new HashSet<>(); - Set foundDefaults = new HashSet<>(); - - for (Resource initialCondition : ics) { - if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) { - foundDefaults.add(initialCondition); - } - if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) { - founds.add(initialCondition); - } - } - if (foundDefaults.size() == 1) { - found = foundDefaults.iterator().next(); - } else if (foundDefaults.size() == 0 && founds.size() == 1) { - found = founds.iterator().next(); - } else { - System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book)); - System.err.println("foundDefaults : " + foundDefaults.size()); - if (!foundDefaults.isEmpty()) { - for (Resource foundDefault : foundDefaults) { - System.err.println(graph.getPossibleURI(foundDefault)); - } - } - System.err.println("founds : " + founds.size()); - if (!founds.isEmpty()) { - for (Resource foun : founds) { - System.err.println(graph.getPossibleURI(foun)); - } - } - } - - if (found != null) { - try { - File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found); - System.err.println("Extracting IC from " + tmp.getAbsolutePath()); - InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp)); - ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn); - SpreadsheetBook srBook = (SpreadsheetBook) in.readObject(); - in.close(); - return srBook; - } catch (IOException e) { - throw new DatabaseException(e); - } catch (ClassNotFoundException e) { - throw new DatabaseException(e); - } - } - return null; - } - - @Override - protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException { - - Variable run = Variables.getVariable(graph, id); - Variable context = ProxyVariables.proxyVariableInput(graph, run); - if (context != null) { - Variable base = ProxyVariables.proxyVariableBase(graph, run); - Resource bookResource = base.getRepresents(graph); - Resource contextResource = context.getRepresents(graph); - if (contextResource != null) { - SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource); - if (ic != null) - return ic; - } - - SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource); - if (ic != null) - return ic; - } - - SpreadsheetBook book = new SpreadsheetBook(); - - Variable base = ProxyVariables.proxyVariableBase(graph, run); - Resource bookResource = base.getRepresents(graph); - Variable configuration = Variables.getVariable(graph, bookResource); - - SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book); - Synchronizer synchronizer = new Synchronizer(graph); - synchronizer.fullSynchronization(configuration, handler); - - return book; - - } - - @Override - protected StandardRealm createRealm(SpreadsheetBook engine, String id) { - return new SpreadsheetRealm(engine, id); - } - -// static ConcurrentHashMap REALMS = -// new ConcurrentHashMap(); -// -// static ConcurrentHashMap> SUPPORTS = -// new ConcurrentHashMap>(); -// -// public static SpreadsheetRealm sclRealmById(String id) { -// // CONNECTIONS is ConcurrentHashMap so no synchronization is needed here -// return REALMS.get(id); -// } -// -// public static NodeSupport getOrCreateNodeSupport(String id) { -// synchronized(SUPPORTS) { -// NodeSupport result = SUPPORTS.get(id); -// if(result == null) { -// SpreadsheetRealm realm = getOrCreateSCLRealm(id); -// result = new NodeSupport(realm.getNodeManager()); -// SUPPORTS.put(id, result); -// } -// return result; -// } -// } -// -// public static SpreadsheetRealm createRealm() { -// synchronized(REALMS) { -// String id = UUID.randomUUID().toString(); -// return createRealm(id); -// } -// } -// -// public static SpreadsheetRealm getOrCreateSCLRealm(String id) { -// synchronized(REALMS) { -// SpreadsheetRealm session = sclRealmById(id); -// if(session == null) -// return createRealm(id); -// else -// return session; -// } -// } -// -// private static SpreadsheetRealm createRealm(String id) { -// SpreadsheetBook book = new SpreadsheetBook(); -// SpreadsheetRealm realm = new SpreadsheetRealm(book, id); -// REALMS.put(id, realm); -// return realm; -// } -// -// public static void removeRealm(String id) { -// REALMS.remove(id); -// } - -} +package org.simantics.spreadsheet.graph; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectStreamClass; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.StandardRealm; +import org.simantics.db.layer0.StandardSessionManager; +import org.simantics.db.layer0.variable.ProxyVariables; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.spreadsheet.graph.synchronization.SpreadsheetSynchronizationEventHandler; +import org.simantics.spreadsheet.resource.SpreadsheetResource; +import org.simantics.structural.synchronization.Synchronizer; + +public class SpreadsheetSessionManager extends StandardSessionManager { + + private static SpreadsheetSessionManager INSTANCE; + + public static SpreadsheetSessionManager getInstance() { + if(INSTANCE == null) { + INSTANCE = new SpreadsheetSessionManager(); + } + return INSTANCE; + } + + public class ClassLoaderObjectInputStream extends ObjectInputStream{ + + private ClassLoader classLoader; + + public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException { + super(in); + this.classLoader = classLoader; + } + + @Override + protected Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException{ + + try{ + String name = desc.getName(); + return Class.forName(name, false, classLoader); + } + catch(ClassNotFoundException e){ + return super.resolveClass(desc); + } + } + } + + private SpreadsheetBook getPossibleInitialCondition(ReadGraph graph, Resource context, Resource book) throws DatabaseException { + SpreadsheetResource SR = SpreadsheetResource.getInstance(graph); + Collection ics = graph.getObjects(context, SR.HasInitialCondition); + + Resource found = null; + Set founds = new HashSet<>(); + Set foundDefaults = new HashSet<>(); + + for (Resource initialCondition : ics) { + if (graph.hasStatement(book, SR.Book_HasDefaultInitialCondition, initialCondition)) { + foundDefaults.add(initialCondition); + } + if (graph.hasStatement(initialCondition, SR.InitialCondition_ConditionOf, book)) { + founds.add(initialCondition); + } + } + if (foundDefaults.size() == 1) { + found = foundDefaults.iterator().next(); + } else if (foundDefaults.size() == 0 && founds.size() == 1) { + found = founds.iterator().next(); + } else { + System.err.println("Could not find IC for SpreadsheetBook " + graph.getPossibleURI(book)); + System.err.println("foundDefaults : " + foundDefaults.size()); + if (!foundDefaults.isEmpty()) { + for (Resource foundDefault : foundDefaults) { + System.err.println(graph.getPossibleURI(foundDefault)); + } + } + System.err.println("founds : " + founds.size()); + if (!founds.isEmpty()) { + for (Resource foun : founds) { + System.err.println(graph.getPossibleURI(foun)); + } + } + } + + if (found != null) { + try { + File tmp = SpreadsheetGraphUtils.extractInitialCondition(graph, found); + System.err.println("Extracting IC from " + tmp.getAbsolutePath()); + InputStream fileIn = new BufferedInputStream(new FileInputStream(tmp)); + ObjectInputStream in = new ClassLoaderObjectInputStream(getClass().getClassLoader(), fileIn); + SpreadsheetBook srBook = (SpreadsheetBook) in.readObject(); + in.close(); + return srBook; + } catch (IOException e) { + throw new DatabaseException(e); + } catch (ClassNotFoundException e) { + throw new DatabaseException(e); + } + } + return null; + } + + @Override + protected SpreadsheetBook createEngine(ReadGraph graph, String id) throws DatabaseException { + + Variable run = Variables.getVariable(graph, id); + Variable context = ProxyVariables.proxyVariableInput(graph, run); + if (context != null) { + Variable base = ProxyVariables.proxyVariableBase(graph, run); + Resource bookResource = base.getRepresents(graph); + Resource contextResource = context.getRepresents(graph); + if (contextResource != null) { + SpreadsheetBook ic = getPossibleInitialCondition(graph, contextResource, bookResource); + if (ic != null) + return ic; + } + + SpreadsheetBook ic = getPossibleInitialCondition(graph, bookResource, bookResource); + if (ic != null) + return ic; + } + + SpreadsheetBook book = new SpreadsheetBook(); + + Variable base = ProxyVariables.proxyVariableBase(graph, run); + Resource bookResource = base.getRepresents(graph); + Variable configuration = Variables.getVariable(graph, bookResource); + + SpreadsheetSynchronizationEventHandler handler = new SpreadsheetSynchronizationEventHandler(graph, book); + Synchronizer synchronizer = new Synchronizer(graph); + synchronizer.fullSynchronization(configuration, handler); + + return book; + + } + + @Override + protected StandardRealm createRealm(SpreadsheetBook engine, String id) { + return new SpreadsheetRealm(engine, id); + } + +// static ConcurrentHashMap REALMS = +// new ConcurrentHashMap(); +// +// static ConcurrentHashMap> SUPPORTS = +// new ConcurrentHashMap>(); +// +// public static SpreadsheetRealm sclRealmById(String id) { +// // CONNECTIONS is ConcurrentHashMap so no synchronization is needed here +// return REALMS.get(id); +// } +// +// public static NodeSupport getOrCreateNodeSupport(String id) { +// synchronized(SUPPORTS) { +// NodeSupport result = SUPPORTS.get(id); +// if(result == null) { +// SpreadsheetRealm realm = getOrCreateSCLRealm(id); +// result = new NodeSupport(realm.getNodeManager()); +// SUPPORTS.put(id, result); +// } +// return result; +// } +// } +// +// public static SpreadsheetRealm createRealm() { +// synchronized(REALMS) { +// String id = UUID.randomUUID().toString(); +// return createRealm(id); +// } +// } +// +// public static SpreadsheetRealm getOrCreateSCLRealm(String id) { +// synchronized(REALMS) { +// SpreadsheetRealm session = sclRealmById(id); +// if(session == null) +// return createRealm(id); +// else +// return session; +// } +// } +// +// private static SpreadsheetRealm createRealm(String id) { +// SpreadsheetBook book = new SpreadsheetBook(); +// SpreadsheetRealm realm = new SpreadsheetRealm(book, id); +// REALMS.put(id, realm); +// return realm; +// } +// +// public static void removeRealm(String id) { +// REALMS.remove(id); +// } + +}