KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > handle > HandlePlugin


1 /*
2  * HandlePlugin.java
3  *
4  * Version: $Revision: 1.10 $
5  *
6  * Date: $Date: 2005/04/20 14:23:26 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.handle;
41
42 import java.sql.SQLException JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Enumeration JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.LinkedList JavaDoc;
47 import java.util.List JavaDoc;
48
49 import net.handle.hdllib.Encoder;
50 import net.handle.hdllib.HandleException;
51 import net.handle.hdllib.HandleStorage;
52 import net.handle.hdllib.HandleValue;
53 import net.handle.hdllib.ScanCallback;
54 import net.handle.hdllib.Util;
55 import net.handle.util.StreamTable;
56
57 import org.apache.log4j.Logger;
58 import org.dspace.core.ConfigurationManager;
59 import org.dspace.core.Context;
60
61 /**
62  * Extension to the CNRI Handle Server that translates requests to resolve
63  * handles into DSpace API calls. The implementation simply stubs out most of
64  * the methods, and delegates the rest to the
65  * {@link org.dspace.handle.HandleManager}. This only provides some of the
66  * functionality (namely, the resolving of handles to URLs) of the CNRI
67  * HandleStorage interface.
68  *
69  * <p>
70  * This class is intended to be embedded in the CNRI Handle Server. It conforms
71  * to the HandleStorage interface that was delivered with Handle Server version
72  * 5.2.0.
73  * </p>
74  *
75  * @author Peter Breton
76  * @version $Revision: 1.10 $
77  */

