Skip to content

Commit

Permalink
feat: Add version with system info.
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinBetanc0urt committed Dec 9, 2024
1 parent a97110f commit 36813bc
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 33 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ jobs:
java-version: 11
architecture: x64

- name: Set Main Version
run: echo "MAIN_VERSION=${{ github.event.release.name }}" >> src/main/java/org/spin/base/version.properties

- name: Set Release Date
run: echo "DATE_VERSION=$(date +'%Y-%m-%d')" >> src/main/java/org/spin/base/version.properties

- name: Set Implementation Version
run: echo "IMPLEMENTATION_VERSION=${{ github.event.release.tag_name }}" >> src/main/java/org/spin/base/version.properties

- name: Build with Gradle
uses: gradle/actions/setup-gradle@v3
with:
Expand All @@ -45,6 +54,7 @@ jobs:
with:
name: adempiere-processors-service.dsc
path: build/descriptors/adempiere-processors-service.dsc
retention-days: 1

- name: Upload dist app zip artifact
uses: actions/upload-artifact@v4
Expand Down
18 changes: 11 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ protobuf {
}

sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
srcDirs 'src/main/proto'
}
}
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
srcDirs 'src/main/proto'
}
resources {
srcDirs 'src/main/java'
include 'org/spin/base/version.properties'
}
}
}

// Copy proto descriptor another folder
Expand Down
Binary file not shown.
94 changes: 94 additions & 0 deletions src/main/java/org/spin/base/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/************************************************************************************
* Copyright (C) 2018-present E.R.P. Consultores y Asociados, C.A. *
* Contributor(s): Edwin Betancourt, [email protected] *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
************************************************************************************/

package org.spin.base;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Properties;

import org.compiere.util.CLogger;

/**
* @author Edwin Betancourt, [email protected], https://github.com/EdwinBetanc0urt
* Service for backend of Version
*/
public final class Version
{

private static CLogger log = CLogger.getCLogger(Version.class);

/** Main Version String */
// Conventions for naming second number is even for stable, and odd for unstable
// the releases will have a suffix (a) for alpha - (b) for beta - (t) for trunk - (s) for stable - and (LTS) for long term support
public static String MAIN_VERSION = "1.0.0-dev";

/** Detail Version as date Used for Client/Server */
public static String DATE_VERSION = (new SimpleDateFormat("yyyy-MM-dd")).format(
new Date(
System.currentTimeMillis()
)
);

public static String IMPLEMENTATION_VERSION = "1.0.0-dev";

static {
ClassLoader loader = Version.class.getClassLoader();
InputStream inputStream = loader.getResourceAsStream("org/spin/base/version.properties");
if (inputStream != null) {
Properties properties = new Properties();
try {
properties.load(inputStream);

if (properties.containsKey("MAIN_VERSION")) {
MAIN_VERSION = properties.getProperty("MAIN_VERSION");
}
if (properties.containsKey("DATE_VERSION")) {
DATE_VERSION = properties.getProperty("DATE_VERSION");
}
if (properties.containsKey("IMPLEMENTATION_VERSION")) {
IMPLEMENTATION_VERSION = properties.getProperty("IMPLEMENTATION_VERSION");
}
} catch (IOException e) {
// file does not exists
log.warning("File version.properties does no exists");
}
}
}

/**
* Get Product Version
* @return Application Version
*/
public static String getVersion()
{
return MAIN_VERSION + " @ " + DATE_VERSION;
} // getVersion

public static String getMainVersion() {
return MAIN_VERSION;
}

public static String getDateVersion() {
return DATE_VERSION;
}

public static String getImplementationVersion() {
return IMPLEMENTATION_VERSION;
}

} // Adempiere
22 changes: 20 additions & 2 deletions src/main/java/org/spin/processor/controller/Processors.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.compiere.util.CLogger;
import org.spin.proto.processor.RunProcessorRequest;
import org.spin.proto.processor.RunProcessorResponse;
import org.spin.proto.processor.SystemInfo;
import org.spin.processor.service.Service;
import org.spin.proto.processor.GetSystemInfoRequest;
import org.spin.proto.processor.ProcessorsGrpc.ProcessorsImplBase;

