This project has retired. For details please refer to its Attic page.
CachedKeyManager xref
View Javadoc

1   package org.apache.archiva.redback.keys.cached;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import javax.inject.Inject;
25  import javax.inject.Named;
26  
27  import org.apache.archiva.redback.components.cache.Cache;
28  import org.apache.archiva.redback.keys.AbstractKeyManager;
29  import org.apache.archiva.redback.keys.AuthenticationKey;
30  import org.apache.archiva.redback.keys.KeyManager;
31  import org.apache.archiva.redback.keys.KeyManagerException;
32  import org.apache.archiva.redback.keys.KeyNotFoundException;
33  import org.springframework.stereotype.Service;
34  
35  /**
36   * CachedKeyManager
37   *
38   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
39   */
40  @Service("keyManager#cached")
41  public class CachedKeyManager
42      extends AbstractKeyManager
43      implements KeyManager
44  {
45      @Inject
46      @Named(value = "keyManager#jdo")
47      private KeyManager keyImpl;
48  
49      @Inject
50      @Named(value = "cache#keys")
51      private Cache<String, AuthenticationKey> keysCache;
52  
53      public AuthenticationKey addKey( AuthenticationKey key )
54      {
55          if ( key != null )
56          {
57              keysCache.remove( key.getKey() );
58          }
59          return this.keyImpl.addKey( key );
60      }
61  
62      public AuthenticationKey createKey( String principal, String purpose, int expirationMinutes )
63          throws KeyManagerException
64      {
65          AuthenticationKey authkey = this.keyImpl.createKey( principal, purpose, expirationMinutes );
66          keysCache.remove( authkey.getKey() );
67          return authkey;
68      }
69  
70      public void deleteKey( AuthenticationKey key )
71          throws KeyManagerException
72      {
73          keysCache.remove( key.getKey() );
74          this.keyImpl.deleteKey( key );
75      }
76  
77      public void deleteKey( String key )
78          throws KeyManagerException
79      {
80          keysCache.remove( key );
81          this.keyImpl.deleteKey( key );
82      }
83  
84      public void eraseDatabase()
85      {
86          try
87          {
88              this.keyImpl.eraseDatabase();
89          }
90          finally
91          {
92              this.keysCache.clear();
93          }
94      }
95  
96      public AuthenticationKey findKey( String key )
97          throws KeyNotFoundException, KeyManagerException
98      {
99          try
100         {
101             AuthenticationKey authkey = keysCache.get( key );
102             if ( authkey != null )
103             {
104                 assertNotExpired( authkey );
105                 return authkey;
106             }
107             else
108             {
109                 authkey = this.keyImpl.findKey( key );
110                 keysCache.put( key, authkey );
111                 return authkey;
112             }
113         }
114         catch ( KeyNotFoundException knfe )
115         {
116             // this is done to remove keys that have been expired.
117             // TODO: need to make a listener for the key manager.
118             keysCache.remove( key );
119             throw knfe;
120         }
121     }
122 
123     public List<AuthenticationKey> getAllKeys()
124     {
125         log.debug( "NOT CACHED - .getAllKeys()" );
126         return this.keyImpl.getAllKeys();
127     }
128 
129     public String getId()
130     {
131         return "Cached Key Manager [" + this.keyImpl.getId() + "]";
132     }
133 
134     public KeyManager getKeyImpl()
135     {
136         return keyImpl;
137     }
138 
139     public void setKeyImpl( KeyManager keyImpl )
140     {
141         this.keyImpl = keyImpl;
142     }
143 
144     public Cache getKeysCache()
145     {
146         return keysCache;
147     }
148 
149     public void setKeysCache( Cache keysCache )
150     {
151         this.keysCache = keysCache;
152     }
153 }