1 package org.apache.archiva.redback.keys.memory;
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 org.apache.archiva.redback.keys.AbstractKeyManager;
23 import org.apache.archiva.redback.keys.AuthenticationKey;
24 import org.apache.archiva.redback.keys.KeyManagerException;
25 import org.apache.archiva.redback.keys.KeyNotFoundException;
26 import org.apache.archiva.redback.keys.memory.MemoryAuthenticationKey;
27 import org.codehaus.plexus.util.StringUtils;
28 import org.springframework.stereotype.Service;
30 import java.util.ArrayList;
31 import java.util.Calendar;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.List;
38 * KeyManager backed by an in-memory only store.
40 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
43 @Service("keyManager#memory")
44 public class MemoryKeyManager
45 extends AbstractKeyManager
47 private Map<String, AuthenticationKey> keys = new HashMap<String, AuthenticationKey>();
49 public AuthenticationKey createKey( String principal, String purpose, int expirationMinutes )
50 throws KeyManagerException
52 AuthenticationKey key = new MemoryAuthenticationKey();
53 key.setKey( super.generateUUID() );
54 key.setForPrincipal( principal );
55 key.setPurpose( purpose );
56 key.setDateCreated( new Date() );
58 if ( expirationMinutes >= 0 )
60 Calendar expiration = Calendar.getInstance();
61 expiration.add( Calendar.MINUTE, expirationMinutes );
62 key.setDateExpires( expiration.getTime() );
65 keys.put( key.getKey(), key );
70 public AuthenticationKey findKey( String key )
71 throws KeyNotFoundException, KeyManagerException
73 if ( StringUtils.isEmpty( key ) )
75 throw new KeyNotFoundException( "Empty key not found." );
78 AuthenticationKey authkey = keys.get( key );
80 if ( authkey == null )
82 throw new KeyNotFoundException( "Key [" + key + "] not found." );
85 assertNotExpired( authkey );
90 public void deleteKey( AuthenticationKey authkey )
91 throws KeyManagerException
93 keys.remove( authkey );
96 public void deleteKey( String key )
97 throws KeyManagerException
99 AuthenticationKey authkey = keys.get( key );
100 if ( authkey != null )
102 keys.remove( authkey );
106 public List<AuthenticationKey> getAllKeys()
108 return new ArrayList<AuthenticationKey>( keys.values() );
111 public AuthenticationKey addKey( AuthenticationKey key )
113 keys.put( key.getKey(), key );
117 public void eraseDatabase()
122 public String getId()
124 return "Memory Key Manager";