View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.user.model.dao;
17  
18  import java.util.Iterator;
19  import java.util.Set;
20  
21  import net.sf.acegisecurity.GrantedAuthority;
22  import net.sf.acegisecurity.GrantedAuthorityImpl;
23  import net.sf.acegisecurity.providers.dao.AuthenticationDao;
24  import net.sf.acegisecurity.providers.dao.UsernameNotFoundException;
25  import net.sf.oness.common.model.dao.hibernate.HibernateDaoSupport;
26  import net.sf.oness.user.model.bo.Authority;
27  import net.sf.oness.user.model.bo.User;
28  
29  import org.springframework.dao.DataAccessException;
30  import org.springframework.orm.hibernate.HibernateObjectRetrievalFailureException;
31  
32  /***
33   * Retrieves user details from database.
34   * 
35   * @author Carlos Sanchez
36   * @version $Revision: 1.3 $
37   */
38  public class UserHibernateDao
39      extends HibernateDaoSupport
40      implements AuthenticationDao, UserDao
41  {
42  
43      public User create( User user )
44      {
45          getHibernateTemplate().save( user );
46          return user;
47      }
48  
49      public Authority create( Authority authority )
50      {
51          getHibernateTemplate().save( authority );
52          return authority;
53      }
54  
55      public net.sf.acegisecurity.providers.dao.User loadUserByUsername( String name ) throws UsernameNotFoundException,
56          DataAccessException
57      {
58  
59          User user;
60          try
61          {
62              user = (User) getHibernateTemplate().load( User.class, name );
63  
64              /* the following is needed if using lazy loading */
65              //            user =
66              //                (User) getHibernateTemplate()
67              //                    .find(
68              //                        "from User as user left join fetch user.authorities where
69              // user.name = ?",
70              //                        name)
71              //                    .get(0);
72          }
73          catch ( HibernateObjectRetrievalFailureException e )
74          {
75              throw new UsernameNotFoundException( "User not found" );
76          }
77  
78          Set auths = user.getAuthorities();
79          if ( auths.size() == 0 )
80          {
81              throw new UsernameNotFoundException( "User has no GrantedAuthority" );
82          }
83  
84          GrantedAuthority[] arrayAuths = new GrantedAuthority[auths.size()];
85          Iterator iter = auths.iterator();
86          int i = 0;
87          while ( iter.hasNext() )
88          {
89              Authority element = (Authority) iter.next();
90              arrayAuths[i++] = new GrantedAuthorityImpl( element.getName() );
91          }
92  
93          return new net.sf.acegisecurity.providers.dao.User( user.getName(), user.getPassword(), user.isEnabled()
94              .booleanValue(), arrayAuths );
95      }
96  }