1 package org.opensrf.util;
4 import java.util.HashMap;
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
15 public class OSRFRegistry {
19 * Global collection of registered net objects.
20 * Maps netClass names to registries.
22 private static HashMap<String, OSRFRegistry>
23 registry = new HashMap<String, OSRFRegistry>();
26 /** Serialization types for registered objects */
27 public enum WireProtocol {
32 /** Array of field names for this registered object */
34 /** The wire protocol for this object */
35 WireProtocol wireProtocol;
36 /** The network class for this object */
40 * Returns the array of field names
42 public String[] getFields() {
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
55 public static OSRFRegistry registerObject(String netClass, WireProtocol wireProtocol, String fields[]) {
56 OSRFRegistry r = new OSRFRegistry(netClass, wireProtocol, fields);
57 registry.put(netClass, r);
62 * Returns the registry for the given netclass
63 * @param netClass The network class to lookup
65 public static OSRFRegistry getRegistry(String netClass) {
66 if( netClass == null ) return null;
67 return (OSRFRegistry) registry.get(netClass);
72 * @param field The name of the field to lookup
73 * @return the index into the fields array of the given field name.
75 public int getFieldIndex(String field) {
76 for( int i = 0; i < fields.length; i++ )
77 if( fields[i].equals(field) )
82 /** Returns the wire protocol of this object */
83 public WireProtocol getWireProtocol() {
84 return this.wireProtocol;
87 /** Returns the netClass ("hint") of this object */
88 public String getNetClass() {
93 * Creates a new registry object.
94 * @param netClass The network class/hint
95 * @param wireProtocol The wire protocol
96 * @param fields The array of field names. For array-based objects,
97 * the fields array must be sorted in accordance with the sorting
98 * of the objects in the array.
100 public OSRFRegistry(String netClass, WireProtocol wireProtocol, String fields[]) {
101 this.netClass = netClass;
102 this.wireProtocol = wireProtocol;
103 this.fields = fields;