Search in sources :

Example 31 with ISolutionEngine

use of org.pentaho.platform.api.engine.ISolutionEngine in project pentaho-platform by pentaho.

the class PojoComponentTest method testStreamingPojo.

public void testStreamingPojo() {
    String instanceId = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SimpleOutputHandler outputHandler = new SimpleOutputHandler(out, false);
    outputHandler.setOutputPreference(IOutputHandler.OUTPUT_TYPE_DEFAULT);
    startTest();
    IPentahoSession session = new StandaloneSession("system");
    ISolutionEngine solutionEngine = ServiceTestHelper.getSolutionEngine();
    if (outputHandler != null) {
        outputHandler.setSession(session);
    }
    try {
        String xactionStr = ServiceTestHelper.getXAction(SOLUTION_PATH, "test/pojo/pojo2.xaction");
        PojoComponentTest.setActionSequenceResourceCalled = false;
        IRuntimeContext runtimeContext = // $NON-NLS-1$ //$NON-NLS-2$
        solutionEngine.execute(// $NON-NLS-1$ //$NON-NLS-2$
        xactionStr, // $NON-NLS-1$ //$NON-NLS-2$
        "test1.xaction", // $NON-NLS-1$ //$NON-NLS-2$
        "empty action sequence test", // $NON-NLS-1$ //$NON-NLS-2$
        false, // $NON-NLS-1$ //$NON-NLS-2$
        true, // $NON-NLS-1$ //$NON-NLS-2$
        null, // $NON-NLS-1$ //$NON-NLS-2$
        false, new HashMap(), outputHandler, null, new SimpleUrlFactory(""), new ArrayList());
        IActionParameter param = runtimeContext.getOutputParameter("outputstream");
        assertNotNull("RuntimeContext is null", runtimeContext);
        assertEquals("Action sequence execution failed", runtimeContext.getStatus(), IRuntimeContext.RUNTIME_STATUS_SUCCESS);
        assertTrue("setResource was not called", PojoComponentTest.setResourceInputStreamCalled);
        assertTrue("setResource was not called", PojoComponentTest.setActionSequenceResourceCalled);
    } catch (Exception e) {
        // we should not get here
        e.printStackTrace();
        assertTrue(e.getMessage(), false);
    }
    String output = new String(out.toByteArray());
    assertEquals("outputstream", "abcdeabcde", output);
    finishTest();
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) HashMap(java.util.HashMap) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 32 with ISolutionEngine

use of org.pentaho.platform.api.engine.ISolutionEngine in project pentaho-platform by pentaho.

the class PentahoSystemTest method testSessionStartup.

