Skip to content

Commit

Permalink
Pass all startup arguments in a JSON string.
Browse files Browse the repository at this point in the history
  • Loading branch information
pekim committed Apr 6, 2011
1 parent ac9d5e6 commit 8c87842
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pom.xml
Expand Up @@ -66,6 +66,27 @@
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.0.0</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.7.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.5</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/uk/co/pekim/nodejdbc/NodeJdbc.java
@@ -0,0 +1,50 @@
package uk.co.pekim.nodejdbc;

import org.apache.log4j.xml.DOMConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import uk.co.pekim.nodejdbc.configuration.Configuration;

/**
*
* @author Mike D Pilsbury
*/
public class NodeJdbc {
private static final Logger LOGGER = LoggerFactory.getLogger(NodeJdbc.class);

private final ApplicationContext context;
private final Configuration configuration;

private NodeJdbc(String configurationJson) {
this.configuration = Configuration.parseJson(configurationJson);
this.context = new ClassPathXmlApplicationContext(new String[] { "/uk/co/pekim/nodejdbc/nodejdbc.xml" });
}

private void run() {
Object bean = context.getBean("testbean", TestBean.class);
LOGGER.info(bean.toString());
}

public static void main(String[] args) {
initialiseLogging();

try {
if (args.length < 2) {
throw new NodeJdbcException("Expected 1 argument, a JSON string");
}

new NodeJdbc(args[1]).run();
} catch (NodeJdbcException exception) {
LOGGER.error("Fatal error", exception);
throw exception;
}
}

private static void initialiseLogging() {
DOMConfigurator.configure(NodeJdbc.class
.getResource("/uk/co/pekim/nodejdbc/log4j.xml"));
}
}
30 changes: 30 additions & 0 deletions src/main/java/uk/co/pekim/nodejdbc/NodeJdbcException.java
@@ -0,0 +1,30 @@
/**
*
*/
package uk.co.pekim.nodejdbc;


/**
* A problem somewhere in the Node/Jdbc bridge.
*
* @author Mike D Pilsbury
*
*/
public class NodeJdbcException extends RuntimeException {
private static final long serialVersionUID = 1L;

/**
* @param message
*/
public NodeJdbcException(String message) {
super(message);
}

/**
* @param string
* @param cause
*/
public NodeJdbcException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,43 @@
package uk.co.pekim.nodejdbc.configuration;

import java.io.IOException;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import uk.co.pekim.nodejdbc.NodeJdbcException;

public class Configuration {
private Node node;
private Jdbc jdbc;

public static Configuration parseJson(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, Configuration.class);
} catch (JsonParseException exception) {
throw new NodeJdbcException("Failed to parse JSON " + json, exception);
} catch (JsonMappingException exception) {
throw new NodeJdbcException("Failed to parse JSON " + json, exception);
} catch (IOException exception) {
throw new NodeJdbcException("Failed to parse JSON " + json, exception);
}
}

public void setNode(Node node) {
this.node = node;
}

public Node getNode() {
return node;
}

public void setJdbc(Jdbc jdbc) {
this.jdbc = jdbc;
}

public Jdbc getJdbc() {
return jdbc;
}
}
5 changes: 5 additions & 0 deletions src/main/java/uk/co/pekim/nodejdbc/configuration/Jdbc.java
@@ -0,0 +1,5 @@
package uk.co.pekim.nodejdbc.configuration;

public class Jdbc {

}
13 changes: 13 additions & 0 deletions src/main/java/uk/co/pekim/nodejdbc/configuration/Node.java
@@ -0,0 +1,13 @@
package uk.co.pekim.nodejdbc.configuration;

public class Node {
private int port;

public void setPort(int port) {
this.port = port;
}

public int getPort() {
return port;
}
}
24 changes: 24 additions & 0 deletions src/test/java/uk/co/pekim/nodejdbc/NodeJdbcTest.java
@@ -0,0 +1,24 @@
package uk.co.pekim.nodejdbc;

import org.junit.Test;


/**
* Unit test for NodeJdbc.
*/
public class NodeJdbcTest {
@Test(expected = NodeJdbcException.class)
public void testMissingJsonArg() {
NodeJdbc.main(new String[] {""});
}

@Test(expected = NodeJdbcException.class)
public void testBadJsonArg() {
NodeJdbc.main(new String[] {"", "bad"});
}

@Test
public void testGoodJsonArg() {
NodeJdbc.main(new String[] {"", "{}"});
}
}
@@ -0,0 +1,17 @@
package uk.co.pekim.nodejdbc.configuration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;


public class ConfigurationTest {
@Test
public void test() throws Exception {
Configuration configuration = Configuration.parseJson("{\"node\": {\"port\": 1234}, \"jdbc\": {}}");

assertEquals(1234, configuration.getNode().getPort());
assertNotNull(configuration.getJdbc());
}
}

0 comments on commit 8c87842

Please sign in to comment.