private WebApplication bindApplication() { // find wicket application and bind it to thread WebApplication application = (WebApplication)Application.get(filterName); if (application == null) { throw new IllegalStateException("Could not find wicket application mapped to filter: " + filterName + ". Make sure you set filterName attribute to the name of the wicket filter " + "for the wicket application whose session you want to access."); } ThreadContext.setApplication(application); return application; }
private WebApplication bindApplication() { // find wicket application and bind it to thread WebApplication application = (WebApplication)Application.get(filterName); if (application == null) { throw new IllegalStateException("Could not find wicket application mapped to filter: " + filterName + ". Make sure you set filterName attribute to the name of the wicket filter " + "for the wicket application whose session you want to access."); } ThreadContext.setApplication(application); return application; }
/** * Destroys the tester. Restores {@link ThreadContext} to state before instance of * {@link WicketTester} was created. */ public void destroy() { try { ThreadContext.setApplication(application); application.internalDestroy(); } finally { ThreadContext.detach(); } }
/** * Get the result from the given supplier inside a bound {@link ThreadContext}. * * @param supplier * supplier * @return result of {@link Supplier#get()} */ private <T> T inThreadContext(Supplier<T> supplier) { ThreadContext oldContext = ThreadContext.detach(); try { ThreadContext.setApplication(application); return supplier.get(); } finally { ThreadContext.restore(oldContext); } }
/** * Get the result from the given supplier inside a bound {@link ThreadContext}. * * @param supplier * supplier * @return result of {@link Supplier#get()} */ private <T> T inThreadContext(Supplier<T> supplier) { ThreadContext oldContext = ThreadContext.detach(); try { ThreadContext.setApplication(application); return supplier.get(); } finally { ThreadContext.restore(oldContext); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { boolean existed = ThreadContext.exists(); if (existed == false) { ThreadContext.setApplication(application); } try { return method.invoke(o, args); } finally { if (existed == false) { ThreadContext.detach(); } } } });
/** * Destroys the tester. Restores {@link ThreadContext} to state before instance of * {@link WicketTester} was created. */ public void destroy() { try { ThreadContext.setApplication(application); application.internalDestroy(); } finally { ThreadContext.detach(); } }
@Override public String call() throws Exception { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { final ConnInstanceTO connector = connectorRestClient.read(key); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", connectorRestClient.check(connector).getLeft() ? TopologyNode.Status.REACHABLE : TopologyNode.Status.UNREACHABLE, key); } catch (Exception e) { LOG.warn("Error checking connection for {}", key, e); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", TopologyNode.Status.FAILURE, key); } finally { ThreadContext.detach(); } } }
@Override public String call() throws Exception { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { final ResourceTO resource = resourceRestClient.read(key); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", resourceRestClient.check(resource).getLeft() ? TopologyNode.Status.REACHABLE : TopologyNode.Status.UNREACHABLE, key); } catch (Exception e) { LOG.warn("Error checking connection for {}", key, e); return String.format("{ \"status\": \"%s\", \"target\": \"%s\"}", TopologyNode.Status.FAILURE, key); } finally { ThreadContext.detach(); } } }
private void executeInNewThread(Runnable runnable) { OrienteerWebSession session = OrienteerWebSession.get(); OrienteerWebApplication app = OrienteerWebApplication.get(); RequestCycle requestCycle = RequestCycle.get(); new Thread(() -> { ThreadContext.setSession(session); ThreadContext.setApplication(app); ThreadContext.setRequestCycle(requestCycle); runnable.run(); }).start(); }
private void postToSingleResource(Object payload, AtmosphereResource resource) { AtmosphereEvent event = new AtmosphereEvent(payload, resource); ThreadContext.detach(); ThreadContext.setApplication(application); PageKey key; Collection<EventSubscription> subscriptionsForPage; synchronized (this) { key = trackedPages.get(resource.uuid()); subscriptionsForPage = Collections2.filter( Collections.unmodifiableCollection(subscriptions.get(key)), new EventFilter(event)); } if (key == null) broadcaster.removeAtmosphereResource(resource); else if (!subscriptionsForPage.isEmpty()) post(resource, key, subscriptionsForPage, event); }
@Override public void shutdown() { /* remove our bundle */ BundleUtil.get().removeBundle("HomeConnect"); if (context.getState() != ContextState.STOPPING) { WebApplication wicket = context.getWebApplication(); Application currentApplication = ThreadContext.getApplication(); ThreadContext.setApplication(wicket); try { wicket.unmount("ack/${id}"); } finally { ThreadContext.setApplication(currentApplication); } } }
/** {@inheritDoc} */ @Override public final PageMounter addingService(ServiceReference<PageMounter> reference) { PageMounter mounter = super.addingService(reference); List<? extends MountPointInfo> infos = mounter.getMountPoints(); for (MountPointInfo info : infos) { LOGGER.trace("Make sure that path {} is clear before trying to remount", info.getPath()); Application oldApp = ThreadContext.getApplication(); ThreadContext.setApplication(application); try { application.unmount(info.getPath()); } catch (IllegalArgumentException e) { LOGGER.trace("Unmounting not possible since nothing here by now."); // this could happen if wicket had not been started at all by now --> simply ignore } LOGGER.trace("Trying to mount {} with {}", info.getPath(), info.getPage().getName()); application.mountPage(info.getPath(), info.getPage()); ThreadContext.setApplication(oldApp); LOGGER.info("Mounted {} with {}", info.getPath(), info.getPage().getName()); } return mounter; }
@Override public CompletableFuture<Void> fetchMailsAsync(OMailSettings settings, String folderName, Consumer<Message> consumer) { OrienteerWebSession session = OrienteerWebSession.get(); OrienteerWebApplication app = OrienteerWebApplication.get(); RequestCycle requestCycle = RequestCycle.get(); return CompletableFuture.runAsync(() -> { ThreadContext.setSession(session); ThreadContext.setApplication(app); ThreadContext.setRequestCycle(requestCycle); try { fetchMails(settings, folderName, consumer); } catch (Exception ex) { LOG.error("Error during fetching mails: {}", settings, ex); } }); }
@Override public void run() { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { timeoutHandlingConnectionChecker( new ResourceChecker(key, this.application), resourceTestTimeout, resources, runningResCheck); } finally { ThreadContext.detach(); } } }
@Override public void run() { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { timeoutHandlingConnectionChecker( new ConnectorChecker(key, this.application), connectorTestTimeout, connectors, runningConnCheck); } finally { ThreadContext.detach(); } } }
private void performTask(OSendMailTaskSessionRuntime runtime) { OrienteerWebSession session = OrienteerWebSession.get(); OrienteerWebApplication app = OrienteerWebApplication.get(); RequestCycle requestCycle = RequestCycle.get(); new Thread(() -> { ThreadContext.setSession(session); ThreadContext.setApplication(app); ThreadContext.setRequestCycle(requestCycle); DBClosure.sudoConsumer(db -> { try { sendMails(runtime); } catch (Exception ex) { LOG.error("Error occurred during perform task {}", OSendMailTask.this, ex); } finally { runtime.finish(); } }); }).start(); }
/** {@inheritDoc} */ @Override public final void removedService(ServiceReference<PageMounter> reference, PageMounter mounter) { PageMounter pageMounter = mounter; List<? extends MountPointInfo> infos = pageMounter.getMountPoints(); for (MountPointInfo info : infos) { LOGGER.trace("Trying to mount {} with {}", info.getPath(), info.getPage().getName()); Application oldApp = ThreadContext.getApplication(); ThreadContext.setApplication(application); if (!Session.exists()) { Request request = new MockWebRequest(Url.parse(info.getPath())); ThreadContext.setSession(new WebSession(request)); } application.unmount(info.getPath()); ThreadContext.setApplication(oldApp); LOGGER.info("Unmounted {} with {}", info.getPath(), info.getPage().getName()); } super.removedService(reference, pageMounter); } }
@Override public void onOpen(Session session, EndpointConfig endpointConfig) { String appName = getApplicationName(session); WebApplication app = (WebApplication) WebApplication.get(appName); app.getApplicationListeners().add(new ApplicationListener(applicationDestroyed)); try { ThreadContext.setApplication(app); javaxWebSocketProcessor = new JavaxWebSocketProcessor(session, app, endpointConfig); } finally { ThreadContext.detach(); } }
@Override public void onOpen(Session session, EndpointConfig endpointConfig) { String appName = getApplicationName(session); WebApplication app = (WebApplication) WebApplication.get(appName); app.getApplicationListeners().add(new ApplicationListener(applicationDestroyed)); try { ThreadContext.setApplication(app); javaxWebSocketProcessor = new JavaxWebSocketProcessor(session, app, endpointConfig); } finally { ThreadContext.detach(); } }