Java Code Examples for org.apache.wicket.ThreadContext#setSession()

The following examples show how to use org.apache.wicket.ThreadContext#setSession() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadHelper.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
public static void startRunnable(Runnable r, String name) {
	final Application app = Application.get();
	final WebSession session = WebSession.get();
	final RequestCycle rc = RequestCycle.get();
	Thread t = new Thread(() -> {
		ThreadContext.setApplication(app);
		ThreadContext.setSession(session);
		ThreadContext.setRequestCycle(rc);
		r.run();
		ThreadContext.detach();
	});
	if (!Strings.isEmpty(name)) {
		t.setName(name);
	}
	t.start();
}
 
Example 2
Source File: TopologyWebSocketBehavior.java    From syncope with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 3
Source File: TopologyWebSocketBehavior.java    From syncope with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 4
Source File: ApplicationHelper.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
public static void destroyApplication() {
	WebApplication app = (WebApplication)Application.get(getWicketApplicationName());
	WebApplicationContext ctx = getWebApplicationContext(app.getServletContext());
	app.internalDestroy(); //need to be called too
	if (ctx != null) {
		((XmlWebApplicationContext)ctx).close();
	}
	ThreadContext.setApplication(null);
	ThreadContext.setRequestCycle(null);
	ThreadContext.setSession(null);
}
 
Example 5
Source File: TopologyWebSocketBehavior.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    ThreadContext.setApplication(application);
    ThreadContext.setSession(session);

    try {
        timeoutHandlingConnectionChecker(
                new ConnectorChecker(key, this.application),
                connectorTestTimeout,
                connectors,
                runningConnCheck);
    } finally {
        ThreadContext.detach();
    }
}
 
Example 6
Source File: TopologyWebSocketBehavior.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    ThreadContext.setApplication(application);
    ThreadContext.setSession(session);

    try {
        timeoutHandlingConnectionChecker(
                new ResourceChecker(key, this.application),
                resourceTestTimeout,
                resources,
                runningResCheck);
    } finally {
        ThreadContext.detach();
    }
}
 
Example 7
Source File: AjaxWizard.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<Serializable, Serializable> call() throws Exception {
    try {
        ThreadContext.setApplication(this.application);
        ThreadContext.setRequestCycle(this.requestCycle);
        ThreadContext.setSession(this.session);
        return AjaxWizard.this.onApplyInternal(this.target);
    } finally {
        ThreadContext.detach();
    }
}
 
Example 8
Source File: AbstractWebSocketProcessor.java    From onedev with MIT License 4 votes vote down vote up
/**
 * Exports the Wicket thread locals and broadcasts the received message from the client to all
 * interested components and behaviors in the page with id {@code #pageId}
 * <p>
 *     Note: ConnectedMessage and ClosedMessage messages are notification-only. I.e. whatever the
 *     components/behaviors write in the WebSocketRequestHandler will be ignored because the protocol
 *     doesn't expect response from the user.
 * </p>
 *
 * @param message
 *      the message to broadcast
 */
public final void broadcastMessage(final IWebSocketMessage message)
{
	IKey key = getRegistryKey();
	IWebSocketConnection connection = connectionRegistry.getConnection(application, sessionId, key);

	if (connection != null && connection.isOpen())
	{
		Application oldApplication = ThreadContext.getApplication();
		Session oldSession = ThreadContext.getSession();
		RequestCycle oldRequestCycle = ThreadContext.getRequestCycle();

		WebResponse webResponse = webSocketSettings.newWebSocketResponse(connection);
		try
		{
			WebSocketRequestMapper requestMapper = new WebSocketRequestMapper(application.getRootRequestMapper());
			RequestCycle requestCycle = createRequestCycle(requestMapper, webResponse);
			ThreadContext.setRequestCycle(requestCycle);

			ThreadContext.setApplication(application);

			Session session;
			if (oldSession == null || message instanceof IWebSocketPushMessage)
			{
				ISessionStore sessionStore = application.getSessionStore();
				session = sessionStore.lookup(webRequest);
				ThreadContext.setSession(session);
			}
			else
			{
				session = oldSession;
			}

			// Session is null if we copy the url with jsessionid to a new browser window 
			if (session != null) {
				IPageManager pageManager = session.getPageManager();
				Page page = getPage(pageManager);

				if (page != null) {
					WebSocketRequestHandler requestHandler = webSocketSettings.newWebSocketRequestHandler(page, connection);

					@SuppressWarnings("rawtypes")
					WebSocketPayload payload = createEventPayload(message, requestHandler);

					if (!(message instanceof ConnectedMessage || message instanceof ClosedMessage))
					{
						requestCycle.scheduleRequestHandlerAfterCurrent(requestHandler);
					}

					IRequestHandler broadcastingHandler = new WebSocketMessageBroadcastHandler(pageId, resourceName, payload);
					requestMapper.setHandler(broadcastingHandler);
					requestCycle.processRequestAndDetach();
				}
			}
		}
		catch (Exception x)
		{
			try {
				connection.sendMessage(WebSocketManager.ERROR_MESSAGE);
			} catch (IOException e1) {
			}
			LOG.error("An error occurred during processing of a WebSocket message", x);
		}
		finally
		{
			try
			{
				webResponse.close();
			}
			finally
			{
				ThreadContext.setApplication(oldApplication);
				ThreadContext.setRequestCycle(oldRequestCycle);
				ThreadContext.setSession(oldSession);
			}
		}
	}
	else
	{
		LOG.debug("Either there is no connection({}) or it is closed.", connection);
	}
}