]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Use Logger in DependencyChanges & related mechanisms 29/2529/3
authorjsimomaa <jani.simomaa@gmail.com>
Fri, 7 Dec 2018 06:55:51 +0000 (08:55 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 7 Dec 2018 09:12:23 +0000 (09:12 +0000)
gitlab #226

Change-Id: If6a85641c64853154a65e77e43e053b3d7f35bb5

bundles/org.simantics.db.common/src/org/simantics/db/common/changeset/GenericChangeListener.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependenciesRelation.java
bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/SessionImplSocket.java
bundles/org.simantics.db/src/org/simantics/db/event/ChangeEvent.java

index a971e02ca77bd87494084f7b1d567ebd6dd9c2e1..a1d2b888ccea0b7d0d4404318acf4e8894175664 100644 (file)
@@ -19,12 +19,12 @@ import org.simantics.db.MetadataI;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Session;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.event.ChangeEvent;
 import org.simantics.db.event.ChangeListener;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.request.Read;
 import org.simantics.utils.ReflectionUtils;
+import org.slf4j.LoggerFactory;
 
 /**
  * A listener added to a Session for receiving notifications for completed
@@ -43,16 +43,13 @@ abstract public class GenericChangeListener<Request, Result> implements ChangeLi
             Class<Read<Result>> clazz = ReflectionUtils.getSingleParameterTypeExtending(getClass());
             this.constructor = clazz.getConstructor(ChangeSet.class);
             return;
-        } catch (SecurityException e) {
-            Logger.defaultLogError(e);
-        } catch (NoSuchMethodException e) {
-            Logger.defaultLogError(e);
+        } catch (SecurityException | NoSuchMethodException e) {
+            LoggerFactory.getLogger(getClass()).error("Could not get constructor with param {}", ChangeSet.class.getSimpleName(), e);
+            throw new IllegalArgumentException(e);
         }
-        throw new IllegalArgumentException();
-
     }
 
-    final public void graphChanged(ChangeEvent e) throws DatabaseException {
+    public final void graphChanged(ChangeEvent e) throws DatabaseException {
 
         try {
             if (!preEventRequest())
@@ -60,14 +57,9 @@ abstract public class GenericChangeListener<Request, Result> implements ChangeLi
 
             Result event = e.getGraph().syncRequest(constructor.newInstance(e.getChanges()), TransientCacheListener.<Result>instance());
             onEvent(e.getGraph(), e.getMetadataI(), event);
-        } catch (IllegalArgumentException e1) {
-            Logger.defaultLogError(e1);
-        } catch (InstantiationException e1) {
-            Logger.defaultLogError(e1);
-        } catch (IllegalAccessException e1) {
-            Logger.defaultLogError(e1);
-        } catch (InvocationTargetException e1) {
-            Logger.defaultLogError(e1.getCause());
+        } catch (IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
+            LoggerFactory.getLogger(getClass()).error("Could not construct new instance with {}", e.getChanges(), ex);
+            throw new DatabaseException(ex);
         }
 
     }
@@ -76,7 +68,7 @@ abstract public class GenericChangeListener<Request, Result> implements ChangeLi
      * Invoked before performing the event request with the received change
      * event and sending the result to {@link #onEvent(ReadGraph, Object)}. Can
      * be used to veto processing of an event.
-     * 
+     *
      * @return <code>true</code> if the event request shall be performed and the
      *         result sent to {@link #onEvent(ReadGraph, Object)},
      *         <code>false</code> to disable any further processing of the
index 4f98af7b4ab33fe4349e7bae9845fe703e728e98..033efc9cc007d859a4ceb458f88284082cc9d302 100644 (file)
@@ -228,7 +228,7 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                                long time2 = System.nanoTime();
 
                                if (PROFILE)
-                                       System.out.println("Found " + entries.size() + " dependencies in " + 1e-6 * (time2 - time) + "ms for " + graph.getPossibleURI(subject) + ".");
+                                       LOGGER.info("Found " + entries.size() + " dependencies in " + 1e-6 * (time2 - time) + "ms for " + graph.getPossibleURI(subject) + ".");
 
                                ArrayList<Object[]> result = new ArrayList<Object[]>();
                                for (Entry entry : entries) {
@@ -290,7 +290,10 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                                if (modifiedComponent == null
                                                || modifiedComponent.getPredicate().equals(changeInformation))
                                        continue;
-                               //System.err.println("+comp modi " + NameUtils.getSafeName(graph, renamedComponent, true));
+                               if (DEBUG) {
+                                   LOGGER.info("+comp modi " + NameUtils.getSafeName(graph, modifiedComponent.getObject(), true));
+                                   LOGGER.info("    +value " + NameUtils.getSafeName(graph, value, true));
+                               }
                                w.addComponentModification(modifiedComponent.getObject());
                        }
                        for (Resource value : parameter.changedResources()) {
@@ -299,7 +302,8 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                                        w.addComponentModification(value);
                        }
                        for (StatementChange change : parameter.changedStatements()) {
-                               //System.err.println("-stm " + NameUtils.getSafeName(graph, change.getSubject(), true) + " " + NameUtils.getSafeName(graph, change.getPredicate(), true) + " " + NameUtils.getSafeName(graph, change.getObject(), true));
+                           if (DEBUG)
+                               LOGGER.info("-stm " + NameUtils.getSafeName(graph, change.getSubject(), true) + " " + NameUtils.getSafeName(graph, change.getPredicate(), true) + " " + NameUtils.getSafeName(graph, change.getObject(), true));
                                Resource subject = change.getSubject();
                                Resource predicate = change.getPredicate();
                                Resource object = change.getObject();
@@ -312,7 +316,8 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                                } else if (predicate.equals(l0.IsLinkedTo)) {
                                        w.addLinkChange(subject);
                                } else /*if (graph.isSubrelationOf(predicate, l0.DependsOn))*/ {
-                                       //System.err.println("-modi " + NameUtils.getSafeName(graph, subject, true));
+                                   if (DEBUG)
+                                       LOGGER.info("-modi " + NameUtils.getSafeName(graph, subject, true));
                                        w.addComponentModification(subject);
                                } 
                        }
