Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass all startup arguments in a JSON string.
- Loading branch information
Showing
8 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/uk/co/pekim/nodejdbc/configuration/Configuration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package uk.co.pekim.nodejdbc.configuration; | ||
|
||
public class Jdbc { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/uk/co/pekim/nodejdbc/configuration/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[] {"", "{}"}); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/uk/co/pekim/nodejdbc/configuration/ConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |