Skip to content

Commit

Permalink
Updating for maintainability. Added back some of the exit and throws …
Browse files Browse the repository at this point in the history
…functionality into Start.java.
  • Loading branch information
wjonassen committed Jan 17, 2024
1 parent 2740c1c commit ac8d90c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
2 changes: 1 addition & 1 deletion opendcs-rest-api-jetty/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ task bundle(type: Tar) {
archiveExtension = 'tar.gz'

into('config') {
from project.file("bin/logging.properties")
from project.file("config/logging.properties")
}
into('libs') {
from configurations.runtimeClasspath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Properties;
import java.util.Scanner;

import org.opendcs.odcsapi.start.StartException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,23 +55,24 @@ public class Start
private static final Logger LOGGER = LoggerFactory.getLogger(ApiConstants.loggerName);

public static void main(String[] args)
//throws Exception
{
try
{
run(args);
} catch (Exception e)
{
LOGGER.atError().setCause(e).log("There was an error running the opendcs-rest-api-jetty start script.");
}
}
private static void run(String[] args) throws Exception
{
ArrayList<String[]> corsList = new ArrayList<>();
corsList.add(new String[] { "Access-Control-Allow-Origin", CrossOriginFilter.ALLOWED_ORIGINS_PARAM });
corsList.add(new String[] { "Access-Control-Allow-Headers", CrossOriginFilter.ALLOWED_HEADERS_PARAM });
corsList.add(new String[] { "Access-Control-Allow-Methods", CrossOriginFilter.ALLOWED_METHODS_PARAM });
corsList.add(new String[] { "Access-Control-Allow-Credentials", CrossOriginFilter.ALLOW_CREDENTIALS_PARAM });

try
{
apiCmdLineArgs.parseArgs(args);
}
catch (Exception e)
{
LOGGER.error("Error parsing arguments. Exiting now. {}", e.getMessage());
System.exit(1);
}
apiCmdLineArgs.parseArgs(args);

LOGGER.info("Listening Http Port={}", apiCmdLineArgs.getHttpPort());
LOGGER.info("Listening Https Port={}", apiCmdLineArgs.getHttpsPort());
Expand Down Expand Up @@ -135,21 +137,17 @@ public static void main(String[] args)
}

ServletHolder serHol = ctx.addServlet(ServletContainer.class,
"/" + apiCmdLineArgs.getContext() + "/*");
"/" + apiCmdLineArgs.getContext() + "/*");
serHol.setInitOrder(1);
serHol.setInitParameter("jersey.config.server.provider.packages",
"org.opendcs.odcsapi.res");
serHol.setInitParameter("jersey.config.server.provider.packages",
"org.opendcs.odcsapi.res");

// Get whatever is needed from decodes properties.
Properties decodesProps = new Properties();
try (FileReader fr = new FileReader(ApiEnvExpander.expand(apiCmdLineArgs.getDecodesPropFile())))
{
decodesProps.load(fr);
}
catch (Exception e)
{
LOGGER.error("There was an error loading the decodes property file. {}", e.getMessage());
}
String dbUrl = null;
String dbType = null;
String dbAuthFile = null;
Expand All @@ -167,7 +165,7 @@ else if ("siteNameTypePreference".equalsIgnoreCase(n))
}
ApiPropertiesUtil.copyProps(DbInterface.decodesProperties, decodesProps);
DbInterface.secureMode = apiCmdLineArgs.isSecureMode();

PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setURL(dbUrl);
String afn = ApiEnvExpander.expand(dbAuthFile);
Expand All @@ -176,36 +174,25 @@ else if ("siteNameTypePreference".equalsIgnoreCase(n))
{
afr.read();
}
catch(Exception e)
catch(Exception ex)
{
String msg = String.format("Cannot read DB auth from file '%s': %s", afn, e);
String msg = String.format("Cannot read DB auth from file '%s': %s", afn, ex);
LOGGER.error(msg);
throw new StartException(String.format("Cannot read auth file: %s", ex));
}
ds.setUser(afr.getUsername());
ds.setPassword(afr.getPassword());
DbInterface.setDataSource(ds);
try
{
DbInterface.setDatabaseType(dbType);
}
catch (Exception e)
{
LOGGER.error("There was an issue setting database type. {}", e.getMessage());
if (dbType != null)
{
LOGGER.error("The attempted database type to be set is {}", dbType);
}
}
DbInterface.setDatabaseType(dbType);

// Setup Swagger-UI static resources
String resourceBasePath = Start.class.getResource("/swaggerui").toExternalForm();
ctx.setWelcomeFiles(new String[] {"index.html"});
ctx.setResourceBase(resourceBasePath);
ctx.addServlet(new ServletHolder(new DefaultServlet()), "/*");

ServerConnector connector = new ServerConnector(server);
ArrayList<ServerConnector> connectors = new ArrayList<>();

ArrayList<ServerConnector> connectors = new ArrayList<>();
if (apiCmdLineArgs.getHttpPort() >= 0)
{
connector.setPort(apiCmdLineArgs.getHttpPort());
Expand All @@ -221,24 +208,16 @@ else if ("siteNameTypePreference".equalsIgnoreCase(n))
sslContextFactory.setKeyStorePath(apiCmdLineArgs.getKeyStorePath());
sslContextFactory.setKeyStorePassword(apiCmdLineArgs.getKeyStorePassword());
ServerConnector sslConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "http/1.1"),
new HttpConnectionFactory(https));
new SslConnectionFactory(sslContextFactory, "http/1.1"),
new HttpConnectionFactory(https));
sslConnector.setPort(apiCmdLineArgs.getHttpsPort());
connectors.add(sslConnector);
}

server.setConnectors(connectors.toArray(new ServerConnector[connectors.size()]));

// Start the server.
try
{
server.start();
server.join();
}
catch (Exception e)
{
LOGGER.warn("Interrupted!", e);
Thread.currentThread().interrupt();
}
server.start();
server.join();
}
}
3 changes: 2 additions & 1 deletion opendcs-web-client/src/main/java/api/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public void setApiDetails(ServletContext sc) throws IOException
{
String name = nameValue[0].trim();
String value = nameValue[1].trim();
if (name.equalsIgnoreCase("url")) {
if ("url".equalsIgnoreCase(name))
{
tempUrl = value;
System.out.println("Setting Base Url to " + tempUrl);
}
Expand Down

0 comments on commit ac8d90c

Please sign in to comment.