martin-g commented on a change in pull request #479: URL: https://github.com/apache/wicket/pull/479#discussion_r746593713
########## File path: wicket-examples/src/main/java/org/apache/wicket/examples/websocket/JSR356Session.java ########## @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.wicket.examples.websocket; + + +import java.util.concurrent.ScheduledExecutorService; + +import org.apache.wicket.examples.websocket.progress.ProgressUpdater; +import org.apache.wicket.protocol.http.WebSession; +import org.apache.wicket.request.Request; + +public class JSR356Session extends WebSession { Review comment: The opening curly bracket should be on a new line. ########## File path: wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/registry/IKey.java ########## @@ -23,4 +23,9 @@ * connection in {@link IWebSocketConnectionRegistry} */ public interface IKey extends IClusterable -{} +{ + /** + * @return return a context for the key. This could for instance a page class name or a resource class name. Review comment: This could `be` ... ########## File path: wicket-examples/src/main/java/org/apache/wicket/examples/websocket/progress/ProgressBarTogglePanel.java ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.websocket.progress; + +import java.util.concurrent.ScheduledExecutorService; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.AjaxLink; +import org.apache.wicket.event.IEvent; +import org.apache.wicket.examples.websocket.JSR356Application; +import org.apache.wicket.examples.websocket.JSR356Session; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.markup.html.panel.Panel; +import org.apache.wicket.model.IModel; +import org.apache.wicket.protocol.ws.api.WebSocketBehavior; +import org.apache.wicket.protocol.ws.api.event.WebSocketPushPayload; +import org.apache.wicket.protocol.ws.api.message.ConnectedMessage; + +public class ProgressBarTogglePanel extends Panel +{ + + private int progress = 0; + private boolean showProgress = true; + + + public ProgressBarTogglePanel(String id) + { + super(id); + + setOutputMarkupId(true); + + add(new WebSocketBehavior() + { + }); + + add(new AjaxLink<Void>("hideShowProgress") + { + @Override + public void onClick(AjaxRequestTarget target) + { + showProgress = !showProgress; + target.add(ProgressBarTogglePanel.this); + } + }.setBody((IModel<String>) () -> showProgress ? "Hide progress" : "Show progress")); + + add(new AjaxLink<Void>("cancelRestartTask") + { + @Override + public void onClick(AjaxRequestTarget target) + { + JSR356Session.get().startOrCancelTask(); + target.add(ProgressBarTogglePanel.this); + } + }.setBody((IModel<String>) () -> { + ProgressUpdater.ProgressUpdateTask progressUpdateTask = JSR356Session.get().getProgressUpdateTask(); + return progressUpdateTask != null && progressUpdateTask.isRunning() && !progressUpdateTask.isCanceled() ? "Cancel task" : "Restart task"; + })); + + add(new Label("progressBar", (IModel<String>) () -> { + ProgressUpdater.ProgressUpdateTask progressUpdateTask = JSR356Session.get().getProgressUpdateTask(); + return progressUpdateTask != null && progressUpdateTask.isRunning() ? "Background Task is " + progress + "% completed" : "No task is running"; Review comment: The line is too long. Move everything after `?` and `:` on a new line ########## File path: wicket-examples/src/main/java/org/apache/wicket/examples/websocket/WebSocketPushUpdateProgressDemoPage.java ########## @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.websocket; + +import org.apache.wicket.examples.WicketExamplePage; +import org.apache.wicket.examples.websocket.progress.ProgressBarTogglePanel; +import org.apache.wicket.examples.websocket.progress.ProgressUpdater; +import org.apache.wicket.protocol.https.RequireHttps; + +@RequireHttps Review comment: Is this really needed ? ########## File path: wicket-native-websocket/wicket-native-websocket-core/src/main/java/org/apache/wicket/protocol/ws/api/BaseWebSocketBehavior.java ########## @@ -120,9 +120,21 @@ protected String getWebSocketSetupScript(Map<String, Object> parameters) { return webSocketSetupTemplate.asString(parameters); } + /** + * Override to return a context. By default, this is the page class name. + * + * @param component the {@link org.apache.wicket.Component} + * @return the context for this websocket behavior. + */ + protected String getContext(Component component) { + return component.getPage().getClass().getName(); + } + private Map<String, Object> getParameters(Component component) { Map<String, Object> variables = Generics.newHashMap(); + variables.put("context", component.getPage().getClass().getName()); Review comment: Use `getContext(component)` ########## File path: wicket-examples/src/main/java/org/apache/wicket/examples/websocket/progress/ProgressUpdater.java ########## @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.websocket.progress; + +import java.io.Serializable; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.apache.wicket.Application; +import org.apache.wicket.WicketRuntimeException; +import org.apache.wicket.page.IManageablePage; +import org.apache.wicket.protocol.ws.WebSocketSettings; +import org.apache.wicket.protocol.ws.api.IWebSocketConnection; +import org.apache.wicket.protocol.ws.api.WebSocketPushBroadcaster; +import org.apache.wicket.protocol.ws.api.message.ConnectedMessage; +import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage; +import org.apache.wicket.protocol.ws.api.registry.IKey; +import org.apache.wicket.protocol.ws.api.registry.IWebSocketConnectionRegistry; +import org.apache.wicket.protocol.ws.api.registry.PageIdKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A helper class that uses the web connection to push components updates to the client. + */ +public class ProgressUpdater +{ + /** + * Marks a page as a listener to task progress. + */ + public interface ITaskProgressListener { + + } + + private static final Logger LOGGER = LoggerFactory.getLogger(ProgressUpdater.class); + + public static ProgressUpdateTask start(Application application, String session, ScheduledExecutorService scheduledExecutorService) + { + // create an asynchronous task that will write the data to the client + ProgressUpdateTask progressUpdateTask = new ProgressUpdateTask(application, session); + scheduledExecutorService.schedule(progressUpdateTask, 1, TimeUnit.SECONDS); + return progressUpdateTask; + } + + /** + * Signal task was canceled. + */ + public static class TaskCanceled implements IWebSocketPushMessage + { + } + + /** + * A push message used to update progress. + */ + public static class ProgressUpdate implements IWebSocketPushMessage + { + + private final int progress; + + public ProgressUpdate(int progress) + { + this.progress = progress; + } + + public int getProgress() + { + return progress; + } + } + + /** + * A task that sends data to the client by pushing it to the web socket connection + */ + public static class ProgressUpdateTask implements Runnable, Serializable + { + /** + * The following fields are needed to be able to lookup the IWebSocketConnection from + * IWebSocketConnectionRegistry + */ + private final String applicationName; + private final String sessionId; + + private boolean canceled = false; + private boolean running = false; Review comment: Those probably should be `volatile` or `AtomicBoolean` ########## File path: wicket-examples/src/main/java/org/apache/wicket/examples/websocket/progress/ProgressUpdater.java ########## @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.examples.websocket.progress; + +import java.io.Serializable; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.apache.wicket.Application; +import org.apache.wicket.WicketRuntimeException; +import org.apache.wicket.page.IManageablePage; +import org.apache.wicket.protocol.ws.WebSocketSettings; +import org.apache.wicket.protocol.ws.api.IWebSocketConnection; +import org.apache.wicket.protocol.ws.api.WebSocketPushBroadcaster; +import org.apache.wicket.protocol.ws.api.message.ConnectedMessage; +import org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage; +import org.apache.wicket.protocol.ws.api.registry.IKey; +import org.apache.wicket.protocol.ws.api.registry.IWebSocketConnectionRegistry; +import org.apache.wicket.protocol.ws.api.registry.PageIdKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A helper class that uses the web connection to push components updates to the client. + */ +public class ProgressUpdater +{ + /** + * Marks a page as a listener to task progress. + */ + public interface ITaskProgressListener { + + } + + private static final Logger LOGGER = LoggerFactory.getLogger(ProgressUpdater.class); + + public static ProgressUpdateTask start(Application application, String session, ScheduledExecutorService scheduledExecutorService) + { + // create an asynchronous task that will write the data to the client + ProgressUpdateTask progressUpdateTask = new ProgressUpdateTask(application, session); + scheduledExecutorService.schedule(progressUpdateTask, 1, TimeUnit.SECONDS); + return progressUpdateTask; + } + + /** + * Signal task was canceled. + */ + public static class TaskCanceled implements IWebSocketPushMessage + { + } + + /** + * A push message used to update progress. + */ + public static class ProgressUpdate implements IWebSocketPushMessage + { + + private final int progress; + + public ProgressUpdate(int progress) + { + this.progress = progress; + } + + public int getProgress() + { + return progress; + } + } + + /** + * A task that sends data to the client by pushing it to the web socket connection + */ + public static class ProgressUpdateTask implements Runnable, Serializable + { + /** + * The following fields are needed to be able to lookup the IWebSocketConnection from + * IWebSocketConnectionRegistry + */ + private final String applicationName; + private final String sessionId; + + private boolean canceled = false; + private boolean running = false; + + private ProgressUpdateTask(Application application, String sessionId) + { + this.applicationName = application.getName(); + this.sessionId = sessionId; + } + + @Override + public void run() + { + running = true; + Application application = Application.get(applicationName); + WebSocketSettings webSocketSettings = WebSocketSettings.Holder.get(application); + + int progress = 0; + + while (progress <= 100) + { + try + { + WebSocketPushBroadcaster broadcaster = + new WebSocketPushBroadcaster(webSocketSettings.getConnectionRegistry()); + + if (canceled) + { + canceled = false; + running = false; + broadcaster.broadcastAllMatchingFilter(application, (sessionId, key) -> + ProgressUpdateTask.this.sessionId.equals(sessionId) && key instanceof PageIdKey + && ITaskProgressListener.class.isAssignableFrom(getPageClass(key)), + new TaskCanceled()); + return; + } + broadcaster.broadcastAllMatchingFilter(application, (sessionId, key) -> + ProgressUpdateTask.this.sessionId.equals(sessionId) && key instanceof PageIdKey && + ITaskProgressListener.class.isAssignableFrom(getPageClass(key)), + new ProgressUpdate(progress)); + + // sleep for a while to simulate work + TimeUnit.SECONDS.sleep(1); + progress++; + } + catch (InterruptedException x) + { + Thread.currentThread().interrupt(); + break; + } + catch (Exception e) + { + LOGGER.error("unexpected exception", e); + break; + } + } + running = false; + } + + protected Class<?> getPageClass(IKey iKey) { + try { + return Thread.currentThread().getContextClassLoader().loadClass(iKey.getContext()); Review comment: At line 110 we get the Application. Let's use it to get the IClassResolver -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
