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

The following examples show how to use org.apache.wicket.ThreadContext#setApplication() . 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: ApplicationHelper.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
public static IApplication ensureApplication() {
	if (Application.exists()) {
		return (IApplication)Application.get();
	}
	synchronized (SYNC_OBJ) {
		if (Application.exists()) {
			return (IApplication)Application.get();
		}
		WebApplication app = createApp((WebApplication)Application.get(getWicketApplicationName()));
		LabelDao.initLanguageMap();
		if (app != null) {
			if (!isInitComplete()) {
				initApp(app);
			}
			ThreadContext.setApplication(app);
		}
		return (IApplication)Application.get(getWicketApplicationName());
	}
}
 
Example 2
Source File: Application.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
public static String getString(String key, final Locale loc, String... _params) {
	if (!exists()) {
		ThreadContext.setApplication(org.apache.wicket.Application.get(appName));
	}
	String[] params = _params;
	if ((params == null || params.length == 0) && STRINGS_WITH_APP.contains(key)) {
		params = new String[]{getApplicationName()};
	}
	Localizer l = get().getResourceSettings().getLocalizer();
	String value = l.getStringIgnoreSettings(key, null, null, loc, null, "[Missing]");
	if (params != null && params.length > 0) {
		final MessageFormat format = new MessageFormat(value, loc);
		value = format.format(params);
	}
	if (RuntimeConfigurationType.DEVELOPMENT == get().getConfigurationType()) {
		value += String.format(" [%s]", key);
	}
	return value;
}
 
Example 3
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 4
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 5
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 6
Source File: ApplicationHelper.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
private static WebApplication initApp(WebApplication app) {
	if (app != null) {
		try {
			app.getServletContext();
		} catch(IllegalStateException e) {
			app.setServletContext(new MockServletContext(app, null));
		}
		app.setConfigurationType(RuntimeConfigurationType.DEPLOYMENT);
		OMContextListener omcl = new OMContextListener();
		omcl.contextInitialized(new ServletContextEvent(app.getServletContext()));
		ThreadContext.setApplication(app);
		app.initApplication();
	}
	return app;
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: ONotificationSendTask.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  OrienteerWebApplication app = OrienteerWebApplication.lookupApplication();
  INotificationService notificationService = app.getServiceInstance(INotificationService.class);
  ThreadContext.setApplication(app);

  DBClosure.sudoConsumer(db -> notificationService.send(notifications));
}
 
Example 12
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);
	}
}