Search in sources :

Example 1 with IRuntimeContext

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

the class PentahoSystem method globalStartup.

public static void globalStartup(final IPentahoSession session) {
    // getGlobalStartupActions doesn't pay any attention to session class
    List<ISessionStartupAction> globalStartupActions = PentahoSystem.getGlobalStartupActions();
    if (globalStartupActions == null) {
        // nothing to do...
        return;
    }
    boolean doGlobals = PentahoSystem.globalAttributes.size() == 0;
    // see if this has been done already
    if (!doGlobals) {
        return;
    }
    if (globalStartupActions != null) {
        for (ISessionStartupAction globalStartupAction : globalStartupActions) {
            // 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();
            IPentahoUrlFactory urlFactory = new SimpleUrlFactory(baseUrl);
            ArrayList messages = new ArrayList();
            IRuntimeContext context = null;
            try {
                context = solutionEngine.execute(globalStartupAction.getActionPath(), "Global 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) {
                            PentahoSystem.globalAttributes.remove(attributeName);
                            PentahoSystem.globalAttributes.put(attributeName, data);
                        }
                    }
                }
            } catch (Throwable th) {
                Logger.warn(PentahoSystem.class.getName(), Messages.getInstance().getString("PentahoSystem.WARN_UNABLE_TO_EXECUTE_GLOBAL_ACTION", th.getLocalizedMessage()), // $NON-NLS-1$
                th);
            } finally {
                if (context != null) {
                    context.dispose();
                }
            }
        }
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) HashMap(java.util.HashMap) 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 2 with IRuntimeContext

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

the class XActionUtilTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws ObjectFactoryException {
    MockitoAnnotations.initMocks(this);
    Map<String, String[]> map = mock(Map.class);
    when(httpServletRequest.getParameterMap()).thenReturn(map);
    when(httpServletRequest.getParameter(anyString())).thenReturn(null);
    when(repository.getFile(anyString())).thenReturn(generatedFile);
    List<IContentItem> items = Arrays.asList(mock(RepositoryFileContentItem.class));
    IRuntimeContext context = mock(IRuntimeContext.class);
    when(context.getOutputContentItems()).thenReturn(items);
    when(engine.execute(anyString(), anyString(), anyBoolean(), anyBoolean(), anyString(), anyBoolean(), anyMap(), any(IOutputHandler.class), any(IActionCompleteListener.class), any(IPentahoUrlFactory.class), anyList())).thenReturn(context);
    pentahoObjectFactoryUnified = mock(IPentahoObjectFactory.class);
    when(pentahoObjectFactoryUnified.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactoryUnified.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if (IUnifiedRepository.class.toString().equals(invocation.getArguments()[0].toString())) {
                return repository;
            } else if (ISolutionEngine.class.toString().equals(invocation.getArguments()[0].toString())) {
                return engine;
            } else if (IMessageFormatter.class.toString().equals(invocation.getArguments()[0].toString())) {
                return formatter;
            } else if (IAuthorizationPolicy.class.toString().equals(invocation.getArguments()[0].toString())) {
                return authPolicy;
            } else {
                return null;
            }
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactoryUnified);
    ISystemSettings systemSettingsService = mock(ISystemSettings.class);
    when(systemSettingsService.getSystemSetting(anyString(), anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0].toString();
        }
    });
    PentahoSystem.setSystemSettingsService(systemSettingsService);
    PentahoSessionHolder.setStrategyName(PentahoSessionHolder.MODE_INHERITABLETHREADLOCAL);
    PentahoSessionHolder.setSession(userSession);
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFileContentItem(org.pentaho.platform.repository2.unified.fileio.RepositoryFileContentItem) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IContentItem(org.pentaho.platform.api.repository.IContentItem) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionCompleteListener(org.pentaho.platform.api.engine.IActionCompleteListener) Before(org.junit.Before)

Example 3 with IRuntimeContext

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

the class ActionSequenceAction method execute.

