Added a worker thread to cleanup expired artifacts from the memory artifact mapper.
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / artifact / provider / MemoryArtifactMapper.java
1 /*
2  * The Shibboleth License, Version 1. Copyright (c) 2002 University Corporation for Advanced Internet Development, Inc.
3  * All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted
4  * provided that the following conditions are met: Redistributions of source code must retain the above copyright
5  * notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above
6  * copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials
7  * provided with the distribution, if any, must include the following acknowledgment: "This product includes software
8  * developed by the University Corporation for Advanced Internet Development <http://www.ucaid.edu> Internet2 Project.
9  * Alternately, this acknowledegement may appear in the software itself, if and wherever such third-party
10  * acknowledgments normally appear. Neither the name of Shibboleth nor the names of its contributors, nor Internet2, nor
11  * the University Corporation for Advanced Internet Development, Inc., nor UCAID may be used to endorse or promote
12  * products derived from this software without specific prior written permission. For written permission, please contact
13  * shibboleth@shibboleth.org Products derived from this software may not be called Shibboleth, Internet2, UCAID, or the
14  * University Corporation for Advanced Internet Development, nor may Shibboleth appear in their name, without prior
15  * written permission of the University Corporation for Advanced Internet Development. THIS SOFTWARE IS PROVIDED BY THE
16  * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE
18  * DISCLAIMED AND THE ENTIRE RISK OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. IN NO
19  * EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC.
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 package edu.internet2.middleware.shibboleth.artifact.provider;
27
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.Map.Entry;
35
36 import org.apache.log4j.Logger;
37 import org.opensaml.artifact.Artifact;
38 import org.w3c.dom.Element;
39
40 import edu.internet2.middleware.shibboleth.artifact.ArtifactMapper;
41 import edu.internet2.middleware.shibboleth.artifact.ArtifactMapping;
42 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
43
44 /**
45  * <code>ArtifactMapper</code> implementation that saves queryable artifacts in memory.
46  * 
47  * @author Walter Hoehn
48  */
49 public class MemoryArtifactMapper extends BaseArtifactMapper implements ArtifactMapper {
50
51         private MemoryArtifactCleaner cleaner = new MemoryArtifactCleaner();
52         private static Logger log = Logger.getLogger(MemoryArtifactMapper.class.getName());
53         private static Map mappings = Collections.synchronizedMap(new HashMap());
54
55         public MemoryArtifactMapper() throws ShibbolethConfigurationException {
56
57                 super();
58         }
59
60         public MemoryArtifactMapper(Element config) throws ShibbolethConfigurationException {
61
62                 super(config);
63         }
64
65         public ArtifactMapping recoverAssertion(Artifact artifact) {
66
67                 ArtifactMapping mapping = (ArtifactMapping) mappings.get(artifact);
68                 mappings.remove(artifact);
69                 if (mapping == null || mapping.isExpired()) { return null; }
70                 return mapping;
71         }
72
73         public void addAssertionImpl(Artifact artifact, ArtifactMapping mapping) {
74
75                 mappings.put(artifact, mapping);
76         }
77
78         protected void destroy() {
79
80                 synchronized (cleaner) {
81                         if (cleaner != null) {
82                                 cleaner.shutdown = true;
83                                 cleaner.interrupt();
84                         }
85                 }
86         }
87
88         protected void finalize() throws Throwable {
89
90                 super.finalize();
91                 destroy();
92         }
93
94         private class MemoryArtifactCleaner extends Thread {
95
96                 private boolean shutdown = false;
97                 private Thread master;
98
99                 public MemoryArtifactCleaner() {
100
101                         super("edu.internet2.middleware.shibboleth.idp.provider.MemoryArtifactMapper..MemoryArtifactCleaner");
102                         this.master = Thread.currentThread();
103                         setDaemon(true);
104                         if (getPriority() > Thread.MIN_PRIORITY) {
105                                 setPriority(getPriority() - 1);
106                         }
107                         log.debug("Starting memory-based artifact mapper cleanup thread.");
108                         start();
109                 }
110
111                 public void run() {
112
113                         try {
114                                 sleep(60 * 1000); // one minute
115                         } catch (InterruptedException e) {
116                                 log.debug("Memory-based artifact mapper cleanup interrupted.");
117                         }
118                         while (true) {
119                                 try {
120                                         if (!master.isAlive()) {
121                                                 shutdown = true;
122                                                 log.debug("Memory-based artifact mapper cleaner is orphaned.");
123                                         }
124                                         if (shutdown) {
125                                                 log.debug("Stopping Memory-based artifact mapper cleanup thread.");
126                                                 return;
127                                         }
128                                         log.debug("Memory cartifact mapper cleanup thread searching for stale entries.");
129                                         Set needsDeleting = new HashSet();
130                                         synchronized (mappings) {
131                                                 Iterator iterator = mappings.entrySet().iterator();
132                                                 while (iterator.hasNext()) {
133                                                         Entry entry = (Entry) iterator.next();
134                                                         ArtifactMapping mapping = (ArtifactMapping) entry.getValue();
135                                                         if (mapping.isExpired()) {
136                                                                 needsDeleting.add(entry.getKey());
137                                                         }
138                                                 }
139                                                 // release the lock to be friendly
140                                                 Iterator deleteIterator = needsDeleting.iterator();
141                                                 while (deleteIterator.hasNext()) {
142                                                         synchronized (mappings) {
143                                                                 log.debug("Expiring an Artifact from the memory cache.");
144                                                                 mappings.remove(deleteIterator.next());
145                                                         }
146                                                 }
147                                         }
148                                         sleep(60 * 1000); // one minute
149                                 } catch (InterruptedException e) {
150                                         log.debug("Memory-based artifact mapper cleanup interrupted.");
151                                 }
152                         }
153                 }
154         }
155
156 }