/** * 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 void run() { ThreadContext.setApplication(application); ThreadContext.setSession(session); try { timeoutHandlingConnectionChecker( new ResourceChecker(key, this.application), resourceTestTimeout, resources, runningResCheck); } finally { ThreadContext.detach(); } } }
/** * Checks if the <code>Application</code> threadlocal is set in this thread * * @return true if {@link Application#get()} can return the instance of application, false * otherwise */ public static boolean exists() { return ThreadContext.getApplication() != null; }
/** * 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(); } } } });
/** * unset request and session to force mocking */ public static <V> V withNewRequest(WebApplication webApplication, IMockRequestCallback<V> callback) { ThreadContext oldContext = ThreadContext.detach(); try { return withRequest(webApplication, callback); } finally { ThreadContext.restore(oldContext); } }
Application oldApplication = ThreadContext.getApplication(); Session oldSession = ThreadContext.getSession(); RequestCycle oldRequestCycle = ThreadContext.getRequestCycle(); ThreadContext.setRequestCycle(requestCycle); ThreadContext.setApplication(application); ThreadContext.setSession(session); ThreadContext.setApplication(oldApplication); ThreadContext.setRequestCycle(oldRequestCycle); ThreadContext.setSession(oldSession);
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(); }
Session oldSession = ThreadContext.exists() ? ThreadContext.getSession() : null; ThreadContext oldContext = ThreadContext.detach(); ThreadContext.setApplication(webApplication); ThreadContext.setSession(oldSession); ThreadContext.setRequestCycle(webApplication.createRequestCycle(request, response)); Session newSession = ThreadContext.getSession(); ThreadContext.restore(oldContext); if (oldSession == null && newSession != null && !newSession.isTemporary()) { ThreadContext.setSession(newSession);
/** {@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); } }
private void internalUmount(String path){ //workaround so wicket doesn't choke because the thread context isn't filled (wrong thread) Application application = ThreadContext.getApplication(); if (application == null) ThreadContext.setApplication(JabylonApplication.this); if(ThreadContext.getSession()==null) ThreadContext.setSession(new WebSession(createFakeRequest(null))); // unmount(path); /* * umount seems to be greedy, e.g. a prefix match is enough. * That's troublesome because umount /settings/log will also umount /settings */ ICompoundRequestMapper rootRequestMapperAsCompound = getRootRequestMapperAsCompound(); if (rootRequestMapperAsCompound instanceof CompoundRequestMapper) { CompoundRequestMapper compound = (CompoundRequestMapper) rootRequestMapperAsCompound; Iterator<IRequestMapper> it = compound.iterator(); while (it.hasNext()) { IRequestMapper iRequestMapper = it.next(); if (iRequestMapper instanceof ResouceAwareMountedMapper) { ResouceAwareMountedMapper mapper = (ResouceAwareMountedMapper) iRequestMapper; if(path.equals(mapper.getMountPath())) { logger.info("Unmounting {}",path); getRootRequestMapperAsCompound().remove(mapper); } } } } }
@Override public Object deserialize(final byte[] data) ThreadContext old = ThreadContext.get(false); final ByteArrayInputStream in = new ByteArrayInputStream(data); ObjectInputStream ois = null; try Application oldApplication = ThreadContext.getApplication(); try if (app != null) ThreadContext.setApplication(app); ThreadContext.setApplication(oldApplication); IOUtils.close(ois); ThreadContext.restore(old);
@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); } } }
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; }
@Override public Object addingService(ServiceReference ref) { PageProvider service = (PageProvider)bundleContext.getService(ref); Object pathObject = ref.getProperty(PageProvider.MOUNT_PATH_PROPERTY); if (pathObject instanceof String) { String path = (String) pathObject; Class pageClass = service.getPageClass(); if(pageClass==ResourcePage.class) { //workaround so wicket doesn't choke because the thread context isn't filled (wrong thread) ThreadContext.setApplication(JabylonApplication.this); ThreadContext.setSession(new WebSession(createFakeRequest(null))); //if the main page is ready, we can mount the rest of the pages initMainPages(); } logger.info("Mounting new page {} at {}", pageClass, path); //$NON-NLS-1$ mount(new ResouceAwareMountedMapper(path, pageClass)); } else { logger.warn("Ignored Page {} because it was registered with invalid path property '{}'", service, pathObject); //$NON-NLS-1$ } return service; }
/** * */ private void newSession() { ThreadContext.setSession(null); // the following will create a new session and put it in the thread context session = Session.get(); }
/** * * @param requestCycle */ private static void set(RequestCycle requestCycle) { ThreadContext.setRequestCycle(requestCycle); }
/** * Checks existence of a <code>Session</code> associated with the current thread. * * @return {@code true} if {@link Session#get()} can return the instance of session, * {@code false} otherwise */ public static boolean exists() { Session session = ThreadContext.getSession(); if (session == null) { // no session is available via ThreadContext, so lookup in session store RequestCycle requestCycle = RequestCycle.get(); if (requestCycle != null) { session = Application.get().getSessionStore().lookup(requestCycle.getRequest()); if (session != null) { ThreadContext.setSession(session); } } } return session != null; }
/** * Returns request cycle associated with current thread. * * @return request cycle instance or <code>null</code> if no request cycle is associated with * current thread. */ public static RequestCycle get() { return ThreadContext.getRequestCycle(); }
private void cleanupBoundApplicationAndSession() { ThreadContext.detach(); }