Skip to content

Commit

Permalink
Interim Commit - Minor Refactoring + LiveReload + updated deps + inte…
Browse files Browse the repository at this point in the history
…rim measures for resource leaks
  • Loading branch information
aldrinleal committed Aug 1, 2014
2 parents 1c3354e + 6598eb9 commit ad9cd06
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 181 deletions.
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.alchim31</groupId>
<artifactId>livereload-jvm</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down Expand Up @@ -121,10 +125,15 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.alchim31</groupId>
<artifactId>livereload-jvm</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.jbake</groupId>
<artifactId>jbake-core</artifactId>
<version>2.3.0</version>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand All @@ -136,6 +145,11 @@
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/br/com/ingenieux/mojo/jbake/GenerateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* limitations under the License.
*/

import com.orientechnologies.orient.core.Orient;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -51,11 +53,33 @@ public class GenerateMojo extends AbstractMojo {
@Parameter(property = "jbake.isClearCache", defaultValue = "false", required = true)
protected boolean isClearCache;

public void execute() throws MojoExecutionException {
public final void execute() throws MojoExecutionException {
try {
Oven oven = new Oven(inputDirectory, outputDirectory);
executeInternal();
} finally {
closeQuietly();
}
}

protected final void closeQuietly() {
try {
Orient.instance().shutdown();
} catch (Exception e) {
getLog().warn("Oops", e);
}
}

protected void executeInternal() throws MojoExecutionException {
reRender();
}

protected void reRender() throws MojoExecutionException {
try {
// TODO: At some point, reuse Oven
Oven oven = new Oven(inputDirectory, outputDirectory, isClearCache);

oven.setupPaths();

oven.bake();
} catch (Exception e) {
getLog().info("Oops", e);
Expand Down
114 changes: 71 additions & 43 deletions src/main/java/br/com/ingenieux/mojo/jbake/InlineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,86 @@

import java.io.File;

import net_alchim31_livereload.LRServer;

/**
* Runs jbake on a folder while watching and serving a folder with it
*/
@Mojo(name = "inline", requiresDirectInvocation = true, requiresProject = false)
public class InlineMojo extends WatchMojo {
/**
* Listen Port
*/
@Parameter(property = "jbake.listenAddress", defaultValue = "127.0.0.1")
private String listenAddress;

/**
* Index File
*/
@Parameter(property = "jbake.indexFile", defaultValue = "index.html")
private String indexFile;

/**
* Listen Port
*/
@Parameter(property = "jbake.port", defaultValue = "8080")
private Integer port;

Server server = new Server();

class Server extends Verticle {
{
vertx = VertxFactory.newVertx();
}

/**
* Listen Port
*/
@Parameter(property = "jbake.listenAddress", defaultValue = "127.0.0.1")
private String listenAddress;

/**
* Index File
*/
@Parameter(property = "jbake.indexFile", defaultValue = "index.html")
private String indexFile;

/**
* Listen Port
*/
@Parameter(property = "jbake.port", defaultValue = "8080")
private Integer port;

/**
* Use livereload.
*/
@Parameter(property = "jbake.livereload", defaultValue = "true")
private Boolean livereload;

Server server = new Server();

LRServer lrServer;

class Server extends Verticle {

{
vertx = VertxFactory.newVertx();
}

@Override
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
@Override
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest req) {
String file = req.path().endsWith("/") ? req.path() + indexFile : req.path();

if (new File(outputDirectory + file).isDirectory()) {
req.response().setStatusCode(301).putHeader("Location", file + "/").close();
} else {
req.response().sendFile(outputDirectory.getAbsolutePath() + file);
}
}
}).listen(port, listenAddress);
public void handle(HttpServerRequest req) {
String file = req.path().endsWith("/") ? req.path() + indexFile : req.path();

if (new File(outputDirectory + file).isDirectory()) {
req.response().setStatusCode(301).putHeader("Location", file + "/").close();
} else {
req.response().sendFile(outputDirectory.getAbsolutePath() + file);
}
}
}).listen(port, listenAddress);
}
}

protected void stopServer() {
server.stop();
}
protected void stopServer() throws MojoExecutionException {
if (lrServer != null) {
try {
lrServer.stop();
} catch (Exception e) {
throw new MojoExecutionException("LiveReload Failure", e);
}
}
server.stop();
}

protected void initServer() throws MojoExecutionException {
server.start();

protected void initServer() throws MojoExecutionException {
server.start();
}
if (Boolean.TRUE.equals(livereload)) {
lrServer = new LRServer(35729, outputDirectory.toPath());
try {
lrServer.start();
} catch (Exception e) {
throw new MojoExecutionException("LiveReload Failure", e);
}
}
}
}
39 changes: 28 additions & 11 deletions src/main/java/br/com/ingenieux/mojo/jbake/WatchMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,32 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;

import br.com.ingenieux.mojo.jbake.util.DirWatcher;

import static org.apache.commons.lang.StringUtils.isBlank;

/**
* Runs jbake on a folder while watching for changes
*/
@Mojo(name = "watch", requiresDirectInvocation = true, requiresProject = false)
public class WatchMojo extends GenerateMojo {

public void execute() throws MojoExecutionException {
super.execute();
public void executeInternal() throws MojoExecutionException {
reRender();

Long lastProcessed = Long.valueOf(System.currentTimeMillis());

getLog().info(
"Now listening for changes on path " + inputDirectory.getPath());

initServer();

DirWatcher dirWatcher = null;

try {
dirWatcher = new DirWatcher(inputDirectory);
final AtomicBoolean done = new AtomicBoolean(false);
final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
Expand All @@ -49,25 +55,33 @@ public void execute() throws MojoExecutionException {
@Override
public void run() {
try {
getLog().info("Running. Hit <ENTER> to finish");
reader.readLine();
getLog()
.info("Running. Enter a blank line to finish. Anything else forces re-rendering.");

while (true) {
String line = reader.readLine();

if (isBlank(line)) {
break;
}

reRender();
}
} catch (Exception exc) {
getLog().info("Ooops", exc);
} finally {
done.set(true);
}
}
}).start();

DirWatcher dirWatcher = new DirWatcher(Paths.get(inputDirectory
.getPath()));

Long lastProcessed = Long.valueOf(System.currentTimeMillis());
dirWatcher.start();

do {
Long result = dirWatcher.processEvents();

if (null == result) {
Thread.sleep(1000);
// do nothing on purpose
} else if (result >= lastProcessed) {
getLog().info("Refreshing");

Expand All @@ -83,11 +97,14 @@ public void run() {
} finally {
getLog().info("Finishing");

if (null != dirWatcher)
dirWatcher.stop();

stopServer();
}
}

protected void stopServer() {
protected void stopServer() throws MojoExecutionException {
}

protected void initServer() throws MojoExecutionException {
Expand Down
Loading

0 comments on commit ad9cd06

Please sign in to comment.