/** * Execute a task an send the output to a subset of the total list of users. * The {@link ScriptSessionFilter} defines which subset. * This method could be used to alert administrators wherever they are on a * site about urgent action that need attention. * @param filter Used to define the set of browser windows which should * receive the update. * @param task A code block to execute */ public static void withAllSessionsFiltered(ScriptSessionFilter filter, Runnable task) { withAllSessionsFiltered(getServerContext(), filter, task); }
/** * Execute some task (represented by a {@link Runnable}) and aim the output * at all browser window open at all pages in this web application. * It is likely that a more fine-grained broadcast method will be applicable * to most situations. The other with* methods in this class provide extra * ways to filter the set of pages to broadcast to. This option could be * useful if all pages contain an 'update' area, or to broadcast status * information: Window.alert("System reboot in 20mins. Please log off"); * To send UI code to browser windows looking at a specific page, see the * {@link #withPage} method. To send to a custom set of browser windows, * see the {@link #withAllSessionsFiltered} method. To send to a specific * session, use {@link #withSession}. * @param task A code block to execute */ public static void withAllSessions(Runnable task) { withAllSessions(getServerContext(), task); }
/** * Execute a task and aim the output at all the browser windows open at a * given page in this web application. No further filtering is performed. * To send to a subset of the browser windows viewing a page, see the * {@link #withPageFiltered} method. * @param page The page to send to, excluding protocol/host/port specifiers * but including context path and servlet path. For example to send to * <code>http://example.com:8080/webapp/controller/path/index.html</code>, * you should use "/webapp/controller/path/index.html" or since the default * PageNormalizer understands default pages, this is the same as sending to * browsers viewing "/webapp/controller/path/". * To discover the contextPath at runtime you can use * javax.servlet.ServletContext#getContextPath with servlet 2.5, or before * version 2.5 you can also use {@link ServerContext#getContextPath} or * {@link javax.servlet.http.HttpServletRequest#getContextPath}. * @param task A code block to execute */ public static void withPage(String page, Runnable task) { withPage(getServerContext(), page, task); }
/** * Execute a task and aim the output at a specific script session. This * method is likely to be useful for directed updates that originate away * from a thread started by the browser window in question. Examples include * an instant message, or the results of a slow method invocation. * This method is implicit in anything called from a DWR thread, so should * not need to be used to send UI updates back to an originating browser. * @param sessionId The {@link ScriptSession}.id of the browser window * @param task A code block to execute */ public static void withSession(String sessionId, Runnable task) { withSession(getServerContext(), sessionId, task); }
/** * Execute a task and aim the output at a subset of the users on a page. * This method is useful when you have a small number of pages that each * have a significant number of functions on them. The filter allows you to * select the set of users that will be interested in the update. * @param page The page to send to. See {@link #withPage} for details * @param filter Used to define the set of browser windows which should * receive the update. * @param task A code block to execute */ public static void withPageFiltered(String page, ScriptSessionFilter filter, Runnable task) { withPageFiltered(getServerContext(), page, filter, task); }
+ "to see the changes.", locale); Browser.withAllSessionsFiltered( new ScriptSessionFilter() { public boolean match(ScriptSession session) {
/** * Add a script to the list waiting for remote execution. * @param script The script to execute */ public static void addScript(ScriptBuffer script) { Collection<ScriptSession> sessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : sessions) { scriptSession.addScript(script); } }
/** * Remove a subscription from the list of people that we are remembering * to keep updated * @param receiver The client side interface to pass async updates to. * Will be <code>null</code> if no async updates are required */ public void unsubscribe(String storeId, StoreChangeListener<Object> receiver) { StoreProvider<Object> provider = Directory.getRegistration(storeId, Object.class); provider.unsubscribe(receiver); Browser.close(receiver); }
/** * This method discovers the sessions that are currently being targeted * by browser updates. * <p> * It will generally only be useful to authors of reverse ajax UI proxy * APIs. Using it directly may cause scaling problems * @return The list of current browser windows. */ public static Collection<ScriptSession> getTargetSessions() { TaskDispatcher taskDispatcher = TaskDispatcherFactory.get(getServerContext()); Collection<ScriptSession> sessions = taskDispatcher.getTargetSessions(); if (sessions != null) { return sessions; } WebContext webContext = WebContextFactory.get(); if (webContext != null) { sessions = new ArrayList<ScriptSession>(); sessions.add(webContext.getScriptSession()); return sessions; } throw new IllegalStateException("No current UI to manipulate. See org.directwebremoting.Browser to set one."); }
/** * Removes the object bound with the specified name from this session. * If the session does not have an object bound with the specified name, * this method does nothing. * <p>After this method executes, and if the object implements * {@link ScriptSessionBindingListener}, the container calls * {@link ScriptSessionBindingListener#valueUnbound}. * @param name the name of the object to remove from this session * @throws IllegalStateException if the page has been invalidated */ public static void removeAttribute(String name) { Collection<ScriptSession> sessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : sessions) { scriptSession.removeAttribute(name); } }
/** * Binds an object to this session, using the name specified. * If an object of the same name is already bound to the session, the * object is replaced. * <p>After this method executes, and if the new object implements * {@link ScriptSessionBindingListener}, the container calls * {@link ScriptSessionBindingListener#valueBound}. * <p>If an object was already bound to this session of this name that * implements {@link ScriptSessionBindingListener}, its * {@link ScriptSessionBindingListener#valueUnbound} method is called. * <p>If the value passed in is null, this has the same effect as calling * {@link #removeAttribute}. * @param name the name to which the object is bound; cannot be null * @param value the object to be bound * @throws IllegalStateException if the page has been invalidated */ public static void setAttribute(String name, Object value) { Collection<ScriptSession> sessions = Browser.getTargetSessions(); for (ScriptSession scriptSession : sessions) { scriptSession.setAttribute(name, value); } }