This project has retired. For details please refer to its
Attic page.
JdoKeyManager xref
1 package org.apache.archiva.redback.keys.jdo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.redback.components.jdo.JdoFactory;
23 import org.apache.archiva.redback.components.jdo.RedbackJdoUtils;
24 import org.apache.archiva.redback.components.jdo.RedbackObjectNotFoundException;
25 import org.apache.archiva.redback.components.jdo.RedbackStoreException;
26 import org.apache.archiva.redback.keys.AuthenticationKey;
27 import org.apache.archiva.redback.keys.AbstractKeyManager;
28 import org.apache.archiva.redback.keys.KeyManagerException;
29 import org.apache.archiva.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
44
45
46
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) RedbackJdoUtils.addObject( getPersistenceManager(), key );
82 }
83
84 public void eraseDatabase()
85 {
86 RedbackJdoUtils.removeAll( getPersistenceManager(), JdoAuthenticationKey.class );
87 RedbackJdoUtils.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) RedbackJdoUtils.getObjectById(
101 getPersistenceManager(), JdoAuthenticationKey.class, key );
102
103 if ( authkey == null )
104 {
105 throw new KeyNotFoundException( "Key [" + key + "] not found." );
106 }
107 assertNotExpired( authkey );
108
109 return authkey;
110 }
111 catch ( RedbackObjectNotFoundException e )
112 {
113 throw new KeyNotFoundException( e.getMessage() );
114 }
115 catch ( RedbackStoreException e )
116 {
117 throw new KeyManagerException(
118 "Unable to get " + JdoAuthenticationKey.class.getName() + "', key '" + key + "' from jdo store." );
119 }
120 }
121
122 public void deleteKey( AuthenticationKey authkey )
123 throws KeyManagerException
124 {
125 RedbackJdoUtils.removeObject( getPersistenceManager(), authkey );
126 }
127
128 public void deleteKey( String key )
129 throws KeyManagerException
130 {
131 try
132 {
133 AuthenticationKey authkey = findKey( key );
134 RedbackJdoUtils.removeObject( getPersistenceManager(), authkey );
135 }
136 catch ( KeyNotFoundException e )
137 {
138
139 }
140 }
141
142 @SuppressWarnings( "unchecked" )
143 public List<AuthenticationKey> getAllKeys()
144 {
145 return RedbackJdoUtils.getAllObjectsDetached( getPersistenceManager(), JdoAuthenticationKey.class );
146 }
147
148 @PostConstruct
149 public void initialize()
150 {
151 pmf = jdoFactory.getPersistenceManagerFactory();
152
153 if ( pmf instanceof PersistenceManagerFactoryImpl )
154 {
155 PersistenceManagerFactoryImpl jpoxpmf = (PersistenceManagerFactoryImpl) pmf;
156 if ( !StringUtils.equals( "JDK_DEFAULT_TIMEZONE", jpoxpmf.getDateTimezone() ) )
157 {
158 throw new RuntimeException( "The JdoFactory property 'org.jpox.rdbms.dateTimezone' MUST BE "
159 + "Set to 'JDK_DEFAULT_TIMEZONE' in order for jpox and JdoKeyManager to operate correctly." );
160 }
161 }
162 }
163
164 private PersistenceManager getPersistenceManager()
165 {
166 PersistenceManager pm = pmf.getPersistenceManager();
167
168 pm.getFetchPlan().setMaxFetchDepth( 5 );
169
170 return pm;
171 }
172
173 public String getId()
174 {
175 return "JDO Key Manager - " + this.getClass().getName();
176 }
177
178 public JdoFactory getJdoFactory()
179 {
180 return jdoFactory;
181 }
182
183 public void setJdoFactory( JdoFactory jdoFactory )
184 {
185 this.jdoFactory = jdoFactory;
186 }
187 }