001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.ws.core;
021:
022: import java.util.*;
023:
024: /**
025: * Maintains a mapping between service ids and services for local services.
026: * This class is used both on the server and client side. On the server side, it
027: * maintains a two way mapping between a Web service and its id. On the client
028: * side, it maintains a two way mapping between a listener and its ID.
029: *
030: * @author Padmanabh Dabke
031: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
032: */
033: public class LocalRegistry {
034: /**
035: * Hashtable that keeps a two way mapping between local services and their
036: * IDs.
037: */
038: private static Hashtable lrLocalServices = new Hashtable();
039:
040: /**
041: * A counter for generating unique service IDs.
042: */
043: private static int lrCounter = 0;
044:
045: /**
046: * Creates an ID for the given service and creates a two way mapping between
047: * the service and the ID.
048: */
049: @SuppressWarnings("unchecked")
050: public static void export(Object service) {
051:
052: String id = Integer.toString(getCounter());
053: lrLocalServices.put(id, service);
054: lrLocalServices.put(service, id);
055: }
056:
057: /**
058: * Generates a unique service ID.
059: *
060: * @return Generated ID
061: */
062: private synchronized static int getCounter() {
063: return lrCounter++;
064: }
065:
066: /**
067: * Get ID of a service.
068: *
069: * @return Service ID
070: * @param service
071: * Service whose ID is to be looked up
072: */
073: public static String getID(Object service) {
074: return (String) lrLocalServices.get(service);
075: }
076:
077: /**
078: * Get service corresponding to the specified ID.
079: *
080: * @return Service
081: * @param id
082: * Service ID
083: */
084: public static Object getService(String id) {
085: return lrLocalServices.get(id);
086: }
087:
088: /**
089: * Removes the service - ID mapping from the hashtable.
090: */
091: public static void unexport(Object rr) {
092:
093: try {
094: String id = (String) lrLocalServices.get(rr);
095: if (id != null) {
096: lrLocalServices.remove(id);
097: lrLocalServices.remove(rr);
098: }
099: } catch (Exception ex) {
100: // LogUtil.logError("LocalRegistry.unexport()",ex);
101: return;
102: }
103:
104: }
105:
106: /**
107: * Adds a two way mapping between
108: * the service and the ID.
109: */
110: @SuppressWarnings("unchecked")
111: protected static void export(Object service, String id) {
112:
113: lrLocalServices.put(id, service);
114: lrLocalServices.put(service, id);
115: }
116: }
|