]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/util/OSRFRegistry.java
8a5991351cb955559556c501385061e1ab9636d3
[OpenSRF.git] / src / java / org / opensrf / util / OSRFRegistry.java
1 package org.opensrf.util;
2
3 import java.util.Map;
4 import java.util.HashMap;
5
6
7 /**
8  * Manages the registration of OpenSRF network-serializable objects.  
9  * A serializable object has a class "hint" (called netClass within) which
10  * describes the type of object.  Each object also has a set of field names
11  * for accessing/mutating object properties.  Finally, objects have a 
12  * serialization wire protocol.  Currently supported protocols are HASH
13  * and ARRAY.
14  */
15 public class OSRFRegistry {
16
17
18     /**
19      * Global collection of registered net objects.  
20      * Maps netClass names to registries.
21      */
22     private static HashMap<String, OSRFRegistry> 
23         registry = new HashMap<String, OSRFRegistry>();
24
25
26     /** Serialization types for registered objects */
27     public enum WireProtocol {
28         ARRAY, HASH
29     };
30
31
32     /** Array of field names for this registered object */
33     String fields[];
34     /** The wire protocol for this object */
35     WireProtocol wireProtocol;
36     /** The network class for this object */
37     String netClass;
38
39     /**
40      * Returns the array of field names
41      */
42     public String[] getFields() {
43         return this.fields;
44     }
45
46
47     /**
48      * Registers a new object.
49      * @param netClass The net class for this object
50      * @param wireProtocol The object's wire protocol
51      * @param fields An array of field names.  For objects whose
52      * wire protocol is ARRAY, the positions of the field names 
53      * will be used as the array indices for the fields at serialization time
54      */
55     public static OSRFRegistry registerObject(String netClass, WireProtocol wireProtocol, String fields[]) {
56         OSRFRegistry r = new OSRFRegistry(netClass, wireProtocol, fields);
57         registry.put(netClass, r);
58         return r;
59     }
60
61     /**
62      * Returns the registry for the given netclass
63      * @param netClass The network class to lookup
64      */
65     public static OSRFRegistry getRegistry(String netClass) {
66         if( netClass == null ) return null;
67         return (OSRFRegistry) registry.get(netClass);
68     }
69
70
71     /**
72      * @param field The name of the field to lookup
73      * @return the index into the fields array of the given field name.
74      */
75     public int getFieldIndex(String field) {
76         for( int i = 0; i < fields.length; i++ )
77             if( fields[i].equals(field) ) 
78                 return i;
79         return -1;
80     }
81
82     public WireProtocol getWireProtocol() {
83         return this.wireProtocol;
84     }
85
86     public String getNetClass() {
87         return this.netClass;
88     }
89
90     /**
91      * Creates a new registry object */
92     public OSRFRegistry(String netClass, WireProtocol wireProtocol, String fields[]) {
93         this.netClass = netClass;
94         this.wireProtocol = wireProtocol;
95         this.fields = fields;
96     }
97 }
98
99