📄 session.java
字号:
/** * @return The suffix default an empty string. */ protected String getAutoPageMapNameSuffix() { return ""; } /** * Registers an error feedback message for this session * * @param message * The feedback message */ public final void error(final String message) { addFeedbackMessage(message, FeedbackMessage.ERROR); } /** * Get the application that is currently working with this session. * * @return Returns the application. */ public final Application getApplication() { return Application.get(); } /** * @return The authorization strategy for this session */ public IAuthorizationStrategy getAuthorizationStrategy() { return getApplication().getSecuritySettings().getAuthorizationStrategy(); } /** * @return The class resolver for this Session */ public final IClassResolver getClassResolver() { return getApplication().getApplicationSettings().getClassResolver(); } /** * Gets the client info object for this session. This method lazily gets the new agent info * object for this session. It uses any cached or set ({@link #setClientInfo(ClientInfo)}) * client info object or uses {@link RequestCycle#newClientInfo()} to get the info object based * on the current request when no client info object was set yet, and then caches the returned * object; we can expect the client to stay the same for the whole session, and implementations * of {@link RequestCycle#newClientInfo()} might be relatively expensive. * * @return the client info object based on this request */ public ClientInfo getClientInfo() { if (clientInfo == null) { clientInfo = RequestCycle.get().newClientInfo(); } return clientInfo; } /** * @return The default page map */ public final IPageMap getDefaultPageMap() { return pageMapForName(PageMap.DEFAULT_NAME, true); } /** * Gets feedback messages stored in session * * @return unmodifiable list of feedback messages */ public final FeedbackMessages getFeedbackMessages() { return feedbackMessages; } /** * Gets the unique id for this session from the underlying SessionStore. May be null if a * concrete session is not yet created. * * @return The unique id for this session or null if it is a temporary session */ public final String getId() { if (id == null) { id = getSessionStore().getSessionId(RequestCycle.get().getRequest(), false); // we have one? if (id != null) { dirty(); } } return id; } /** * Get this session's locale. * * @return This session's locale */ public Locale getLocale() { return locale; } /** * Gets metadata for this session using the given key. * * @param key * The key for the data * @return The metadata * @see MetaDataKey */ public final Serializable getMetaData(final MetaDataKey key) { return key.get(metaData); } /** * When a regular request on certain page with certain version is being processed, we don't * allow ajax requests to same page and version. * * @param lockedRequestCycle * @return whether current request is valid or should be discarded */ protected boolean isCurrentRequestValid(RequestCycle lockedRequestCycle) { return true; } /** * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT. * * Returns the page with given id and versionNumber. It keeps asking pageMaps for given page * until it finds one that contains it. * * @param pageId * @param versionNumber * @return The page of that pageid and version, null if not found */ public final Page getPage(final int pageId, final int versionNumber) { if (Application.get().getSessionSettings().isPageIdUniquePerSession() == false) { throw new IllegalStateException( "To call this method ISessionSettings.setPageIdUniquePerSession must be set to true"); } List pageMaps = getPageMaps(); for (Iterator i = pageMaps.iterator(); i.hasNext();) { IPageMap pm = (IPageMap)i.next(); if (pm.containsPage(pageId, versionNumber)) { return getPage(pm.getName(), "" + pageId, versionNumber); } } return null; } /** * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT. * * Get the page for the given path. * * @param pageMapName * The name of the page map where the page is * @param path * Component path * @param versionNumber * The version of the page required * @return The page based on the first path component (the page id), or null if the requested * version of the page cannot be found. */ public final Page getPage(final String pageMapName, final String path, final int versionNumber) { if (log.isDebugEnabled()) { log.debug("Getting page [path = " + path + ", versionNumber = " + versionNumber + "]"); } // Get page map by name, creating the default page map automatically IPageMap pageMap = pageMapForName(pageMapName, pageMapName == PageMap.DEFAULT_NAME); if (pageMap != null) { synchronized (usedPageMaps) // get a lock so be sure that only one // is made { if (pageMapsUsedInRequest == null) { pageMapsUsedInRequest = new HashMap(3); } } synchronized (pageMapsUsedInRequest) { long startTime = System.currentTimeMillis(); // TODO For now only use the setting. Might be extended with // something overridable on request/ page/ request target level // later Duration timeout = Application.get().getRequestCycleSettings().getTimeout(); PageMapsUsedInRequestEntry entry = (PageMapsUsedInRequestEntry)pageMapsUsedInRequest.get(pageMap); // Get page entry for id and version Thread t = entry != null ? entry.thread : null; while (t != null && t != Thread.currentThread()) { if (isCurrentRequestValid(entry.requestCycle) == false) { // we need to ignore this request. That's because it is // an ajax request // while regular page request is being processed throw new IgnoreAjaxRequestException(); } try { pageMapsUsedInRequest.wait(timeout.getMilliseconds()); } catch (InterruptedException ex) { throw new WicketRuntimeException(ex); } entry = (PageMapsUsedInRequestEntry)pageMapsUsedInRequest.get(pageMap); t = entry != null ? entry.thread : null; if (t != null && t != Thread.currentThread() && (startTime + timeout.getMilliseconds()) < System.currentTimeMillis()) { // if it is still not the right thread.. // This either points to long running code (a report // page?) or a deadlock or such throw new WicketRuntimeException("After " + timeout + " the Pagemap " + pageMapName + " is still locked by: " + t + ", giving up trying to get the page for path: " + path); } } PageMapsUsedInRequestEntry newEntry = new PageMapsUsedInRequestEntry(); newEntry.thread = Thread.currentThread(); newEntry.requestCycle = RequestCycle.get(); pageMapsUsedInRequest.put(pageMap, newEntry); final String id = Strings.firstPathComponent(path, Component.PATH_SEPARATOR); Page page = pageMap.get(Integer.parseInt(id), versionNumber); if (page == null) { pageMapsUsedInRequest.remove(pageMap); pageMapsUsedInRequest.notifyAll(); } else { // attach the page now. page.onPageAttached(); touch(page); } return page; } } return null; } /** * @return The page factory for this session */ public final IPageFactory getPageFactory() { return getApplication().getSessionSettings().getPageFactory(); } /** * @param page * The page, or null if no page context is available * @return The page factory for the page, or the default page factory if page was null */ public final IPageFactory getPageFactory(final Page page) { if (page != null) { return page.getPageFactory(); } return getPageFactory(); } /** * @return A list of all PageMaps in this session. */ public final List getPageMaps() { final List list = new ArrayList(); for (final Iterator iterator = getAttributeNames().iterator(); iterator.hasNext();) { final String attribute = (String)iterator.next(); if (attribute.startsWith(pageMapAttributePrefix)) { list.add(getAttribute(attribute)); } } return list; } /** * @return Size of this session, including all the pagemaps it contains */ public final long getSizeInBytes() { long size = Objects.sizeof(this); for (final Iterator iterator = getPageMaps().iterator(); iterator.hasNext();) { final IPageMap pageMap = (IPageMap)iterator.next(); size += pageMap.getSizeInBytes(); } return size; } /** * Get the style (see {@link org.apache.wicket.Session}). * * @return Returns the style (see {@link org.apache.wicket.Session}) */ public final String getStyle() { return style; } /** * Registers an informational feedback message for this session * * @param message * The feedback message */ public final void info(final String message) { addFeedbackMessage(message, FeedbackMessage.INFO); } /** * Invalidates this session at the end of the current request. If you need to invalidate the * session immediately, you can do this by calling invalidateNow(), however this will remove all * Wicket components from this session, which means that you will no longer be able to work with * them. */ public void invalidate() { sessionInvalidated = true; } /** * Invalidates this session immediately. Calling this method will remove all Wicket components * from this session, which means that you will no longer be able to work with them. */ public void invalidateNow() { sessionInvalidated = true; // set this for isSessionInvalidated getSessionStore().invalidate(RequestCycle.get().getRequest()); } /** * Whether the session is invalid now, or will be invalidated by the end of the request. Clients * should rarely need to use this method if ever. * * @return Whether the session is invalid when the current request is done * * @see #invalidate() * @see #invalidateNow() */ public final boolean isSessionInvalidated() { return sessionInvalidated; } /** * Whether this session is temporary. A Wicket application can operate in a session-less mode as * long as stateless pages are used. If this session object is temporary, it will not be * available on a next request. * * @return Whether this session is temporary (which is the same as it's id being null) */ public final boolean isTemporary() { return getId() == null; } /** * Creates a new page map with a given name * * @param name * The name for the new page map * @return The newly created page map */ public final IPageMap newPageMap(final String name) { // Check that session doesn't have too many page maps already final int maxPageMaps = getApplication().getSessionSettings().getMaxPageMaps(); synchronized (usedPageMaps) { if (usedPageMaps.size() >= maxPageMaps) { IPageMap pm = (IPageMap)usedPageMaps.getFirst(); pm.remove(); } } // Create new page map final IPageMap pageMap = getSessionStore().createPageMap(name); setAttribute(attributeForPageMapName(name), pageMap); dirty(); return pageMap; } /** * Gets a page map for the given name, automatically creating it if need be. * * @param pageMapName * Name of page map, or null for default page map * @param autoCreate * True if the page map should be automatically created if it does not exist * @return PageMap for name */ public final IPageMap pageMapForName(String pageMapName, final boolean autoCreate) { IPageMap pageMap = (IPageMap)getAttribute(attributeForPageMapName(pageMapName)); if (pageMap == null && autoCreate) { pageMap = newPageMap(pageMapName); } return pageMap; } /** * @param pageMap * Page map to remove */ public final void removePageMap(final IPageMap pageMap) { PageMapAccessMetaData pagemapMetaData = (PageMapAccessMetaData)getMetaData(PAGEMAP_ACCESS_MDK); if (pagemapMetaData != null) { pagemapMetaData.pageMapNames.remove(pageMap.getName()); } synchronized (usedPageMaps) { usedPageMaps.remove(pageMap); } removeAttribute(attributeForPageMapName(pageMap.getName())); dirty(); } /** * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT. * <p> * Sets the application that this session is associated with. * * @param application * The application */ public final void setApplication(final Application application) { } /** * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT. * <p> * Sets the client info object for this session. This will only work when * {@link #getClientInfo()} is not overridden. * * @param clientInfo * the client info object */ public final void setClientInfo(ClientInfo clientInfo) { this.clientInfo = clientInfo; dirty(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -