Demystifying JINI: Non-Secure Server: Part 2

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on July 08, 2014 · 4 mins read

In Part 1, we had sieved through the example available on the JINI site and distilled the Rmi client server code to the bare minimum. But still, its far from production ready. Its not Spring-ified yet. Lets try to do that here. We will build this on top of Part 1.

Server

I need the following utility which can double up as a Spring bean, will do the export for me.

public class ServiceExporter implements InitializingBean {
    private DiscoveryManagement discoveryManager;
    private LeaseRenewalManager leaseRenewalManager;
    private Exporter exporter;
    private Remote service;
    private String serviceName;
    public DiscoveryManagement getDiscoveryManager() {
        return discoveryManager;
    }
    public void setDiscoveryManager(DiscoveryManagement discoveryManager) {
        this.discoveryManager = discoveryManager;
    }
    public LeaseRenewalManager getLeaseRenewalManager() {
        return leaseRenewalManager;
    }
    public void setLeaseRenewalManager(LeaseRenewalManager leaseRenewalManager) {
        this.leaseRenewalManager = leaseRenewalManager;
    }
    public Exporter getExporter() {
        return exporter;
    }
    public void setExporter(Exporter exporter) {
        this.exporter = exporter;
    }
    public Remote getService() {
        return service;
    }
    public void setService(Remote service) {
        this.service = service;
    }
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        Remote exportedService = exporter.export(service);
        JoinManager joinManager = new JoinManager(exportedService, new Entry[]{new Name(serviceName)},
                (ServiceID) null, discoveryManager, leaseRenewalManager);
    }
}

This is the main Spring configuration:



	
		
			
				
					
				
			
		
	
	
	
	
		
		
		
		
		
			
		
	
	
		
		
		
		
		
			
		
	

And, see how simple my main class has become, just loads the Spring file:

public class SpringNonSecureRmiServer {
	public static void main(String[] args) {
		try(
				ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("server-application.xml")
		){
			System.out.println("SpringNonSecureRmiServer.main(): " + "The server is ready");
		}
	}
}

Client

The client is a bit tricky, as I need to lookup the remote service from the registry. In Spring's nomenclature, this is nothing but a FactoryBean.

public class ProxyRemoteFactoryBean implements FactoryBean, InitializingBean {
    private LookupLocator lookupLocator;
    private Class serviceInterface;
    private String serviceName;
    private Remote remoteService;
    @Override
    public Remote getObject() throws Exception {
        return remoteService;
    }
    @Override
    public Class getObjectType() {
        return serviceInterface;
    }
    @Override
    public boolean isSingleton() {
        return true;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        remoteService = (Remote) lookupLocator.getRegistrar().lookup(new ServiceTemplate(null,
                new Class[]{serviceInterface}, new Entry[]{new Name(serviceName)}));
    }
    public LookupLocator getLookupLocator() {
        return lookupLocator;
    }
    public void setLookupLocator(LookupLocator lookupLocator) {
        this.lookupLocator = lookupLocator;
    }
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    public Class getServiceInterface() {
        return serviceInterface;
    }
    public void setServiceInterface(Class serviceInterface) {
        this.serviceInterface = serviceInterface;
    }
}

The Spring configuration is:



	
		
	
	
		
		
		
	
	
		
		
		
	

And this is the way we can use it.

public class SpringNonSecureRmiClient {
	/**
	 * Needs to be run with the following JVM args:
*
    *
  • * -Djava.security.policy=/full-path/RmiClient/src/main/resources/policy.all *
  • *
  • -Djava.rmi.server.RMIClassLoaderSpi=net.jini.loader.pref. * PreferredClassProvider
  • *
*/ public static void main(String[] args) throws Exception { try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "client-application.xml")) { UserService userService = (UserService) context .getBean("userService"); System.out.println("Getting current users from remote: " + userService.getCurrentUsers()); BankAccountService bankAccountService = (BankAccountService) context .getBean("bankAccountService"); System.out.println("Creating bank account from remote, id: " + bankAccountService.createBankAccount("aaa")); } } }

Resources

The code can be found in GitHub, under the code/jini/unsecure/plain:
https://github.com/paawak/blog.git