Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added weld / CDI #72

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
<junit.version>4.8.1</junit.version>
<pegdown.version>1.4.1</pegdown.version>
<jetty.version>8.1.12.v20130726</jetty.version>
<cdi.weld.version>2.1.1.Final</cdi.weld.version>
<test.mockito.version>1.9.5</test.mockito.version>
</properties>

<build>
Expand Down Expand Up @@ -139,22 +141,22 @@
<configuration>
<outputDirectory>dist</outputDirectory>
<finalName>jbake-${project.version}</finalName>
<descriptors>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<attach>false</attach>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
</descriptors>
<attach>false</attach>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
Expand Down Expand Up @@ -230,6 +232,17 @@
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>${cdi.weld.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${test.mockito.version}</version>
<scope>test</scope>
</dependency>
<!-- sl4j Logging -->
<!-- <dependency>
<groupId>org.slf4j</groupId>
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/jbake/app/ConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;

import javax.inject.Singleton;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
Expand All @@ -12,13 +14,14 @@
* @author Jonathan Bullock <[email protected]>
*
*/
@Singleton
public class ConfigUtil {

public final static String DATE_FORMAT = "date.format";

private static CompositeConfiguration config;
private CompositeConfiguration config;

public static CompositeConfiguration load(File source) throws ConfigurationException {
public CompositeConfiguration load(File source) throws ConfigurationException {
if (config == null) {
config = new CompositeConfiguration();
config.setListDelimiter(',');
Expand All @@ -35,7 +38,7 @@ public static CompositeConfiguration load(File source) throws ConfigurationExcep
return config;
}

public static void reset() {
public void reset() {
config = null;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jbake/app/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author Jonathan Bullock <[email protected]>
*
*/
public class FileUtil {
public abstract class FileUtil {

/**
* Filters files based on their file extension.
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/jbake/app/Oven.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.configuration.CompositeConfiguration;

/**
Expand All @@ -16,6 +19,7 @@
* @author Jonathan Bullock <[email protected]>
*
*/
@Singleton
public class Oven {

private CompositeConfiguration config;
Expand All @@ -24,6 +28,7 @@ public class Oven {
private File templatesPath;
private File contentsPath;
private File assetsPath;
@Inject ConfigUtil configUtil;

/**
* Creates a new instance of the Oven.
Expand All @@ -39,10 +44,10 @@ public Oven() {
* @param destination The destination folder
* @throws Exception
*/
public Oven(File source, File destination) throws Exception {
public void prepare(File source, File destination) throws Exception {
this.source = source;
this.destination = destination;
this.config = ConfigUtil.load(source);
this.config = configUtil.load(source);
}

private void ensureSource() throws Exception {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jbake/launcher/LaunchOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class LaunchOptions {
@Argument(index = 1, usage = "destination folder for output, if not supplied will default to a folder called \"output\" in the current working directory", metaVar = "destination_folder")
private File destination = null;

@Option(name = "-b", aliases = {"--bake"}, usage="start baking")
private boolean bake;

@Option(name = "-i", aliases = {"--init"}, usage="initialises required folder structure with default templates")
private boolean init;

Expand Down Expand Up @@ -40,4 +43,8 @@ public boolean isRunServer() {
public boolean isInit() {
return init;
}

public boolean isBake() {
return bake;
}
}
128 changes: 70 additions & 58 deletions src/main/java/org/jbake/launcher/Main.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
package org.jbake.launcher;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.List;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationUtils;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.nio.NetworkTrafficSelectChannelConnector;
import org.jbake.app.ConfigUtil;
import org.jbake.app.FileUtil;
import org.jbake.app.Oven;
import org.jbake.app.ZipUtil;
import org.jboss.weld.environment.se.StartMain;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.environment.se.bindings.Parameters;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

/**
* Launcher for JBake.
*
* @author Jonathan Bullock <[email protected]>
*
*/
@Singleton
public class Main {
// public static final String VERSION = "v2.2";

private final String USAGE_PREFIX = "Usage: jbake";

Expand All @@ -49,13 +38,17 @@ public class Main {
* @param String[] args
*/
public static void main(String[] args) {
Main m = new Main();
m.run(m.parseArguments(args));
// will
// - create a CDI/Weld context (See META-INF/beans.xml)
// - and invoke Main#run()
StartMain.main(args);
}

private void run(LaunchOptions options) {
@Inject Oven oven;

private void bake(LaunchOptions options) {
try {
Oven oven = new Oven(options.getSource(), options.getDestination());
oven.prepare(options.getSource(), options.getDestination());
oven.setupPaths();
oven.bake();
} catch (Exception e) {
Expand All @@ -64,48 +57,67 @@ private void run(LaunchOptions options) {
}
}

@Inject ConfigUtil configUtil;

void run(@Observes ContainerInitialized event, @Parameters String[] args) {
LaunchOptions res = parseArguments(args);

CompositeConfiguration config = null;
try {
config = configUtil.load(res.getSource());
} catch (ConfigurationException e) {
e.printStackTrace();
System.exit(1);
}

System.out.println("JBake " + config.getString("version") + " (" + config.getString("build.timestamp") + ") [http://jbake.org]");
System.out.println();
boolean didSomething=false;

if (res.isHelpNeeded()) {
didSomething=true;
printUsage(res);
}

if(res.isBake()) {
didSomething=true;
bake(res);
}

if (res.isRunServer()) {
didSomething=true;
if (res.getSource().getPath().equals(".")) {
// use the default destination folder
runServer(config.getString("destination.folder"), config.getString("server.port"));
} else {
runServer(res.getSource().getPath(), config.getString("server.port"));
}
}

if (res.isInit()) {
didSomething=true;
initStructure(config);
}

if(!didSomething) {
bake(res);
}
}
private LaunchOptions parseArguments(String[] args) {
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);

try {
parser.parseArgument(args);

CompositeConfiguration config = null;
try {
config = ConfigUtil.load(res.getSource());
} catch (ConfigurationException e) {
e.printStackTrace();
System.exit(1);
}

System.out.println("JBake " + config.getString("version") + " (" + config.getString("build.timestamp") + ") [http://jbake.org]");
System.out.println();

if (res.isHelpNeeded()) {
printUsage(parser);
}

if (res.isRunServer()) {
if (res.getSource().getPath().equals(".")) {
// use the default destination folder
runServer(config.getString("destination.folder"), config.getString("server.port"));
} else {
runServer(res.getSource().getPath(), config.getString("server.port"));
}
}

if (res.isInit()) {
initStructure(config);
}
} catch (CmdLineException e) {
printUsage(parser);
printUsage(res);
}

return res;
}

private void printUsage(CmdLineParser parser) {
private void printUsage(Object options) {
CmdLineParser parser = new CmdLineParser(options);
StringWriter sw = new StringWriter();
sw.append(USAGE_PREFIX);
parser.printSingleLineUsage(sw, null);
Expand All @@ -117,7 +129,7 @@ private void printUsage(CmdLineParser parser) {

private void runServer(String path, String port) {
JettyServer.run(path, port);
System.exit(0);
System.exit(0);
}

private void initStructure(CompositeConfiguration config) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
<scan>
<!-- <exclude name="org.jboss.**"/> -->
</scan>
</beans>
11 changes: 2 additions & 9 deletions src/test/java/org/jbake/app/ConfigUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package org.jbake.app;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.List;

import junit.framework.Assert;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.io.IOUtils;
import org.jbake.utils.TestUtils;
import org.junit.Test;

public class ConfigUtilTest {

@Test
public void load() throws Exception {
ConfigUtil.reset();
CompositeConfiguration config = ConfigUtil.load(new File(this.getClass().getResource("/").getFile()));
CompositeConfiguration config = TestUtils.loadTestConfig();

// check default.properties values exist
Assert.assertEquals("output", config.getString("destination.folder"));
Expand Down
Loading