Skip to content

Commit

Permalink
Make ControlServices comply with autostart server settings
Browse files Browse the repository at this point in the history
- do not start the service(s) if their respective autostart setting is set to false,
  a regression probably existing since Dicoogle 3.0.0
- remove unused variable `webServicesRunning` in ControlServices
- remove use of raw types in startStorage()
- remove outdated TODO note
- refactor ControlServices#startStorage() to return and raise exception
- adjust ServicesServlet according to changes to ControlServices
  and to be more informative
  • Loading branch information
Enet4 committed Nov 13, 2024
1 parent 15c65f5 commit 2049ce2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
59 changes: 23 additions & 36 deletions dicoogle/src/main/java/pt/ua/dicoogle/server/ControlServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ public class ControlServices {
private static ControlServices instance = null;
// Services vars
private DicomStorage storage = null;
private boolean webServicesRunning = false;
private boolean webServerRunning = false;
private QueryRetrieve retrieve = null;

private DicoogleWeb webServices;

private ControlServices() {
startInicialServices();
autoStartServices();
}

public static synchronized ControlServices getInstance() {
Expand All @@ -60,30 +59,26 @@ public static synchronized ControlServices getInstance() {
}


/* Strats the inicial services based on ServerSettingsManager */
private void startInicialServices() {
/* Starts the initial services based on server settings. */
private void autoStartServices() {
ServerSettings settings = ServerSettingsManager.getSettings();

try {
/* if (settings.isP2P())
{
startP2P();
}*/
ServerSettings.DicomServices dicomSettings = settings.getDicomServicesSettings();

if (!this.storageIsRunning()) {
if (!this.storageIsRunning() && dicomSettings.getStorageSettings().isAutostart()) {
try {
startStorage();
} catch (IOException ex) {
logger.error("Failed to start the DICOM storage service", ex);
}
}

if (!this.queryRetrieveIsRunning()) {
startQueryRetrieve();
}

if (!this.webServerIsRunning()) {
startWebServer();
}
if (!this.queryRetrieveIsRunning() && dicomSettings.getQueryRetrieveSettings().isAutostart()) {
startQueryRetrieve();
}

} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
if (!this.webServerIsRunning() && settings.getWebServerSettings().isAutostart()) {
startWebServer();
}
}

Expand All @@ -104,38 +99,33 @@ public boolean stopAllServices() {
}

/**
*
* @return 0 - if everything is fine and the service was started
* -1 - if the server's storage path is not defined
* -2 - service is already running
*
* @throws IOException
* @return `true` if everything is fine and the service was started,
* `false` if the service was already running
* @throws IOException if the storage service could not start
*/
public int startStorage() throws IOException {
public boolean startStorage() throws IOException {
if (storage == null) {
ServerSettings settings = ServerSettingsManager.getSettings();


SOPList list = SOPList.getInstance();
list.readFromSettings(settings);

int i;

List l = list.getKeys();
List<String> l = list.getKeys();
String[] keys = new String[l.size()];

int i;
for (i = 0; i < l.size(); i++) {
keys[i] = (String) l.get(i);
keys[i] = l.get(i);
}
storage = new DicomStorage(keys, list);
storage.start();

logger.info("Starting DICOM Storage SCP");

return 0;
return true;
}

return -2;
return false;
}

public void stopStorage() {
Expand Down Expand Up @@ -175,15 +165,13 @@ public boolean webServerIsRunning() {
return webServerRunning;
}

// TODO: Review those below!
public void startWebServer() {
logger.info("Starting WebServer");

try {
if (webServices == null) {
webServices = new DicoogleWeb(ServerSettingsManager.getSettings().getWebServerSettings().getPort());
webServerRunning = true;
webServicesRunning = true;
logger.info("Starting Dicoogle Web");
}
} catch (Exception ex) {
Expand All @@ -197,7 +185,6 @@ public void stopWebServer() {

if (webServices != null) {
try {
webServicesRunning = false;
webServerRunning = false;

webServices.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,32 +160,45 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S

// update running
if (updateRunning) {
switch (mType) {
case STORAGE:
if (running) {
controlServices.startStorage();
obj.element("running", true);
} else {
controlServices.stopStorage();
obj.element("running", false);
}
break;

case QUERY:
if (running) {
controlServices.startQueryRetrieve();
obj.element("running", true);

} else {
controlServices.stopQueryRetrieve();
obj.element("running", false);
}
break;

default:
break;
try {
switch (mType) {
case STORAGE:
if (running) {
boolean out = controlServices.startStorage();
if (!out) {
resp.addHeader("Warning", "Service was already running");
obj.element("warning", "Service was already running");
}
obj.element("running", true);
} else {
controlServices.stopStorage();
obj.element("running", false);
}
break;

case QUERY:
if (running) {
controlServices.startQueryRetrieve();
obj.element("running", true);

} else {
controlServices.stopQueryRetrieve();
obj.element("running", false);
}
break;

default:
break;
}
} catch (Exception ex) {
obj.element("success", false);
obj.element("error", ex.getMessage());
reply(resp, 500, obj);
return;
}
}

// finalize
ServerSettingsManager.saveSettings();
obj.element("success", true);
reply(resp, 200, obj);
Expand Down

0 comments on commit 2049ce2

Please sign in to comment.