View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filemanager/common/src/java/org/astrogrid/filemanager/common/ivorn/FileManagerIvornFactory.java,v $</cvs:source>
3    * <cvs:author>$Author: jdt $</cvs:author>
4    * <cvs:date>$Date: 2005/01/13 17:23:15 $</cvs:date>
5    * <cvs:version>$Revision: 1.4 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: FileManagerIvornFactory.java,v $
9    *   Revision 1.4  2005/01/13 17:23:15  jdt
10   *   merges from dave-dev-200412201250
11   *
12   *   Revision 1.3.4.2  2005/01/12 13:16:27  dave
13   *   Changed tabs to spaces ...
14   *
15   *   Revision 1.3.4.1  2004/12/24 02:05:05  dave
16   *   Refactored exception handling, removing IdentifierException from the public API ...
17   *
18   *   Revision 1.3  2004/12/16 17:25:49  jdt
19   *   merge from dave-dev-200410061224-200412161312
20   *
21   *   Revision 1.1.2.5  2004/11/24 16:15:08  dave
22   *   Added node functions to client ...
23   *
24   *   Revision 1.1.2.4  2004/11/18 17:10:21  dave
25   *   Updated mock ivorn handling
26   *
27   *   Revision 1.1.2.3  2004/11/18 16:06:11  dave
28   *   Added delegate resolver and tests ....
29   *
30   *   Revision 1.1.2.2  2004/11/05 02:23:45  dave
31   *   Refactored identifiers are properties ...
32   *
33   *   Revision 1.1.2.1  2004/11/04 15:50:17  dave
34   *   Added ivorn pareser and factory.
35   *
36   * </cvs:log>
37   *
38   */
39  package org.astrogrid.filemanager.common.ivorn ;
40  
41  import java.util.Random ;
42  
43  import org.apache.commons.logging.Log ;
44  import org.apache.commons.logging.LogFactory ;
45  
46  import org.astrogrid.store.Ivorn ;
47  import org.astrogrid.filemanager.common.exception.FileManagerIdentifierException ;
48  
49  import java.net.URISyntaxException ;
50  
51  /***
52   * A factory for generating filemanager related Ivorn identifiers.
53   *
54   */
55  public class FileManagerIvornFactory
56      {
57      /***
58       * Our debug logger.
59       *
60       */
61      private static Log log = LogFactory.getLog(FileManagerIvornFactory.class);
62  
63      /***
64       * Create a filemanager ivorn.
65       * @param  base The base filemanager ivorn.
66       * @param  path The node identifier or path.
67       * @return A new resource ivorn.
68       * @throws FileManagerIdentifierException if the filemanager or resource identifiers are invalid or null.
69       *
70       */
71      public Ivorn ivorn(Ivorn base, String path)
72          throws FileManagerIdentifierException
73          {
74          try {
75              return new Ivorn(
76                  ident(
77                      base,
78                      path
79                      )
80                  ) ;
81              }
82          catch (URISyntaxException ouch)
83              {
84              throw new FileManagerIdentifierException(
85                  ouch
86                  ) ;
87              }
88          }
89  
90      /***
91       * Create a filemanager ivorn.
92       * @param  base The base filemanager ivorn.
93       * @return A new resource ivorn.
94       * @throws FileManagerIdentifierException if the filemanager or resource identifiers are invalid or null.
95       *
96       */
97      public Ivorn ivorn(Ivorn base)
98          throws FileManagerIdentifierException
99          {
100         try {
101             return new Ivorn(
102                 ident(
103                     base
104                     )
105                 ) ;
106             }
107         catch (URISyntaxException ouch)
108             {
109             throw new FileManagerIdentifierException(
110                 ouch
111                 ) ;
112             }
113         }
114 
115     /***
116      * Create a filemanager ident.
117      * @param  base The filemanager service ivorn.
118      * @return A new (ivorn) identifer.
119      * @throws FileManagerIdentifierException if the filemanager or resource identifiers are null.
120      *
121      */
122     public String ident(Ivorn base)
123         throws FileManagerIdentifierException
124         {
125         return this.ident(
126             base.toString()
127             );
128         }
129 
130     /***
131      * Create a filemanager ident.
132      * @param  base The filemanager service ivorn.
133      * @param  path The node identifier or path.
134      * @return A new (ivorn) identifer.
135      * @throws FileManagerIdentifierException if the filemanager or resource identifiers are null.
136      * @todo Check that the base does not already have a #fragment ....
137      *
138      */
139     public String ident(Ivorn base, String path)
140         throws FileManagerIdentifierException
141         {
142         return this.ident(
143             base.toString(),
144             path
145             );
146         }
147 
148     /***
149      * Create a filemanager ident.
150      * @param  base   The filemanager service ivorn.
151      * @param  ident  The resource identifier.
152      * @return A new (ivorn) identifer.
153      * @throws FileManagerIdentifierException if the filemanager or resource identifiers are null.
154      * @todo Check that the base does not already have a #fragment ....
155      *
156      */
157     public String ident(String base)
158         throws FileManagerIdentifierException
159         {
160         return this.ident(
161             base,
162             this.unique()
163             );
164         }
165 
166     /***
167      * Create a filemanager ident.
168      * @param  base   The filemanager service ivorn.
169      * @param  ident  The resource identifier.
170      * @return A new (ivorn) identifer.
171      * @throws FileManagerIdentifierException if the filemanager or resource identifiers are null.
172      *
173      */
174     public String ident(String base, String ident)
175         throws FileManagerIdentifierException
176         {
177         log.debug("") ;
178         log.debug("----\"----") ;
179         log.debug("FileManagerIvornFactory.ident(String, String)") ;
180         log.debug("  Base   : " + base) ;
181         log.debug("  Ident  : " + ident) ;
182         //
183         // Check for null params.
184         if (null == base)
185             {
186             throw new FileManagerIdentifierException(
187                 "Null service identifier"
188                 ) ;
189             }
190         if (null == ident)
191             {
192             throw new FileManagerIdentifierException(
193                 "Null resource identifier"
194                 ) ;
195             }
196         //
197         // Put it all together.
198         StringBuffer buffer = new StringBuffer() ;
199         //
200         // If the service identifier isn't an Ivorn yet.
201         if (false == base.startsWith(Ivorn.SCHEME))
202             {
203             buffer.append(Ivorn.SCHEME) ;
204             buffer.append("://") ;
205             }
206         buffer.append(base) ;
207         //
208         // If the base already has a fragment delimiter.
209         if (-1 != base.indexOf('#'))
210             {
211             buffer.append("/") ;
212             buffer.append(ident) ;
213             }
214         //
215         // If the base does not have a fragment delimiter.
216         else {
217             buffer.append("#") ;
218             buffer.append(ident) ;
219             }
220         log.debug("  Result : " + buffer.toString()) ;
221         //
222         // Return the new string.
223         return buffer.toString() ;
224         }
225 
226     /***
227      * Internal random number generator.
228      *
229      */
230     private Random random = new Random() ;
231 
232     /***
233      * Generate a new unique identifier.
234      * @return A new node identifier.
235      *
236      */
237     public String unique()
238         {
239         StringBuffer buffer = new StringBuffer() ;
240         buffer.append("node") ;
241         buffer.append("-") ;
242         buffer.append(
243             Long.toHexString(
244                 System.currentTimeMillis()
245                 ).toUpperCase()
246             ) ;
247         buffer.append("-") ;
248         buffer.append(
249             Integer.toHexString(
250                 random.nextInt()
251                 ).toUpperCase()
252             ) ;
253         return buffer.toString() ;
254         }
255     }