1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
198 StringBuffer buffer = new StringBuffer() ;
199
200
201 if (false == base.startsWith(Ivorn.SCHEME))
202 {
203 buffer.append(Ivorn.SCHEME) ;
204 buffer.append("://") ;
205 }
206 buffer.append(base) ;
207
208
209 if (-1 != base.indexOf('#'))
210 {
211 buffer.append("/") ;
212 buffer.append(ident) ;
213 }
214
215
216 else {
217 buffer.append("#") ;
218 buffer.append(ident) ;
219 }
220 log.debug(" Result : " + buffer.toString()) ;
221
222
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 }