Skip to content

Commit

Permalink
Change addXXListener method in VaadinService to return Registration.
Browse files Browse the repository at this point in the history
Fixes vaadin/framework8-issues#492
Old removeXXXListener methods are deprecated. New style of
unregistration is used.


Change-Id: If986dc9f8813ee7a5efc374a3facd5a8d23bc6a9
  • Loading branch information
Denis Anisimov authored and Vaadin Code Review committed Nov 28, 2016
1 parent 4f4b47d commit 0090a48
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions server/src/main/java/com/vaadin/server/VaadinService.java
Expand Up @@ -60,6 +60,7 @@
import com.vaadin.server.communication.UidlRequestHandler; import com.vaadin.server.communication.UidlRequestHandler;
import com.vaadin.shared.ApplicationConstants; import com.vaadin.shared.ApplicationConstants;
import com.vaadin.shared.JsonConstants; import com.vaadin.shared.JsonConstants;
import com.vaadin.shared.Registration;
import com.vaadin.shared.ui.ui.UIConstants; import com.vaadin.shared.ui.ui.UIConstants;
import com.vaadin.ui.UI; import com.vaadin.ui.UI;
import com.vaadin.util.CurrentInstance; import com.vaadin.util.CurrentInstance;
Expand Down Expand Up @@ -165,8 +166,8 @@ public VaadinService(DeploymentConfiguration deploymentConfiguration) {
.loadClass(classLoaderName); .loadClass(classLoaderName);
final Constructor<?> c = classLoaderClass final Constructor<?> c = classLoaderClass
.getConstructor(ClassLoader.class); .getConstructor(ClassLoader.class);
setClassLoader((ClassLoader) c.newInstance( setClassLoader((ClassLoader) c
getClass().getClassLoader())); .newInstance(getClass().getClassLoader()));
} catch (final Exception e) { } catch (final Exception e) {
throw new RuntimeException( throw new RuntimeException(
"Could not find specified class loader: " "Could not find specified class loader: "
Expand Down Expand Up @@ -411,10 +412,13 @@ public SystemMessages getSystemMessages(Locale locale,
* *
* @param listener * @param listener
* the Vaadin service session initialization listener * the Vaadin service session initialization listener
* @return a registration object for removing the listener
*/ */
public void addSessionInitListener(SessionInitListener listener) { public Registration addSessionInitListener(SessionInitListener listener) {
eventRouter.addListener(SessionInitEvent.class, listener, eventRouter.addListener(SessionInitEvent.class, listener,
SESSION_INIT_METHOD); SESSION_INIT_METHOD);
return () -> eventRouter.removeListener(SessionInitEvent.class,
listener, SESSION_INIT_METHOD);
} }


/** /**
Expand All @@ -425,7 +429,11 @@ public void addSessionInitListener(SessionInitListener listener) {
* *
* @param listener * @param listener
* the Vaadin service session initialization listener to remove. * the Vaadin service session initialization listener to remove.
* @deprecated use the {@link Registration} object returned by
* {@link #addSessionInitListener(SessionInitListener)} to
* remove the listener
*/ */
@Deprecated
public void removeSessionInitListener(SessionInitListener listener) { public void removeSessionInitListener(SessionInitListener listener) {
eventRouter.removeListener(SessionInitEvent.class, listener, eventRouter.removeListener(SessionInitEvent.class, listener,
SESSION_INIT_METHOD); SESSION_INIT_METHOD);
Expand All @@ -442,10 +450,14 @@ public void removeSessionInitListener(SessionInitListener listener) {
* *
* @param listener * @param listener
* the vaadin service session destroy listener * the vaadin service session destroy listener
* @return a registration object for removing the listener
*/ */
public void addSessionDestroyListener(SessionDestroyListener listener) { public Registration addSessionDestroyListener(
SessionDestroyListener listener) {
eventRouter.addListener(SessionDestroyEvent.class, listener, eventRouter.addListener(SessionDestroyEvent.class, listener,
SESSION_DESTROY_METHOD); SESSION_DESTROY_METHOD);
return () -> eventRouter.removeListener(SessionInitEvent.class,
listener, SESSION_DESTROY_METHOD);
} }


/** /**
Expand All @@ -468,11 +480,11 @@ public void fireSessionDestroy(VaadinSession vaadinSession) {
for (final UI ui : uis) { for (final UI ui : uis) {
ui.accessSynchronously(() -> { ui.accessSynchronously(() -> {
/* /*
* close() called here for consistency so that it is * close() called here for consistency so that it is always
* always called before a UI is removed. * called before a UI is removed. UI.isClosing() is thus
* UI.isClosing() is thus always true in UI.detach() * always true in UI.detach() and associated detach
* and associated detach listeners. * listeners.
*/ */
if (!ui.isClosing()) { if (!ui.isClosing()) {
ui.close(); ui.close();
} }
Expand All @@ -483,8 +495,8 @@ public void fireSessionDestroy(VaadinSession vaadinSession) {
// have an API for using some other handler for session init and // have an API for using some other handler for session init and
// destroy listeners // destroy listeners
eventRouter.fireEvent( eventRouter.fireEvent(
new SessionDestroyEvent(VaadinService.this, session), new SessionDestroyEvent(VaadinService.this, session),
session.getErrorHandler()); session.getErrorHandler());
session.setState(State.CLOSED); session.setState(State.CLOSED);
}); });
} }
Expand All @@ -496,7 +508,11 @@ public void fireSessionDestroy(VaadinSession vaadinSession) {
* *
* @param listener * @param listener
* the vaadin service session destroy listener * the vaadin service session destroy listener
* @deprecated use the {@link Registration} object returned by
* {@link #addSessionDestroyListener(SessionDestroyListener)} to
* remove the listener
*/ */
@Deprecated
public void removeSessionDestroyListener(SessionDestroyListener listener) { public void removeSessionDestroyListener(SessionDestroyListener listener) {
eventRouter.removeListener(SessionDestroyEvent.class, listener, eventRouter.removeListener(SessionDestroyEvent.class, listener,
SESSION_DESTROY_METHOD); SESSION_DESTROY_METHOD);
Expand Down Expand Up @@ -1188,7 +1204,7 @@ private void removeClosedUIs(final VaadinSession session) {
if (ui.isClosing()) { if (ui.isClosing()) {
ui.accessSynchronously(() -> { ui.accessSynchronously(() -> {
getLogger().log(Level.FINER, "Removing closed UI {0}", getLogger().log(Level.FINER, "Removing closed UI {0}",
ui.getUIId()); ui.getUIId());
session.removeUI(ui); session.removeUI(ui);
}); });
} }
Expand All @@ -1207,8 +1223,8 @@ private void closeInactiveUIs(VaadinSession session) {
if (!isUIActive(ui) && !ui.isClosing()) { if (!isUIActive(ui) && !ui.isClosing()) {
ui.accessSynchronously(() -> { ui.accessSynchronously(() -> {
getLogger().log(Level.FINE, getLogger().log(Level.FINE,
"Closing inactive UI #{0} in session {1}", "Closing inactive UI #{0} in session {1}",
new Object[] { ui.getUIId(), sessionId }); new Object[] { ui.getUIId(), sessionId });
ui.close(); ui.close();
}); });
} }
Expand Down Expand Up @@ -1487,7 +1503,7 @@ public void writeStringResponse(VaadinResponse response, String contentType,


final OutputStream out = response.getOutputStream(); final OutputStream out = response.getOutputStream();
try (PrintWriter outWriter = new PrintWriter( try (PrintWriter outWriter = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) { new BufferedWriter(new OutputStreamWriter(out, "UTF-8")))) {
outWriter.print(reponseString); outWriter.print(reponseString);
} }
} }
Expand Down Expand Up @@ -1857,10 +1873,14 @@ public void runPendingAccessTasks(VaadinSession session) {
* @see #destroy() * @see #destroy()
* @see #removeServiceDestroyListener(ServiceDestroyListener) * @see #removeServiceDestroyListener(ServiceDestroyListener)
* @see ServiceDestroyListener * @see ServiceDestroyListener
* @return a registration object for removing the listener
*/ */
public void addServiceDestroyListener(ServiceDestroyListener listener) { public Registration addServiceDestroyListener(
ServiceDestroyListener listener) {
eventRouter.addListener(ServiceDestroyEvent.class, listener, eventRouter.addListener(ServiceDestroyEvent.class, listener,
SERVICE_DESTROY_METHOD); SERVICE_DESTROY_METHOD);
return () -> eventRouter.removeListener(ServiceDestroyEvent.class,
listener, SERVICE_DESTROY_METHOD);
} }


/** /**
Expand All @@ -1870,7 +1890,11 @@ public void addServiceDestroyListener(ServiceDestroyListener listener) {
* @since 7.2 * @since 7.2
* @param listener * @param listener
* the service destroy listener to remove * the service destroy listener to remove
* @deprecated use the {@link Registration} object returned by
* {@link #addServiceDestroyListener(ServiceDestroyListener)} to
* remove the listener
*/ */
@Deprecated
public void removeServiceDestroyListener(ServiceDestroyListener listener) { public void removeServiceDestroyListener(ServiceDestroyListener listener) {
eventRouter.removeListener(ServiceDestroyEvent.class, listener, eventRouter.removeListener(ServiceDestroyEvent.class, listener,
SERVICE_DESTROY_METHOD); SERVICE_DESTROY_METHOD);
Expand Down

0 comments on commit 0090a48

Please sign in to comment.