From f126bc114a7b4f82c54961ffe5d763219493f946 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Sun, 29 Jul 2012 20:08:11 +0000
Subject: [PATCH] Remove DataStoreUserSessionCleaner and call the
 cleanupSession() method of all DSSes always on user session close.

SVN: 26262
---
 .../server/CommonBusinessObjectFactory.java   | 15 +---
 .../generic/server/SessionFactory.java        | 43 ++++++++++--
 .../server/business/bo/DataSetTable.java      |  7 --
 .../util/DataStoreUserSessionCleaner.java     | 70 -------------------
 .../source/java/genericApplicationContext.xml |  8 +--
 .../server/business/bo/DataSetTableTest.java  |  8 +--
 6 files changed, 47 insertions(+), 104 deletions(-)
 delete mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/util/DataStoreUserSessionCleaner.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonBusinessObjectFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonBusinessObjectFactory.java
index f8de35e2299..adf5605dce5 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonBusinessObjectFactory.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/CommonBusinessObjectFactory.java
@@ -80,7 +80,6 @@ import ch.systemsx.cisd.openbis.generic.server.business.bo.samplelister.ISampleL
 import ch.systemsx.cisd.openbis.generic.server.business.bo.samplelister.SampleLister;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
 import ch.systemsx.cisd.openbis.generic.server.jython.api.v1.impl.IMasterDataScriptRegistrationRunner;
-import ch.systemsx.cisd.openbis.generic.server.util.DataStoreUserSessionCleaner;
 import ch.systemsx.cisd.openbis.generic.shared.IEntityOperationChecker;
 import ch.systemsx.cisd.openbis.generic.shared.IRelationshipService;
 import ch.systemsx.cisd.openbis.generic.shared.dto.Session;
@@ -95,20 +94,11 @@ public final class CommonBusinessObjectFactory extends AbstractBusinessObjectFac
         ICommonBusinessObjectFactory
 {
 
-    private final DataStoreUserSessionCleaner dssUserSessionCleaner;
-
     public CommonBusinessObjectFactory(IDAOFactory daoFactory, IDataStoreServiceFactory dssFactory,
             IRelationshipService relationshipService,
-            IEntityOperationChecker entityOperationChecker,
-            DataStoreUserSessionCleaner dssUserSessionCleaner)
+            IEntityOperationChecker entityOperationChecker)
     {
         super(daoFactory, dssFactory, relationshipService, entityOperationChecker);
-        this.dssUserSessionCleaner = dssUserSessionCleaner;
-    }
-
-    protected DataStoreUserSessionCleaner getDssUserSessionCleaner()
-    {
-        return dssUserSessionCleaner;
     }
 
     @Override
@@ -176,8 +166,7 @@ public final class CommonBusinessObjectFactory extends AbstractBusinessObjectFac
     @Override
     public final IDataSetTable createDataSetTable(final Session session)
     {
-        return new DataSetTable(getDaoFactory(), getDSSFactory(), getDssUserSessionCleaner(),
-                session, getRelationshipService());
+        return new DataSetTable(getDaoFactory(), getDSSFactory(), session, getRelationshipService());
     }
 
     @Override
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/SessionFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/SessionFactory.java
index b910428f793..011b8c16edb 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/SessionFactory.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/SessionFactory.java
@@ -18,15 +18,34 @@ package ch.systemsx.cisd.openbis.generic.server;
 
 import ch.systemsx.cisd.authentication.ISessionFactory;
 import ch.systemsx.cisd.authentication.Principal;
