2626import java .util .Set ;
2727import java .util .function .BiConsumer ;
2828
29+ /**
30+ * Allow to restart an application on file changes. This lets client to listen for file changes
31+ * and trigger a restart.
32+ *
33+ * This class doesn't compile source code. Instead, let a client (Maven/Gradle) to listen for
34+ * changes, fire a compilation process and restart the application once compilation finished.
35+ *
36+ * @author edgar
37+ * @since 2.0.0
38+ */
2939public class JoobyRun {
3040
3141 private static class ExtModuleLoader extends ModuleLoader {
3242
33- public ExtModuleLoader (ModuleFinder ... finders ) {
43+ ExtModuleLoader (ModuleFinder ... finders ) {
3444 super (finders );
3545 }
3646
@@ -42,13 +52,13 @@ public void unload(String name, final Module module) {
4252 private static class AppModule {
4353 private final Logger logger ;
4454 private final ExtModuleLoader loader ;
45- private final JoobyRunConf conf ;
55+ private final JoobyRunOptions conf ;
4656 private Module module ;
4757 private ClassLoader contextClassLoader ;
4858 private int counter ;
4959
50- public AppModule (Logger logger , ExtModuleLoader loader , ClassLoader contextClassLoader ,
51- JoobyRunConf conf ) {
60+ AppModule (Logger logger , ExtModuleLoader loader , ClassLoader contextClassLoader ,
61+ JoobyRunOptions conf ) {
5262 this .logger = logger ;
5363 this .loader = loader ;
5464 this .conf = conf ;
@@ -116,7 +126,7 @@ private void closeServer() {
116126
117127 private final Logger logger = LoggerFactory .getLogger (getClass ());
118128
119- private final JoobyRunConf conf ;
129+ private final JoobyRunOptions options ;
120130
121131 private final Set <Path > resources = new LinkedHashSet <>();
122132
@@ -128,10 +138,24 @@ private void closeServer() {
128138
129139 private AppModule module ;
130140
131- public JoobyRun (JoobyRunConf conf ) {
132- this .conf = conf ;
141+ /**
142+ * Creates a new instances with the given options.
143+ *
144+ * @param options Run options.
145+ */
146+ public JoobyRun (JoobyRunOptions options ) {
147+ this .options = options ;
133148 }
134149
150+ /**
151+ * Add the given path to the project classpath. Path must be a jar or file system directory.
152+ * File system directory are listen for changes on file changes this method invokes the given
153+ * callback.
154+ *
155+ * @param path Path.
156+ * @param callback Callback to listen for file changes.
157+ * @return True if the path was added it to the classpath.
158+ */
135159 public boolean addResource (Path path , BiConsumer <String , Path > callback ) {
136160 if (addResource (path )) {
137161 if (Files .isDirectory (path )) {
@@ -142,6 +166,12 @@ public boolean addResource(Path path, BiConsumer<String, Path> callback) {
142166 return false ;
143167 }
144168
169+ /**
170+ * Add the given path to the project classpath. Path must be a jar or file system directory.
171+ *
172+ * @param path Path.
173+ * @return True if the path was added it to the classpath.
174+ */
145175 public boolean addResource (Path path ) {
146176 if (Files .exists (path )) {
147177 if (path .toString ().endsWith (".jar" )) {
@@ -154,27 +184,39 @@ public boolean addResource(Path path) {
154184 return false ;
155185 }
156186
187+ /**
188+ * Start the application.
189+ *
190+ * @throws Exception If something goes wrong.
191+ */
157192 public void start () throws Exception {
158193 this .watcher = newWatcher ();
159194 try {
160195 logger .debug ("project: {}" , toString ());
161196
162197 ModuleFinder [] finders = {
163- new FlattenClasspath (conf .getProjectName (), resources , dependencies )};
198+ new FlattenClasspath (options .getProjectName (), resources , dependencies )};
164199
165200 ExtModuleLoader loader = new ExtModuleLoader (finders );
166- module = new AppModule (logger , loader , Thread .currentThread ().getContextClassLoader (), conf );
201+ module = new AppModule (logger , loader , Thread .currentThread ().getContextClassLoader (),
202+ options );
167203 module .start ();
168204 watcher .watch ();
169205 } catch (ClosedWatchServiceException expected ) {
170206 logger .trace ("Watcher.close resulted in exception" , expected );
171207 }
172208 }
173209
210+ /**
211+ * Restart the application.
212+ */
174213 public void restart () {
175214 module .restart ();
176215 }
177216
217+ /**
218+ * Stop and shutdown the application.
219+ */
178220 public void shutdown () {
179221 if (module != null ) {
180222 module .close ();
@@ -203,7 +245,7 @@ private DirectoryWatcher newWatcher() throws IOException {
203245
204246 @ Override public String toString () {
205247 StringBuilder buff = new StringBuilder ();
206- buff .append (conf .getProjectName ()).append ("\n " );
248+ buff .append (options .getProjectName ()).append ("\n " );
207249 buff .append (" watch-dirs: " ).append ("\n " );
208250 watchDirs .forEach (
209251 (path , callback ) -> buff .append (" " ).append (path .toAbsolutePath ()).append ("\n " ));
0 commit comments