Skip to content

Commit

Permalink
Separated crac dep
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kec <[email protected]>
  • Loading branch information
danielkec committed Jan 9, 2025
1 parent 0f704d2 commit 1ff2732
Show file tree
Hide file tree
Showing 14 changed files with 549 additions and 38 deletions.
45 changes: 45 additions & 0 deletions crac/bridge/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024 Oracle and/or its affiliates.
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.helidon.crac</groupId>
<artifactId>helidon-crac-project</artifactId>
<version>4.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-crac-bridge</artifactId>
<name>Helidon CRaC support SPI bridge</name>

<dependencies>
<dependency>
<groupId>io.helidon.crac</groupId>
<artifactId>helidon-crac-spi</artifactId>
<version>4.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>

</project>
216 changes: 216 additions & 0 deletions crac/bridge/src/main/java/io/helidon/crac/CracImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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.
*/

package io.helidon.crac;

import io.helidon.crac.spi.Crac;

import org.crac.CheckpointException;
import org.crac.RestoreException;

public class CracImpl implements Crac {

System.Logger LOGGER = System.getLogger(CracImpl.class.getName());

@Override
public io.helidon.crac.spi.Crac.Context<Resource> getGlobalContext() {
return new HelidonContext(org.crac.Core.getGlobalContext());
}

@Override
public void checkpointRestore() {
if ("onStart".equalsIgnoreCase(System.getProperty("io.helidon.crac.checkpoint"))) {
try {
org.crac.Core.checkpointRestore();
} catch (UnsupportedOperationException e) {
LOGGER.log(System.Logger.Level.DEBUG, "CRaC feature is not available", e);
} catch (org.crac.RestoreException e) {
LOGGER.log(System.Logger.Level.ERROR, "CRaC restore wasn't successful!", e);
} catch (org.crac.CheckpointException e) {
LOGGER.log(System.Logger.Level.ERROR, "CRaC checkpoint creation wasn't successful!", e);
}
}
}

private static class HelidonContext extends io.helidon.crac.spi.Crac.Context<Resource> {

private final org.crac.Context<org.crac.Resource> delegate;

HelidonContext(org.crac.Context<org.crac.Resource> delegate) {
this.delegate = delegate;
}

@SuppressWarnings("unchecked")
@Override
public void beforeCheckpoint(io.helidon.crac.spi.Crac.Context<? extends io.helidon.crac.spi.Crac.Resource> context)
throws io.helidon.crac.spi.Crac.CheckpointException {
try {
delegate.beforeCheckpoint(new CracContext((Context<Resource>) context));
} catch (org.crac.CheckpointException e) {
throw new HelidonCheckpointException(e);
}
}

@SuppressWarnings("unchecked")
@Override
public void afterRestore(Context<? extends Resource> context) throws io.helidon.crac.spi.Crac.RestoreException {
try {
delegate.afterRestore(new CracContext((Context<Resource>) context));
} catch (org.crac.RestoreException e) {
throw new HelidonRestoreException(e);
}
}

@Override
public void register(Resource resource) {
delegate.register(new CracResource(resource));
}

}

private static class HelidonCheckpointException extends io.helidon.crac.spi.Crac.CheckpointException {
private final org.crac.CheckpointException delegate;

public HelidonCheckpointException(org.crac.CheckpointException delegate) {
this.delegate = delegate;
this.setStackTrace(delegate.getStackTrace());
}

@Override
public String getMessage() {
return delegate.getMessage();
}

}

private static class CracCheckpointException extends org.crac.CheckpointException {
private final io.helidon.crac.spi.Crac.CheckpointException delegate;

public CracCheckpointException(io.helidon.crac.spi.Crac.CheckpointException delegate) {
this.delegate = delegate;
this.setStackTrace(delegate.getStackTrace());
}

@Override
public String getMessage() {
return delegate.getMessage();
}

}

private static class HelidonRestoreException extends io.helidon.crac.spi.Crac.RestoreException {
private final org.crac.RestoreException delegate;

public HelidonRestoreException(org.crac.RestoreException delegate) {
this.delegate = delegate;
this.setStackTrace(delegate.getStackTrace());
}

@Override
public String getMessage() {
return delegate.getMessage();
}
}

private static class CracRestoreException extends org.crac.RestoreException {
private final io.helidon.crac.spi.Crac.RestoreException delegate;

public CracRestoreException(io.helidon.crac.spi.Crac.RestoreException delegate) {
this.delegate = delegate;
this.setStackTrace(delegate.getStackTrace());
}

@Override
public String getMessage() {
return delegate.getMessage();
}
}

private static class CracContext extends org.crac.Context<org.crac.Resource> {

private final io.helidon.crac.spi.Crac.Context<io.helidon.crac.spi.Crac.Resource> delegate;

CracContext(io.helidon.crac.spi.Crac.Context<io.helidon.crac.spi.Crac.Resource> delegate) {
this.delegate = delegate;
}

@SuppressWarnings("unchecked")
@Override
public void beforeCheckpoint(org.crac.Context<? extends org.crac.Resource> context) throws org.crac.CheckpointException {
try {
delegate.beforeCheckpoint(new HelidonContext((org.crac.Context<org.crac.Resource>) context));
} catch (CheckpointException e) {
throw new CracCheckpointException(e);
}
}

@SuppressWarnings("unchecked")
@Override
public void afterRestore(org.crac.Context<? extends org.crac.Resource> context) throws org.crac.RestoreException {
try {
delegate.afterRestore(new HelidonContext((org.crac.Context<org.crac.Resource>) context));
} catch (RestoreException e) {
throw new CracRestoreException(e);
}
}

@Override
public void register(org.crac.Resource resource) {
delegate.register(new HelidonResource(resource));
}

}

private record CracResource(Resource delegate) implements org.crac.Resource {

@SuppressWarnings("unchecked")
@Override
public void beforeCheckpoint(org.crac.Context<? extends org.crac.Resource> context) throws Exception {
delegate.beforeCheckpoint(new HelidonContext((org.crac.Context<org.crac.Resource>) context));
}

@SuppressWarnings("unchecked")
@Override
public void afterRestore(org.crac.Context<? extends org.crac.Resource> context) throws Exception {
delegate.afterRestore(new HelidonContext((org.crac.Context<org.crac.Resource>) context));
}

}

private static class HelidonResource implements io.helidon.crac.spi.Crac.Resource {

private final org.crac.Resource delegate;

HelidonResource(org.crac.Resource delegate) {
this.delegate = delegate;
}

@SuppressWarnings("unchecked")
@Override
public void beforeCheckpoint(io.helidon.crac.spi.Crac.Context<? extends io.helidon.crac.spi.Crac.Resource> context)
throws Exception {
delegate.beforeCheckpoint(new CracContext((Context<Resource>) context));
}

@SuppressWarnings("unchecked")
@Override
public void afterRestore(io.helidon.crac.spi.Crac.Context<? extends io.helidon.crac.spi.Crac.Resource> context)
throws Exception {
delegate.afterRestore(new CracContext((Context<Resource>) context));
}

}
}
6 changes: 6 additions & 0 deletions crac/bridge/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module io.helidon.crac {
requires crac;
requires io.helidon.crac.spi;
exports io.helidon.crac to io.helidon.webserver;
provides io.helidon.crac.spi.Crac with io.helidon.crac.CracImpl;
}
40 changes: 40 additions & 0 deletions crac/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024 Oracle and/or its affiliates.
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.helidon</groupId>
<artifactId>helidon-project</artifactId>
<version>4.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<packaging>pom</packaging>

<groupId>io.helidon.crac</groupId>
<artifactId>helidon-crac-project</artifactId>
<name>Helidon CRaC support project</name>

<modules>
<module>spi</module>
<module>bridge</module>
</modules>

</project>
32 changes: 32 additions & 0 deletions crac/spi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024 Oracle and/or its affiliates.
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.helidon.crac</groupId>
<artifactId>helidon-crac-project</artifactId>
<version>4.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-crac-spi</artifactId>
<name>Helidon CRaC support SPI</name>

</project>
Loading

0 comments on commit 1ff2732

Please sign in to comment.