Search in sources :

Example 1 with ISessionStartupAction

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

the class GlobalListPublisherTest method test1.

public void test1() throws ObjectFactoryException {
    StandaloneSession session = new StandaloneSession("test");
    StandaloneApplicationContext appContext = new StandaloneApplicationContext("src/test/resources/solution", "");
    StandaloneSpringPentahoObjectFactory factory = new StandaloneSpringPentahoObjectFactory();
    factory.init("src/test/resources/solution/system/pentahoObjects.GlobalListPublisherTest.spring.xml", null);
    PentahoSystem.init(appContext);
    PentahoSystem.registerObjectFactory(factory);
    PentahoSystem.setSystemSettingsService(factory.get(ISystemSettings.class, "systemSettingsService", session));
    List<ISessionStartupAction> actions = new ArrayList<ISessionStartupAction>();
    SessionStartupAction startupAction1 = new SessionStartupAction();
    startupAction1.setSessionType(PentahoSystem.SCOPE_GLOBAL);
    startupAction1.setActionPath("testsolution/testpath/test.xaction");
    startupAction1.setActionOutputScope(PentahoSystem.SCOPE_GLOBAL);
    actions.add(startupAction1);
    TestRuntimeContext context = new TestRuntimeContext();
    context.status = IRuntimeContext.RUNTIME_STATUS_SUCCESS;
    TestSolutionEngine engine = PentahoSystem.get(TestSolutionEngine.class, "ISolutionEngine", session);
    engine.testRuntime = context;
    Map<String, IActionParameter> outputs = new HashMap<String, IActionParameter>();
    TestActionParameter param = new TestActionParameter();
    param.setValue("testvalue");
    outputs.put("testoutput", param);
    context.outputParameters = outputs;
    engine.executeCount = 0;
    GlobalListsPublisher globals = new GlobalListsPublisher();
    assertEquals(Messages.getInstance().getString("GlobalListsPublisher.USER_SYSTEM_SETTINGS"), globals.getName());
    assertEquals(Messages.getInstance().getString("GlobalListsPublisher.USER_DESCRIPTION"), globals.getDescription());
    assertTrue(!globals.getName().startsWith("!"));
    assertTrue(!globals.getDescription().startsWith("!"));
    assertNotNull(globals.getLogger());
    String resultMsg = globals.publish(session);
    assertEquals(Messages.getInstance().getString("GlobalListsPublisher.USER_SYSTEM_SETTINGS_UPDATED"), resultMsg);
    assertEquals(0, engine.executeCount);
    PentahoSystem.setSessionStartupActions(actions);
    IParameterProvider globalParams = PentahoSystem.getGlobalParameters();
    resultMsg = globals.publish(session);
    assertEquals(1, engine.executeCount);
    assertEquals(Messages.getInstance().getString("GlobalListsPublisher.USER_SYSTEM_SETTINGS_UPDATED"), resultMsg);
    // check that we made it all the way to executing the startup action
    assertEquals(session, engine.initSession);
    assertEquals(startupAction1.getActionPath(), engine.actionPath);
    assertEquals("testvalue", globalParams.getParameter("testoutput"));
    param.setValue("testvalue2");
    resultMsg = globals.publish(session);
    assertEquals(Messages.getInstance().getString("GlobalListsPublisher.USER_SYSTEM_SETTINGS_UPDATED"), resultMsg);
    assertEquals(2, engine.executeCount);
    assertNotNull(globalParams);
    assertEquals("testvalue2", globalParams.getParameter("testoutput"));
    assertEquals(2, engine.executeCount);
}
Also used : ISessionStartupAction(org.pentaho.platform.api.engine.ISessionStartupAction) SessionStartupAction(org.pentaho.platform.engine.core.system.SessionStartupAction) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StandaloneApplicationContext(org.pentaho.platform.engine.core.system.StandaloneApplicationContext) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) GlobalListsPublisher(org.pentaho.platform.engine.core.system.GlobalListsPublisher) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) ISessionStartupAction(org.pentaho.platform.api.engine.ISessionStartupAction) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) StandaloneSpringPentahoObjectFactory(org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)

