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.services.security;
16  
17  import org.figure8.join.core.InvalidParameterException;
18  
19  import java.util.Map;
20  import java.util.List;
21  import java.util.HashMap;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  /**
25   * Stub implementation of <code>PermissionResourceResolver</code>.
26   * Only used for test purpose.
27   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28   * @version $Revision: 1.1 $
29   */
30  public class PermissionResourceResolverStub implements PermissionResourceResolver{
31  
32     // Static -------------------------------------------------------------------
33  
34     /** Hold a dummy resource list */
35     private static List resourceList = null;
36  
37     static{
38        resourceList = new ArrayList();
39        resourceList.add("resource1");
40        resourceList.add("resource2");
41        resourceList.add("resource3");
42     }
43  
44  
45     // Public -------------------------------------------------------------------
46  
47     /**
48      * Retrieve a list of resolved resources
49      * @return A List of resources to expose for security permission allocation
50      */
51     public List getResourceList(){
52        return resourceList;
53     }
54  
55     /**
56      * Retrieve a list of resolved resources wrapped within {@link PermissionResource}
57      * @return A List of <code>org.figure8.join.services.security.PermissionResource</code>
58      */
59     public List getPermissionResourceList(){
60        ArrayList result = new ArrayList();
61        // Browse resource list.
62        Iterator strings = getResourceList().iterator();
63        while (strings.hasNext()){
64           String res = (String)strings.next();
65           result.add(res);
66        }
67        return result;
68     }
69  
70     /**
71      * Tells if given resource is a valid one
72      * @param resource Object to try to resolve
73      * @return true is resource is managed by this resolver, false otherwise
74      */
75     public boolean isValidResource(Object resource){
76        if (resource instanceof String){
77           if (resourceList.indexOf(resource) != 0)
78              return true;
79        }
80        return false;
81     }
82  
83     /**
84      * Get a resource using its unique identifier
85      * @param resourceId Unique identifier corresponding to resource
86      * @return Object representing the resource
87      * @throws InvalidParameterException if the <b>resourceId</b> is unknown.
88      */
89     public Object getResource(String resourceId) throws InvalidParameterException{
90        if (resourceList.indexOf(resourceId) != 0){
91           return resourceId;
92        }
93        throw new InvalidParameterException(resourceId + " is not managed by this resolver");
94     }
95  
96     /**
97      * Get a resource unique identifier
98      * @param resource The object representing the resource
99      * @return The string representation of the resource unique identifier
100     * @throws org.figure8.join.core.InvalidParameterException if the <b>resource</b> cannot be resolved
101     */
102    public String getResourceId(Object resource) throws InvalidParameterException{
103       if (isValidResource(resource)){
104          return (String)resource;
105       }
106       throw new InvalidParameterException(resource + " is not managed by this resolver");
107    }
108 
109    /**
110     * Get a resource label for display
111     * @param resource The object representing the resource
112     * @return A string representing the display label of resource
113     * @throws InvalidParameterException if the <b>resource</b> cannot be resolved
114     */
115    public String getResourceLabel(Object resource) throws InvalidParameterException{
116       if (isValidResource(resource)){
117          return (String)resource;
118       }
119       throw new InvalidParameterException(resource + " is not managed by this resolver");
120    }
121 }