public void execute() throws Exception {
    IOutputHandler outputHandler = null;
    if (xactionResultsOutputStream instanceof RepositoryFileOutputStream) {
        outputHandler = new RepositoryFileOutputHandler(((RepositoryFileOutputStream) xactionResultsOutputStream));
    } else {
        outputHandler = new SimpleOutputHandler(xactionResultsOutputStream, false);
    }
    IRuntimeContext rt = null;
    try {
        ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, null);
        solutionEngine.setCreateFeedbackParameterCallback(null);
        solutionEngine.setLoggingLevel(ILogger.DEBUG);
        solutionEngine.setForcePrompt(false);
        ArrayList messages = new ArrayList();
        HashMap<String, Object> parameterProviders = new HashMap<String, Object>();
        parameterProviders.put(IParameterProvider.SCOPE_REQUEST, new SimpleParameterProvider(xActionInputParams));
        parameterProviders.put(IParameterProvider.SCOPE_SESSION, new PentahoSessionParameterProvider(PentahoSessionHolder.getSession()));
        String xactionPath = null;
        if (xactionDefInputStream instanceof RepositoryFileInputStream) {
            xactionPath = ((RepositoryFileInputStream) xactionDefInputStream).getFile().getPath();
        }
        rt = solutionEngine.execute(xactionPath, this.getClass().getName(), false, true, null, true, parameterProviders, outputHandler, null, null, messages);
        if (!outputHandler.contentDone()) {
            if ((rt != null) && (rt.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
                // set content which generated by sequence for pass it to caller
                List<IContentItem> components = new ArrayList<IContentItem>(rt.getOutputContentItems());
                setActionOutputContents(components);
                boolean isFlushed = false;
                boolean isEmpty;
                if (xactionResultsOutputStream instanceof RepositoryFileOutputStream) {
                    RepositoryFileOutputStream repositoryFileOutputStream = (RepositoryFileOutputStream) xactionResultsOutputStream;
                    isFlushed = repositoryFileOutputStream.isFlushed();
                    isEmpty = repositoryFileOutputStream.size() > 0 ? false : true;
                    String extension = RepositoryFilenameUtils.getExtension(repositoryFileOutputStream.getFilePath());
                    String mimeTypeFromExtension = MimeHelper.getMimeTypeFromExtension("." + extension);
                    if (mimeTypeFromExtension == null) {
                        // unknown type, treat it not as an extension but part of the name
                        extension = "";
                    }
                    if (extension.isEmpty() && xactionResultsOutputStream.toString().isEmpty()) {
                        repositoryFileOutputStream.setFilePath(repositoryFileOutputStream.getFilePath() + ".html");
                    }
                } else {
                    isEmpty = xactionResultsOutputStream.toString().isEmpty();
                }
                if (!isFlushed) {
                    if (isEmpty) {
                        StringBuffer buffer = new StringBuffer();
                        // $NON-NLS-1$
                        MessageFormatUtils.formatSuccessMessage("text/html", rt, buffer, false);
                        xactionResultsOutputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
                    }
                    xactionResultsOutputStream.close();
                }
            } else {
                // we need an error message...
                StringBuffer buffer = new StringBuffer();
                // $NON-NLS-1$
                MessageFormatUtils.formatFailureMessage("text/html", rt, buffer, messages);
                xactionResultsOutputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
                xactionResultsOutputStream.close();
            }
        }
    } finally {
        if (rt != null) {
            rt.dispose();
        }
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) RepositoryFileOutputHandler(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputHandler) HashMap(java.util.HashMap) PentahoSessionParameterProvider(org.pentaho.platform.engine.core.solution.PentahoSessionParameterProvider) ArrayList(java.util.ArrayList) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) RepositoryFileOutputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileOutputStream) IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) IContentItem(org.pentaho.platform.api.repository.IContentItem) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider)

Example 4 with IRuntimeContext

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

the class IPreparedComponentIT method testIPreparedComponentSQLAvailable.

public void testIPreparedComponentSQLAvailable() {
    startTest();
    // $NON-NLS-1$
    info("Expected: Successful execution with object available");
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    IRuntimeContext context = run("/test/ipreparedcomponents/ipreparedcomponent_sql_available.xaction");
    assertEquals(Messages.getInstance().getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS, // $NON-NLS-1$
    context.getStatus());
    // $NON-NLS-1$
    IActionParameter rtn = context.getOutputParameter("prepared_component");
    assertNotNull(rtn);
    IPreparedComponent preparedComponent = (IPreparedComponent) rtn.getValue();
    assertNotNull(preparedComponent);
    finishTest();
}
Also used : IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) IPreparedComponent(org.pentaho.platform.api.data.IPreparedComponent)

Example 5 with IRuntimeContext

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

the class IPreparedComponentIT method testIPreparedComponentXQueryShareConnection.

public void testIPreparedComponentXQueryShareConnection() {
    startTest();
    // $NON-NLS-1$
    info("Expected: Successful execution with object available");
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    IRuntimeContext context = run("/test/ipreparedcomponents/ipreparedcomponent_xquery_shareconn.xaction");
    assertEquals(Messages.getInstance().getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS, // $NON-NLS-1$
    context.getStatus());
    // $NON-NLS-1$
    IActionParameter rtn1 = context.getOutputParameter("prepared_component");
    assertNotNull(rtn1);
    IPreparedComponent preparedComponent1 = (IPreparedComponent) rtn1.getValue();
    assertNotNull(preparedComponent1);
    IPentahoResultSet resultset1 = preparedComponent1.executePrepared(null);
    assertTrue(resultset1.getRowCount() >= 1);
    Object val1 = resultset1.getValueAt(0, 0);
    assertNotNull(val1);
    finishTest();
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) IPreparedComponent(org.pentaho.platform.api.data.IPreparedComponent)

Aggregations

IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)100 ArrayList (java.util.ArrayList)28 HashMap (java.util.HashMap)28 ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)28 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)24 SimpleParameterProvider (org.pentaho.platform.engine.core.solution.SimpleParameterProvider)21 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)20 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)17 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)14 OutputStream (java.io.OutputStream)12 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)12 List (java.util.List)10 IPentahoUrlFactory (org.pentaho.platform.api.engine.IPentahoUrlFactory)9 IOException (java.io.IOException)8 Map (java.util.Map)8 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOutputHandler (org.pentaho.platform.api.engine.IOutputHandler)7 IParameterProvider (org.pentaho.platform.api.engine.IParameterProvider)6 Document (org.dom4j.Document)5