View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/common/src/java/org/astrogrid/community/common/policy/manager/ResourceManagerMock.java,v $</cvs:source>
3    * <cvs:author>$Author: jdt $</cvs:author>
4    * <cvs:date>$Date: 2004/11/22 13:03:04 $</cvs:date>
5    * <cvs:version>$Revision: 1.7 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: ResourceManagerMock.java,v $
9    *   Revision 1.7  2004/11/22 13:03:04  jdt
10   *   Merges from Comm_KMB_585
11   *
12   *   Revision 1.6.22.1  2004/11/12 09:12:09  KevinBenson
13   *   Still need to javadoc and check exceptions on a couple of new methods
14   *   for ResourceManager and PermissionManager, but for the most part it is ready.
15   *   I will also add some stylesheets around the jsp pages later.
16   *
17   *   Revision 1.6  2004/09/16 23:18:08  dave
18   *   Replaced debug logging in Community.
19   *   Added stream close() to FileStore.
20   *
21   *   Revision 1.5.82.1  2004/09/16 09:58:48  dave
22   *   Replaced debug with commons logging ....
23   *
24   *   Revision 1.5  2004/06/18 13:45:20  dave
25   *   Merged development branch, dave-dev-200406081614, into HEAD
26   *
27   *   Revision 1.4.40.3  2004/06/17 13:38:59  dave
28   *   Tidied up old CVS log entries
29   *
30   * </cvs:log>
31   *
32   */
33  package org.astrogrid.community.common.policy.manager ;
34  
35  import org.apache.commons.logging.Log ;
36  import org.apache.commons.logging.LogFactory ;
37  
38  import java.util.Map ;
39  import java.util.HashMap ;
40  
41  import org.astrogrid.community.common.policy.data.ResourceData ;
42  import org.astrogrid.community.common.service.CommunityServiceMock ;
43  
44  import org.astrogrid.community.common.identifier.ResourceIdentifier ;
45  
46  import org.astrogrid.community.common.exception.CommunityServiceException  ;
47  import org.astrogrid.community.common.exception.CommunityResourceException ;
48  import org.astrogrid.community.common.exception.CommunityIdentifierException ;
49  
50  /***
51   * Mock implementation of our ResourceManager service.
52   *
53   */
54  public class ResourceManagerMock
55      extends CommunityServiceMock
56      implements ResourceManager
57      {
58      /***
59       * Our debug logger.
60       *
61       */
62  	private static Log log = LogFactory.getLog(ResourceManagerMock.class);
63  
64      /***
65       * Switch for testing service exceptions.
66       * Set this to true, and the service calls will throw CommunityServiceExceptions.
67       *
68       */
69      public static boolean SERVICE_EXCEPTIONS = false ;
70  
71      /***
72       * Public constructor.
73       *
74       */
75      public ResourceManagerMock()
76          {
77          super() ;
78          }
79  
80      /***
81       * Our hash table of values.
82       *
83       */
84      private static Map map = new HashMap() ;
85  
86      /***
87       * Reset our map.
88       *
89       */
90      public static void reset()
91          {
92          log.debug("") ;
93          log.debug("----\"----") ;
94          log.debug("ResourceManagerMock.reset()") ;
95          map.clear() ;
96          }
97  
98      /***
99       * Register a new Resource.
100      * @return A new ResourceData object to represent the resource.
101      * @throws CommunityServiceException If there is an internal error in the service.
102      *
103      */
104     public ResourceData addResource()
105         throws CommunityServiceException
106         {
107         log.debug("") ;
108         log.debug("----\"----") ;
109         log.debug("ResourceManagerMock.addResource()") ;
110         //
111         // Check for CommunityServiceException tests.
112         if (SERVICE_EXCEPTIONS)
113             {
114             throw new CommunityServiceException("Mock exception test") ;
115             }
116         //
117         // Create a new Resource.
118         ResourceData resource = new ResourceData(
119             new ResourceIdentifier()
120             ) ;
121         //
122         // Add it to our map.
123         map.put(resource.getIdent(), resource) ;
124         //
125         // Return the new Resource.
126         return resource ;
127         }
128 
129     /***
130      * Request a Resource details.
131      * @param The resource identifier.
132      * @return The requested ResourceData object.
133      * @throws CommunityIdentifierException If the identifier is not valid.
134      * @throws CommunityResourceException If unable to locate the resource.
135      * @throws CommunityServiceException If there is an internal error in the service.
136      *
137      */
138     public ResourceData getResource(String ident)
139         throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
140         {
141         log.debug("") ;
142         log.debug("----\"----") ;
143         log.debug("ResourceManagerMock.getResource()") ;
144         log.debug("  Ident : " + ident) ;
145         //
146         // Check for CommunityServiceException tests.
147         if (SERVICE_EXCEPTIONS)
148             {
149             throw new CommunityServiceException("Mock exception test") ;
150             }
151         //
152         // Check for null ident.
153         if (null == ident)
154             {
155             throw new CommunityIdentifierException(
156                 "Null identifier"
157                 ) ;
158             }
159         //
160         // Lookup the Resource in our map.
161         ResourceData resource = (ResourceData) map.get(ident) ;
162         //
163         // If we found a matching resource.
164         if (null != resource)
165             {
166             return resource ;
167             }
168         //
169         // If we didn't find a matching resource.
170         else {
171             throw new CommunityResourceException(
172                 "Unable to locate resource",
173                 ident
174                 ) ;
175             }
176         }
177     
178     /***
179      * Request a Resource details.
180      * @param The resource identifier.
181      * @return The requested ResourceData object.
182      * @throws CommunityIdentifierException If the identifier is not valid.
183      * @throws CommunityResourceException If unable to locate the resource.
184      * @throws CommunityServiceException If there is an internal error in the service.
185      *
186      */
187     public Object[] getResources() {
188         log.debug("") ;
189         log.debug("----\"----") ;
190         log.debug("ResourceManagerMock.getResources()") ;
191         return null;
192         
193     }
194     
195 
196     /***
197      * Update a Resource details.
198      * @param The ResourceData to update.
199      * @return The updated ResourceData.
200      * @throws CommunityResourceException If unable to locate the resource.
201      * @throws CommunityServiceException If there is an internal error in the service.
202      * @throws CommunityIdentifierException If the resource identifier is not valid.
203      *
204      */
205     public ResourceData setResource(ResourceData resource)
206         throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
207         {
208         log.debug("") ;
209         log.debug("----\"----") ;
210         log.debug("ResourceManagerMock.setResource()") ;
211         log.debug("  Resource : " + resource) ;
212         //
213         // Check for CommunityServiceException tests.
214         if (SERVICE_EXCEPTIONS)
215             {
216             throw new CommunityServiceException("Mock exception test") ;
217             }
218         //
219         // Check for null resource.
220         if (null == resource)
221             {
222             throw new CommunityResourceException(
223                 "Null resource"
224                 ) ;
225             }
226         //
227         // Check for null ident.
228         if (null == resource.getIdent())
229             {
230             throw new CommunityIdentifierException(
231                 "Null identifier"
232                 ) ;
233             }
234         //
235         // Check if we already have this resource.
236         if (map.containsKey(resource.getIdent()))
237             {
238             //
239             // Replace the existing Resource.
240             map.put(resource.getIdent(), resource) ;
241             //
242             // Return the new resource.
243             return resource ;
244             }
245         //
246         // If we don't have a matching resource.
247         else {
248             throw new CommunityResourceException(
249                 "Unable to locate resource",
250                 resource.getIdent()
251                 ) ;
252             }
253         }
254 
255     /***
256      * Delete a Resource object.
257      * @param The resource identifier.
258      * @return The original ResourceData.
259      * @throws CommunityResourceException If unable to locate the resource.
260      * @throws CommunityServiceException If there is an internal error in the service.
261      * @throws CommunityIdentifierException If the resource identifier is not valid.
262      *
263      */
264     public ResourceData delResource(String ident)
265         throws CommunityIdentifierException, CommunityResourceException, CommunityServiceException
266         {
267         log.debug("") ;
268         log.debug("----\"----") ;
269         log.debug("ResourceManagerMock.delResource()") ;
270         log.debug("  Ident : " + ident) ;
271         //
272         // Check for CommunityServiceException tests.
273         if (SERVICE_EXCEPTIONS)
274             {
275             throw new CommunityServiceException("Mock exception test") ;
276             }
277         //
278         // Check for null ident.
279         if (null == ident)
280             {
281             throw new CommunityIdentifierException(
282                 "Null identifier"
283                 ) ;
284             }
285         //
286         // Try to find the original resource.
287         ResourceData original = (ResourceData) map.get(ident) ;
288         //
289         // If we found a matching resource.
290         if (null != original)
291             {
292             //
293             // Remove the original resource.
294             map.remove(ident) ;
295             //
296             // Return the original resource.
297             return original ;
298             }
299         //
300         // If we didn't find a matching resource.
301         else {
302             throw new CommunityResourceException(
303                 "Unable to locate resource",
304                 ident
305                 ) ;
306             }
307         }
308     }