From e27bde845c514eb8cec5136086d64e79c555866b Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 11:13:18 +0300 Subject: [PATCH 1/9] Start REST service on plugin startup --- LICENSE | 202 ++++++++++++++++++ META-INF/MANIFEST.MF | 1 + README.md | 7 + src/com/vaadin/plugin/Activator.java | 39 ++++ src/com/vaadin/plugin/CopilotRestService.java | 57 +++++ 5 files changed, 306 insertions(+) create mode 100644 LICENSE create mode 100644 src/com/vaadin/plugin/Activator.java create mode 100644 src/com/vaadin/plugin/CopilotRestService.java diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index e3b70f5..704345b 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -5,5 +5,6 @@ Bundle-SymbolicName: vaadin-eclipse-plugin;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Vendor: Vaadin Require-Bundle: org.eclipse.ui +Bundle-Activator: com.vaadin.plugin.Activator Automatic-Module-Name: vaadin.eclipse.plugin Bundle-RequiredExecutionEnvironment: JavaSE-21 diff --git a/README.md b/README.md index 362e39d..46fbf8e 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ # eclipse-plugin + +This repository contains a minimal Eclipse plugin example for Vaadin. +The plugin now starts a small REST service on a random TCP port during startup to allow communication with Vaadin Copilot. + +## License + +This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details. diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java new file mode 100644 index 0000000..dfa7852 --- /dev/null +++ b/src/com/vaadin/plugin/Activator.java @@ -0,0 +1,39 @@ +package com.vaadin.plugin; + +import org.eclipse.core.runtime.Plugin; +import org.osgi.framework.BundleContext; +import java.io.IOException; + +public class Activator extends Plugin { + private static Activator plugin; + private CopilotRestService restService; + + public Activator() { + plugin = this; + } + + public static Activator getDefault() { + return plugin; + } + + @Override + public void start(BundleContext context) throws Exception { + super.start(context); + restService = new CopilotRestService(); + try { + restService.start(); + } catch (IOException e) { + System.err.println("Failed to start Copilot REST service: " + e.getMessage()); + } + } + + @Override + public void stop(BundleContext context) throws Exception { + if (restService != null) { + restService.stop(); + restService = null; + } + plugin = null; + super.stop(context); + } +} diff --git a/src/com/vaadin/plugin/CopilotRestService.java b/src/com/vaadin/plugin/CopilotRestService.java new file mode 100644 index 0000000..4975376 --- /dev/null +++ b/src/com/vaadin/plugin/CopilotRestService.java @@ -0,0 +1,57 @@ +package com.vaadin.plugin; + +import com.sun.net.httpserver.HttpServer; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpExchange; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; + +/** + * Minimal REST service for Vaadin Copilot integration. Starts on a random + * free port and exposes a single /copilot endpoint accepting POST requests. + */ +public class CopilotRestService { + private HttpServer server; + private int port; + + public void start() throws IOException { + server = HttpServer.create(new InetSocketAddress(0), 0); + port = server.getAddress().getPort(); + server.createContext("/copilot", new CopilotHandler()); + server.setExecutor(null); // default executor + server.start(); + System.out.println("Copilot REST service started on port " + port); + } + + public void stop() { + if (server != null) { + server.stop(0); + server = null; + } + } + + public int getPort() { + return port; + } + + private static class CopilotHandler implements HttpHandler { + @Override + public void handle(HttpExchange exchange) throws IOException { + if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) { + exchange.sendResponseHeaders(405, -1); + return; + } + InputStream is = exchange.getRequestBody(); + String body = new String(is.readAllBytes(), StandardCharsets.UTF_8); + System.out.println("Received Copilot request: " + body); + byte[] response = "OK".getBytes(StandardCharsets.UTF_8); + exchange.sendResponseHeaders(200, response.length); + try (OutputStream os = exchange.getResponseBody()) { + os.write(response); + } + } + } +} From 787a076abf129c4f76054a239ad0901c238ec32d Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 12:24:26 +0300 Subject: [PATCH 2/9] Improve Copilot REST service --- README.md | 2 +- src/com/vaadin/plugin/Activator.java | 1 + src/com/vaadin/plugin/CopilotRestService.java | 14 +++++++++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 46fbf8e..47beaf3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # eclipse-plugin This repository contains a minimal Eclipse plugin example for Vaadin. -The plugin now starts a small REST service on a random TCP port during startup to allow communication with Vaadin Copilot. +The plugin now starts a small REST service on a random TCP port during startup to allow communication with Vaadin Copilot. The service exposes a unique `/api/{serviceName}` endpoint and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. ## License diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java index dfa7852..3a7bff3 100644 --- a/src/com/vaadin/plugin/Activator.java +++ b/src/com/vaadin/plugin/Activator.java @@ -22,6 +22,7 @@ public void start(BundleContext context) throws Exception { restService = new CopilotRestService(); try { restService.start(); + System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); } catch (IOException e) { System.err.println("Failed to start Copilot REST service: " + e.getMessage()); } diff --git a/src/com/vaadin/plugin/CopilotRestService.java b/src/com/vaadin/plugin/CopilotRestService.java index 4975376..9a7ea5a 100644 --- a/src/com/vaadin/plugin/CopilotRestService.java +++ b/src/com/vaadin/plugin/CopilotRestService.java @@ -8,22 +8,26 @@ import java.io.OutputStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; +import java.util.UUID; /** * Minimal REST service for Vaadin Copilot integration. Starts on a random - * free port and exposes a single /copilot endpoint accepting POST requests. + * free port and exposes a unique /api/{serviceName} endpoint accepting POST + * requests. */ public class CopilotRestService { private HttpServer server; private int port; + private String serviceName; public void start() throws IOException { + serviceName = "copilot-" + UUID.randomUUID(); server = HttpServer.create(new InetSocketAddress(0), 0); port = server.getAddress().getPort(); - server.createContext("/copilot", new CopilotHandler()); + server.createContext("/api/" + serviceName, new CopilotHandler()); server.setExecutor(null); // default executor server.start(); - System.out.println("Copilot REST service started on port " + port); + System.out.println("Copilot REST service started on " + getEndpoint()); } public void stop() { @@ -37,6 +41,10 @@ public int getPort() { return port; } + public String getEndpoint() { + return "http://127.0.0.1:" + port + "/api/" + serviceName; + } + private static class CopilotHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { From 53a511e1eec75314c6f6b6327c4b26a9467b3662 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 12:24:33 +0300 Subject: [PATCH 3/9] Use OSGi HttpService for Copilot endpoint --- META-INF/MANIFEST.MF | 1 + README.md | 2 +- src/com/vaadin/plugin/Activator.java | 11 +-- src/com/vaadin/plugin/CopilotRestService.java | 78 ++++++++----------- src/com/vaadin/plugin/CopilotServlet.java | 21 +++++ 5 files changed, 62 insertions(+), 51 deletions(-) create mode 100644 src/com/vaadin/plugin/CopilotServlet.java diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index 704345b..bd681da 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -5,6 +5,7 @@ Bundle-SymbolicName: vaadin-eclipse-plugin;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Vendor: Vaadin Require-Bundle: org.eclipse.ui +Import-Package: javax.servlet,javax.servlet.http,org.osgi.service.http Bundle-Activator: com.vaadin.plugin.Activator Automatic-Module-Name: vaadin.eclipse.plugin Bundle-RequiredExecutionEnvironment: JavaSE-21 diff --git a/README.md b/README.md index 47beaf3..5e85c77 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # eclipse-plugin This repository contains a minimal Eclipse plugin example for Vaadin. -The plugin now starts a small REST service on a random TCP port during startup to allow communication with Vaadin Copilot. The service exposes a unique `/api/{serviceName}` endpoint and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. +The plugin now registers a servlet using the OSGi `HttpService` when it starts. The servlet exposes a unique `/api/{serviceName}` endpoint on the container's HTTP server, and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. ## License diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java index 3a7bff3..8001c76 100644 --- a/src/com/vaadin/plugin/Activator.java +++ b/src/com/vaadin/plugin/Activator.java @@ -2,7 +2,8 @@ import org.eclipse.core.runtime.Plugin; import org.osgi.framework.BundleContext; -import java.io.IOException; +import org.osgi.service.http.NamespaceException; +import javax.servlet.ServletException; public class Activator extends Plugin { private static Activator plugin; @@ -21,17 +22,17 @@ public void start(BundleContext context) throws Exception { super.start(context); restService = new CopilotRestService(); try { - restService.start(); + restService.start(context); System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); - } catch (IOException e) { - System.err.println("Failed to start Copilot REST service: " + e.getMessage()); + } catch (ServletException | NamespaceException e) { + System.err.println("Failed to register Copilot servlet: " + e.getMessage()); } } @Override public void stop(BundleContext context) throws Exception { if (restService != null) { - restService.stop(); + restService.stop(context); restService = null; } plugin = null; diff --git a/src/com/vaadin/plugin/CopilotRestService.java b/src/com/vaadin/plugin/CopilotRestService.java index 9a7ea5a..14ea271 100644 --- a/src/com/vaadin/plugin/CopilotRestService.java +++ b/src/com/vaadin/plugin/CopilotRestService.java @@ -1,65 +1,53 @@ package com.vaadin.plugin; -import com.sun.net.httpserver.HttpServer; -import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpExchange; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.service.http.HttpService; +import org.osgi.service.http.NamespaceException; +import javax.servlet.ServletException; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.InetSocketAddress; -import java.nio.charset.StandardCharsets; import java.util.UUID; /** - * Minimal REST service for Vaadin Copilot integration. Starts on a random - * free port and exposes a unique /api/{serviceName} endpoint accepting POST - * requests. + * Registers a simple servlet for Vaadin Copilot integration using the OSGi + * {@link HttpService}. The servlet is bound to a unique "/api/{serviceName}" + * alias and the full endpoint URL can be retrieved via {@link #getEndpoint()}. */ public class CopilotRestService { - private HttpServer server; - private int port; + private HttpService httpService; + private ServiceReference serviceRef; private String serviceName; + private String endpoint; - public void start() throws IOException { + public void start(BundleContext context) throws ServletException, NamespaceException { serviceName = "copilot-" + UUID.randomUUID(); - server = HttpServer.create(new InetSocketAddress(0), 0); - port = server.getAddress().getPort(); - server.createContext("/api/" + serviceName, new CopilotHandler()); - server.setExecutor(null); // default executor - server.start(); - System.out.println("Copilot REST service started on " + getEndpoint()); - } - - public void stop() { - if (server != null) { - server.stop(0); - server = null; + serviceRef = context.getServiceReference(HttpService.class); + if (serviceRef == null) { + throw new IllegalStateException("HttpService not available"); + } + httpService = context.getService(serviceRef); + String alias = "/api/" + serviceName; + httpService.registerServlet(alias, new CopilotServlet(), null, null); + String port = context.getProperty("org.osgi.service.http.port"); + if (port == null) { + port = "8080"; } + endpoint = "http://localhost:" + port + alias; + System.out.println("Copilot REST service registered at " + endpoint); } - public int getPort() { - return port; + public void stop(BundleContext context) { + if (httpService != null) { + String alias = "/api/" + serviceName; + httpService.unregister(alias); + context.ungetService(serviceRef); + httpService = null; + serviceRef = null; + } } public String getEndpoint() { - return "http://127.0.0.1:" + port + "/api/" + serviceName; + return endpoint; } - private static class CopilotHandler implements HttpHandler { - @Override - public void handle(HttpExchange exchange) throws IOException { - if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) { - exchange.sendResponseHeaders(405, -1); - return; - } - InputStream is = exchange.getRequestBody(); - String body = new String(is.readAllBytes(), StandardCharsets.UTF_8); - System.out.println("Received Copilot request: " + body); - byte[] response = "OK".getBytes(StandardCharsets.UTF_8); - exchange.sendResponseHeaders(200, response.length); - try (OutputStream os = exchange.getResponseBody()) { - os.write(response); - } - } - } } diff --git a/src/com/vaadin/plugin/CopilotServlet.java b/src/com/vaadin/plugin/CopilotServlet.java new file mode 100644 index 0000000..126b8c7 --- /dev/null +++ b/src/com/vaadin/plugin/CopilotServlet.java @@ -0,0 +1,21 @@ +package com.vaadin.plugin; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.stream.Collectors; + +/** + * Simple servlet used by {@link CopilotRestService} to receive Copilot requests. + */ +public class CopilotServlet extends HttpServlet { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator())); + System.out.println("Received Copilot request: " + body); + resp.setStatus(HttpServletResponse.SC_OK); + resp.getWriter().write("OK"); + } +} From 4fc4e7d99b6175ab18db30889074b8658cb85ed7 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 13:19:26 +0300 Subject: [PATCH 4/9] Fix activator API usage --- src/com/vaadin/plugin/Activator.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java index 8001c76..5f7f1ff 100644 --- a/src/com/vaadin/plugin/Activator.java +++ b/src/com/vaadin/plugin/Activator.java @@ -1,11 +1,19 @@ package com.vaadin.plugin; -import org.eclipse.core.runtime.Plugin; +import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; import org.osgi.service.http.NamespaceException; import javax.servlet.ServletException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; + +/** + * Bundle activator that starts the Copilot REST service on plugin startup. + */ +public class Activator extends AbstractUIPlugin { + /** The plug-in ID as defined in MANIFEST.MF. */ + public static final String PLUGIN_ID = "vaadin-eclipse-plugin"; -public class Activator extends Plugin { private static Activator plugin; private CopilotRestService restService; @@ -25,7 +33,8 @@ public void start(BundleContext context) throws Exception { restService.start(context); System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); } catch (ServletException | NamespaceException e) { - System.err.println("Failed to register Copilot servlet: " + e.getMessage()); + getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, + "Failed to register Copilot servlet", e)); } } From bfc9b5898f1a5c7228c15e89aa4bf8fdf36e8aad Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 13:26:30 +0300 Subject: [PATCH 5/9] Fix OSGi API access --- META-INF/MANIFEST.MF | 2 +- src/com/vaadin/plugin/Activator.java | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index bd681da..9ae034d 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -5,7 +5,7 @@ Bundle-SymbolicName: vaadin-eclipse-plugin;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Vendor: Vaadin Require-Bundle: org.eclipse.ui -Import-Package: javax.servlet,javax.servlet.http,org.osgi.service.http +Import-Package: javax.servlet,javax.servlet.http,org.osgi.service.http,org.osgi.framework Bundle-Activator: com.vaadin.plugin.Activator Automatic-Module-Name: vaadin.eclipse.plugin Bundle-RequiredExecutionEnvironment: JavaSE-21 diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java index 5f7f1ff..5880af6 100644 --- a/src/com/vaadin/plugin/Activator.java +++ b/src/com/vaadin/plugin/Activator.java @@ -4,8 +4,6 @@ import org.osgi.framework.BundleContext; import org.osgi.service.http.NamespaceException; import javax.servlet.ServletException; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; /** * Bundle activator that starts the Copilot REST service on plugin startup. @@ -33,8 +31,8 @@ public void start(BundleContext context) throws Exception { restService.start(context); System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); } catch (ServletException | NamespaceException e) { - getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, - "Failed to register Copilot servlet", e)); + System.err.println("Failed to register Copilot servlet"); + e.printStackTrace(); } } From 66ce9aa9f5edfbff53e31af42a382e676fe2d352 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 13:38:55 +0300 Subject: [PATCH 6/9] Use BundleActivator for servlet startup --- README.md | 2 +- src/com/vaadin/plugin/Activator.java | 19 ++----------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5e85c77..8d7e162 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # eclipse-plugin This repository contains a minimal Eclipse plugin example for Vaadin. -The plugin now registers a servlet using the OSGi `HttpService` when it starts. The servlet exposes a unique `/api/{serviceName}` endpoint on the container's HTTP server, and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. +The plug-in defines a `BundleActivator` that registers a servlet using the OSGi `HttpService` when it starts. The servlet exposes a unique `/api/{serviceName}` endpoint on the container's HTTP server, and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. ## License diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java index 5880af6..80472e0 100644 --- a/src/com/vaadin/plugin/Activator.java +++ b/src/com/vaadin/plugin/Activator.java @@ -1,6 +1,6 @@ package com.vaadin.plugin; -import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.service.http.NamespaceException; import javax.servlet.ServletException; @@ -8,24 +8,11 @@ /** * Bundle activator that starts the Copilot REST service on plugin startup. */ -public class Activator extends AbstractUIPlugin { - /** The plug-in ID as defined in MANIFEST.MF. */ - public static final String PLUGIN_ID = "vaadin-eclipse-plugin"; - - private static Activator plugin; +public class Activator implements BundleActivator { private CopilotRestService restService; - public Activator() { - plugin = this; - } - - public static Activator getDefault() { - return plugin; - } - @Override public void start(BundleContext context) throws Exception { - super.start(context); restService = new CopilotRestService(); try { restService.start(context); @@ -42,7 +29,5 @@ public void stop(BundleContext context) throws Exception { restService.stop(context); restService = null; } - plugin = null; - super.stop(context); } } From a8843ed7deda950e59a77cac4d41e14e30c4f945 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 13:47:55 +0300 Subject: [PATCH 7/9] Ensure REST service starts when Eclipse launches --- plugin.xml | 7 ++++++- src/com/vaadin/plugin/EarlyStartup.java | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/com/vaadin/plugin/EarlyStartup.java diff --git a/plugin.xml b/plugin.xml index d85dcef..d845d6a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -57,6 +57,11 @@ - + + + + + diff --git a/src/com/vaadin/plugin/EarlyStartup.java b/src/com/vaadin/plugin/EarlyStartup.java new file mode 100644 index 0000000..48c704d --- /dev/null +++ b/src/com/vaadin/plugin/EarlyStartup.java @@ -0,0 +1,15 @@ +package com.vaadin.plugin; + +import org.eclipse.ui.IStartup; + +/** + * Triggers plug-in activation when the Eclipse workbench starts so that the + * {@link Activator} can register the REST service. + */ +public class EarlyStartup implements IStartup { + @Override + public void earlyStartup() { + // Intentionally left empty - the presence of this class ensures that + // the bundle is started during Eclipse startup. + } +} From c7971820fb6fb1c1d6c410fb5595f554fb04279b Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 15:01:51 +0300 Subject: [PATCH 8/9] Start REST service from EarlyStartup --- META-INF/MANIFEST.MF | 1 - README.md | 2 +- src/com/vaadin/plugin/Activator.java | 33 ------------------------- src/com/vaadin/plugin/EarlyStartup.java | 31 ++++++++++++++++++++--- 4 files changed, 28 insertions(+), 39 deletions(-) delete mode 100644 src/com/vaadin/plugin/Activator.java diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index 9ae034d..6c9a3c1 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -6,6 +6,5 @@ Bundle-Version: 1.0.0.qualifier Bundle-Vendor: Vaadin Require-Bundle: org.eclipse.ui Import-Package: javax.servlet,javax.servlet.http,org.osgi.service.http,org.osgi.framework -Bundle-Activator: com.vaadin.plugin.Activator Automatic-Module-Name: vaadin.eclipse.plugin Bundle-RequiredExecutionEnvironment: JavaSE-21 diff --git a/README.md b/README.md index 8d7e162..3869c61 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # eclipse-plugin This repository contains a minimal Eclipse plugin example for Vaadin. -The plug-in defines a `BundleActivator` that registers a servlet using the OSGi `HttpService` when it starts. The servlet exposes a unique `/api/{serviceName}` endpoint on the container's HTTP server, and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. +The plug-in registers a servlet using the OSGi `HttpService` when the workbench starts. The servlet exposes a unique `/api/{serviceName}` endpoint on the container's HTTP server, and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. ## License diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java deleted file mode 100644 index 80472e0..0000000 --- a/src/com/vaadin/plugin/Activator.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.vaadin.plugin; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.service.http.NamespaceException; -import javax.servlet.ServletException; - -/** - * Bundle activator that starts the Copilot REST service on plugin startup. - */ -public class Activator implements BundleActivator { - private CopilotRestService restService; - - @Override - public void start(BundleContext context) throws Exception { - restService = new CopilotRestService(); - try { - restService.start(context); - System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); - } catch (ServletException | NamespaceException e) { - System.err.println("Failed to register Copilot servlet"); - e.printStackTrace(); - } - } - - @Override - public void stop(BundleContext context) throws Exception { - if (restService != null) { - restService.stop(context); - restService = null; - } - } -} diff --git a/src/com/vaadin/plugin/EarlyStartup.java b/src/com/vaadin/plugin/EarlyStartup.java index 48c704d..906f61b 100644 --- a/src/com/vaadin/plugin/EarlyStartup.java +++ b/src/com/vaadin/plugin/EarlyStartup.java @@ -1,15 +1,38 @@ package com.vaadin.plugin; import org.eclipse.ui.IStartup; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.BundleListener; +import org.osgi.framework.FrameworkUtil; /** - * Triggers plug-in activation when the Eclipse workbench starts so that the - * {@link Activator} can register the REST service. + * Starts the REST service when the workbench starts. The service is registered + * using the OSGi {@code HttpService} and stopped when the bundle shuts down. */ public class EarlyStartup implements IStartup { + private CopilotRestService restService; + @Override public void earlyStartup() { - // Intentionally left empty - the presence of this class ensures that - // the bundle is started during Eclipse startup. + BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); + restService = new CopilotRestService(); + try { + restService.start(context); + System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); + } catch (Exception e) { + System.err.println("Failed to register Copilot servlet"); + e.printStackTrace(); + } + + context.addBundleListener(new BundleListener() { + @Override + public void bundleChanged(BundleEvent event) { + if (event.getBundle().equals(FrameworkUtil.getBundle(getClass())) && + event.getType() == BundleEvent.STOPPING) { + restService.stop(context); + } + } + }); } } From a1f437cda0b571c0479503c88c2b486887b0c308 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 23 Jun 2025 15:12:30 +0300 Subject: [PATCH 9/9] Start REST service via bundle activator --- META-INF/MANIFEST.MF | 1 + README.md | 4 ++-- src/com/vaadin/plugin/Activator.java | 27 +++++++++++++++++++++++++ src/com/vaadin/plugin/EarlyStartup.java | 26 ++---------------------- 4 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 src/com/vaadin/plugin/Activator.java diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index 6c9a3c1..928f7e9 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -5,6 +5,7 @@ Bundle-SymbolicName: vaadin-eclipse-plugin;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Vendor: Vaadin Require-Bundle: org.eclipse.ui +Bundle-Activator: com.vaadin.plugin.Activator Import-Package: javax.servlet,javax.servlet.http,org.osgi.service.http,org.osgi.framework Automatic-Module-Name: vaadin.eclipse.plugin Bundle-RequiredExecutionEnvironment: JavaSE-21 diff --git a/README.md b/README.md index 3869c61..a4a7b48 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # eclipse-plugin -This repository contains a minimal Eclipse plugin example for Vaadin. -The plug-in registers a servlet using the OSGi `HttpService` when the workbench starts. The servlet exposes a unique `/api/{serviceName}` endpoint on the container's HTTP server, and the full endpoint URL is available in the `vaadin.copilot.endpoint` system property. +This repository contains a minimal Eclipse plug-in example for Vaadin. +At workbench startup the plug-in is activated via an `org.eclipse.ui.startup` extension. The `BundleActivator` registers a servlet using the OSGi `HttpService`. The servlet exposes a unique `/api/{serviceName}` endpoint on the container's HTTP server, and the full endpoint URL is stored in the `vaadin.copilot.endpoint` system property. ## License diff --git a/src/com/vaadin/plugin/Activator.java b/src/com/vaadin/plugin/Activator.java new file mode 100644 index 0000000..f5165ad --- /dev/null +++ b/src/com/vaadin/plugin/Activator.java @@ -0,0 +1,27 @@ +package com.vaadin.plugin; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; + +/** + * Bundle activator that starts the REST service when the plug-in is + * activated and stops it on shutdown. + */ +public class Activator implements BundleActivator { + private CopilotRestService restService; + + @Override + public void start(BundleContext context) throws Exception { + restService = new CopilotRestService(); + restService.start(context); + System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); + } + + @Override + public void stop(BundleContext context) throws Exception { + if (restService != null) { + restService.stop(context); + restService = null; + } + } +} diff --git a/src/com/vaadin/plugin/EarlyStartup.java b/src/com/vaadin/plugin/EarlyStartup.java index 906f61b..cab972f 100644 --- a/src/com/vaadin/plugin/EarlyStartup.java +++ b/src/com/vaadin/plugin/EarlyStartup.java @@ -1,38 +1,16 @@ package com.vaadin.plugin; import org.eclipse.ui.IStartup; -import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleEvent; -import org.osgi.framework.BundleListener; -import org.osgi.framework.FrameworkUtil; + /** * Starts the REST service when the workbench starts. The service is registered * using the OSGi {@code HttpService} and stopped when the bundle shuts down. */ public class EarlyStartup implements IStartup { - private CopilotRestService restService; @Override public void earlyStartup() { - BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext(); - restService = new CopilotRestService(); - try { - restService.start(context); - System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint()); - } catch (Exception e) { - System.err.println("Failed to register Copilot servlet"); - e.printStackTrace(); - } - - context.addBundleListener(new BundleListener() { - @Override - public void bundleChanged(BundleEvent event) { - if (event.getBundle().equals(FrameworkUtil.getBundle(getClass())) && - event.getType() == BundleEvent.STOPPING) { - restService.stop(context); - } - } - }); + // Trigger plug-in activation so the BundleActivator runs } }