📄 session.java
字号:
/** * Set the locale for this session. * * @param locale * New locale */ public final void setLocale(final Locale locale) { if (locale == null) { throw new IllegalArgumentException("Parameter 'locale' must not be null"); } this.locale = locale; dirty(); } /** * Sets the metadata for this session using the given key. If the metadata object is not of the * correct type for the metadata key, an IllegalArgumentException will be thrown. For * information on creating MetaDataKeys, see {@link MetaDataKey}. * * @param key * The singleton key for the metadata * @param object * The metadata object * @throws IllegalArgumentException * @see MetaDataKey */ public final void setMetaData(final MetaDataKey key, final Serializable object) { metaData = key.set(metaData, object); } /** * Set the style (see {@link org.apache.wicket.Session}). * * @param style * The style to set. * @return the Session object */ public final Session setStyle(final String style) { this.style = style; dirty(); return this; } /** * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT. * <p> * The page will be 'touched' in the session. If it wasn't added yet to the pagemap, it will be * added to the page map else it will set this page to the front. * * If another page was removed because of this it will be cleaned up. * * @param page */ public final void touch(Page page) { // store it in a list, so that the pages are really pushed // to the pagemap when the session does it update/detaches. // all the pages are then detached List lst = (List)touchedPages.get(); if (lst == null) { lst = new ArrayList(); touchedPages.set(lst); lst.add(page); } else if (!lst.contains(page)) { lst.add(page); } } /** * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT. * <p> * This method will remove a page that was previously added via touch() * * @param page */ public final void untouch(Page page) { List lst = (List)touchedPages.get(); if (lst != null) { lst.remove(page); } } /** * @param visitor * The visitor to call at each Page in this PageMap. */ public final void visitPageMaps(final IPageMapVisitor visitor) { for (final Iterator iterator = getAttributeNames().iterator(); iterator.hasNext();) { final String attribute = (String)iterator.next(); if (attribute.startsWith(pageMapAttributePrefix)) { visitor.pageMap((IPageMap)getAttribute(attribute)); } } } /** * Registers a warning feedback message for this session * * @param message * The feedback message */ public final void warn(final String message) { addFeedbackMessage(message, FeedbackMessage.WARNING); } /** * Adds a feedback message to the list of messages * * @param message * @param level * */ private void addFeedbackMessage(String message, int level) { getFeedbackMessages().add(null, message, level); dirty(); } /** * @param pageMapName * Name of page map * @return Session attribute holding page map */ private final String attributeForPageMapName(final String pageMapName) { return pageMapAttributePrefix + pageMapName; } /** * Any attach logic for session subclasses. Called when a session is set for the thread. */ protected void attach() { } /** * Any detach logic for session subclasses. This is called on the end of handling a request, * when the RequestCycle is about to be detached from the current thread. */ protected void detach() { if (sessionInvalidated) { invalidateNow(); } } /** * Marks session state as dirty so that it will be flushed at the end of the request. */ public final void dirty() { dirty = true; } /** * Gets the attribute value with the given name * * @param name * The name of the attribute to store * @return The value of the attribute */ protected final Object getAttribute(final String name) { if (!isTemporary()) { RequestCycle cycle = RequestCycle.get(); if (cycle != null) { return getSessionStore().getAttribute(cycle.getRequest(), name); } } else { if (temporarySessionAttributes != null) { return temporarySessionAttributes.get(name); } } return null; } /** * @return List of attributes for this session */ protected final List getAttributeNames() { if (!isTemporary()) { RequestCycle cycle = RequestCycle.get(); if (cycle != null) { return getSessionStore().getAttributeNames(cycle.getRequest()); } } else { if (temporarySessionAttributes != null) { return new ArrayList(temporarySessionAttributes.keySet()); } } return Collections.EMPTY_LIST; } /** * Gets the session store. * * @return the session store */ protected ISessionStore getSessionStore() { if (sessionStore == null) { sessionStore = getApplication().getSessionStore(); } return sessionStore; } /** * Removes the attribute with the given name. * * @param name * the name of the attribute to remove */ protected final void removeAttribute(String name) { if (!isTemporary()) { RequestCycle cycle = RequestCycle.get(); if (cycle != null) { getSessionStore().removeAttribute(cycle.getRequest(), name); } } else { if (temporarySessionAttributes != null) { temporarySessionAttributes.remove(name); } } } /** * Adds or replaces the attribute with the given name and value. * * @param name * The name of the attribute * @param value * The value of the attribute */ protected final void setAttribute(String name, Object value) { if (!isTemporary()) { RequestCycle cycle = RequestCycle.get(); if (cycle == null) { throw new IllegalStateException( "Cannot set the attribute: no RequestCycle available. If you get this error when using WicketTester.startPage(Page), make sure to call WicketTester.createRequestCycle() beforehand."); } ISessionStore store = getSessionStore(); Request request = cycle.getRequest(); // extra check on session binding event if (value == this) { Object current = store.getAttribute(request, name); if (current == null) { String id = store.getSessionId(request, false); if (id != null) { // this is a new instance. wherever it came from, bind // the session now store.bind(request, (Session)value); } } } // Set the actual attribute store.setAttribute(request, name, value); } else { // we don't have to synchronize, as it is impossible a temporary // session instance gets shared across threads if (temporarySessionAttributes == null) { temporarySessionAttributes = new HashMap(3); } temporarySessionAttributes.put(name, value); } } /** * NOT TO BE CALLED BY FRAMEWORK USERS. * * @deprecated obsolete method (was meant for internal book keeping really). Clients should * override {@link #detach()} instead. */ protected final void update() { throw new UnsupportedOperationException(); } /** * @param page * The page to add to dirty objects list */ void dirtyPage(final Page page) { List dirtyObjects = getDirtyObjectsList(); if (!dirtyObjects.contains(page)) { dirtyObjects.add(page); } } /** * @param map * The page map to add to dirty objects list */ void dirtyPageMap(final IPageMap map) { if (!map.isDefault()) { synchronized (usedPageMaps) { usedPageMaps.remove(map); usedPageMaps.addLast(map); } } List dirtyObjects = getDirtyObjectsList(); if (!dirtyObjects.contains(map)) { dirtyObjects.add(map); } } /** * @return The current thread dirty objects list */ List getDirtyObjectsList() { List list = (List)dirtyObjects.get(); if (list == null) { list = new ArrayList(4); dirtyObjects.set(list); } return list; } // TODO remove after deprecation release /** * INTERNAL API. The request cycle when detached will call this. * */ final void requestDetached() { List touchedPages = (List)Session.touchedPages.get(); Session.touchedPages.set(null); if (touchedPages != null) { for (int i = 0; i < touchedPages.size(); i++) { Page page = (Page)touchedPages.get(i); page.getPageMap().put(page); } } // If state is dirty if (dirty) { // State is no longer dirty dirty = false; // Set attribute. setAttribute(SESSION_ATTRIBUTE_NAME, this); } else { if (log.isDebugEnabled()) { log.debug("update: Session not dirty."); } } List dirtyObjects = (List)Session.dirtyObjects.get(); Session.dirtyObjects.set(null); Map tempMap = new HashMap(); // Go through all dirty entries, replicating any dirty objects if (dirtyObjects != null) { for (final Iterator iterator = dirtyObjects.iterator(); iterator.hasNext();) { String attribute = null; Object object = iterator.next(); if (object instanceof Page) { final Page page = (Page)object; if (page.isStateless()) { // check, can it be that stateless pages where added to // the session? // and should be removed now? continue; } attribute = page.getPageMap().attributeForId(page.getNumericId()); if (getAttribute(attribute) == null) { // page removed by another thread. don't add it again. continue; } object = page.getPageMapEntry(); } else if (object instanceof IPageMap) { attribute = attributeForPageMapName(((IPageMap)object).getName()); } // we might override some attributes, so we use a temporary map // and then just copy the last values to real sesssion tempMap.put(attribute, object); } } // in case we have dirty attributes, set them to session if (tempMap.isEmpty() == false) { for (Iterator i = tempMap.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry)i.next(); setAttribute((String)entry.getKey(), entry.getValue()); } } if (pageMapsUsedInRequest != null) { synchronized (pageMapsUsedInRequest) { Thread t = Thread.currentThread(); Iterator it = pageMapsUsedInRequest.entrySet().iterator(); while (it.hasNext()) { Entry entry = (Entry)it.next(); if (((PageMapsUsedInRequestEntry)entry.getValue()).thread == t) { it.remove(); } } pageMapsUsedInRequest.notifyAll(); } } } private int pageIdCounter = 0; synchronized protected int nextPageId() { return pageIdCounter++; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -