View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.frontend.fileupload;
20  
21  import org.apache.logging.log4j.LogManager;
22  import org.apache.logging.log4j.Logger;
23  import org.mycore.common.MCRCache;
24  import org.mycore.common.MCRSession;
25  import org.mycore.common.MCRSessionMgr;
26  import org.mycore.common.MCRUsageException;
27  import org.mycore.common.processing.MCRProcessableCollection;
28  import org.mycore.common.processing.MCRProcessableDefaultCollection;
29  import org.mycore.common.processing.MCRProcessableRegistry;
30  
31  /**
32   * @author Frank Lützenkirchen
33   * @author Harald Richter
34   * @author Jens Kupferschmidt
35   * 
36   * @version $Revision$
37   */
38  public class MCRUploadHandlerManager {
39  
40      private static Logger LOGGER = LogManager.getLogger(MCRUploadHandlerManager.class);
41  
42      /** Cache of currently active upload handler sessions */
43      protected static MCRCache<String, MCRUploadHandlerCacheEntry> HANDLERS;
44  
45      protected static MCRProcessableCollection COLLECTION;
46  
47      static {
48          HANDLERS = new MCRCache<>(100, "UploadHandlerManager UploadHandlers");
49          COLLECTION = new MCRProcessableDefaultCollection("Upload Manager");
50          MCRProcessableRegistry registry = MCRProcessableRegistry.getSingleInstance();
51          registry.register(COLLECTION);
52      }
53  
54      static void register(MCRUploadHandler handler) {
55          LOGGER.debug("Registering {} with upload ID {}", handler.getClass().getName(), handler.getID());
56          String sessionID = MCRSessionMgr.getCurrentSession().getID();
57          HANDLERS.put(handler.getID(), new MCRUploadHandlerCacheEntry(sessionID, handler));
58          COLLECTION.add(handler);
59      }
60  
61      public static MCRUploadHandler getHandler(String uploadID) {
62  
63          long yesterday = System.currentTimeMillis() - 86400000;
64          MCRUploadHandlerCacheEntry entry = HANDLERS.getIfUpToDate(uploadID, yesterday);
65  
66          if (entry == null) {
67              throw new MCRUsageException("Upload session " + uploadID + " timed out");
68          }
69  
70          String sessionID = entry.getSessionID();
71  
72          if (!sessionID.equals(MCRSessionMgr.getCurrentSessionID())) {
73              MCRSession session = MCRSessionMgr.getSession(sessionID);
74              if (session != null) {
75                  MCRSessionMgr.setCurrentSession(session);
76              }
77          }
78  
79          return entry.getUploadHandler();
80      }
81  
82      public static void unregister(String uploadID) {
83          MCRUploadHandlerCacheEntry cacheEntry = HANDLERS.get(uploadID);
84          HANDLERS.remove(uploadID);
85          COLLECTION.remove(cacheEntry.handler);
86      }
87  
88      /** Represents a cache entry of currently active upload handler session */
89      private static class MCRUploadHandlerCacheEntry {
90  
91          /** The ID of the MCRSession this upload is associated with */
92          private String sessionID;
93  
94          /** The MCRUploadHander instance to be used */
95          private MCRUploadHandler handler;
96  
97          MCRUploadHandlerCacheEntry(String sessionID, MCRUploadHandler handler) {
98              this.sessionID = sessionID;
99              this.handler = handler;
100         }
101 
102         public String getSessionID() {
103             return sessionID;
104         }
105 
106         public MCRUploadHandler getUploadHandler() {
107             return handler;
108         }
109     }
110 }