@@ -368,7 +373,7 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                        TimeLogger.log(DependenciesRelation.class, "trackAndIndex.onEvent: starting index update processing");
 
                        if(DEBUG)
-                           System.err.println("Adding metadata " + event + " in revision " + graph.getService(ManagementSupport.class).getHeadRevisionId());
+                           LOGGER.info("Adding metadata " + event + " in revision " + graph.getService(ManagementSupport.class).getHeadRevisionId());
 
                        WriteGraph w = (WriteGraph)graph;
                        if(!event.isEmpty())
@@ -392,9 +397,9 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                            Collection<Object[]> _replacementObjects = Collections.emptyList();
                            Collection<Pair<String, String>> _typeChanges = Collections.emptyList();
 
-                           if(DEBUG) System.out.println("MODEL: " + NameUtils.getSafeLabel(graph, model));
+                           if(DEBUG) LOGGER.info("MODEL: " + NameUtils.getSafeLabel(graph, model));
                            //                final Change[] changes = event.get(model);
-                           if(DEBUG) System.out.println("  CHANGES: " + Arrays.toString(changes));
+                           if(DEBUG) LOGGER.info("  CHANGES: " + Arrays.toString(changes));
                            if (changes != null) {
                                _additions = new ArrayList<Object[]>();
                                _removals = new ArrayList<Object>();
@@ -414,10 +419,10 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                                            if (parent != null) {
                                                _additions.add(new Object[] { ss.getRandomAccessId(parent), ss.getRandomAccessId(entry.component), name, types, id != null ? id.indexString() : "" });
                                            } else {
-                                                   //System.err.println("resource " + entry.component + ": no parent for entry " + name + " " + types);
+                                                   //LOGGER.info("resource " + entry.component + ": no parent for entry " + name + " " + types);
                                            }
                                        } else {
-                                           //System.err.println("resource " + entry.component + ": " + name + " " + types);
+                                           //LOGGER.info("resource " + entry.component + ": " + name + " " + types);
                                        }
                                    } else if(_entry instanceof ComponentModification) {
                                        ComponentModification entry = (ComponentModification)_entry;
@@ -448,7 +453,7 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                            }
 
                            final boolean reset = linkChange || event.hasUnresolved;
