View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.businessobjects.security;
16  
17  import org.figure8.join.core.SortableEntityObject;
18  import org.figure8.join.core.InvalidParameterException;
19  import org.figure8.join.services.security.PermissionResourceResolver;
20  import org.figure8.join.util.LogUtil;
21  
22  import org.apache.commons.logging.Log;
23  /**
24   * This is an entity representing a security role into Join system.
25   * Role can also be seen a permission type handler.
26   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
27   * @version $Revision: 1.1 $
28   *
29   * @hibernate.class table="join_roles"
30   * @hibernate.cache usage="read-write"
31   *
32   * @hibernate.query name="join.role_findByName"
33   *    query="from Role rol where rol.name = :roleName"
34   */
35  public class Role extends SortableEntityObject{
36  
37     // Static -------------------------------------------------------------------
38  
39     /** Get a commons logger. */
40     private static final Log log = LogUtil.getLog(Role.class);
41  
42     /** Default role name for Join administation */
43     public static final String DEFAULT_ADMIN_ROLE = "admin";
44     /** Default role name for integration team members */
45     public static final String DEFAULT_JOINER_ROLE = "joiner";
46     /** Default role name for software project architect */
47     public static final String DEFAULT_ARCHITECT_ROLE = "architect";
48     /** Default role name for deliverables supplier */
49     public static final String DEFAULT_SUPPLIER_ROLE = "supplier";
50  
51  
52     // Attributes ---------------------------------------------------------------
53  
54     /** Role name */
55     private String name;
56     /** Role decription */
57     private String description;
58     /** Java class used to resolve resource subject to permissions within this role */
59     private String permissionResourceResolverClass;
60  
61     /** The resolver instance of type <b>permissionResourceResolverClass</b> */
62     private PermissionResourceResolver resourceResolver = null;
63  
64  
65     // Constructors -------------------------------------------------------------
66  
67     /** Creates a new instance of Role */
68     public Role(){
69     }
70  
71     /**
72      * Creates a new instance of Role with mandatory fields
73      * @param name Name of this new role (must be unique)
74      * @param description Description of this new role
75      */
76     public Role(String name, String description){
77        this.name = name;
78        this.description = description;
79        this.permissionResourceResolverClass = null;
80     }
81     
82     
83     // Public -------------------------------------------------------------------
84  
85     /**
86      * @hibernate.property column="s_name"
87      *    not-null="true" unique="true"
88      *    length="20" update="false"
89      * @return This role name
90      */
91     public String getName(){
92        return name;
93     }
94     /** @param name The name of this role */
95     public void setName(String name){
96        this.name = name;
97     }
98  
99     /**
100     * @hibernate.property column="s_description"
101     *    not-null="true" length="60"
102     * @return The decription of this role
103     */
104    public String getDescription(){
105       return description;
106    }
107    /** @param description Description of this role (tips: use a key for localization) */
108    public void setDescription(String description){
109       this.description = description;
110    }
111 
112    /**
113     * @hibernate.property column="s_permtypeclass" length="200"
114     * @return The FQN of the Java class used for managing permissions within this role
115     */
116    public String getPermissionResourceResolverClass(){
117       return permissionResourceResolverClass;
118    }
119    /**
120     * Give a resource resolver to this role. ie : an helper object that allows to retrieve resources
121     * to whom should apply security permission within this role. This parameter must be the name of a
122     * Java class that implements <code>org.figure8.join.services.security.PermissionResourceResolver</code>
123     * @param permissionResourceResolverClass FQN of Java class
124     * @throws InvalidParameterException if the <b>permissionResourceResolverClass</b> is not valid
125     */
126    public void setPermissionResourceResolverClass(String permissionResourceResolverClass) throws InvalidParameterException{
127       if (permissionResourceResolverClass != null && !permissionResourceResolverClass.equals("")){
128          this.permissionResourceResolverClass = permissionResourceResolverClass;
129          // Try instantiating resource resolver now.
130          instantiateResourceResolver();
131       }
132    }
133 
134    /**
135     * Get the <code>PermissionResourceResolver</code> asssociated to this role (if any).
136     * @throws InvalidParameterException if the <b>permissionResourceResolverClass</b> inner field is not valid
137     * @return The resolver instance or null if no resolver is associated to this role
138     */
139    public PermissionResourceResolver getPermissionResourceResolver() throws InvalidParameterException{
140       // Only available if there's a resource resolver.
141       if (permissionResourceResolverClass != null){
142          // Instantiate it if not already done.
143          if (resourceResolver == null)
144             instantiateResourceResolver();
145          return resourceResolver;
146       }
147       return null;
148    }
149 
150 
151    // Protected ----------------------------------------------------------------
152 
153    /**
154     * Try to instantiate the <code>PermissionResourceResolver</code>
155     * associated with this role (if any ...)
156     * @throws InvalidParameterException if the <b>permissionResourceResolverClass</b> inner field is not valid
157     */
158    protected void instantiateResourceResolver() throws InvalidParameterException{
159       if (permissionResourceResolverClass != null && resourceResolver == null){
160          try{
161             // Load class from context class loader and retrieve new instance.
162             Class clazz = Thread.currentThread().getContextClassLoader().loadClass(permissionResourceResolverClass);
163             resourceResolver = (PermissionResourceResolver)clazz.newInstance();
164          }
165          catch (Exception e){
166             // Log and wrap within an InvalidParameterException.
167             log.error("Exception while trying to instantiate permissionResourceResolver for role '" + name + "'");
168             log.error("Here's the exception message : " + e.getMessage());
169             throw new InvalidParameterException("'" + permissionResourceResolverClass + "' cannot be found or does not extend PermissionResourceResolver", e);
170          }
171       }
172    }
173 
174 
175    // Implementation of SortableEntityObject -----------------------------------
176 
177    /**
178     * Get the comparison criterion as a string. Criterion is the name.
179     * @return The string representation of comparison and sort criterion
180     */
181    public String getStringForComparison(){
182       return name;
183    }
184 }