Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ 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
Bundle-ActivationPolicy: lazy
Import-Package: org.osgi.framework,com.sun.net.httpserver
Automatic-Module-Name: vaadin.eclipse.plugin
Bundle-RequiredExecutionEnvironment: JavaSE-21
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# eclipse-plugin

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` starts a small REST service using the JDK `HttpServer`. The server exposes a `/api/copilot` endpoint and its URL is stored in the `vaadin.copilot.endpoint` system property.

## License

This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details.
7 changes: 6 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
</command>
</toolbar>
</menuContribution>
</extension>
</extension>

<extension
point="org.eclipse.ui.startup">
<startup class="com.vaadin.plugin.EarlyStartup"/>
</extension>

</plugin>
26 changes: 26 additions & 0 deletions src/com/vaadin/plugin/Activator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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();
System.setProperty("vaadin.copilot.endpoint", restService.getEndpoint());
}

@Override
public void stop(BundleContext context) throws Exception {
if (restService != null) {
restService.stop();
restService = null;
}
}
}
61 changes: 61 additions & 0 deletions src/com/vaadin/plugin/CopilotRestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.vaadin.plugin;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**
* Starts a small HTTP server for Copilot integration.
*/
public class CopilotRestService {
private HttpServer server;
private String endpoint;

/** Start the embedded HTTP server on a random port. */
public void start() throws IOException {
server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 0), 0);
server.createContext("/vaadin/copilot", new Handler());
server.start();
endpoint = "http://localhost:" + server.getAddress().getPort() + "/vaadin/copilot";
System.out.println("Copilot REST service started at " + endpoint);
}

/** Stop the server if it is running. */
public void stop() {
if (server != null) {
server.stop(0);
server = null;
}
}

/** Returns the full endpoint URL. */
public String getEndpoint() {
return endpoint;
}

private static class Handler 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[] resp = "OK".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, resp.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(resp);
}
}
}
}
15 changes: 15 additions & 0 deletions src/com/vaadin/plugin/EarlyStartup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.vaadin.plugin;

import org.eclipse.ui.IStartup;

/**
* Ensures the plug-in is activated when the workbench starts so the {@link Activator} can launch the embedded REST
* service.
*/
public class EarlyStartup implements IStartup {

@Override
public void earlyStartup() {
// Trigger plug-in activation so the BundleActivator runs
}
}