@Test
public void testSessionStartup() throws ObjectFactoryException {
    int oldLogLevel = Logger.getLogLevel();
    Logger.setLogLevel(ILogger.TRACE);
    final ISolutionEngine engine = mock(ISolutionEngine.class);
    pentahoObjectFactory = mock(IPentahoObjectFactory.class);
    when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public ISolutionEngine answer(InvocationOnMock invocation) throws Throwable {
            return engine;
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
    ISessionStartupAction action = mock(ISessionStartupAction.class);
    when(action.getActionOutputScope()).thenReturn(PentahoSystem.SCOPE_SESSION);
    when(action.getSessionType()).thenReturn(session.getClass().getName());
    when(session.isAuthenticated()).thenReturn(true);
    PentahoSystem.setSessionStartupActions(Arrays.asList(action));
    PentahoSystem.sessionStartup(session, null);
    System.out.flush();
    assertNotNull(baos);
    assertTrue(baos.toString().contains("Process session startup actions"));
    Logger.setLogLevel(oldLogLevel);
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ISessionStartupAction(org.pentaho.platform.api.engine.ISessionStartupAction) Test(org.junit.Test)

Example 33 with ISolutionEngine

use of org.pentaho.platform.api.engine.ISolutionEngine in project pentaho-platform by pentaho.

the class PentahoSystem method sessionStartup.

public static void sessionStartup(final IPentahoSession session, IParameterProvider sessionParameters) {
    List<ISessionStartupAction> sessionStartupActions = PentahoSystem.getSessionStartupActionsForType(session.getClass().getName());
    if (sessionStartupActions == null) {
        // nothing to do...
        return;
    }
    if (!session.isAuthenticated()) {
        return;
    }
    Boolean startupActionsFired = (Boolean) session.getAttribute("StartupActionsFired");
    if ((startupActionsFired == null) || (!startupActionsFired)) {
        try {
            if (debug) {
                // $NON-NLS-1$
                Logger.debug(PentahoSystem.class, "Process session startup actions");
            }
            if (sessionStartupActions != null) {
                for (ISessionStartupAction sessionStartupAction : sessionStartupActions) {
                    // parse the actionStr out to identify an action
                    // now execute the action...
                    SimpleOutputHandler outputHandler = null;
                    String instanceId = null;
                    ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, session);
                    solutionEngine.setLoggingLevel(PentahoSystem.loggingLevel);
                    solutionEngine.init(session);
                    // $NON-NLS-1$
                    String baseUrl = "";
                    HashMap parameterProviderMap = new HashMap();
                    if (sessionParameters == null) {
                        sessionParameters = new PentahoSessionParameterProvider(session);
                    }
                    parameterProviderMap.put(SCOPE_SESSION, sessionParameters);
                    IPentahoUrlFactory urlFactory = new SimpleUrlFactory(baseUrl);
                    ArrayList messages = new ArrayList();
                    IRuntimeContext context = null;
                    try {
                        context = solutionEngine.execute(sessionStartupAction.getActionPath(), "Session startup actions", false, true, instanceId, false, parameterProviderMap, outputHandler, null, urlFactory, // $NON-NLS-1$
                        messages);
                        // if context is null, then we cannot check the status
                        if (null == context) {
                            return;
                        }
                        if (context.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
                            // now grab any outputs
                            Iterator outputNameIterator = context.getOutputNames().iterator();
                            while (outputNameIterator.hasNext()) {
                                String attributeName = (String) outputNameIterator.next();
                                IActionParameter output = context.getOutputParameter(attributeName);
                                Object data = output.getValue();
                                if (data != null) {
                                    session.removeAttribute(attributeName);
                                    session.setAttribute(attributeName, data);
                                }
                            }
                        }
                    } catch (Throwable th) {
                        Logger.warn(PentahoSystem.class.getName(), Messages.getInstance().getString("PentahoSystem.WARN_UNABLE_TO_EXECUTE_SESSION_ACTION", th.getLocalizedMessage()), // $NON-NLS-1$
                        th);
                    } finally {
                        if (context != null) {
                            context.dispose();
                        }
                    }
                }
            }
        } finally {
            session.setAttribute("StartupActionsFired", true);
        }
    } else {
        if (debug) {
            Logger.debug(PentahoSystem.class, "Session startup actions already fired");
        }
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) HashMap(java.util.HashMap) PentahoSessionParameterProvider(org.pentaho.platform.engine.core.solution.PentahoSessionParameterProvider) ArrayList(java.util.ArrayList) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) ISessionStartupAction(org.pentaho.platform.api.engine.ISessionStartupAction) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionParameter(org.pentaho.platform.api.engine.IActionParameter)

Example 34 with ISolutionEngine

use of org.pentaho.platform.api.engine.ISolutionEngine in project pentaho-platform by pentaho.

the class DefaultChartBeansGenerator method runActionSequence.

/**
 * Executes an action sequence from an <code>ActionSequenceDocument</code>.
 *
 * @param pentahoSession
 *          current <code>IPentahoSession</code>
 * @param parameterProviders
 *          map of parameter providers; there should a single entry with "request" as the key
 * @param outputHandler
 *          output handler
 * @param doc
 *          action sequence document
 * @throws RuntimeException
 *           if anything goes wrong
 */
protected void runActionSequence(final IPentahoSession pentahoSession, final Map<String, IParameterProvider> parameterProviders, final IOutputHandler outputHandler, final ActionSequenceDocument doc) throws RuntimeException {
    // Get the solution engine
    ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, pentahoSession);
    if (solutionEngine == null) {
        // $NON-NLS-1$
        throw new RuntimeException("solutionEngine is null");
    }
    solutionEngine.setLoggingLevel(ILogger.DEBUG);
    solutionEngine.init(pentahoSession);
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    String contextPath = requestContext.getContextPath();
    // $NON-NLS-1$
    IPentahoUrlFactory urlFactory = new SimpleUrlFactory(contextPath);
    IRuntimeContext runtime;
    IParameterProvider requestParmProvider = parameterProviders.get("request");
    if (requestParmProvider.hasParameter("obj_id")) {
        final String obj_id = (String) requestParmProvider.getParameter("obj_id");
        final String msg_name = (String) requestParmProvider.getParameter("message_name");
        final String job_id = (String) requestParmProvider.getParameter("job_id");
        runtime = // $NON-NLS-1$ //$NON-NLS-2$
        solutionEngine.execute(// $NON-NLS-1$ //$NON-NLS-2$
        doc.toString(), // $NON-NLS-1$ //$NON-NLS-2$
        obj_id, // $NON-NLS-1$ //$NON-NLS-2$
        job_id, // $NON-NLS-1$ //$NON-NLS-2$
        false, // $NON-NLS-1$ //$NON-NLS-2$
        true, msg_name, true, parameterProviders, outputHandler, null, urlFactory, // $NON-NLS-1$
        new ArrayList());
    } else {
        runtime = // $NON-NLS-1$ //$NON-NLS-2$
        solutionEngine.execute(// $NON-NLS-1$ //$NON-NLS-2$
        doc.toString(), // $NON-NLS-1$ //$NON-NLS-2$
        "chartbeans_mql", // $NON-NLS-1$ //$NON-NLS-2$
        "myprocessid", // $NON-NLS-1$ //$NON-NLS-2$
        false, // $NON-NLS-1$ //$NON-NLS-2$
        true, "myinstanceid", true, parameterProviders, outputHandler, null, urlFactory, // $NON-NLS-1$
        new ArrayList());
    }
    if ((runtime != null) && (runtime.getStatus() != IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
        StringBuilder buf = new StringBuilder();
        boolean firstIteration = true;
        for (Object /* String */
        message : runtime.getMessages()) {
            if (message instanceof Exception) {
                Exception ex = (Exception) message;
                if (ex.getCause() instanceof RuntimeException) {
                    throw (RuntimeException) ex.getCause();
                }
            }
            if (!firstIteration) {
                // $NON-NLS-1$
                buf.append(" \\\\ ");
            }
            buf.append(message);
        }
        String errorStr;
        if (buf.indexOf("action_sequence_failed") > -1 && buf.indexOf("MQLRelationalDataComponent") > -1) {
            errorStr = Messages.getInstance().getString("DefaultChartBeansGenerator.ERROR_0001_SECURITY_ERROR");
        } else {
            errorStr = Messages.getInstance().getString("DefaultChartBeansGenerator.ERROR_0002_UNKNOWN_ERROR");
        }
        throw new RuntimeException(errorStr);
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext)

Example 35 with ISolutionEngine

use of org.pentaho.platform.api.engine.ISolutionEngine in project pentaho-platform by pentaho.

the class SubActionComponent method executeAction.

@SuppressWarnings("deprecation")
@Override
protected boolean executeAction() throws Throwable {
    SubActionAction subAction = (SubActionAction) getActionDefinition();
    List<Object> ignoreParameters = new ArrayList<Object>();
    String actionPath = buildActionPath(subAction.getSolution().getStringValue(), subAction.getPath().getStringValue(), subAction.getAction().getStringValue());
    // see if we are supposed to proxy the session
    IPentahoSession session = getSession();
    if (subAction.getSessionProxy() != ActionInputConstant.NULL_INPUT) {
        String sessionName = subAction.getSessionProxy().getStringValue();
        // TODO support user-by-user locales
        PentahoSessionParameterProvider params = new PentahoSessionParameterProvider(session);
        session = new UserSession(sessionName, LocaleHelper.getLocale(), params);
    }
    // create a parameter provider
    HashMap<String, Object> parameters = new HashMap<String, Object>();
    Iterator<?> iterator = getInputNames().iterator();
    while (iterator.hasNext()) {
        String inputName = (String) iterator.next();
        if (!StandardSettings.SOLUTION.equals(inputName) && !StandardSettings.PATH.equals(inputName) && !StandardSettings.ACTION.equals(inputName)) {
            Object value = getInputValue(inputName);
            ignoreParameters.add(value);
            parameters.put(inputName, value);
        }
    }
    parameters.put(StandardSettings.ACTION_URL_COMPONENT, getInputStringValue(StandardSettings.ACTION_URL_COMPONENT));
    // get the ouptut stream
    // TODO verify this with MB and JD
    // getDefaultOutputStream();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ISolutionEngine solutionEngine = null;
    try {
        solutionEngine = SolutionHelper.execute(getProcessId(), session, actionPath, parameters, outputStream, null, true, false);
        if (outputStream.size() > 0) {
            getDefaultOutputStream(null).write(outputStream.toByteArray());
        }
        int status = solutionEngine.getStatus();
        if (status == IRuntimeContext.RUNTIME_STATUS_SUCCESS) {
            // now pass any outputs back
            Iterator<?> it = this.getOutputNames().iterator();
            while (it.hasNext()) {
                String outputName = (String) it.next();
                IActionParameter param = solutionEngine.getExecutionContext().getOutputParameter(outputName);
                if (param != null) {
                    setOutputValue(outputName, param.getValue());
                    ignoreParameters.add(param.getValue());
                }
            }
            return true;
        } else {
            return false;
        }
    } finally {
        if (solutionEngine != null) {
            solutionEngine.getExecutionContext().dispose(ignoreParameters);
        }
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) PentahoSessionParameterProvider(org.pentaho.platform.engine.core.solution.PentahoSessionParameterProvider) HashMap(java.util.HashMap) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UserSession(org.pentaho.platform.engine.core.system.UserSession) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) SubActionAction(org.pentaho.actionsequence.dom.actions.SubActionAction)

Aggregations

ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)37 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)27 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)25 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)22 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)13 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)11 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)9 IPentahoUrlFactory (org.pentaho.platform.api.engine.IPentahoUrlFactory)9 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SimpleParameterProvider (org.pentaho.platform.engine.core.solution.SimpleParameterProvider)5 OutputStream (java.io.OutputStream)4 Iterator (java.util.Iterator)4 Map (java.util.Map)4 File (java.io.File)3 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 Reader (java.io.Reader)3 BigDecimal (java.math.BigDecimal)3