Exporting a Proxy Service

Sometimes it is desirable to export and register a proxy for a service, rather than the service object itself. The most common reason for wanting to do this is performance, since the creation of the service object is delayed until it needed. By registering a proxy service the creation of the service object can be delayed indefinitely if the service is never used. The following methods are available:

The methods differ in that the first method is used to export the proxy service under a single service class, whereas the second method allows the proxy service to be exported under multiple service classes. In both cases the second parameter is an IProxyServiceHandler that is responsible for handling the proxy service, and the third parameter is a Dictionary of properties to be registered with the proxy service. If no properties are to be registered, simply pass null.

The method addExportedProxyService(Class, IProxyServiceHandler, Dictionary) is most commonly used to export a proxy service, for example:

  protected void activate() {
    IProxyServiceHandler handler = new IProxyServiceHandler() {
      	public Object createService() {
      	  return new HotdogVendor();
      	}
      	
      	public void preInvoke(Object service, Method method, Object[] args) {
      	  //...
      	}
      	
      	public void postInvoke(Object service, Method method, Object[] args) {
      	  //...
      	}
    };
      	
    Dictionary properties = new Hashtable(11);
    properties.put("spicy", new Integer(7));  //$NON-NLS-1$
    addExportedProxyService(VendorService.class, handler, properties);
  }

The adapter class ProxyServiceHandlerAdapter is available to simplify the creation of an IProxyServiceHandler by allowing you to override just the methods you require, for example:

  protected void activate() {
    IProxyServiceHandler handler = new ProxyServiceHandlerAdapter() {
      	public Object createService() {
      	  return new HotdogVendor();
      	}
    };
      	
    Dictionary properties = new Hashtable(11);
    properties.put("spicy", new Integer(7));  //$NON-NLS-1$
    addExportedProxyService(VendorService.class, handler, properties);
  }

To register a single service under multiple service classes, use the method addExportedProxyService(Class[], IProxyServiceHandler, Dictionary), for example:

  protected void activate() {
    IProxyServiceHandler handler = new ProxyServiceHandlerAdapter() {
      	public Object createService() {
      	  return new Shimmer();
      	}
    };
  
    Class[] classes = new Class[] {
      DessertToppingService.class,
      FloorWaxService.class
    };
    
    Dictionary properties = new Hashtable(11);    
    properties.put("viscosity", "medium");  //$NON-NLS-1$  //$NON-NLS-2$
    addExportedProxyServices(classes, handler, properties);
  }

When registering multiple proxy services in this way, the same set of properties are used to register the proxy service under each of the service classes. This means that changing the properties later will affect the proxy service for each of its registered service classes. To register a unique set of properties for each service class, register the proxy service individually for each service name, for example:

  protected void activate() {
    IProxyServiceHandler handler = new ProxyServiceHandlerAdapter() {
      	public Object createService() {
      	  return new Shimmer();
      	}
    };
    
    // DessertToppingService
    Dictionary dessertToppingProperties = new Hashtable(11);    
    dessertToppingProperties.put("flavor", "banana");  //$NON-NLS-1$  //$NON-NLS-2$
    addExportedProxyService(DessertToppingService.class, handler, dessertToppingProperties);

	// FloorWaxService
    Dictionary floorWaxProperties = new Hashtable(11);    
    floorWaxProperties.put("viscosity", "medium");  //$NON-NLS-1$  //$NON-NLS-2$
    addExportedProxyService(FloorWaxService.class, handler, floorWaxProperties);
  }