This example illustrates how to setup a project to use the ProcessExecutionSessionImpl class in a plugin to execute external OS processes and capture the result in execution result objects.
To setup a project to use the asserter follow these steps:
The ProcessExecutionSessionImpl class is defined in the pineapple-process-execution-support project, so add a Maven dependency to the project POM:
<dependency> <groupId>com.alpha.pineapple</groupId> <artifactId>pineapple-process-execution-support</artifactId> </dependency>
If the project contain a Spring configuration file for definition of the beans used by the project then add an import directive to import the Spring configuration file from the process execution project:
<!-- Configures pineapple-process-execution-plugin-support --> <import resource="classpath:com.alpha.pineapple.process.execution-config.xml" />
Now the session class and other command class is available in the project with the bean id's:
Please notice: that the purpose isn't to get access to the session class through Spring, but to enable the session class to access the process execution command through Spring.
Subclass the ProcessExecutionSessionImpl into a plugin specific session class to reuse the functionality of the class.
Example: in the JRockit installation plugion the ProcessExecutionSessionImpl class is subclasses into JRockitInstallationSessionImpl
package com.alpha.pineapple.plugin.jrockit.installation.session; import com.alpha.pineapple.plugin.PluginSession; import com.alpha.pineapple.session.ProcessExecutionSessionImpl; /** * Subclass of {@link ProcessExecutionSessionImpl} to reuse it. */ @PluginSession public class JRockitInstallationSessionImpl extends ProcessExecutionSessionImpl { }
To enable the usage of the session class at runtime , annotate it with the @PluginSession annotation.
Example: From the JRockit installation plugin:
package com.alpha.pineapple.plugin.jrockit.installation.session; import com.alpha.pineapple.plugin.PluginSession; import com.alpha.pineapple.session.ProcessExecutionSessionImpl; /** * Subclass of {@link ProcessExecutionSessionImpl} to reuse it. */ @PluginSession public class JRockitInstallationSessionImpl extends ProcessExecutionSessionImpl { }
The session object will now be initialized by Pineapple when an operation is executed. The session object is available to any operation since it is passed as any argument to operations in the plugin.
Example: From the JRockit installation plugin:
@PluginOperation( OperationNames.DEPLOY_CONFIGURATION ) public class DeployConfiguration implements Operation { public void execute( Object content, Session session, ExecutionResult executionResult ) throws PluginExecutionFailedException { // validate parameters Validate.notNull( content, "content is undefined." ); Validate.notNull( session, "session is undefined." ); Validate.notNull( executionResult, "executionResult is undefined." ); // throw exception if required type isn't available if ( !( session instanceof ProcessExecutionSession ) ) { Object[] args = { ProcessExecutionSession.class }; String message = messageProvider.getMessage("dc.session_typecast_failed", args ); throw new PluginExecutionFailedException( message ); } try { // type cast to JRockit installation model object JRockitInstallation model = (JRockitInstallation) content; // type cast to process execution session ProcessExecutionSession externalProcessSession = (ProcessExecutionSession) session; //.. more code goes here... // execute external process session.execute(installer , arguments, DEFAULT_PROCESS_TIMEOUT, description2, result); } catch ( Exception e ) { Object[] args = { e.toString() }; String message = messageProvider.getMessage("dc.error", args ); throw new PluginExecutionFailedException( message, e ); } } }