View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filemanager/common/src/java/org/astrogrid/filemanager/common/FileManagerStoreNodeMock.java,v $</cvs:source>
3    * <cvs:author>$Author: clq2 $</cvs:author>
4    * <cvs:date>$Date: 2005/01/28 10:43:58 $</cvs:date>
5    * <cvs:version>$Revision: 1.3 $</cvs:version>
6    * <cvs:log>
7    *   $Log: FileManagerStoreNodeMock.java,v $
8    *   Revision 1.3  2005/01/28 10:43:58  clq2
9    *   dave_dev_200501141257 (filemanager)
10   *
11   *   Revision 1.2.2.1  2005/01/18 14:52:48  dave
12   *   Added node create and modify dates ..
13   *
14   *   Revision 1.2  2005/01/13 17:23:15  jdt
15   *   merges from dave-dev-200412201250
16   *
17   *   Revision 1.1.2.2  2005/01/12 13:16:27  dave
18   *   Changed tabs to spaces ...
19   *
20   *   Revision 1.1.2.1  2005/01/10 21:27:47  dave
21   *   Refactores NodeMock as FileManagerStoreNode ...
22   *
23   * </cvs:log>
24   *
25   */
26  package org.astrogrid.filemanager.common ;
27  
28  import java.util.Map ;
29  import java.util.HashMap ;
30  import java.util.Collection ;
31  import java.util.StringTokenizer ;
32  
33  import org.apache.commons.logging.Log ;
34  import org.apache.commons.logging.LogFactory ;
35  
36  import org.astrogrid.store.Ivorn ;
37  
38  import org.astrogrid.filestore.common.file.FileProperty ;
39  import org.astrogrid.filestore.common.file.FileProperties ;
40  
41  import org.astrogrid.filemanager.common.ivorn.FileManagerIvornParser;
42  import org.astrogrid.filemanager.common.ivorn.FileManagerIvornFactory;
43  
44  import org.astrogrid.filemanager.common.exception.NodeNotFoundException ;
45  import org.astrogrid.filemanager.common.exception.DuplicateNodeException ;
46  import org.astrogrid.filemanager.common.exception.FileManagerIdentifierException ;
47  
48  /***
49   * A mock implementation of the node interface.
50   *
51   */
52  public class FileManagerStoreNodeMock
53      implements FileManagerStoreNode
54      {
55  
56      /***
57       * Our debug logger.
58       *
59       */
60      private static Log log = LogFactory.getLog(FileManagerStoreNodeMock.class);
61  
62      /***
63       * Public constructor.
64       * @param parent  The parent node identifier.
65       * @param ivorn   The full ivorn identifier.
66       * @param name    The node name.
67       * @param name    The node type.
68       *
69       */
70      public FileManagerStoreNodeMock(Ivorn parent, Ivorn ivorn, String name, String type)
71          {
72          if (null == ivorn)
73              {
74              throw new IllegalArgumentException(
75                  "Null identifier"
76                  ) ;
77              }
78          if (null == name)
79              {
80              throw new IllegalArgumentException(
81                  "Null name"
82                  ) ;
83              }
84          if (null == type)
85              {
86              throw new IllegalArgumentException(
87                  "Null type"
88                  ) ;
89              }
90          //
91          // Set our parent ivorn.
92          if (null != parent)
93              {
94              this.setParentIvorn(
95                  parent
96                  );
97              }
98          //
99          // Set the rest of our properties.
100         this.setIvorn(ivorn) ;
101         this.setName(name)   ;
102         this.setType(type)   ;
103 		//
104 		// Set our node dates.
105 		properties.created() ;
106         }
107 
108 	/***
109 	 * Update our modify date.
110 	 *
111 	 */
112 	protected void modified()
113 		{
114 		properties.modified();
115 		}
116 
117     /***
118      * Our internal map of child nodes.
119      *
120      */
121     private Map children = new HashMap() ;
122 
123     /***
124      * Get an array of the child nodes.
125      *
126      */
127     public Collection getChildren()
128         {
129         //
130         // Convert the map values into an array.
131         return children.values() ;
132         }
133 
134     /***
135      * Add a child node.
136      * @param node The child node to add.
137      * @throws DuplicateNodeException If a node with the same name already exists.
138      *
139      */
140     public void addNode(FileManagerStoreNode node)
141         throws DuplicateNodeException
142         {
143         log.debug("");
144         log.debug("FileManagerStoreNode.addNode(Node)");
145         log.debug("  This : " + this.getName());
146         log.debug("  This : " + this.getIdent());
147         if (null == node)
148             {
149             throw new IllegalArgumentException(
150                 "Null node"
151                 ) ;
152             }
153         log.debug("  Node : " + node.getName());
154         log.debug("  Node : " + node.getIdent());
155         //
156         // Check if a node already exists.
157         if (children.containsKey(node.getName()))
158             {
159             throw new DuplicateNodeException() ;
160             }
161         //
162         // If the node does not exist yet.
163         else {
164             //
165             // Add the node to our list.
166             children.put(
167                 node.getName(),
168                 node
169                 );
170             //
171             // Set the parent ivorn.
172             try {
173                 node.setParentIvorn(
174                     this.getIvorn()
175                     );
176                 }
177             catch (FileManagerIdentifierException ouch)
178                 {
179                 log.warn("");
180                 log.warn("Exception parsing parent ivorn");
181                 log.warn(ouch);
182                 }
183             }
184 		//
185 		// Update the modified date.
186 		this.modified();
187         }
188 
189     /***
190      * Remove a child node.
191      * @param name The name of the child node to remove.
192      * @throws NodeNotFoundException If the node does not exist.
193      *
194      */
195     public void delNode(String name)
196         throws NodeNotFoundException
197         {
198         log.debug("");
199         log.debug("FileManagerStoreNode.delNode(String)");
200         log.debug("  This : " + this.getName());
201         log.debug("  This : " + this.getIdent());
202         log.debug("  Name : " + name);
203         if (null == name)
204             {
205             throw new IllegalArgumentException(
206                 "Null name"
207                 ) ;
208             }
209         //
210         // Remove the node if it exists.
211         if (children.containsKey(name))
212             {
213             children.remove(
214                 name
215                 );
216             }
217         else {
218             throw new NodeNotFoundException() ;
219             }
220 		//
221 		// Update the modified date.
222 		this.modified();
223         }
224 
225     /***
226      * Get a child node, indexed by path/name.
227      * @param path The target node path.
228      * @return The target node.
229      * @throws NodeNotFoundException if the child node does not exist.
230      *
231      */
232     public FileManagerStoreNode getChild(String path)
233         throws NodeNotFoundException
234         {
235         log.debug("");
236         log.debug("FileManagerStoreNode.getChild()");
237         log.debug("  This : " + this.getName());
238         log.debug("  This : " + this.getIdent());
239         log.debug("  Path : " + path);
240         if (null == path)
241             {
242             throw new IllegalArgumentException(
243                 "Null path"
244                 ) ;
245             }
246         //
247         // Split the path on '/'
248         StringTokenizer tokens = new StringTokenizer(path, "/") ;
249         //
250         // Use the StringTokenizer to find the child node(s).
251         return this.getChild(
252             tokens
253             );
254         }
255 
256     /***
257      * Get a child node, indexed by path/name.
258      * @param tokens A StringTokenizer of the path.
259      * @return The target node.
260      * @throws NodeNotFoundException if the child node does not exist.
261      *
262      */
263     public FileManagerStoreNode getChild(StringTokenizer tokens)
264         throws NodeNotFoundException
265         {
266         log.debug("");
267         log.debug("FileManagerStoreNode.getChild(StringTokenizer)");
268         log.debug("  This  : " + this.getName());
269         log.debug("  This  : " + this.getIdent());
270         if (null == tokens)
271             {
272             throw new IllegalArgumentException(
273                 "Null tokens"
274                 ) ;
275             }
276         //
277         // If we have an initial token.
278         if (tokens.hasMoreTokens())
279             {
280             String name = tokens.nextToken();
281             log.debug("  Name : " + name);
282             //
283             // If we have a node for the first step.
284             if (children.containsKey(name))
285                 {
286                 FileManagerStoreNode node = (FileManagerStoreNode) children.get(name) ;
287                 //
288                 // If we have more steps.
289                 if (tokens.hasMoreTokens())
290                     {
291                     return node.getChild(
292                         tokens
293                         ) ;
294                     }
295                 //
296                 // If we don't have more steps.
297                 else {
298                     return node ;
299                     }
300                 }
301             //
302             // If we don't have a matching node.
303             else {
304                 throw new NodeNotFoundException() ;
305                 }
306             }
307         //
308         // If we don't have an initial token.
309         else {
310             throw new NodeNotFoundException() ;
311             }
312         }
313 
314     /***
315      * The file properties.
316      *
317      */
318     private FileManagerProperties properties = new FileManagerProperties() ;
319 
320     /***
321      * Public access to the properties.
322      * @return The array of file properties.
323      * @todo Need to make this a clone to prevent changes.
324      *
325      */
326     public FileManagerProperties getProperties()
327         {
328         return this.properties ;
329         }
330 
331     /***
332      * Access to our properties.
333      *
334      */
335     public void setProperties(FileProperties properties)
336         {
337         this.setProperties(
338             properties.toArray()
339             );
340         }
341 
342     /***
343      * Access to our properties.
344      *
345      */
346     public void setProperties(FileProperty[] properties)
347         {
348         this.properties.merge(
349             properties,
350             new FileManagerPropertyFilter()
351             ) ;
352         }
353 
354     /***
355      * Get the node ivorn.
356      *
357      */
358     public Ivorn getIvorn()
359         throws FileManagerIdentifierException
360         {
361         return properties.getManagerResourceIvorn() ;
362         }
363 
364     /***
365      * Get the node ident.
366      *
367      */
368     public String getIdent()
369         {
370         try {
371             return properties.getManagerResourceIdent() ;
372             }
373         catch (FileManagerIdentifierException ouch)
374             {
375             log.warn("");
376             log.warn("Exception parsing node ivorn");
377             log.warn(ouch);
378             return null ;
379             }
380         }
381 
382     /***
383      * Set the node ivorn.
384      *
385      */
386     protected void setIvorn(Ivorn ivorn)
387         {
388         this.properties.setManagerResourceIvorn(
389             ivorn
390             ) ;
391         }
392 
393     /***
394      * Get the parent ivorn.
395      *
396      */
397     public Ivorn getParentIvorn()
398         throws FileManagerIdentifierException
399         {
400         return properties.getManagerParentIvorn();
401         }
402 
403     /***
404      * Set the parent ivorn.
405      *
406      */
407     public void setParentIvorn(Ivorn ivorn)
408         {
409         this.properties.setManagerParentIvorn(
410             ivorn
411             ) ;
412         }
413 
414     /***
415      * Get the node name.
416      *
417      */
418     public String getName()
419         {
420         return this.properties.getManagerResourceName() ;
421         }
422 
423     /***
424      * Set the node name.
425      *
426      */
427     public void setName(String name)
428         {
429         this.properties.setManagerResourceName(
430             name
431             ) ;
432         }
433 
434     /***
435      * Get the node type.
436      *
437      */
438     public String getType()
439         {
440         return this.properties.getManagerResourceType() ;
441         }
442 
443     /***
444      * Set the node type.
445      *
446      */
447     public void setType(String type)
448         {
449         this.properties.setManagerResourceType(
450             type
451             ) ;
452         }
453 
454     /***
455      * Check if this node is a container.
456      *
457      */
458     public boolean isContainer()
459         {
460         if (null != this.getType())
461             {
462             return FileManagerProperties.CONTAINER_NODE_TYPE.equals(
463                 this.getType()
464                 );
465             }
466         else {
467             return false ;
468             }
469         }
470 
471     /***
472      * Check if this node is a data node.
473      *
474      */
475     public boolean isDataNode()
476         {
477         if (null != this.getType())
478             {
479             return FileManagerProperties.DATA_NODE_TYPE.equals(
480                 this.getType()
481                 );
482             }
483         else {
484             return false ;
485             }
486         }
487 
488     }