Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove static object #508

Merged
merged 11 commits into from
Oct 13, 2021
4 changes: 2 additions & 2 deletions classifai-api/src/main/java/ai/classifai/ClassifaiApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ClassifaiApp
public static void main(String[] args)
{
//initiate to run cli arguments
new CLIArgument(args);
final CLIArgument cliArgs = new CLIArgument(args);

// Load native library to implement OpenCV Java
nu.pattern.OpenCV.loadLocally();
Expand All @@ -48,6 +48,6 @@ public static void main(String[] args)
opt.setWorker(true);

Vertx vertx = Vertx.vertx(vertxOptions);
vertx.deployVerticle(new MainVerticle(), opt);
vertx.deployVerticle(new MainVerticle(cliArgs.getInitiator()), opt);
}
}
13 changes: 6 additions & 7 deletions classifai-api/src/main/java/ai/classifai/config/CLIArgument.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

import ai.classifai.loader.CLIProjectInitiator;
import ai.classifai.util.ParamConfig;
import ai.classifai.util.project.ProjectHandler;
import ai.classifai.util.type.AnnotationHandler;
import ai.classifai.util.type.AnnotationType;
import com.formdev.flatlaf.FlatLightLaf;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
Expand Down Expand Up @@ -67,6 +66,10 @@ public class CLIArgument

private String dataPath = null;

@Getter
private CLIProjectInitiator initiator;


public CLIArgument(String[] args)
{
for (String arg : args)
Expand Down Expand Up @@ -141,7 +144,7 @@ private void checkToInitiateCLIProject()
return;
}

AnnotationType type = AnnotationHandler.getType(projectType);
AnnotationType type = AnnotationType.get(projectType);

//scenario 2
if (type == null)
Expand All @@ -151,8 +154,6 @@ private void checkToInitiateCLIProject()
return;
}

CLIProjectInitiator initiator;

boolean isDataPathValid = dataPath != null && !dataPath.equals("") && new File(dataPath).exists();
boolean isProjectNameValid = (projectName != null) && (!projectName.equals(""));

Expand Down Expand Up @@ -193,8 +194,6 @@ private void checkToInitiateCLIProject()
}

}

ProjectHandler.setCliProjectInitiator(initiator);
}

private void printMessageForCLIProjectFailed()
Expand Down
119 changes: 66 additions & 53 deletions classifai-core/src/main/java/ai/classifai/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@
*/
package ai.classifai;

