use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class ClientExceptionHandler method showExceptionInternal.
protected void showExceptionInternal(final Throwable t) {
final IClientSession session = ClientSessionProvider.currentSession();
if (session == null) {
return;
}
if (session.getDesktop() == null || !session.getDesktop().isOpened()) {
return;
}
// Prevent loops while displaying the exception.
final Semaphore loopDetectionSemaphore = getLoopDetectionSemaphore(session);
if (loopDetectionSemaphore.tryAcquire()) {
try {
// Synchronize with the model thread if not applicable.
if (ModelJobs.isModelThread()) {
showException(t);
} else {
try {
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
showException(t);
}
}, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withExceptionHandling(null, true).withName("Visualizing PlatformException")).awaitDone();
} catch (final ThreadInterruptedError e) {
// NOSONAR
// NOOP
}
}
} finally {
loopDetectionSemaphore.release();
}
} else {
Exception e = new Exception("Stacktrace and suppressed exception");
// add original exception for analysis
e.addSuppressed(t);
LOG.warn("Loop detection in {}", getClass().getName(), e);
if (ModelJobs.isModelThread()) {
IMessageBox msgBox = MessageBoxes.createOk().withSeverity(IStatus.ERROR).withHeader(TEXTS.get("Error"));
if (t instanceof VetoException) {
IProcessingStatus status = ((VetoException) t).getStatus();
msgBox.withHeader(status.getTitle()).withBody(status.getBody());
}
msgBox.show();
}
}
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class ClientRunContextStatement method evaluateWithClientRunContext.
private void evaluateWithClientRunContext() throws Throwable {
final Subject currentSubject = Subject.getSubject(AccessController.getContext());
if (currentSubject == null) {
Assertions.fail("Subject must not be null. Use the annotation '{}' to execute your test under a particular user. ", RunWithSubject.class.getSimpleName());
}
Class<? extends ISession> sessionClass = m_clientSessionAnnotation.value();
IBean<? extends ISession> sessionBean = BEANS.getBeanManager().uniqueBean(sessionClass);
if (sessionBean != null) {
sessionClass = sessionBean.getBeanClazz();
}
final IBean clientSessionBean = BEANS.getBeanManager().registerBean(new BeanMetaData(sessionClass).withOrder(-Long.MAX_VALUE));
try {
// Obtain the client session for the given subject. Depending on the session provider, a new session is created or a cached session returned.
final IClientSession clientSession = BEANS.get(m_clientSessionAnnotation.provider()).provide(ClientRunContexts.copyCurrent().withSubject(currentSubject));
// Run the test on behalf of a ClientRunContext.
final SafeStatementInvoker invoker = new SafeStatementInvoker(m_next);
ClientRunContexts.copyCurrent().withSession(clientSession, true).withSubject(// set the test subject explicitly in case it is different to the session subject
currentSubject).run(invoker);
invoker.throwOnError();
} finally {
BEANS.getBeanManager().unregisterBean(clientSessionBean);
}
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class ClientRunContextTest method testRunContextsCopyCurrent.
@Test
public void testRunContextsCopyCurrent() {
final IClientSession session = mock(IClientSession.class);
final UserAgent sessionUserAgent = UserAgents.create().build();
final Locale sessionLocale = Locale.CANADA_FRENCH;
final Subject sessionSubject = new Subject();
final IDesktop sessionDesktop = mock(IDesktop.class);
when(session.getUserAgent()).thenReturn(sessionUserAgent);
when(session.getLocale()).thenReturn(sessionLocale);
when(session.getSubject()).thenReturn(sessionSubject);
when(session.getDesktopElseVirtualDesktop()).thenReturn(sessionDesktop);
ClientRunContexts.empty().withSession(session, true).run(new IRunnable() {
@Override
public void run() throws Exception {
RunContext runContext = RunContexts.copyCurrent();
assertThat(runContext, CoreMatchers.instanceOf(ClientRunContext.class));
ClientRunContext clientCtx = (ClientRunContext) runContext;
assertSame(session, clientCtx.getSession());
assertSame(sessionLocale, clientCtx.getLocale());
assertSame(sessionUserAgent, clientCtx.getUserAgent());
assertSame(sessionDesktop, clientCtx.getDesktop());
}
});
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class ClientRunContextTest method testCurrentSessionAndDerivedValues.
@Test
public void testCurrentSessionAndDerivedValues() {
final IClientSession session = mock(IClientSession.class);
final UserAgent sessionUserAgent = UserAgents.create().build();
final Locale sessionLocale = Locale.CANADA_FRENCH;
final Subject sessionSubject = new Subject();
final IDesktop sessionDesktop = mock(IDesktop.class);
when(session.getUserAgent()).thenReturn(sessionUserAgent);
when(session.getLocale()).thenReturn(sessionLocale);
when(session.getSubject()).thenReturn(sessionSubject);
when(session.getDesktopElseVirtualDesktop()).thenReturn(sessionDesktop);
ClientRunContexts.empty().withSession(null, false).run(new IRunnable() {
@Override
public void run() throws Exception {
assertNull(ISession.CURRENT.get());
assertNull(NlsLocale.CURRENT.get());
assertNull(UserAgent.CURRENT.get());
assertNull(IDesktop.CURRENT.get());
assertNull(ClientRunContexts.copyCurrent().getSession());
assertNull(ClientRunContexts.copyCurrent().getLocale());
assertNull(ClientRunContexts.copyCurrent().getUserAgent());
assertNull(ClientRunContexts.copyCurrent().getDesktop());
}
});
ClientRunContexts.empty().withSession(session, false).run(new IRunnable() {
@Override
public void run() throws Exception {
assertSame(session, ISession.CURRENT.get());
assertNull(NlsLocale.CURRENT.get());
assertNull(UserAgent.CURRENT.get());
assertNull(IDesktop.CURRENT.get());
assertSame(session, ClientRunContexts.copyCurrent().getSession());
assertNull(ClientRunContexts.copyCurrent().getLocale());
assertNull(ClientRunContexts.copyCurrent().getUserAgent());
assertNull(ClientRunContexts.copyCurrent().getDesktop());
final UserAgent customUserAgent = UserAgents.create().build();
assertSame(customUserAgent, ClientRunContexts.copyCurrent().withUserAgent(customUserAgent).getUserAgent());
final Locale customLocale = Locale.ITALIAN;
assertSame(customLocale, ClientRunContexts.copyCurrent().withLocale(customLocale).getLocale());
}
});
ClientRunContexts.empty().withSession(session, true).run(new IRunnable() {
@Override
public void run() throws Exception {
assertSame(session, ISession.CURRENT.get());
assertSame(sessionLocale, NlsLocale.CURRENT.get());
assertSame(sessionUserAgent, UserAgent.CURRENT.get());
assertSame(sessionDesktop, IDesktop.CURRENT.get());
assertSame(session, ClientRunContexts.copyCurrent().getSession());
assertSame(sessionLocale, ClientRunContexts.copyCurrent().getLocale());
assertSame(sessionUserAgent, ClientRunContexts.copyCurrent().getUserAgent());
assertSame(sessionDesktop, ClientRunContexts.copyCurrent().getDesktop());
final UserAgent customUserAgent = UserAgents.create().build();
assertSame(customUserAgent, ClientRunContexts.copyCurrent().withUserAgent(customUserAgent).getUserAgent());
final Locale customLocale = Locale.ITALIAN;
assertSame(customLocale, ClientRunContexts.copyCurrent().withLocale(customLocale).getLocale());
final IDesktop customDesktop = mock(IDesktop.class);
assertSame(customDesktop, ClientRunContexts.copyCurrent().withDesktop(customDesktop).getDesktop());
}
});
}
use of org.eclipse.scout.rt.client.IClientSession in project scout.rt by eclipse.
the class ClientUIPreferences method getUserAgentPrefix.
protected String getUserAgentPrefix() {
UserAgent currentUserAgent = null;
if (m_prefs != null && m_prefs.userScope() instanceof IClientSession) {
currentUserAgent = ((IClientSession) m_prefs.userScope()).getUserAgent();
} else {
currentUserAgent = UserAgentUtility.getCurrentUserAgent();
}
if (currentUserAgent == null) {
return "";
}
String uiLayer = null;
if (!UiLayer.UNKNOWN.equals(currentUserAgent.getUiLayer())) {
uiLayer = currentUserAgent.getUiLayer().getIdentifier();
}
String uiDeviceType = null;
if (!UiDeviceType.UNKNOWN.equals(currentUserAgent.getUiDeviceType())) {
uiDeviceType = currentUserAgent.getUiDeviceType().getIdentifier();
}
return StringUtility.concatenateTokens(uiLayer, ".", uiDeviceType, ".");
}
Aggregations