78 public class HandlePlugin implements HandleStorage
79 {
80     /** log4j category */
81     private static Logger log = Logger.getLogger(HandlePlugin.class);
82
83     /**
84      * Constructor
85      */

86     public HandlePlugin()
87     {
88     }
89
90     ////////////////////////////////////////
91
// Non-Resolving methods -- unimplemented
92
////////////////////////////////////////
93

94     /**
95      * HandleStorage interface method - not implemented.
96      */

97     public void init(StreamTable st) throws Exception JavaDoc
98     {
99         // Not implemented
100
if (log.isInfoEnabled())
101         {
102             log.info("Called init (not implemented)");
103         }
104     }
105
106     /**
107      * HandleStorage interface method - not implemented.
108      */

109     public void setHaveNA(byte[] theHandle, boolean haveit)
110             throws HandleException
111     {
112         // Not implemented
113
if (log.isInfoEnabled())
114         {
115             log.info("Called setHaveNA (not implemented)");
116         }
117     }
118
119     /**
120      * HandleStorage interface method - not implemented.
121      */

122     public void createHandle(byte[] theHandle, HandleValue[] values)
123             throws HandleException
124     {
125         // Not implemented
126
if (log.isInfoEnabled())
127         {
128             log.info("Called createHandle (not implemented)");
129         }
130     }
131
132     /**
133      * HandleStorage interface method - not implemented.
134      */

135     public boolean deleteHandle(byte[] theHandle) throws HandleException
136     {
137         // Not implemented
138
if (log.isInfoEnabled())
139         {
140             log.info("Called deleteHandle (not implemented)");
141         }
142
143         return false;
144     }
145
146     /**
147      * HandleStorage interface method - not implemented.
148      */

149     public void updateValue(byte[] theHandle, HandleValue[] values)
150             throws HandleException
151     {
152         // Not implemented
153
if (log.isInfoEnabled())
154         {
155             log.info("Called updateValue (not implemented)");
156         }
157     }
158
159     /**
160      * HandleStorage interface method - not implemented.
161      */

162     public void deleteAllRecords() throws HandleException
163     {
164         // Not implemented
165
if (log.isInfoEnabled())
166         {
167             log.info("Called deleteAllRecords (not implemented)");
168         }
169     }
170
171     /**
172      * HandleStorage interface method - not implemented.
173      */

174     public void checkpointDatabase() throws HandleException
175     {
176         // Not implemented
177
if (log.isInfoEnabled())
178         {
179             log.info("Called checkpointDatabase (not implemented)");
180         }
181     }
182
183     /**
184      * HandleStorage interface method - not implemented.
185      */

186     public void shutdown()
187     {
188         // Not implemented
189
if (log.isInfoEnabled())
190         {
191             log.info("Called shutdown (not implemented)");
192         }
193     }
194
195     /**
196      * HandleStorage interface method - not implemented.
197      */

198     public void scanHandles(ScanCallback callback) throws HandleException
199     {
200         // Not implemented
201
if (log.isInfoEnabled())
202         {
203             log.info("Called scanHandles (not implemented)");
204         }
205     }
206
207     /**
208      * HandleStorage interface method - not implemented.
209      */

210     public void scanNAs(ScanCallback callback) throws HandleException
211     {
212         // Not implemented
213
if (log.isInfoEnabled())
214         {
215             log.info("Called scanNAs (not implemented)");
216         }
217     }
218
219     ////////////////////////////////////////
220
// Resolving methods
221
////////////////////////////////////////
222

223     /**
224      * Return the raw values for this handle. This implementation returns a
225      * single URL value.
226      *
227      * @param theHandle
228      * byte array representation of handle
229      * @param indexList
230      * ignored
231      * @param typeList
232      * ignored
233      * @return A byte array with the raw data for this handle. Currently, this
234      * consists of a single URL value.
235      * @exception HandleException
236      * If an error occurs while calling the Handle API.
237      */

238     public byte[][] getRawHandleValues(byte[] theHandle, int[] indexList,
239             byte[][] typeList) throws HandleException
240     {
241         if (log.isInfoEnabled())
242         {
243             log.info("Called getRawHandleValues");
244         }
245
246         Context context = null;
247
248         try
249         {
250             if (theHandle == null)
251             {
252                 throw new HandleException(HandleException.INTERNAL_ERROR);
253             }
254
255             String JavaDoc handle = Util.decodeString(theHandle);
256
257             context = new Context();
258
259             String JavaDoc url = HandleManager.resolveToURL(context, handle);
260
261             if (url == null)
262             {
263                 throw new HandleException(HandleException.HANDLE_DOES_NOT_EXIST);
264             }
265
266             HandleValue value = new HandleValue();
267
268             value.setIndex(100);
269             value.setType(Util.encodeString("URL"));
270             value.setData(Util.encodeString(url));
271             value.setTTLType((byte) 0);
272             value.setTTL(100);
273             value.setTimestamp(100);
274             value.setReferences(null);
275             value.setAdminCanRead(true);
276             value.setAdminCanWrite(false);
277             value.setAnyoneCanRead(true);
278             value.setAnyoneCanWrite(false);
279
280             List JavaDoc values = new LinkedList JavaDoc();
281
282             values.add(value);
283
284             byte[][] rawValues = new byte[values.size()][];
285
286             for (int i = 0; i < values.size(); i++)
287             {
288                 HandleValue hvalue = (HandleValue) values.get(i);
289
290                 rawValues[i] = new byte[Encoder.calcStorageSize(hvalue)];
291                 Encoder.encodeHandleValue(rawValues[i], 0, hvalue);
292             }
293
294             return rawValues;
295         }
296         catch (HandleException he)
297         {
298             throw he;
299         }
300         catch (Exception JavaDoc e)
301         {
302             if (log.isDebugEnabled())
303             {
304                 log.debug("Exception in getRawHandleValues", e);
305             }
306
307             throw new HandleException(HandleException.INTERNAL_ERROR);
308         }
309         finally
310         {
311             if (context != null)
312             {
313                 try
314                 {
315                     context.complete();
316                 }
317                 catch (SQLException JavaDoc sqle)
318                 {
319                 }
320             }
321         }
322     }
323
324     /**
325      * Return true if we have this handle in storage.
326      *
327      * @param theHandle
328      * byte array representation of handle
329      * @return True if we have this handle in storage
330      * @exception HandleException
331      * If an error occurs while calling the Handle API.
332      */

333     public boolean haveNA(byte[] theHandle) throws HandleException
334     {
335         if (log.isInfoEnabled())
336         {
337             log.info("Called haveNA");
338         }
339
340         /*
341          * Naming authority Handles are in the form: 0.NA/1721.1234
342          *
343          * 0.NA is basically the naming authority for naming authorities. For
344          * this simple implementation, we will just check that the prefix
345          * configured in dspace.cfg is the one in the request, returning true if
346          * this is the case, false otherwise.
347          *
348          * FIXME: For more complex Handle situations, this will need enhancing.
349          */

350
351         // First, construct a string representating the naming authority Handle
352
// we'd expect.
353
String JavaDoc expected = "0.NA/"
354                 + ConfigurationManager.getProperty("handle.prefix");
355
356         // Which authority does the request pertain to?
357
String JavaDoc received = Util.decodeString(theHandle);
358
359         // Return true if they match
360
return expected.equals(received);
361     }
362
363     /**
364      * Return all handles in local storage which start with the naming authority
365      * handle.
366      *
367      * @param theNAHandle
368      * byte array representation of naming authority handle
369      * @return All handles in local storage which start with the naming
370      * authority handle.
371      * @exception HandleException
372      * If an error occurs while calling the Handle API.
373      */

374     public Enumeration JavaDoc getHandlesForNA(byte[] theNAHandle)
375             throws HandleException
376     {
377         String JavaDoc naHandle = Util.decodeString(theNAHandle);
378
379         if (log.isInfoEnabled())
380         {
381             log.info("Called getHandlesForNA for NA " + naHandle);
382         }
383
384         Context context = null;
385
386         try
387         {
388             context = new Context();
389
390             List JavaDoc handles = HandleManager.getHandlesForPrefix(context, naHandle);
391             List JavaDoc results = new LinkedList JavaDoc();
392
393             for (Iterator JavaDoc iterator = handles.iterator(); iterator.hasNext();)
394             {
395                 String JavaDoc handle = (String JavaDoc) iterator.next();
396
397                 // Transforms to byte array
398
results.add(Util.encodeString(handle));
399             }
400
401             return Collections.enumeration(results);
402         }
403         catch (SQLException JavaDoc sqle)
404         {
405             if (log.isDebugEnabled())
406             {
407                 log.debug("Exception in getHandlesForNA", sqle);
408             }
409
410             throw new HandleException(HandleException.INTERNAL_ERROR);
411         }
412         finally
413         {
414             if (context != null)
415             {
416                 try
417                 {
418                     context.complete();
419                 }
420                 catch (SQLException JavaDoc sqle)
421                 {
422                 }
423             }
424         }
425     }
426 }
427
Popular Tags