import ai.classifai.action.ProjectExport;
import ai.classifai.action.ProjectImport;
import ai.classifai.database.DbConfig;
import ai.classifai.database.DbOps;
import ai.classifai.database.annotation.bndbox.BoundingBoxVerticle;
import ai.classifai.database.annotation.seg.SegVerticle;
import ai.classifai.database.portfolio.PortfolioVerticle;
import ai.classifai.database.JDBCPoolHolder;
import ai.classifai.database.annotation.AnnotationDB;
import ai.classifai.database.portfolio.PortfolioDB;
import ai.classifai.database.wasabis3.WasabiVerticle;
import ai.classifai.loader.CLIProjectInitiator;
import ai.classifai.router.EndpointRouter;
import ai.classifai.ui.component.LookFeelSetter;
import ai.classifai.ui.launcher.LogoLauncher;
import ai.classifai.ui.launcher.RunningStatus;
import ai.classifai.ui.launcher.WelcomeLauncher;
import ai.classifai.ui.ContainerUI;
import ai.classifai.ui.DesktopUI;
import ai.classifai.ui.NativeUI;
import ai.classifai.ui.enums.RunningStatus;
import ai.classifai.util.ParamConfig;
import ai.classifai.util.project.ProjectHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -38,67 +43,63 @@
@Slf4j
public class MainVerticle extends AbstractVerticle
{
private static PortfolioVerticle portfolioVerticle;
private final WasabiVerticle wasabiVerticle;
private final EndpointRouter serverVerticle;
private final NativeUI ui;
private final ProjectHandler projectHandler;
private final JDBCPoolHolder jdbcPoolHolder;
private final PortfolioDB portfolioDB;

public MainVerticle(CLIProjectInitiator initiator){
jdbcPoolHolder = new JDBCPoolHolder();

/* TODO: fix circular dependency */
final ProjectImport projectImport = new ProjectImport(null, null, null);
if(ParamConfig.isDockerEnv()) {
ui = new ContainerUI();
} else {
ui = new DesktopUI(this::closeVerticles, projectImport);
}
this.projectHandler = new ProjectHandler(ui, initiator);
final ProjectExport projectExport = new ProjectExport(projectHandler);

private static BoundingBoxVerticle boundingBoxVerticle;
private static SegVerticle segVerticle;
final AnnotationDB annotationDB = new AnnotationDB(jdbcPoolHolder, projectHandler, null/* TODO: fix circular dependency */);
this.portfolioDB = new PortfolioDB(jdbcPoolHolder, projectHandler, projectExport, annotationDB);
annotationDB.setPortfolioDB(portfolioDB);

private static WasabiVerticle wasabiVerticle;
private static EndpointRouter serverVerticle;
//TODO: Fix circular dependency
projectImport.setProjectHandler(projectHandler);
projectImport.setAnnotationDB(annotationDB);
projectImport.setPortfolioDB(portfolioDB);

static
{
portfolioVerticle = new PortfolioVerticle();
boundingBoxVerticle = new BoundingBoxVerticle();
segVerticle = new SegVerticle();
wasabiVerticle = new WasabiVerticle();
serverVerticle = new EndpointRouter();

wasabiVerticle = new WasabiVerticle(projectHandler, portfolioDB, annotationDB);
serverVerticle = new EndpointRouter(ui, portfolioDB, annotationDB, projectHandler, projectExport);
}

@Override
public void start(Promise<Void> promise)
{
if(!ParamConfig.isDockerEnv()) LookFeelSetter.setDarkMode(); //to align dark mode for windows

DbOps.configureDatabase();

if (!ParamConfig.isDockerEnv()) WelcomeLauncher.start();

Promise<String> portfolioDeployment = Promise.promise();
vertx.deployVerticle(portfolioVerticle, portfolioDeployment);

portfolioDeployment.future().compose(id_ -> {

Promise<String> bndBoxDeployment = Promise.promise();
vertx.deployVerticle(boundingBoxVerticle, bndBoxDeployment);
return bndBoxDeployment.future();
ui.start();
new DbOps(ui).configureDatabase();

}).compose(id_ -> {

Promise<String> segDeployment = Promise.promise();
vertx.deployVerticle(segVerticle, segDeployment);
return segDeployment.future();

}).compose(id_ -> {

Promise<String> serverDeployment = Promise.promise();
vertx.deployVerticle(serverVerticle, serverDeployment);
return serverDeployment.future();

}).compose(id_ -> {
Promise<String> serverDeployment = Promise.promise();
vertx.deployVerticle(serverVerticle, serverDeployment);

serverDeployment.future().compose(id_ -> {
Promise<String> wasabiDeployment = Promise.promise();
vertx.deployVerticle(wasabiVerticle, wasabiDeployment);
return wasabiDeployment.future();

}).onComplete(ar -> {
jdbcPoolHolder.init(vertx, DbConfig.getH2());

if (ar.succeeded())
{
portfolioVerticle.configProjectLoaderFromDb();
portfolioVerticle.buildProjectFromCLI();
portfolioDB.configProjectLoaderFromDb();
portfolioDB.buildProjectFromCLI();

LogoLauncher.print();
printLogo();

log.info("Classifai started successfully");
log.info("Go on and open http://localhost:" + ParamConfig.getHostingPort());
Expand All @@ -108,7 +109,7 @@ public void start(Promise<Void> promise)
{
try
{
WelcomeLauncher.setRunningStatus(RunningStatus.RUNNING);
ui.setRunningStatus(RunningStatus.RUNNING);
}
catch (Exception e)
{
Expand All @@ -127,12 +128,10 @@ public void start(Promise<Void> promise)

}

public static void closeVerticles()
public void closeVerticles()
{
try
{
boundingBoxVerticle.stop(Promise.promise());
segVerticle.stop(Promise.promise());
serverVerticle.stop(Promise.promise());
wasabiVerticle.stop(Promise.promise());
}
Expand All @@ -145,8 +144,22 @@ public static void closeVerticles()
@Override
public void stop(Promise<Void> promise) throws Exception
{
jdbcPoolHolder.stop();
vertx.close( r -> {if(r.succeeded()){
log.info("Classifai close successfully");
}});
}

public static void printLogo()
{
log.info("\n");
log.info(" ********* *** ***** ********* ********* ********* ********* ***** ********* ");
log.info(" ********* *** ********* *** *** ********* ********* ********* ********* ");
log.info(" *** *** *** *** *** *** *** *** *** *** *** ");
log.info(" *** *** *** *** ********* ********* *** ********* *** *** *** ");
log.info(" *** *** ********** *** *** *** ********* ********* *** ");
log.info(" ********* ********* *** *** *** *** ********* *** *** *** ********* ");
log.info(" ********* ********* *** *** ********* ********* ********* *** *** *** ********* ");
log.info("\n");
}
}

This file was deleted.

37 changes: 15 additions & 22 deletions classifai-core/src/main/java/ai/classifai/action/FileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.io.IOException;

/**
Expand All @@ -31,35 +30,29 @@
@Slf4j
public class FileGenerator {

public void run(@NonNull ProjectLoader loader, @NonNull ProjectConfigProperties configContent, @NonNull int exportType) {
EventQueue.invokeLater(() -> {
public void run(@NonNull ProjectExport projectExport, @NonNull ProjectLoader loader, @NonNull ProjectConfigProperties configContent, @NonNull int exportType) {
final Runnable runnable = () -> {
String exportPath = null;
if(exportType == ActionConfig.ExportType.CONFIG_WITH_DATA.ordinal())
{
if (exportType == ActionConfig.ExportType.CONFIG_WITH_DATA.ordinal()) {
log.info("Exporting project config with data");
try
{
exportPath = ProjectExport.exportToFileWithData(loader, loader.getProjectId(), configContent);
try {
exportPath = projectExport.exportToFileWithData(loader, loader.getProjectId(), configContent);
} catch (IOException e) {
log.info("Exporting fail", e);
}
catch (IOException e)
{
log.info("Exporting fail");
}
}
else if (exportType == ActionConfig.ExportType.CONFIG_ONLY.ordinal())
{
} else if (exportType == ActionConfig.ExportType.CONFIG_ONLY.ordinal()) {
log.info("Exporting project config");
exportPath = ProjectExport.exportToFile(loader.getProjectId(), configContent);
exportPath = projectExport.exportToFile(loader.getProjectId(), configContent);
}

if(exportPath != null)
{
ProjectExport.setExportStatus(ProjectExport.ProjectExportStatus.EXPORT_SUCCESS);
ProjectExport.setExportPath(exportPath);
if (exportPath != null) {
projectExport.setExportStatus(ProjectExport.ProjectExportStatus.EXPORT_SUCCESS);
projectExport.setExportPath(exportPath);

return;
}
ProjectExport.setExportStatus(ProjectExport.ProjectExportStatus.EXPORT_FAIL);
});
projectExport.setExportStatus(ProjectExport.ProjectExportStatus.EXPORT_FAIL);
};
new Thread(runnable, "Project Export").start();
}
}
Loading