< joris

Advanced IRConnection example

You might want to check out the sources and after compiling use the example subfolder rconnection

The first and only thing you will do is to set up which libraries you might want to use in a configuration file, like

@Configuration
@ImportResource("classpath*:spring/r-service.spring.xml")
public class SpringConfiguration {

    @Bean
    public IRConnection managedIRConnection(IRConnectionConfig configuration, @Qualifier("rStarterProcess") ProcessService rStarterService) {
	final IRConnectionMgrImpl irConnectionMgrImpl = new IRConnectionMgrImpl();
	irConnectionMgrImpl.setService(rStarterService);
	irConnectionMgrImpl.setFactory(new REngineConnectionFactory());
	return irConnectionMgrImpl.getIRConnection(configuration);
    }

    // -- helper
    
    private List requiredInstallLibraries() {
	return Arrays.asList("TIMP");
    }
    
    private List requiredLibraries() {
	return Arrays.asList("TIMP", "MASS");
    }

    @Bean 
    IRConnectionConfig connectionConfiguration() {
	final IRConnectionConfigImpl irConnectionConfigImpl = new IRConnectionConfigImpl();
	final List configurationSteps = new ArrayList();
	for (final String libraryName : requiredInstallLibraries()) {
	    final LibraryInstallationStep installLib = new LibraryInstallationStep();
	    installLib.setLibrary(libraryName);
	    configurationSteps.add(installLib);
	}
	for (final String libraryName : requiredLibraries()) {
	    final LoadLibraryStep loadLib = new LoadLibraryStep();
	    loadLib.setLibrary(libraryName);
	    configurationSteps.add(loadLib);
	}
	irConnectionConfigImpl.setSteps(configurationSteps );
	return irConnectionConfigImpl;
    }
}
All you need to change is the requiredInstallLibraries() and requiredLibraries(). As the name hints, the first one are checked for existance and installed if not present. The required libs are loaded whenever you create a new IRConnection.
To use this in an example you need just two lines of code:
	final ApplicationContext springContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
	final IRConnection connection = springContext.getBean(IRConnection.class);
Now connection is your IRConnection, running with loaded TIMP and MASS library and you are ready to do with that whatever you like.