import io.grpc.Status;
Expand All @@ -27,8 +29,24 @@ public class Processors extends ProcessorsImplBase {

/** Logger */
private CLogger log = CLogger.getCLogger(Processors.class);




@Override
public void getSystemInfo(GetSystemInfoRequest request, StreamObserver<SystemInfo> responseObserver) {
try {
SystemInfo.Builder builder = Service.getSystemInfo();
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
} catch (Exception e) {
log.severe(e.getLocalizedMessage());
responseObserver.onError(Status.INTERNAL
.withDescription(e.getLocalizedMessage())
.withCause(e)
.asRuntimeException()
);
}
}

@Override
public void runAccounting(RunProcessorRequest request, StreamObserver<RunProcessorResponse> responseObserver) {
try {
Expand Down
71 changes: 50 additions & 21 deletions src/main/java/org/spin/processor/server/ProcessorServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import org.compiere.util.Env;
Expand All @@ -33,8 +35,30 @@

public class ProcessorServer {
private static final Logger logger = Logger.getLogger(ProcessorServer.class.getName());

private ServiceContextProvider contextProvider = new ServiceContextProvider();

private Server server;

/** Services/Methods allow request without Bearer token validation */
private List<String> ALLOW_REQUESTS_WITHOUT_TOKEN = Arrays.asList(
// proto package . proto service / proto method
"processor.Processors/GetSystemInfo"
);

/** Revoke session */
private List<String> REVOKE_TOKEN_SERVICES = Arrays.asList(
// proto package . proto service / proto method
);

private AuthorizationServerInterceptor getInterceptor() {
AuthorizationServerInterceptor interceptor = new AuthorizationServerInterceptor();
interceptor.setAllowRequestsWithoutToken(this.ALLOW_REQUESTS_WITHOUT_TOKEN);
interceptor.setRevokeTokenServices(this.REVOKE_TOKEN_SERVICES);
return interceptor;
}


/**
* Get SSL / TLS context
* @return
Expand All @@ -48,10 +72,11 @@ private SslContextBuilder getSslContextBuilder() {
}
return GrpcSslContexts.configure(sslClientContextBuilder);
}

private void start() throws IOException {
// Start based on provider
Env.setContextProvider(contextProvider);
Env.setContextProvider(contextProvider);

logger.info("Service Template added on " + SetupLoader.getInstance().getServer().getPort());
//
ServerBuilder<?> serverBuilder;
Expand All @@ -60,8 +85,12 @@ private void start() throws IOException {
.sslContext(getSslContextBuilder().build());
} else {
serverBuilder = ServerBuilder.forPort(SetupLoader.getInstance().getServer().getPort());
serverBuilder.intercept(new AuthorizationServerInterceptor());
}

// Validate JWT on all requests
AuthorizationServerInterceptor interceptor = getInterceptor();
serverBuilder.intercept(interceptor);

serverBuilder.addService(new Processors());
server = serverBuilder.build().start();
logger.info("Server started, listening on " + SetupLoader.getInstance().getServer().getPort());
Expand All @@ -75,22 +104,22 @@ public void run() {
}
});
}

private void stop() {
if (server != null) {
server.shutdown();
}
if (server != null) {
server.shutdown();
}
}

/**
* Await termination on the main thread since the grpc library uses daemon threads.
*/
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
}

public static void main(String[] args) throws Exception {
if (args == null) {
throw new Exception("Arguments Not Found");
Expand All @@ -99,15 +128,15 @@ public static void main(String[] args) throws Exception {
if (args.length == 0) {
throw new Exception("Arguments Must Be: [property file name]");
}
String setupFileName = args[0];
if(setupFileName == null || setupFileName.trim().length() == 0) {
throw new Exception("Setup File not found");
}
SetupLoader.loadSetup(setupFileName);
// Validate load
SetupLoader.getInstance().validateLoad();
final ProcessorServer server = new ProcessorServer();
server.start();
server.blockUntilShutdown();
}
String setupFileName = args[0];
if(setupFileName == null || setupFileName.trim().length() == 0) {
throw new Exception("Setup File not found");
}
SetupLoader.loadSetup(setupFileName);
// Validate load
SetupLoader.getInstance().validateLoad();
final ProcessorServer server = new ProcessorServer();
server.start();
server.blockUntilShutdown();
}
}
36 changes: 34 additions & 2 deletions src/main/java/org/spin/processor/service/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.compiere.util.Msg;
import org.compiere.util.Util;
import org.eevolution.services.dsl.ProcessBuilder;
import org.spin.base.Version;
import org.spin.eca46.process.AcctProcessor;
import org.spin.eca46.process.AlertProcessor;
import org.spin.eca46.process.ProjectProcessor;
Expand All @@ -32,10 +33,37 @@
import org.spin.eca46.process.WorkflowProcessor;
import org.spin.proto.processor.ProcessorLog;
import org.spin.proto.processor.RunProcessorResponse;
import org.spin.proto.processor.SystemInfo;
import org.spin.service.grpc.util.value.StringManager;
import org.spin.service.grpc.util.value.TimeManager;
import org.spin.service.grpc.util.value.ValueManager;

public class Service {


public static SystemInfo.Builder getSystemInfo() {
SystemInfo.Builder builder = SystemInfo.newBuilder();
// backend info
builder.setDateVersion(
ValueManager.getTimestampFromDate(
TimeManager.getTimestampFromString(
Version.DATE_VERSION
)
)
)
.setMainVersion(
StringManager.getValidString(
Version.MAIN_VERSION
)
)
.setImplementationVersion(
StringManager.getValidString(
Version.IMPLEMENTATION_VERSION
)
)
;
return builder;
}

public static RunProcessorResponse.Builder runAccounting(int clientId, int id) {
return runProcess(AcctProcessor.getProcessId(), AcctProcessor.C_ACCTPROCESSOR_ID, clientId, id);
}
Expand Down Expand Up @@ -104,7 +132,11 @@ private static RunProcessorResponse.Builder runProcess(int processId, String par
// Set error message
String summary = Msg.parseTranslation(Env.getCtx(), result.getSummary());
if(Util.isEmpty(summary, true)) {
result.setSummary(ValueManager.validateNull(e.getLocalizedMessage()));
result.setSummary(
StringManager.getValidString(
e.getLocalizedMessage()
)
);
}
}
//
Expand Down
Loading

0 comments on commit 36813bc

Please sign in to comment.