Skip to content

Commit

Permalink
Impl eclipse-osgi-technology#44 - Add CRaC support
Browse files Browse the repository at this point in the history
  • Loading branch information
fipro78 committed Oct 4, 2024
1 parent 1fef9eb commit 94e0f4f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
25 changes: 25 additions & 0 deletions org.eclipse.osgitech.rest.jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.http.jetty</artifactId>
</dependency>
<dependency>
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -117,6 +121,27 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<id>jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<bnd><![CDATA[
Import-Package: org.crac.*;resolution:=optional, *
-noextraheaders: true
-noimportjava: true
-fixupmessages: The JAR is empty:
]]></bnd>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-resolver-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2012 - 2022 Data In Motion and others.
* Copyright (c) 2012 - 2024 Data In Motion and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the terms of the
Expand All @@ -10,6 +10,7 @@
* Data In Motion - initial API and implementation
* Stefan Bishof - API and implementation
* Tim Ward - implementation
* Dirk Fauth - add CRaC support
*/
package org.eclipse.osgitech.rest.jetty;

Expand Down Expand Up @@ -62,6 +63,10 @@ public class JettyBackedWhiteboardComponent {
Logger logger = Logger.getLogger(JettyBackedWhiteboardComponent.class.getName());

private JerseyServiceRuntime<WhiteboardServletContainer> serviceRuntime;

// need to keep a strong reference to avoid that the resource gets garbage collected
@SuppressWarnings("unused")
private JettyCracResource cracHandler;

public enum State {
INIT, STARTED, STOPPED, EXCEPTION
Expand Down Expand Up @@ -93,6 +98,17 @@ public void activate(BundleContext context, Map<String, Object> properties) thro


serviceRuntime.start(getServiceRuntimeProperties(properties));

try {
Class.forName("org.crac.Resource");

// org.crac.Resource was found, so we create an instance of the JettyCracResource
cracHandler = new JettyCracResource(this);
} catch (ClassNotFoundException e) {
// org.crac.Resource could not be found
// we simply do nothing, as CRaC support is not available
}

}

private Map<String, Object> getServiceRuntimeProperties(Map<String, Object> properties) {
Expand Down Expand Up @@ -348,4 +364,12 @@ private void stopServer() {
logger.log(Level.SEVERE, "Error stopping Jetty server", e);
}
}

/**
*
* @return the Jetty server managed by this class. Can be <code>null</code>.
*/
Server getJettyServer() {
return jettyServer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2024 Dirk Fauth and others.
* All rights reserved.
*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Dirk Fauth - initial API and implementation
*/
package org.eclipse.osgitech.rest.jetty;

import java.util.Arrays;

import org.crac.Context;
import org.crac.Core;
import org.crac.Resource;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.LifeCycle;

/**
* This class is used to add CRaC support to the jetty bundle by using the
* <code>org.crac</code> API. It adapts the examples and best practices from the
* CRaC documentation and the examples.
*
* @see <a href="https://github.com/CRaC/org.crac">`org.crac` API</a>
* @see <a href=
* "https://docs.azul.com/core/crac/crac-tips-tricks#implementing-resource-as-inner-class">Implementing
* Resource as Inner Class</a>
* @see <a href=
* "https://github.com/CRaC/docs/blob/master/STEP-BY-STEP.md">Step-by-step
* CRaC support for a Jetty app</a>
* @see <a href=
* "https://github.com/CRaC/example-jetty/blob/master/src/main/java/com/example/App.java">CRaC
* example-jetty</a>
*/
public class JettyCracResource {

// the org.crac.Resource is implemented as an inner class and kept as a strong
// reference to avoid that it is garbage collected after the registration.
private Resource cracHandler;

public JettyCracResource(JettyBackedWhiteboardComponent jettyComponent) {
cracHandler = new Resource() {
@Override
public void beforeCheckpoint(Context<? extends Resource> context) {
Server jettyServer = jettyComponent.getJettyServer();
if (jettyServer != null && !jettyServer.isStopped()) {
// Stop the connectors only and keep the expensive application running
Arrays.asList(jettyServer.getConnectors()).forEach(c -> LifeCycle.stop(c));
}
}

@Override
public void afterRestore(Context<? extends Resource> context) {
Server jettyServer = jettyComponent.getJettyServer();
if (jettyServer != null && !jettyServer.isStopped()) {
Arrays.asList(jettyServer.getConnectors()).forEach(c -> LifeCycle.start(c));
}
}
};
Core.getGlobalContext().register(cracHandler);
}
}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<asm.version>9.5</asm.version>
<jackson.version>2.15.2</jackson.version>
<parsson.version>1.1.5</parsson.version>
<crac.version>1.5.0</crac.version>
</properties>

<modules>
Expand Down Expand Up @@ -464,6 +465,12 @@
<artifactId>biz.aQute.bnd.annotation</artifactId>
<version>6.4.0</version>
</dependency>
<dependency>
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
<version>${crac.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 94e0f4f

Please sign in to comment.