This project has retired. For details please refer to its Attic page.
JdoKeyManager xref
View Javadoc

1   package org.apache.archiva.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.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   * JdoKeyManager
44   *
45   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
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             // not found? nothing to do.
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 }