1 package org.codehaus.plexus.redback.keys.jdo;
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.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;
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;
45 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
48 @Service( "keyManager#jdo" )
49 public class JdoKeyManager
50 extends AbstractKeyManager
53 @Named( value = "jdoFactory#users" )
54 private JdoFactory jdoFactory;
56 private PersistenceManagerFactory pmf;
58 public AuthenticationKey createKey( String principal, String purpose, int expirationMinutes )
59 throws KeyManagerException
61 JdoAuthenticationKey authkey = new JdoAuthenticationKey();
62 authkey.setKey( super.generateUUID() );
63 authkey.setForPrincipal( principal );
64 authkey.setPurpose( purpose );
66 Calendar now = getNowGMT();
67 authkey.setDateCreated( now.getTime() );
69 if ( expirationMinutes >= 0 )
71 Calendar expiration = getNowGMT();
72 expiration.add( Calendar.MINUTE, expirationMinutes );
73 authkey.setDateExpires( expiration.getTime() );
76 return addKey( authkey );
79 public AuthenticationKey addKey( AuthenticationKey key )
81 return (AuthenticationKey) PlexusJdoUtils.addObject( getPersistenceManager(), key );
84 public void eraseDatabase()
86 PlexusJdoUtils.removeAll( getPersistenceManager(), JdoAuthenticationKey.class );
87 PlexusJdoUtils.removeAll( getPersistenceManager(), RedbackKeyManagementJdoModelloMetadata.class );
90 public AuthenticationKey findKey( String key )
91 throws KeyNotFoundException, KeyManagerException
93 if ( StringUtils.isEmpty( key ) )
95 throw new KeyNotFoundException( "Empty key not found." );
100 JdoAuthenticationKey authkey = (JdoAuthenticationKey) PlexusJdoUtils.getObjectById( getPersistenceManager(),
101 JdoAuthenticationKey.class,
104 if ( authkey == null )
106 throw new KeyNotFoundException( "Key [" + key + "] not found." );
108 assertNotExpired( authkey );
112 catch ( PlexusObjectNotFoundException e )
114 throw new KeyNotFoundException( e.getMessage() );
116 catch ( PlexusStoreException e )
118 throw new KeyManagerException(
119 "Unable to get " + JdoAuthenticationKey.class.getName() + "', key '" + key + "' from jdo store." );
123 public void deleteKey( AuthenticationKey authkey )
124 throws KeyManagerException
126 PlexusJdoUtils.removeObject( getPersistenceManager(), authkey );
129 public void deleteKey( String key )
130 throws KeyManagerException
134 AuthenticationKey authkey = findKey( key );
135 PlexusJdoUtils.removeObject( getPersistenceManager(), authkey );
137 catch ( KeyNotFoundException e )
139 // not found? nothing to do.
143 @SuppressWarnings( "unchecked" )
144 public List<AuthenticationKey> getAllKeys()
146 return PlexusJdoUtils.getAllObjectsDetached( getPersistenceManager(), JdoAuthenticationKey.class );
150 public void initialize()
152 pmf = jdoFactory.getPersistenceManagerFactory();
154 if ( pmf instanceof PersistenceManagerFactoryImpl )
156 PersistenceManagerFactoryImpl jpoxpmf = (PersistenceManagerFactoryImpl) pmf;
157 if ( !StringUtils.equals( "JDK_DEFAULT_TIMEZONE", jpoxpmf.getDateTimezone() ) )
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." );
165 private PersistenceManager getPersistenceManager()
167 PersistenceManager pm = pmf.getPersistenceManager();
169 pm.getFetchPlan().setMaxFetchDepth( 5 );
174 public String getId()
176 return "JDO Key Manager - " + this.getClass().getName();
179 public JdoFactory getJdoFactory()
184 public void setJdoFactory( JdoFactory jdoFactory )
186 this.jdoFactory = jdoFactory;