Skip to content

Commit

Permalink
Merge 'upstream/master' into issue_43029
Browse files Browse the repository at this point in the history
  • Loading branch information
ravinperera00 committed Aug 23, 2024
2 parents d438418 + 7d5eabf commit 685cbbe
Show file tree
Hide file tree
Showing 54 changed files with 935 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ public interface Repository {
* Get whether remote management is enabled.
* @return True if remote management is enabled, false otherwise.
*/
boolean isRemoteEnabled();
boolean isRemoteManagementEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class RepositoryImpl implements Repository {
private static final String nodeId = generateNodeId();
private static String balHome;
private static String balVersion;
private static boolean isRemoteEnabled = false;
private static boolean isRemoteManagementEnabled = false;

@Override
public List<Artifact> getArtifacts() {
Expand Down Expand Up @@ -73,8 +73,8 @@ public Node getNode() {
}

@Override
public boolean isRemoteEnabled() {
return isRemoteEnabled;
public boolean isRemoteManagementEnabled() {
return isRemoteManagementEnabled;
}

private Artifact createArtifact(ObjectValue service, ObjectValue listener) {
Expand All @@ -92,7 +92,7 @@ private Artifact createArtifact(ObjectValue service, ObjectValue listener) {
}

public static void addServiceListener(BObject listener, BObject service, Object attachPoint) {
if (!isRemoteEnabled) {
if (!isRemoteManagementEnabled) {
return;
}
BServiceType serviceType = (BServiceType) service.getType();
Expand All @@ -101,10 +101,11 @@ public static void addServiceListener(BObject listener, BObject service, Object
listenerServiceMap.put((ObjectValue) listener, (ObjectValue) service);
}

public static void addBallerinaRuntimeInformation(String balHome, String balVersion, boolean isRemoteEnabled) {
public static void addBallerinaRuntimeInformation(String balHome, String balVersion,
boolean isRemoteManagementEnabled) {
RepositoryImpl.balHome = balHome;
RepositoryImpl.balVersion = balVersion;
RepositoryImpl.isRemoteEnabled = isRemoteEnabled;
RepositoryImpl.isRemoteManagementEnabled = isRemoteManagementEnabled;
}

private static String generateNodeId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException;
import static io.ballerina.projects.util.ProjectConstants.BLANG_SOURCE_EXT;
import static io.ballerina.projects.util.ProjectConstants.DEPENDENCIES_TOML;
import static io.ballerina.projects.util.ProjectConstants.MODULES_ROOT;
Expand All @@ -67,10 +66,13 @@ public class ProjectWatcher {
private final ScheduledExecutorService scheduledExecutorService;
private final Map<Path, Long> debounceMap = new ConcurrentHashMap<>();
private static final long debounceTimeMillis = 250;
private final RunCommandExecutor[] thread;
private volatile boolean forceStop = false;

public ProjectWatcher(RunCommand runCommand, Path projectPath, PrintStream outStream) throws IOException {
this.fileWatcher = FileSystems.getDefault().newWatchService();
this.runCommand = runCommand;
thread = new RunCommandExecutor[]{new RunCommandExecutor(runCommand, outStream)};
this.projectPath = projectPath.toAbsolutePath();
this.outStream = outStream;
this.watchKeys = new HashMap<>();
Expand All @@ -80,10 +82,16 @@ public ProjectWatcher(RunCommand runCommand, Path projectPath, PrintStream outSt
registerFileTree(projectPath);
}

public void watch() throws IOException {
final RunCommandExecutor[] thread = {new RunCommandExecutor(runCommand, outStream)};
/**
* Watches for any file changes in a Ballerina service project and restarts the service.
* Changes on source files, resources and .toml files are considered valid file changes. Changes to
* Dependencies.toml, tests, target directory and other files are ignored.
*
* @throws IOException if the watcher cannot register files for watching.
*/
public void watch() throws IOException { // TODO: find out why panics and removing service doesn't exit the code
thread[0].start();
while (thread[0].shouldWatch()) {
while (thread[0].shouldWatch() && !forceStop) {
WatchKey key;
key = fileWatcher.poll();
Path dir = watchKeys.get(key);
Expand All @@ -109,11 +117,7 @@ public void watch() throws IOException {
}
outStream.println("\nDetected file changes. Re-running the project...");
thread[0].terminate();
try {
thread[0].join();
} catch (InterruptedException e) {
throw createLauncherException("unable to watch the project:" + e.getMessage());
}
waitForRunCmdThreadToJoin();
thread[0] = new RunCommandExecutor(runCommand, outStream);
thread[0].start();
debounceMap.remove(changedFilePath);
Expand All @@ -131,6 +135,20 @@ public void watch() throws IOException {
}
}
}
waitForRunCmdThreadToJoin();
}

public void stopWatching() {
try {
if (thread != null) {
thread[0].terminate();
thread[0].join();
}
forceStop = true;
fileWatcher.close();
} catch (IOException | InterruptedException e) {
outStream.println("Error occurred while stopping the project watcher: " + e.getMessage());
}
}

private ProjectKind deriveProjectKind() {
Expand Down Expand Up @@ -208,4 +226,12 @@ private void validateProjectPath() {
private static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>) event;
}

private void waitForRunCmdThreadToJoin() {
try {
thread[0].join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,24 @@ protected String readOutput() throws IOException {
}

protected String readOutput(boolean silent) throws IOException {
return readOutput(silent, true);
}

protected String readOutput(boolean silent, boolean closeConsole) throws IOException {
String output = "";
output = console.toString();
console.close();
console = new ByteArrayOutputStream();
printStream = new PrintStream(console);
if (closeConsole) {
console.close();
console = new ByteArrayOutputStream();
printStream = new PrintStream(console);
}
if (!silent) {
PrintStream out = System.out;
out.println(output);
}
return output;
}

/**
* Execute a command and get the exception.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public class PackCommandTest extends BaseCommandTest {
private static final String VALID_PROJECT = "validApplicationProject";
private Path testResources;

static Path logFile = Paths.get("./src/test/resources/compiler_plugin_tests/" +
"log_creator_combined_plugin/compiler-plugin.txt");
private static final Path logFile = Paths.get("build/logs/log_creator_combined_plugin/compiler-plugin.txt")
.toAbsolutePath();

@BeforeClass
public void setup() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
public class ProfileCommandTest extends BaseCommandTest {
private Path testResources;

static Path logFile = Paths.get(".", "src", "test", "resources", "compiler_plugin_tests",
"log_creator_combined_plugin", "compiler-plugin.txt");
private static final Path logFile = Paths.get("build/logs/log_creator_combined_plugin/compiler-plugin.txt")
.toAbsolutePath();

@BeforeSuite
public void setupSuite() throws IOException {
Expand Down
Loading

0 comments on commit 685cbbe

Please sign in to comment.