-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the mokamint-application start command
- Loading branch information
Showing
15 changed files
with
332 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...okamint-application-tools/src/main/java/io/mokamint/application/tools/internal/Start.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2024 Fausto Spoto | ||
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.mokamint.application.tools.internal; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import io.mokamint.application.Applications; | ||
import io.mokamint.application.api.ApplicationException; | ||
import io.mokamint.application.service.ApplicationServices; | ||
import io.mokamint.tools.AbstractCommand; | ||
import io.mokamint.tools.CommandException; | ||
import jakarta.websocket.DeploymentException; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Help.Ansi; | ||
import picocli.CommandLine.Parameters; | ||
|
||
@Command(name = "start", | ||
description = "Start a network service for an application.", | ||
showDefaultValues = true) | ||
public class Start extends AbstractCommand { | ||
|
||
@Parameters(index = "0", description = "the name of the application to start") | ||
private String application; | ||
|
||
@Parameters(index = "1", description = "the port number where the service to the application will be published", defaultValue = "8050") | ||
private int port; | ||
|
||
private final static Logger LOGGER = Logger.getLogger(Start.class.getName()); | ||
|
||
@Override | ||
protected void execute() throws CommandException { | ||
System.out.print("Publishing a service for application " + application + " at ws://localhost:" + port + "... "); | ||
|
||
try (var app = Applications.load(application); var service = ApplicationServices.open(app, port)) { | ||
System.out.println(Ansi.AUTO.string("@|blue done.|@")); | ||
|
||
try (var reader = new BufferedReader(new InputStreamReader(System.in))) { | ||
System.out.print(Ansi.AUTO.string("@|green Press ENTER to stop the application service: |@")); | ||
reader.readLine(); | ||
} | ||
catch (IOException e) { | ||
System.out.println(Ansi.AUTO.string("@|red Cannot access the standard input!|@")); | ||
LOGGER.log(Level.WARNING, "cannot access the standard input", e); | ||
} | ||
} | ||
catch (DeploymentException | IOException e) { | ||
throw new CommandException("Failed to open the application service. Is port " + port + " available?", e); | ||
} | ||
catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new CommandException("The application service has been interrupted", e); | ||
} | ||
catch (ApplicationException e) { | ||
throw new CommandException("The application is misbehaving", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
io-mokamint-application/src/main/java/io/mokamint/application/Applications.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2024 Fausto Spoto | ||
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.mokamint.application; | ||
|
||
import java.util.ServiceLoader.Provider; | ||
import java.util.stream.Stream; | ||
|
||
import io.mokamint.application.api.Application; | ||
import io.mokamint.application.internal.ApplicationLoader; | ||
|
||
/** | ||
* Provider of applications. | ||
*/ | ||
public abstract class Applications { | ||
|
||
private Applications() {} | ||
|
||
/** | ||
* Loads from the module path the application with the given name. | ||
* | ||
* @return the application | ||
* @throws IllegalArgumentException if there is no application with the given name | ||
* or if there is more than one | ||
*/ | ||
public static Application load(String name) { | ||
return ApplicationLoader.load(name); | ||
} | ||
|
||
/** | ||
* Yields the providers of the applications accessible from the module path. | ||
* | ||
* @return the providers | ||
*/ | ||
public static Stream<Provider<Application>> available() { | ||
return ApplicationLoader.available(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...okamint-application/src/main/java/io/mokamint/application/internal/ApplicationLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2024 Fausto Spoto | ||
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.mokamint.application.internal; | ||
|
||
import java.util.List; | ||
import java.util.ServiceLoader; | ||
import java.util.ServiceLoader.Provider; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import io.mokamint.application.api.Application; | ||
import io.mokamint.application.api.Name; | ||
|
||
/** | ||
* A loader of an application from the module path. | ||
*/ | ||
public class ApplicationLoader { | ||
|
||
/** | ||
* Loads from the module path the application with the given name. | ||
* | ||
* @return the application | ||
* @throws IllegalArgumentException if there is no application with the given name | ||
* or if there is more than one | ||
*/ | ||
public static Application load(String name) { | ||
List<Provider<Application>> providers = available() | ||
.filter(app -> provides(name, app)) | ||
.collect(Collectors.toList()); | ||
|
||
if (providers.size() == 0) | ||
throw new IllegalArgumentException("There are no providers for application " + name); | ||
else if (providers.size() > 1) | ||
throw new IllegalArgumentException("There is more than one provider for application " + name); | ||
|
||
return providers.get(0).get(); | ||
} | ||
|
||
/** | ||
* Yields the providers of the applications accessible from the module path. | ||
* | ||
* @return the providers | ||
*/ | ||
public static Stream<Provider<Application>> available() { | ||
return ServiceLoader.load(Application.class).stream(); | ||
} | ||
|
||
private static boolean provides(String name, ServiceLoader.Provider<Application> provider) { | ||
Name ann = provider.type().getAnnotation(Name.class); | ||
return ann != null && name.equals(ann.value()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.