001    package nl.cwi.sen1.tunit.execution;
002    
003    import java.lang.reflect.Constructor;
004    import java.lang.reflect.InvocationTargetException;
005    
006    import nl.cwi.sen1.tunit.TUnitTestCase;
007    import nl.cwi.sen1.tunit.ToolComValidator;
008    import toolbus.exceptions.ToolBusException;
009    import toolbus.logging.ILogger;
010    import toolbus.logging.IToolBusLoggerConstants;
011    import toolbus.logging.LoggerFactory;
012    import toolbus.tool.execution.IToolExecutor;
013    
014    public class ToolComValidatorExecutor implements IToolExecutor{
015            private final TUnitTestCase testCase;
016            
017            private final String name;
018            private final int id;
019            private final Class<?> toolClass;
020            
021            public ToolComValidatorExecutor(TUnitTestCase testCase, String name, int id, Class<?> toolClass){
022                    super();
023                    
024                    this.testCase = testCase;
025                    
026                    this.id = id;
027                    this.name = name;
028                    this.toolClass = toolClass;
029            }
030            
031            public void execute() throws ToolBusException{
032                    // Instantiate the toolstub.
033                    ToolComValidator tool;
034                    try{
035                            Constructor<?> toolConstructor = toolClass.getConstructor(new Class[]{TUnitTestCase.class, String.class, int.class, boolean.class});
036                            
037                            tool = (ToolComValidator) toolConstructor.newInstance(new Object[]{testCase, name, new Integer(id), Boolean.TRUE});
038                            testCase.connectToolComValidator(tool);
039                            
040                            Thread toolExecutor = new Thread(tool);
041                            toolExecutor.setDaemon(true);
042                            toolExecutor.start();
043                    }catch(InstantiationException iex){
044                            String error = "Unable to instantiate the tool. Classname: " + toolClass.getName();
045                            LoggerFactory.log(error, iex, ILogger.ERROR, IToolBusLoggerConstants.TOOLINSTANCE);
046                            throw new ToolBusException(error);
047                    }catch(IllegalAccessException iaex){
048                            String error = "The constructor of the tool we are trying to instantiate isn't public. Classname: " + toolClass.getName();
049                            LoggerFactory.log(error, iaex, ILogger.ERROR, IToolBusLoggerConstants.TOOLINSTANCE);
050                            throw new ToolBusException(error);
051                    }catch(InvocationTargetException itex){
052                            String error = "An exception occured during the invokation of the constructor of the tool we are trying to instantiate. Classname: " + toolClass.getName();
053                            LoggerFactory.log(error, itex, ILogger.ERROR, IToolBusLoggerConstants.TOOLINSTANCE);
054                            throw new ToolBusException(error);
055                    }catch(NoSuchMethodException nsmex){
056                            String error = "The tool we are trying to instantiate doesn't have a proper constructor. Classname: " + toolClass.getName();
057                            LoggerFactory.log(error, nsmex, ILogger.ERROR, IToolBusLoggerConstants.TOOLINSTANCE);
058                            throw new ToolBusException(error);
059                    }catch(SecurityException sex){
060                            String error = "We don't have permission to invoke the constructor of: " + toolClass.getName();
061                            LoggerFactory.log(error, sex, ILogger.ERROR, IToolBusLoggerConstants.TOOLINSTANCE);
062                            throw new ToolBusException(error);
063                    }catch(Exception ex){
064                            String error = "Unable to connect tool to the ToolBus directly: " + toolClass.getName();
065                            LoggerFactory.log(error, ex, ILogger.ERROR, IToolBusLoggerConstants.TOOLINSTANCE);
066                            throw new ToolBusException(error);
067                    }
068            }
069    }