+import ch.systemsx.cisd.openbis.generic.server.business.IDataStoreServiceFactory;
+import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
+import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDataStoreDAO;
+import ch.systemsx.cisd.openbis.generic.shared.dto.DataStorePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.Session;
+import ch.systemsx.cisd.openbis.generic.shared.dto.Session.ISessionCleaner;
 
 /**
  * Factory of {@link Session} objects.
  * 
- * @author     Franz-Josef Elmer
+ * @author Franz-Josef Elmer
  */
 public final class SessionFactory implements ISessionFactory<Session>
 {
+    private final IDataStoreDAO datastoreDAO;
+
+    private final IDataStoreServiceFactory dssFactory;
+
+    public SessionFactory()
+    {
+        this(null, null);
+    }
+
+    public SessionFactory(IDAOFactory daoFactory, IDataStoreServiceFactory dssFactory)
+    {
+        this.datastoreDAO = (daoFactory != null) ? daoFactory.getDataStoreDAO() : null;
+        this.dssFactory = dssFactory;
+    }
 
     //
     // ISessionFactory
@@ -37,8 +56,24 @@ public final class SessionFactory implements ISessionFactory<Session>
             final Principal principal, final String remoteHost, final long sessionStart,
             final int expirationTime)
     {
-        return new Session(userName, sessionToken, principal, remoteHost, sessionStart,
-                expirationTime);
+        final Session session =
+                new Session(userName, sessionToken, principal, remoteHost, sessionStart,
+                        expirationTime);
+        if (datastoreDAO != null && dssFactory != null)
+        {
+            session.addCleanupListener(new ISessionCleaner()
+                {
+                    @Override
+                    public void cleanup()
+                    {
+                        for (DataStorePE datastore : datastoreDAO.listDataStores())
+                        {
+                            dssFactory.create(datastore.getRemoteUrl())
+                                    .cleanupSession(sessionToken);
+                        }
+                    }
+                });
+        }
+        return session;
     }
-
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTable.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTable.java
index 618e81a06ec..e2dd5d7b26a 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTable.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTable.java
@@ -49,7 +49,6 @@ import ch.systemsx.cisd.openbis.generic.server.business.bo.exception.DataSetDele
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDataDAO;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.event.DeleteDataSetEventBuilder;
-import ch.systemsx.cisd.openbis.generic.server.util.DataStoreUserSessionCleaner;
 import ch.systemsx.cisd.openbis.generic.shared.Constants;
 import ch.systemsx.cisd.openbis.generic.shared.IDataStoreService;
 import ch.systemsx.cisd.openbis.generic.shared.IRelationshipService;
@@ -198,15 +197,11 @@ public final class DataSetTable extends AbstractDataSetBusinessObject implements
 
     private List<DataPE> dataSets;
 
-    private final DataStoreUserSessionCleaner dssUserSessionCleaner;
-
     public DataSetTable(IDAOFactory daoFactory, IDataStoreServiceFactory dssFactory,
-            DataStoreUserSessionCleaner dssUserSessionCleaner,
             Session session, IRelationshipService relationshipService)
     {
         super(daoFactory, session, relationshipService);
         this.dssFactory = dssFactory;
-        this.dssUserSessionCleaner = dssUserSessionCleaner;
     }
 
     //
@@ -601,7 +596,6 @@ public final class DataSetTable extends AbstractDataSetBusinessObject implements
         List<DatasetDescription> locations = loadAvailableDatasetDescriptions(datasetCodes);
         String sessionToken = dataStore.getSessionToken();
         String userSessionToken = session.getSessionToken();
-        dssUserSessionCleaner.add(session, service);
         return service.createReportFromDatasets(sessionToken, userSessionToken,
                 datastoreServiceKey, locations, tryGetLoggedUserEmail());
     }
@@ -1056,7 +1050,6 @@ public final class DataSetTable extends AbstractDataSetBusinessObject implements
         }
         String sessionToken = dataStore.getSessionToken();
         String userSessionToken = session.getSessionToken();
-        dssUserSessionCleaner.add(session, service);
         return service.createReportFromAggregationService(sessionToken, userSessionToken,
                 datastoreServiceKey, parameters, tryGetLoggedUserEmail());
     }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/util/DataStoreUserSessionCleaner.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/util/DataStoreUserSessionCleaner.java
