1 package org.apache.archiva.redback.keys.cached;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.List;
24 import javax.inject.Inject;
25 import javax.inject.Named;
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;
38 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
41 @Service("keyManager#cached")
42 public class CachedKeyManager
43 extends AbstractKeyManager
46 @Inject @Named(value="keyManager#jdo")
47 private KeyManager keyImpl;
49 @Inject @Named(value="cache#keys")
50 private Cache keysCache;
52 public AuthenticationKey addKey( AuthenticationKey key )
56 keysCache.remove( key.getKey() );
58 return this.keyImpl.addKey( key );
61 public AuthenticationKey createKey( String principal, String purpose, int expirationMinutes )
62 throws KeyManagerException
64 AuthenticationKey authkey = this.keyImpl.createKey( principal, purpose, expirationMinutes );
65 keysCache.remove( authkey.getKey() );
69 public void deleteKey( AuthenticationKey key )
70 throws KeyManagerException
72 keysCache.remove( key.getKey() );
73 this.keyImpl.deleteKey( key );
76 public void deleteKey( String key )
77 throws KeyManagerException
79 keysCache.remove( key );
80 this.keyImpl.deleteKey( key );
83 public void eraseDatabase()
87 this.keyImpl.eraseDatabase();
91 this.keysCache.clear();
95 public AuthenticationKey findKey( String key )
96 throws KeyNotFoundException, KeyManagerException
100 AuthenticationKey authkey = (AuthenticationKey) keysCache.get( key );
101 if ( authkey != null )
103 assertNotExpired( authkey );
108 authkey = this.keyImpl.findKey( key );
109 keysCache.put( key,authkey );
113 catch ( KeyNotFoundException knfe )
115 // this is done to remove keys that have been expired.
116 // TODO: need to make a listener for the key manager.
117 keysCache.remove( key );
122 public List<AuthenticationKey> getAllKeys()
124 log.debug( "NOT CACHED - .getAllKeys()" );
125 return this.keyImpl.getAllKeys();
128 public String getId()
130 return "Cached Key Manager [" + this.keyImpl.getId() + "]";
133 public KeyManager getKeyImpl()
138 public void setKeyImpl( KeyManager keyImpl )
140 this.keyImpl = keyImpl;
143 public Cache getKeysCache()
148 public void setKeysCache( Cache keysCache )
150 this.keysCache = keysCache;