This method is called to cause the component to perform its function. Within this method you can call other internal API methods to get the values of inputs, to get resources, to ask users for parameters, and to get output streams. Typically, during the execute method a component will:
- Gather input values. These might be parameters or component settings. If the input values are not complete, the component can stop executing or prompt the user for additional information.
- Gather resources. These might be templates or definition files.
- Get an output pipe of some kind (e.g. an output stream).
- Create output contents.
- Return the status of the execution.
See 'Internal API' below for descriptions of the methods available for the component to use. We will use theĀ executeAction method of the PrintComponent (org.pentaho.plugin.print.PrintComponent) as an example (minor modifications have been made for clarity).
protected boolean executeAction() { String printFileName = null; IActionResource printFileResource = null; // see if we are printing a file if (isDefinedInput("print-file")) { // get the name of the file to print printFileName = getInputStringValue("print-file"); } InputStream inStream = null; // Get the name of the printer to use if (isDefinedInput("printer-name")) { printerName = getInputStringValue("printer-name"); } // try to find the requested printer PrintService printer = getPrinterInternal(printerName); if (printer == null) { // the requested printer is not available if (!feedbackAllowed()) { // we are not allowed to prompt the user for a printer, we have to fail error( "The requested printer "+printerName+" is not available" ); return false; } // prompt the user for an available printer // get a list of available print services PrintService[] services = PrinterJob.lookupPrintServices(); ArrayList values = new ArrayList(); // add each print service to our list of printers for (int i = 0; i < services.length; i++) { String value = services[i].getName(); values.add(value); } // create a parameter for the user to select from createFeedbackParameter("printer-name", "select a printer", "", null, values, null, "select"); return null; } promptNeeded(); return true; } // Get the number of copies int copies = 1; if (isDefinedInput("copies")) { copies = Integer.valueOf(getInputStringValue("copies")).intValue(); } // Check for a valid printFileName or printFile Resource if (printFileName != null) { try { inStream = new FileInputStream(printFileName); } catch (FileNotFoundException fnfe) { error(fnfe.toString(), fnfe); return false; } // Set the input source for sending to the driver. InputSource source = new InputSource(inStream); try { Driver driver = new Driver(source, null); PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintService(printer); PrintRenderer renderer = new PrintRenderer(pj, copies); driver.setRenderer(renderer); driver.run(); } catch (Exception ex) { error( "Could not print the document", ex); return false; } return true; }