]> source.dussan.org Git - archiva.git/blob
d159b83dc40615aea517e0a9ef7a0d5e4d0d23a8
[archiva.git] /
1 package org.codehaus.plexus.redback.keys.jdo;
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 org.codehaus.plexus.jdo.JdoFactory;
23 import org.codehaus.plexus.jdo.PlexusJdoUtils;
24 import org.codehaus.plexus.jdo.PlexusObjectNotFoundException;
25 import org.codehaus.plexus.jdo.PlexusStoreException;
26 import org.codehaus.plexus.redback.keys.AbstractKeyManager;
27 import org.codehaus.plexus.redback.keys.AuthenticationKey;
28 import org.codehaus.plexus.redback.keys.KeyManagerException;
29 import org.codehaus.plexus.redback.keys.KeyNotFoundException;
30 import org.codehaus.plexus.util.StringUtils;
31 import org.jpox.PersistenceManagerFactoryImpl;
32 import org.springframework.stereotype.Service;
33
34 import javax.annotation.PostConstruct;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37 import javax.jdo.PersistenceManager;
38 import javax.jdo.PersistenceManagerFactory;
39 import java.util.Calendar;
40 import java.util.List;
41
42 /**
43  * JdoKeyManager
44  *
45  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
46  * @version $Id$
47  */
48 @Service( "keyManager#jdo" )
49 public class JdoKeyManager
50     extends AbstractKeyManager
51 {
52     @Inject
53     @Named( value = "jdoFactory#users" )
54     private JdoFactory jdoFactory;
55
56     private PersistenceManagerFactory pmf;
57
58     public AuthenticationKey createKey( String principal, String purpose, int expirationMinutes )
59         throws KeyManagerException
60     {
61         JdoAuthenticationKey authkey = new JdoAuthenticationKey();
62         authkey.setKey( super.generateUUID() );
63         authkey.setForPrincipal( principal );
64         authkey.setPurpose( purpose );
65
66         Calendar now = getNowGMT();
67         authkey.setDateCreated( now.getTime() );
68
69         if ( expirationMinutes >= 0 )
70         {
71             Calendar expiration = getNowGMT();
72             expiration.add( Calendar.MINUTE, expirationMinutes );
73             authkey.setDateExpires( expiration.getTime() );
74         }
75
76         return addKey( authkey );
77     }
78
79     public AuthenticationKey addKey( AuthenticationKey key )
80     {
81         return (AuthenticationKey) PlexusJdoUtils.addObject( getPersistenceManager(), key );
82     }
83
84     public void eraseDatabase()
85     {
86         PlexusJdoUtils.removeAll( getPersistenceManager(), JdoAuthenticationKey.class );
87         PlexusJdoUtils.removeAll( getPersistenceManager(), RedbackKeyManagementJdoModelloMetadata.class );
88     }
89
90     public AuthenticationKey findKey( String key )
91         throws KeyNotFoundException, KeyManagerException
92     {
93         if ( StringUtils.isEmpty( key ) )
94         {
95             throw new KeyNotFoundException( "Empty key not found." );
96         }
97
98         try
99         {
100             JdoAuthenticationKey authkey = (JdoAuthenticationKey) PlexusJdoUtils.getObjectById( getPersistenceManager(),
101                                                                                                 JdoAuthenticationKey.class,
102                                                                                                 key );
103
104             if ( authkey == null )
105             {
106                 throw new KeyNotFoundException( "Key [" + key + "] not found." );
107             }
108             assertNotExpired( authkey );
109
110             return authkey;
111         }
112         catch ( PlexusObjectNotFoundException e )
113         {
114             throw new KeyNotFoundException( e.getMessage() );
115         }
116         catch ( PlexusStoreException e )
117         {
118             throw new KeyManagerException(
119                 "Unable to get " + JdoAuthenticationKey.class.getName() + "', key '" + key + "' from jdo store." );
120         }
121     }
122
123     public void deleteKey( AuthenticationKey authkey )
124         throws KeyManagerException
125     {
126         PlexusJdoUtils.removeObject( getPersistenceManager(), authkey );
127     }
128
129     public void deleteKey( String key )
130         throws KeyManagerException
131     {
132         try
133         {
134             AuthenticationKey authkey = findKey( key );
135             PlexusJdoUtils.removeObject( getPersistenceManager(), authkey );
136         }
137         catch ( KeyNotFoundException e )
138         {
139             // not found? nothing to do.
140         }
141     }
142
143     @SuppressWarnings( "unchecked" )
144     public List<AuthenticationKey> getAllKeys()
145     {
146         return PlexusJdoUtils.getAllObjectsDetached( getPersistenceManager(), JdoAuthenticationKey.class );
147     }
148
149     @PostConstruct
150     public void initialize()
151     {
152         pmf = jdoFactory.getPersistenceManagerFactory();
153
154         if ( pmf instanceof PersistenceManagerFactoryImpl )
155         {
156             PersistenceManagerFactoryImpl jpoxpmf = (PersistenceManagerFactoryImpl) pmf;
157             if ( !StringUtils.equals( "JDK_DEFAULT_TIMEZONE", jpoxpmf.getDateTimezone() ) )
158             {
159                 throw new RuntimeException( "The JdoFactory property 'org.jpox.rdbms.dateTimezone' MUST BE "
160                                                        + "Set to 'JDK_DEFAULT_TIMEZONE' in order for jpox and JdoKeyManager to operate correctly." );
161             }
162         }
163     }
164
165     private PersistenceManager getPersistenceManager()
166     {
167         PersistenceManager pm = pmf.getPersistenceManager();
168
169         pm.getFetchPlan().setMaxFetchDepth( 5 );
170
171         return pm;
172     }
173
174     public String getId()
175     {
176         return "JDO Key Manager - " + this.getClass().getName();
177     }
178
179     public JdoFactory getJdoFactory()
180     {
181         return jdoFactory;
182     }
183
184     public void setJdoFactory( JdoFactory jdoFactory )
185     {
186         this.jdoFactory = jdoFactory;
187     }
188 }