Example usage for org.apache.wicket ThreadContext setApplication

List of usage examples for org.apache.wicket ThreadContext setApplication

Introduction

In this page you can find the example usage for org.apache.wicket ThreadContext setApplication.

Prototype

public static void setApplication(Application application) 

Source Link

Document

Binds the specified application to current thread.

Usage

From source file:at.molindo.wicketutils.utils.MockUtils.java

License:Apache License

/**
 * reuse an existing session if possible
 *///w  w  w.j a v a  2 s.co m
public static <V> V withRequest(WebApplication webApplication, IMockRequestCallback<V> callback) {
    Session oldSession = ThreadContext.exists() ? ThreadContext.getSession() : null;
    ThreadContext oldContext = ThreadContext.detach();

    try {
        ThreadContext.setApplication(webApplication);
        ThreadContext.setSession(oldSession);

        // mock http session
        ServletContext context = webApplication.getServletContext();
        MockHttpSession httpSession = new MockHttpSession(context);

        // mock servlet request
        MockServletRequest servletRequest = new MockServletRequest(webApplication, httpSession, context);
        callback.configure(new MockRequest(servletRequest));
        servletRequest.setDefaultHeaders();

        // mock response
        MockHttpServletResponse servletResponse = new MockHttpServletResponse(servletRequest);

        // mock web request
        final WebRequest request = VisibilityHelper.newWebRequest(webApplication, servletRequest, "/");

        // mock web response
        final WebResponse response = VisibilityHelper.newWebResponse(webApplication, request, servletResponse);

        // create
        ThreadContext.setRequestCycle(webApplication.createRequestCycle(request, response));

        return callback.call();
    } finally {
        Session newSession = ThreadContext.getSession();
        ThreadContext.restore(oldContext);
        if (oldSession == null && newSession != null && !newSession.isTemporary()) {
            // reuse session if a new one was created
            ThreadContext.setSession(newSession);
        }
    }
}

From source file:com.evolveum.midpoint.web.component.SecurityContextAwareCallable.java

License:Apache License

protected void setupContext(Application application, Session session) {
    if (!Application.exists() && application != null) {
        ThreadContext.setApplication(application);
    }/*from w w w . j  a v a 2s  .  co  m*/
    if (!Session.exists() && session != null) {
        ThreadContext.setSession(session);
    }
}

From source file:com.inductiveautomation.ignition.examples.hce.GatewayHook.java

@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 {//from   w w w. java 2s. c  om
            wicket.unmount("ack/${id}");
        } finally {
            ThreadContext.setApplication(currentApplication);
        }
    }
}

From source file:com.mastfrog.acteur.wicket.WicketActeur.java

License:Open Source License

@Inject
WicketActeur(HttpEvent evt, Application application, PathFactory pf, Charset charset, WicketConfig config,
        ByteBufAllocator alloc, Settings settings, ReentrantScope scope) {
    RequestAdapter request = new RequestAdapter(evt, config.locale(), charset, settings);
    ResponseAdapter response = new ResponseAdapter(response(), charset, alloc, pf);
    try (QuietAutoCloseable closeScope = scope.enter(request, response, response())) {
        ThreadContext.setApplication(application);
        RequestCycle requestCycle = application.createRequestCycle(request, response);
        ThreadContext.setRequestCycle(requestCycle);
        boolean processed = requestCycle.processRequestAndDetach();
        if (!processed) {
            reject();//from w  w  w . j av a2s . co m
        } else {
            HttpResponseStatus status = response.status();
            reply(status == null ? OK : status);
        }
    } finally {
        ThreadContext.detach();
    }
}

From source file:com.mastfrog.acteur.wicket.WicketApplicationInitializer.java

License:Open Source License

protected void init(Application application) throws NoSuchFieldException, IllegalArgumentException,
        IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    WebApplication wa = (WebApplication) application;
    wa.setWicketFilter(filter);//from w  w  w  .ja va  2  s . c o m
    wa.setServletContext(ctx);
    wa.setSessionStoreProvider(this);
    ThreadContext.setApplication(application);
    application.setName(application.getClass().getName());
    application.initApplication();
    wa.setSessionStoreProvider(this);
    Field field = Application.class.getDeclaredField("pageFactory");
    field.setAccessible(true);
    field.set(application, factory);
    Method logStarted = WebApplication.class.getDeclaredMethod("logStarted");
    logStarted.setAccessible(true);
    logStarted.invoke(application);
}

From source file:com.norconex.commons.wicket.ThreadContextMocker.java

License:Apache License

/**
 * Invoke this method in the new thread before doing something with Wicket.
 *///from  w  ww  .  java 2  s  .c o m
