org.apache.wicket.ThreadContext.setSession()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(116)

本文整理了Java中org.apache.wicket.ThreadContext.setSession()方法的一些代码示例,展示了ThreadContext.setSession()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadContext.setSession()方法的具体详情如下:
包路径:org.apache.wicket.ThreadContext
类名称:ThreadContext
方法名:setSession

ThreadContext.setSession介绍

[英]Binds the session to current thread.
[中]将会话绑定到当前线程。

代码示例

代码示例来源:origin: org.apache.wicket/wicket-core

/**
 *
 */
private void newSession()
{
  ThreadContext.setSession(null);
  // the following will create a new session and put it in the thread context
  session = Session.get();
}

代码示例来源:origin: apache/wicket

/**
 *
 */
private void newSession()
{
  ThreadContext.setSession(null);
  // the following will create a new session and put it in the thread context
  session = Session.get();
}

代码示例来源:origin: org.apache.wicket/wicket-core

private void bindSession(ServletRequest request, WebApplication application)
{
  // find wicket session and bind it to thread
  HttpSession httpSession = ((HttpServletRequest)request).getSession(false);
  Session session = getSession(httpSession, application);
  if (session == null)
  {
    if (logger.isDebugEnabled())
    {
      logger.debug("could not set Wicket session: key " + sessionKey +
        " not found in http session for " +
        ((HttpServletRequest)request).getContextPath() + "," + request.getServerName() +
        ", or http session does not exist");
    }
  }
  else
  {
    ThreadContext.setSession(session);
  }
}

代码示例来源:origin: apache/wicket

private void bindSession(ServletRequest request, WebApplication application)
{
  // find wicket session and bind it to thread
  HttpSession httpSession = ((HttpServletRequest)request).getSession(false);
  Session session = getSession(httpSession, application);
  if (session == null)
  {
    if (logger.isDebugEnabled())
    {
      logger.debug("could not set Wicket session: key " + sessionKey +
        " not found in http session for " +
        ((HttpServletRequest)request).getContextPath() + "," + request.getServerName() +
        ", or http session does not exist");
    }
  }
  else
  {
    ThreadContext.setSession(session);
  }
}

代码示例来源:origin: OrienteerBAP/Orienteer

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();
}

代码示例来源:origin: apache/syncope

@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();
    }
  }
}

代码示例来源:origin: apache/syncope

@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();
    }
  }
}

代码示例来源:origin: org.jabylon/rest.ui

@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;
}

代码示例来源:origin: OrienteerBAP/Orienteer

@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);
    }
  });
}

代码示例来源:origin: OrienteerBAP/Orienteer

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();
}

代码示例来源:origin: apache/syncope

@Override
  public void run() {
    ThreadContext.setApplication(application);
    ThreadContext.setSession(session);
    try {
      timeoutHandlingConnectionChecker(
          new ConnectorChecker(key, this.application),
          connectorTestTimeout,
          connectors,
          runningConnCheck);
    } finally {
      ThreadContext.detach();
    }
  }
}

代码示例来源:origin: apache/syncope

@Override
  public void run() {
    ThreadContext.setApplication(application);
    ThreadContext.setSession(session);
    try {
      timeoutHandlingConnectionChecker(
          new ResourceChecker(key, this.application),
          resourceTestTimeout,
          resources,
          runningResCheck);
    } finally {
      ThreadContext.detach();
    }
  }
}

代码示例来源:origin: org.jabylon/rest.ui

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);
          }
        }
      }
    }
  }

代码示例来源:origin: org.apache.wicket/wicket-core

/**
 * 
 * @param requestCycle
 * @return Session
 */
public Session fetchCreateAndSetSession(final RequestCycle requestCycle)
{
  Args.notNull(requestCycle, "requestCycle");
  Session session = getSessionStore().lookup(requestCycle.getRequest());
  if (session == null)
  {
    session = newSession(requestCycle.getRequest(), requestCycle.getResponse());
    ThreadContext.setSession(session);
    internalGetPageManager().newSessionCreated();
    sessionListeners.onCreated(session);
  }
  else
  {
    ThreadContext.setSession(session);
  }
  return session;
}

代码示例来源:origin: apache/wicket

/**
 * 
 * @param requestCycle
 * @return Session
 */
public Session fetchCreateAndSetSession(final RequestCycle requestCycle)
{
  Args.notNull(requestCycle, "requestCycle");
  Session session = getSessionStore().lookup(requestCycle.getRequest());
  if (session == null)
  {
    session = newSession(requestCycle.getRequest(), requestCycle.getResponse());
    ThreadContext.setSession(session);
    internalGetPageManager().newSessionCreated();
    sessionListeners.onCreated(session);
  }
  else
  {
    ThreadContext.setSession(session);
  }
  return session;
}

代码示例来源:origin: org.ops4j.pax.wicket/org.ops4j.pax.wicket.service

/** {@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);
  }
}

代码示例来源:origin: ops4j/org.ops4j.pax.wicket

/** {@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);
  }
}

代码示例来源:origin: apache/wicket

/**
 * 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;
}

代码示例来源:origin: org.apache.wicket/wicket-core

/**
 * 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;
}

代码示例来源:origin: com.norconex.commons/norconex-commons-wicket

/**
   * Invoke this method in the new thread before doing something with Wicket.
   */
  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())
    );        
  }
}

相关文章