001: /*
002: * Created on 26.04.2005 from Linke
003: *
004: */
005: package net.sf.crispy.impl.xmlrpc;
006:
007: import java.util.Hashtable;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Vector;
012: import java.util.Map.Entry;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.apache.xmlrpc.XmlRpcServer;
017:
018: /**
019: * Implementation of service interface, to add, remove or to ask to register services.
020: *
021: * @author Linke
022: *
023: */
024: public class XmlRpcAdminServiceImpl implements XmlRpcAdminService {
025:
026: protected static final Log log = LogFactory
027: .getLog(XmlRpcAdminServiceImpl.class);
028:
029: private XmlRpcServer xmlRpcServer = null;
030: private Map services = new Hashtable();
031: private String nullValue = null;
032:
033: public final void setNullValue(String pvNullValue) {
034: nullValue = pvNullValue;
035: }
036:
037: public final String getNullValue() {
038: return nullValue;
039: }
040:
041: /**
042: * @param pvServicesMap Map von Diensten, die bereits registriert sind (String ServiceInterface, String ServiceImplClass)
043: * @param pvXmlRpcServer Referenz zum XmlRpcServer, um Dienste hinzuzufuegen und zu entfernen
044: */
045: public XmlRpcAdminServiceImpl(XmlRpcServer pvXmlRpcServer,
046: Map pvServicesMap, String pvNullValue) {
047: xmlRpcServer = pvXmlRpcServer;
048: setNullValue(pvNullValue);
049: if (pvServicesMap != null) {
050: Iterator it = pvServicesMap.entrySet().iterator();
051: while (it.hasNext()) {
052: Map.Entry lvMapEntry = (Entry) it.next();
053: addService((String) lvMapEntry.getKey(),
054: (String) lvMapEntry.getValue());
055: }
056: }
057: // sich selbst hinzufuegen
058: addService(this .getClass().getName(), this );
059: addService(XmlRpcAdminService.class.getName(), this );
060: }
061:
062: public XmlRpcServer getXmlRpcServer() {
063: return xmlRpcServer;
064: }
065:
066: /**
067: * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#addService(java.lang.String,java.lang.String)
068: */
069: public final String addService(String pvServiceInterface,
070: String pvImplClass) {
071: try {
072: Object lvServiceImpl = Class.forName(pvImplClass)
073: .newInstance();
074: return addService(pvServiceInterface, lvServiceImpl);
075: } catch (Exception e) {
076: return e.toString();
077: }
078: }
079:
080: public final String addService(String pvServiceInterface,
081: Object pvServiceImpl) {
082: if (log.isDebugEnabled()) {
083: log.debug("XmlRpcAdminService.addService: "
084: + pvServiceInterface);
085: }
086: try {
087: xmlRpcServer.addHandler(pvServiceInterface,
088: new XmlRpcConvertHandler(pvServiceImpl,
089: getNullValue()));
090: services.put(pvServiceInterface, pvServiceImpl);
091: return pvServiceInterface;
092: } catch (Exception e) {
093: // e.printStackTrace();
094: return e.toString();
095: }
096: }
097:
098: /**
099: * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#removeService(java.lang.String)
100: */
101: public String removeService(String pvServiceInterface) {
102: if (log.isDebugEnabled()) {
103: log.debug("XmlRpcAdminService.removeService: "
104: + pvServiceInterface);
105: }
106: xmlRpcServer.removeHandler(pvServiceInterface);
107: services.remove(pvServiceInterface);
108: return pvServiceInterface;
109: }
110:
111: /**
112: * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#existService(java.lang.String)
113: */
114: public Boolean existService(String pvServiceInterface) {
115: if (log.isDebugEnabled()) {
116: log.debug("XmlRpcAdminService.existService: "
117: + pvServiceInterface);
118: }
119: boolean ret = services.containsKey(pvServiceInterface);
120: // try {
121: // Object lvHandler = xmlRpcServer.getHandlerMapping().getHandler(pvServiceInterface);
122: // System.out.println("-----> Handler: " + pvServiceInterface + " --> " + lvHandler);
123: // } catch (Exception e) {
124: // e.printStackTrace();
125: // }
126: return Boolean.valueOf(ret);
127: }
128:
129: /**
130: * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#getAllService()
131: */
132: public List getAllService() {
133: if (log.isDebugEnabled()) {
134: log.debug("XmlRpcAdminService.getAllService by Server: "
135: + xmlRpcServer);
136: }
137: // return (String[]) services.keySet().toArray(new String[0]);
138: List retList = new Vector();
139: Iterator it = services.keySet().iterator();
140: while (it.hasNext()) {
141: retList.add(it.next());
142: }
143: return retList;
144: }
145:
146: }
|