Example 2 with ISessionStartupAction

use of org.pentaho.platform.api.engine.ISessionStartupAction 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 3 with ISessionStartupAction

use of org.pentaho.platform.api.engine.ISessionStartupAction 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(invocation -> 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(Collections.singletonList(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) ISessionStartupAction(org.pentaho.platform.api.engine.ISessionStartupAction) Test(org.junit.Test)

Example 4 with ISessionStartupAction

use of org.pentaho.platform.api.engine.ISessionStartupAction 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 5 with ISessionStartupAction

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

the class BootTest method testBootActions.

@Test
public void testBootActions() throws Exception {
    PentahoSystemBoot boot = new PentahoSystemBoot();
    boot.setFilePath("src/test/resources/solution");
    boot.define(ISolutionEngine.class.getSimpleName(), Object1.class.getName(), IPentahoDefinableObjectFactory.Scope.GLOBAL);
    TestStartupAction startupAction1 = new TestStartupAction();
    TestStartupAction startupAction2 = new TestStartupAction();
    boot.addStartupAction(startupAction1);
    List<ISessionStartupAction> startupActions1 = boot.getStartupActions();
    assertEquals(1, startupActions1.size());
    assertEquals(startupAction1, startupActions1.get(0));
    List<ISessionStartupAction> startupActions2 = new ArrayList<ISessionStartupAction>();
    startupActions2.add(startupAction2);
    boot.setStartupActions(startupActions2);
    List<ISessionStartupAction> startupActions3 = boot.getStartupActions();
    assertEquals(1, startupActions3.size());
    assertEquals(startupAction2, startupActions3.get(0));
    assertEquals(startupActions2, startupActions3);
    IPentahoObjectFactory factory = boot.getFactory();
    assertNotNull("object factory is null", factory);
    assertTrue("object factory not definable", factory instanceof IPentahoDefinableObjectFactory);
    assertFalse(boot.isInitialized());
    boolean ok = boot.start();
    assertNull(boot.getSettingsProvider());
    assertTrue(boot.isInitialized());
    assertTrue(ok);
    boot.stop();
    assertFalse(boot.isInitialized());
}
Also used : PentahoSystemBoot(org.pentaho.platform.engine.core.system.boot.PentahoSystemBoot) IPentahoDefinableObjectFactory(org.pentaho.platform.api.engine.IPentahoDefinableObjectFactory) ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) ArrayList(java.util.ArrayList) ISessionStartupAction(org.pentaho.platform.api.engine.ISessionStartupAction) Test(org.junit.Test)

Aggregations

ISessionStartupAction (org.pentaho.platform.api.engine.ISessionStartupAction)5 ArrayList (java.util.ArrayList)4 ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)4 HashMap (java.util.HashMap)3 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)3 Iterator (java.util.Iterator)2 ListIterator (java.util.ListIterator)2 Test (org.junit.Test)2 IPentahoObjectFactory (org.pentaho.platform.api.engine.IPentahoObjectFactory)2 IPentahoUrlFactory (org.pentaho.platform.api.engine.IPentahoUrlFactory)2 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)2 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)2 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)2 IParameterProvider (org.pentaho.platform.api.engine.IParameterProvider)1 IPentahoDefinableObjectFactory (org.pentaho.platform.api.engine.IPentahoDefinableObjectFactory)1 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)1 ISystemSettings (org.pentaho.platform.api.engine.ISystemSettings)1 PentahoSessionParameterProvider (org.pentaho.platform.engine.core.solution.PentahoSessionParameterProvider)1 GlobalListsPublisher (org.pentaho.platform.engine.core.system.GlobalListsPublisher)1 SessionStartupAction (org.pentaho.platform.engine.core.system.SessionStartupAction)1