public void mock() {
    ThreadContext.setApplication(application);
    ThreadContext.setSession(session);
    final MockServletContext context = new MockServletContext(application, SystemUtils.JAVA_IO_TMPDIR);
    ThreadContext.setRequestCycle(
            application.createRequestCycle(new MockWebRequest(Url.parse("http://localhost/mock")) {
                @Override
                public Object getContainerRequest() {
                    return new MockHttpServletRequest(application, new MockHttpSession(context), context);
                }
            }, new MockWebResponse()));
}

From source file:com.ttdev.wicketpagetest.WicketAppJettyLauncher.java

License:Open Source License

private void waitForApplication() {
    for (;;) {// w ww .j  a v  a 2  s .co  m
        WebApplication app = getApplication();
        if (app != null) {
            ThreadContext.setApplication(app);
            break;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.tysanclan.site.projectewok.util.scheduler.TysanTaskExecutor.java

License:Open Source License

/**
 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
 *//* w  ww. j  a va 2  s. c o  m*/
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    TysanTask task = (TysanTask) context.getMergedJobDataMap().get(TASK_KEY);
    TysanApplication application = (TysanApplication) context.getMergedJobDataMap().get(APP_KEY);
    ThreadContext.setApplication(application);

    if (task != null) {
        MockServletContext sctx = new MockServletContext(application, "/src/main/webapp/");
        MockHttpServletRequest request = new MockHttpServletRequest(application, new MockHttpSession(sctx),
                sctx);
        RequestAttributes attr = new ServletRequestAttributes(request);

        RequestContextHolder.setRequestAttributes(attr);
        Injector.get().inject(task);
        try {
            task.run();
        } catch (Exception e) {
            BugTrackerService tracker = TysanApplication.getApplicationContext()
                    .getBean(BugTrackerService.class);
            if (!tracker.isKnownIssue(e)) {
                tracker.reportCrash(null, task.getClass().getName(), e);
            }
        }
        task.cleanUp();
        RequestContextHolder.resetRequestAttributes();
    } else {
        logger.error("No task passed to TysanTaskExecutor!");
    }
}

From source file:org.apache.openmeetings.db.util.ApplicationHelper.java

License:Apache License

public static IApplication ensureApplication(Long langId) {
    IApplication a = null;//  w  w  w  .  ja v a2s . co m
    if (Application.exists()) {
        a = (IApplication) Application.get();
    } else {
        WebApplication app = (WebApplication) Application.get(wicketApplicationName);
        LabelDao.initLanguageMap();
        if (app == null) {
            try {
                app = (WebApplication) LabelDao.getAppClass().newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                log.error("Failed to create Application");
                return null;
            }
            app.setServletContext(new MockServletContext(app, null));
            app.setName(wicketApplicationName);
            ServletContext sc = app.getServletContext();
            OMContextListener omcl = new OMContextListener();
            omcl.contextInitialized(new ServletContextEvent(sc));
            XmlWebApplicationContext xmlContext = new XmlWebApplicationContext();
            xmlContext.setConfigLocation("classpath:openmeetings-applicationContext.xml");
            xmlContext.setServletContext(sc);
            xmlContext.refresh();
            sc.setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, xmlContext);
            app.setConfigurationType(RuntimeConfigurationType.DEPLOYMENT);
            ThreadContext.setApplication(app);
            app.initApplication();
        } else {
            ThreadContext.setApplication(app);
        }
        a = (IApplication) Application.get(wicketApplicationName);
    }
    if (ThreadContext.getRequestCycle() == null) {
        ServletWebRequest req = new ServletWebRequest(new MockHttpServletRequest((Application) a,
                new MockHttpSession(a.getServletContext()), a.getServletContext()), "");
        RequestCycleContext rctx = new RequestCycleContext(req, new MockWebResponse(), a.getRootRequestMapper(),
                a.getExceptionMapperProvider().get());
        ThreadContext.setRequestCycle(new RequestCycle(rctx));
    }
    if (ThreadContext.getSession() == null) {
        WebSession s = WebSession.get();
        if (langId > 0) {
            ((IWebSession) s).setLanguage(langId);
        }
    }
    return a;
}

From source file:org.apache.openmeetings.db.util.ApplicationHelper.java

License:Apache License

public static void destroyApplication() {
    WebApplicationContext ctx = getWebApplicationContext(
            ((WebApplication) ensureApplication()).getServletContext());
    ((XmlWebApplicationContext) ctx).destroy();
    ThreadContext.setApplication(null);
    ThreadContext.setRequestCycle(null);
    ThreadContext.setSession(null);/*  ww  w  . j  a v  a 2  s .com*/
}