View Javadoc

1   /*
2    * Created on 26.04.2005 from Linke
3    *
4    */
5   package net.sf.crispy.impl.xmlrpc;
6   
7   import java.util.Hashtable;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.Map;
11  import java.util.Vector;
12  import java.util.Map.Entry;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.xmlrpc.XmlRpcServer;
17  
18  /**
19   * Implementation of service interface, to add, remove or to ask to register services.
20   * 
21   * @author Linke
22   *
23   */
24  public class XmlRpcAdminServiceImpl implements XmlRpcAdminService {
25  	
26  	protected static final Log log = LogFactory.getLog (XmlRpcAdminServiceImpl.class);
27  
28  	private XmlRpcServer xmlRpcServer = null;
29  	private Map services = new Hashtable();
30      private String nullValue = null;
31      
32      public final void setNullValue (String pvNullValue) {
33      	nullValue = pvNullValue;
34      }
35      public final String getNullValue() {
36  		return nullValue;
37  	}
38  
39  	
40  	/** 
41  	 * @param pvServicesMap Map von Diensten, die bereits registriert sind (String ServiceInterface, String ServiceImplClass)
42  	 * @param pvXmlRpcServer Referenz zum XmlRpcServer, um Dienste hinzuzufuegen und zu entfernen 
43  	 */
44  	public XmlRpcAdminServiceImpl (XmlRpcServer pvXmlRpcServer, Map pvServicesMap, String pvNullValue) {
45  		xmlRpcServer = pvXmlRpcServer;
46  		setNullValue(pvNullValue);
47  		if (pvServicesMap != null) {
48  			Iterator it = pvServicesMap.entrySet().iterator();
49  			while (it.hasNext()) {
50  				Map.Entry lvMapEntry = (Entry) it.next();
51  				addService((String) lvMapEntry.getKey() , (String) lvMapEntry.getValue());
52  			}
53  		}
54  		// sich selbst hinzufuegen
55  		addService(this.getClass().getName(), this);
56  		addService(XmlRpcAdminService.class.getName(), this);
57  	}
58  	
59  	public XmlRpcServer getXmlRpcServer() { return xmlRpcServer; }
60  	
61  	/**
62  	 * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#addService(java.lang.String,java.lang.String)
63  	 */
64  	public final String addService(String pvServiceInterface, String pvImplClass) {
65  		try {
66  			Object lvServiceImpl = Class.forName(pvImplClass).newInstance();
67  			return addService(pvServiceInterface, lvServiceImpl);
68  		} catch (Exception e) {
69  			return e.toString();
70  		}					
71  	}
72  	
73  	public final String addService(String pvServiceInterface, Object pvServiceImpl) {
74  		if (log.isDebugEnabled()) { log.debug("XmlRpcAdminService.addService: " + pvServiceInterface); }
75  		try { 
76  			xmlRpcServer.addHandler(pvServiceInterface, new XmlRpcConvertHandler(pvServiceImpl, getNullValue()));
77  			services.put(pvServiceInterface, pvServiceImpl);
78  			return pvServiceInterface;
79  		} catch (Exception e) {
80  //			e.printStackTrace();
81  			return e.toString();
82  		}		
83  	}
84  
85  	/**
86  	 * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#removeService(java.lang.String)
87  	 */
88  	public String removeService(String pvServiceInterface) {
89  		if (log.isDebugEnabled()) { log.debug("XmlRpcAdminService.removeService: " + pvServiceInterface); }
90  		xmlRpcServer.removeHandler(pvServiceInterface);
91  		services.remove(pvServiceInterface);
92  		return pvServiceInterface;
93  	}
94  
95  	/**
96  	 * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#existService(java.lang.String)
97  	 */
98  	public Boolean existService(String pvServiceInterface) {
99  		if (log.isDebugEnabled()) { log.debug("XmlRpcAdminService.existService: " + pvServiceInterface); }
100 		boolean ret = services.containsKey(pvServiceInterface);
101 //		try {
102 //			Object lvHandler = xmlRpcServer.getHandlerMapping().getHandler(pvServiceInterface);
103 //			System.out.println("-----> Handler: " + pvServiceInterface + " --> " + lvHandler);
104 //		} catch (Exception e) {
105 //			e.printStackTrace();
106 //		}
107 		return Boolean.valueOf(ret);
108 	}
109 
110 	/**
111 	 * @see net.sf.crispy.impl.xmlrpc.XmlRpcAdminService#getAllService()
112 	 */
113 	public List getAllService() {
114 		if (log.isDebugEnabled()) { log.debug("XmlRpcAdminService.getAllService by Server: " + xmlRpcServer); }
115 //		return (String[]) services.keySet().toArray(new String[0]);
116 		List retList = new Vector();
117 		Iterator it = services.keySet().iterator();
118 		while (it.hasNext()) {
119 			retList.add(it.next());
120 		}
121 		return retList;
122 	}
123 
124 }