-                           //System.err.println("dependencies(" + NameUtils.getSafeLabel(graph, model) + "): reset=" + reset + " linkChange=" + linkChange + " unresolved=" + event.hasUnresolved );
+                           //LOGGER.info("dependencies(" + NameUtils.getSafeLabel(graph, model) + "): reset=" + reset + " linkChange=" + linkChange + " unresolved=" + event.hasUnresolved );
 
                            if (reset || !_additions.isEmpty() || !_removals.isEmpty() || !_replacementKeys.isEmpty() || !_typeChanges.isEmpty()) {
 
@@ -472,7 +477,7 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                                            if (doReset) {
 
                                             if(DEBUG) {
-                                                System.err.println("resetIndex " + reset + " " + typeNameChanges);
+                                                LOGGER.info("resetIndex " + reset + " " + typeNameChanges);
                                             }
 
                                                indexer.removeAll(null, graph, DependenciesRelation.this, resource, model);
@@ -482,22 +487,22 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
 
                                                if (!replacementKeys.isEmpty() && (replacementKeys.size() == replacementObjects.size())) {
                                                    if(DEBUG) {
-                                                       System.out.println(replacementKeys.size() + " index replacements: " + replacementKeys);
+                                                       LOGGER.info(replacementKeys.size() + " index replacements: " + replacementKeys);
                                                    }
                                                    didChange |= indexer.replace(null, graph, DependenciesRelation.this, resource, model, Dependencies.FIELD_RESOURCE, replacementKeys, replacementObjects);
                                                }
                                                if (!removals.isEmpty()) {
                                                    if(DEBUG) {
-                                                       System.out.println(removals.size() + " index removals: " + removals);
+                                                       LOGGER.info(removals.size() + " index removals: " + removals);
                                                    }
                                                    indexer.remove(null, graph, DependenciesRelation.this, resource, model, Dependencies.FIELD_RESOURCE, removals);
                                                    didChange = true;
                                                }
                                                if (!additions.isEmpty()) {
                                                    if(DEBUG) {
-                                                       for(Object[] os : additions) System.err.println("Adding to index " + model + ": " + Arrays.toString(os));
+                                                       for(Object[] os : additions) LOGGER.info("Adding to index " + model + ": " + Arrays.toString(os));
                                                    }
-                                                   //System.out.println(additions.size() + " index insertions");
+                                                   //LOGGER.info(additions.size() + " index insertions");
                                                    indexer.insert(null, graph, DependenciesRelation.this, resource, model, additions);
                                                    didChange = true;
                                                }
@@ -557,7 +562,7 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
 
                for (Pair<String, String> nr : typeChanges) {
                        String query = Dependencies.FIELD_RESOURCE + ":[" + nr.second + " TO " + nr.second + "]";
-                       //System.out.println("query: " + query);
+                       //LOGGER.info("query: " + query);
                        List<Map<String, Object>> results = indexer.query(null, query, graph, resource, model, Integer.MAX_VALUE);
                        if (results.size() != 1) {
                                return true;
@@ -567,7 +572,7 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
                                        return true;
                                }
                        }
-//                     System.err.println("Type " + nr.first + " was unchanged.");
+//                     LOGGER.info("Type " + nr.first + " was unchanged.");
                }
                return false;
        }
@@ -593,7 +598,7 @@ public class DependenciesRelation extends UnsupportedRelation implements Generic
        @Override
        public void reset(RequestProcessor processor, Resource input) {
                if (DEBUG) {
-                       System.out.println("DependenciesRelation.reset: " + input);
+                       LOGGER.info("DependenciesRelation.reset: " + input);
                        new Exception("DependenciesRelation.reset(" + listener + ")").printStackTrace(System.out);
                }
                DependenciesListenerStore store = processor.getSession().getService(DependenciesListenerStore.class);
index 9b152f2da02f16e80a1b16a2dbbe7e8e9579e981..a18b66e6978a03d66e46b965848b0f9c956fa961 100644 (file)
@@ -166,6 +166,7 @@ import org.simantics.utils.DataContainer;
 import org.simantics.utils.Development;
 import org.simantics.utils.threads.logger.ITask;
 import org.simantics.utils.threads.logger.ThreadLogger;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.procedure.TLongProcedure;
 import gnu.trove.set.hash.TLongHashSet;
@@ -173,6 +174,8 @@ import gnu.trove.set.hash.TLongHashSet;
 
 public abstract class SessionImplSocket implements Session, WriteRequestScheduleSupport {
 
+    private static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SessionImplSocket.class);
+
     protected static final boolean DEBUG        = false;
 
     private static final boolean DIAGNOSTICS       = false;
@@ -2481,7 +2484,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
                     try {
                         l.graphChanged(e2);
                     } catch (Throwable ex) {
-                        ex.printStackTrace();
+                        LOGGER.error("Could not invoke listener {} with event {}", l, e2, ex);
                     }
                 }
 
@@ -2490,7 +2493,7 @@ public abstract class SessionImplSocket implements Session, WriteRequestSchedule
             }
 
         } catch (Throwable t) {
-            t.printStackTrace();
+            LOGGER.error("Could not fire metadata listeners {} {}", graph, cs2, t);
         }
 
     }
index 39e3abbaa80de48dd0e95a2519c27df5e78b330b..caf158ae1d99a571c54a85a637a85e1be230ecf0 100644 (file)
@@ -62,5 +62,13 @@ public class ChangeEvent {
        return metadataGraph;
     }
     
-
+    @Override
+    public String toString() {
+        return new StringBuilder()
+                .append(getClass().getSimpleName()).append(" ")
+                .append(changes).append(" ")
+                .append(graph).append(" ")
+                .append(metadataGraph).append(" ")
+                .append(session).toString();
+    }
 }