package com.dbxml.labrador.objects;
/*
* The dbXML Labrador Software License, Version 1.0
*
*
* Copyright (c) 2003 The dbXML Group, L.L.C. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by The
* dbXML Group, L.L.C. (http://www.dbxml.com/)."
* Alternately, this acknowledgment may appear in the software
* itself, if and wherever such third-party acknowledgments normally
* appear.
*
* 4. The names "Labrador" and "dbXML Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* info@dbxml.com
*
* 5. Products derived from this software may not be called "Labrador",
* nor may "Labrador" appear in their name, without prior written
* permission of The dbXML Group, L.L.C..
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE DBXML GROUP, L.L.C. OR ITS
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* $Id: ObjectResolver.java,v 1.5 2003/12/15 06:43:10 bradford Exp $
*/
import com.dbxml.labrador.Discovery;
import com.dbxml.labrador.ID;
import com.dbxml.labrador.Instance;
import com.dbxml.labrador.Resolver;
import com.dbxml.labrador.broker.Broker;
import java.util.HashMap;
import java.util.Map;
/**
* ObjectResolver is the standard Labrador resolver For introspected
* Java objects. Most of the time, you will only need to call the
* methods of this class in order to expose objects via Labrador.
*/
public final class ObjectResolver implements Resolver {
private static final Map objs = new HashMap();
private static final Object objMutex = new Object();
private ObjectResolver() {
}
/**
* initialize initializes the Resolver, insuring that it is available
* as part of the Labrador Broker.
*/
public static void initialize() {
Broker.getInstance().addResolver(new ObjectResolver());
}
public boolean isIDValid(ID id) {
return objs.containsKey(id);
}
public Instance getInstance(ID id) {
synchronized ( objMutex ) {
return new ObjectInstance(objs.get(id));
}
}
public Discovery getDiscovery(ID id) {
synchronized ( objMutex ) {
return new ObjectInstance(objs.get(id)).getDiscovery();
}
}
/**
* register registers a Java object with the ObjectResolver using the
* specified name. The Resolver will attempt to introspect the
* object's class and expose all public methods that have valid
* parameter types.
*
* @param namide The ID to register the Object under
* @param obj The Object to register
* @return The Object's ID
*/
public static ID register(ID id, Object obj) {
synchronized ( objMutex ) {
objs.put(id, obj);
}
return id;
}
/**
* register registers a Java object with the ObjectResolver using,
* allowing the Resolver to generate a unique ID for that Object.
* The Resolver will attempt to introspect the object's class and
* expose all public methods that have valid parameter types.
*
* @param obj The Object to register
* @return A unique ID
*/
public static ID register(Object obj) {
ID id = new ID();
return register(id, obj);
}
/**
* unregister removes a Java object from the ObjectResolver using the
* specified name.
*
* @param id The ID of the Object unregister
*/
public static void unregister(ID id) {
synchronized ( objMutex ) {
objs.remove(id);
}
}
}