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);
}
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();
}
}
}
}
}
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);
}
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");
}
}
}
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());
}
Aggregations