deleted file mode 100644
index b10a5c1fbf7..00000000000
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/util/DataStoreUserSessionCleaner.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright 2012 ETH Zuerich, CISD
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package ch.systemsx.cisd.openbis.generic.server.util;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import ch.systemsx.cisd.openbis.generic.shared.IDataStoreService;
-import ch.systemsx.cisd.openbis.generic.shared.dto.Session;
-import ch.systemsx.cisd.openbis.generic.shared.dto.Session.ISessionCleaner;
-
-/**
- * A cleaner for user session data on data store servers.
- * 
- * @author Bernd Rinn
- */
-public class DataStoreUserSessionCleaner
-{
-
-    private final Map<String, Set<IDataStoreService>> sessions =
-            new HashMap<String, Set<IDataStoreService>>();
-
-    public void add(Session userSession, IDataStoreService dataStore)
-    {
-        final String userSessionToken = userSession.getSessionToken();
-        final Set<IDataStoreService> dataStores = getOrCreateDataStores(userSessionToken);
-        // Note: data stores are cached by DataStoreServiceFactory, thus the set works out.
-        dataStores.add(dataStore);
-        userSession.addCleanupListener(new ISessionCleaner()
-            {
-                @Override
-                public void cleanup()
-                {
-                    for (IDataStoreService service : dataStores)
-                    {
-                        service.cleanupSession(userSessionToken);
-                    }
-                    sessions.remove(userSessionToken);
-                }
-            });
-    }
-
-    private Set<IDataStoreService> getOrCreateDataStores(String userSessionToken)
-    {
-        Set<IDataStoreService> dataStores = sessions.get(userSessionToken);
-        if (dataStores == null)
-        {
-            dataStores = new HashSet<IDataStoreService>();
-            sessions.put(userSessionToken, dataStores);
-        }
-        return dataStores;
-    }
-
-}
diff --git a/openbis/source/java/genericApplicationContext.xml b/openbis/source/java/genericApplicationContext.xml
index 340492fb763..d6c42de0193 100644
--- a/openbis/source/java/genericApplicationContext.xml
+++ b/openbis/source/java/genericApplicationContext.xml
@@ -49,8 +49,6 @@
     
     <bean id="dss-factory" class="ch.systemsx.cisd.openbis.generic.server.business.DataStoreServiceFactory"/>
 
-		<bean id="dss-user-session-cleaner" class="ch.systemsx.cisd.openbis.generic.server.util.DataStoreUserSessionCleaner" />
-
     <bean id="authentication-service"
         class="ch.systemsx.cisd.openbis.generic.server.AuthenticationServiceHolder">
         <constructor-arg ref="${authentication-service}" />
@@ -58,7 +56,10 @@
 
     <bean id="session-manager" class="ch.systemsx.cisd.authentication.DefaultSessionManager">
         <constructor-arg>
-            <bean class="ch.systemsx.cisd.openbis.generic.server.SessionFactory" />
+            <bean class="ch.systemsx.cisd.openbis.generic.server.SessionFactory">
+                <constructor-arg ref="dao-factory" />
+                <constructor-arg ref="dss-factory" />
+            </bean>
         </constructor-arg>
         <constructor-arg>
             <bean class="ch.systemsx.cisd.openbis.generic.shared.LogMessagePrefixGenerator" />
@@ -85,7 +86,6 @@
         <constructor-arg ref="dss-factory" />
         <constructor-arg ref="relationship-service" />
         <constructor-arg ref="entity-operation-checker" />
-        <constructor-arg ref="dss-user-session-cleaner" />
     </bean>
 
     <bean id="last-modification-state"
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTableTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTableTest.java
index c0ece750234..e6e68f0df91 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTableTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/DataSetTableTest.java
@@ -43,7 +43,6 @@ import ch.systemsx.cisd.openbis.generic.server.business.IDataStoreServiceFactory
 import ch.systemsx.cisd.openbis.generic.server.business.ManagerTestTool;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.exception.DataSetDeletionUnknownLocationsException;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.event.DeleteDataSetEventBuilder;
-import ch.systemsx.cisd.openbis.generic.server.util.DataStoreUserSessionCleaner;
 import ch.systemsx.cisd.openbis.generic.shared.CommonTestUtils;
 import ch.systemsx.cisd.openbis.generic.shared.IDataStoreService;
 import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant;
@@ -80,8 +79,6 @@ public final class DataSetTableTest extends AbstractBOTest
 {
     private IDataStoreServiceFactory dssFactory;
 
-    private DataStoreUserSessionCleaner dssUserSessionCleaner;
-
     private DataStorePE dss1;
 
     private DataStorePE dss2;
@@ -96,8 +93,8 @@ public final class DataSetTableTest extends AbstractBOTest
 
     private final DataSetTable createDataSetTable()
     {
-        return new DataSetTable(daoFactory, dssFactory, dssUserSessionCleaner,
-                ManagerTestTool.EXAMPLE_SESSION, relationshipService);
+        return new DataSetTable(daoFactory, dssFactory, ManagerTestTool.EXAMPLE_SESSION,
+                relationshipService);
     }
 
     @BeforeMethod
@@ -106,7 +103,6 @@ public final class DataSetTableTest extends AbstractBOTest
     {
         super.beforeMethod();
         dssFactory = context.mock(IDataStoreServiceFactory.class);
-        dssUserSessionCleaner = new DataStoreUserSessionCleaner();
         dss1 = createDataStore("dss1", false);
         dss2 = createDataStore("dss2", true);
         dss3 = createDataStore("dss3", true);
-- 
GitLab