david king

web xml for servlet 3.0 — September 7, 2014
Mock Utility Class — July 23, 2014

Mock Utility Class

Doing some exploration, I created the following Mock class with a mix of methods for filtering using different methods. The client simply invokes the method in a static way.

public final class Mock {
private static String mockFileBaseLocation = System.getProperty("user.dir");
private static String mockFileResourcesLocation = "src\\test\\resources";

public Mock() {

public static Map<String, Object> getEachXmlFileAsString(Map<String, Object> data) {
return iterateAndConvert(data).get();

public static String getXmlAsString(final String whichFileFromTheMockDatabase) {
return readContentsOfFileIntoString(

public static Observable<Map<String, Object>> map(Map<String, Object> data) {
return Observable.from(data).map(Mock::transformMap);

public static Observable<Map<String, Object>> filter(Map<String, Object> data, String removeKey) {
return Observable.from(data).filter((entry) -> isItemInDataThenRemove(entry, removeKey));

public static Observable<Map<String, Object>> preserve(Map<String, Object> data, String preserveKey) {
return Observable.from(data).filter((entry) -> isItemInDataThenPreserveAndRemoveEverythingElse(data, preserveKey));

private static Map<String, Object> iterateAndConvertValueXmlToString(Map<String, Object> data) {
Map<String, Object> returnMap = new HashMap<>();
for (Map.Entry<String,Object> entry : data.entrySet()) {
returnMap.put(entry.getKey(), getXmlAsString((String)entry.getValue()));
return returnMap;

private static boolean isItemInDataThenRemove(Map<String, Object> data, String removeKey) {
data.entrySet().stream().filter(entry ->
entry.getKey().equalsIgnoreCase(removeKey)).forEach(entry -> {
return true;

private static boolean isItemInDataThenPreserveAndRemoveEverythingElse(Map<String, Object> data, String preserveKey) {
data.entrySet().stream().filter(entry -> !entry.getKey().equalsIgnoreCase(preserveKey)).forEach(entry -> {
System.out.printf("Found element that is not to be preserved %s, removing. %n", entry.getKey());

return data.get(preserveKey) != null;

private static Supplier<Map<String, Object>> iterateAndConvert(Map<String, Object> data) {
Map<String, Object> returnMap = new HashMap<>();
data.entrySet().forEach((entry) ->
returnMap.put(entry.getKey(), getXmlAsString((String)entry.getValue())));
return () -> returnMap;

private static Map<String, Object> transformMap(Map<String, Object> data) {
Map<String, Object> returnMap = new HashMap<>();
data.entrySet().forEach((entry) ->
returnMap.put(entry.getKey(), getXmlAsString((String)entry.getValue())));
return returnMap;

private static String buildFullPathToXmlTestFile(final String xmlFileName) {
return String.format("%s\\%s\\%s",
Mock.getMockFileBaseLocation(), Mock.getMockFileResourcesLocation(),

private static String getMockFileBaseLocation() {
return mockFileBaseLocation;

private static String getMockFileResourcesLocation() {
return mockFileResourcesLocation;

private static String readContentsOfFileIntoString(final String whichFileFromTheMockDatabase) {
String contentToReturn = "";

try (Scanner scanner = new Scanner(new File(whichFileFromTheMockDatabase))) {
contentToReturn =;
} catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());

return contentToReturn;

private static InputStream openXmlFile(String fileClassPath) {
File temp = new File(fileClassPath);

try (InputStream inputStream = new FileInputStream(temp)) {
System.out.printf("Returning openXmlFile: %s%n", temp.getAbsolutePath());
return inputStream;
} catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());

return null;

private static File createFileFromInputStream(String fileNameAndPath, InputStream inputStream) {
File outFile = new File(fileNameAndPath);

try (OutputStream outputStream = new FileOutputStream(outFile)) {
int readBytes = 0;
byte[] byteBuffer = new byte[2048];
while ((readBytes = != -1) {
outputStream.write(byteBuffer, 0, readBytes);
} catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());
} finally {
if (inputStream != null) {
try { inputStream.close(); } catch (IOException ioe) {
System.out.printf("IOException occured: %s%n", ioe.toString());

return outFile;

Create Nested Directories From List — July 21, 2014

Create Nested Directories From List

import org.junit.Before;
import org.junit.Test;

import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertNotNull;

* Created by dking on 7/18/2014.
public class NioStructureTest {
final String FILE_PREFIX = "file://";
final String DIRECTORY_FILE_PREFIX = "file:///";
final List<URI> dirsList = new ArrayList<>();
final List<String> dirNamesList = new ArrayList<>();

public void initializeFrontEndStructure() throws IOException {
//URI rootPathStr = URI.create(String.format("%s%s", DIRECTORY_FILE_PREFIX, "Temp//niostructuretest"));
//boolean yesOverwriteExistingDirectory = true;
//createDirectory(rootPathStr.getPath(), yesOverwriteExistingDirectory);

private void createDirectory(String rootPathStr, boolean overwrite, boolean isNested) throws IOException {
if (!FileSystems.getDefault().getPath(rootPathStr).toFile().exists()) {
doCreateDirectory(rootPathStr, isNested);
} else if (FileSystems.getDefault().getPath(rootPathStr).toFile().exists() && overwrite) {
doCreateDirectory(rootPathStr, isNested);
} else
System.out.println("RootPath already exists.");

private void doDeleteDirectory(String rootPathStr) throws IOException {
System.out.printf("doDeleteDirectory with arg: %s%n", rootPathStr);
Files.delete(Paths.get(URI.create(String.format("%s%s", FILE_PREFIX, rootPathStr))));

private void doCreateDirectory(String rootPathStr, boolean isNested) throws IOException {
System.out.printf("doCreateDirectory with arg: %s%n", rootPathStr);
if (isNested) {
File file = new File(rootPathStr);
boolean wasDirsCreated = file.mkdirs();
if (wasDirsCreated) {
System.out.printf("Directories were created: %s%n", file.getAbsolutePath());
} else {
System.out.printf("Directores were NOT created: %s%n", file.getAbsolutePath());
} else {
Path rootPath = Files.createDirectory(Paths.get(URI.create(String.format("%s%s", FILE_PREFIX, rootPathStr))));
assertNotNull("RootPath was null", rootPath);
System.out.printf("Path created: %s%n", rootPath.toString());

public void setUp() throws IOException {

System.out.println("Building directory structure in memory.");
for (String directoryName : dirNamesList) {
if (null != directoryName) {
dirsList.add(URI.create(String.format("%s%s", DIRECTORY_FILE_PREFIX, directoryName)));
System.out.println("In memory structure is: " + dirsList);

//createDirectory(dirsList.get(4).getPath(), true, false);

System.out.println("Creating the directories from the stored structures.");
for (URI uriPath : dirsList) {
if (null != uriPath) {
try {
createDirectory(uriPath.getPath(), false, true);
} catch (IOException e) {

System.out.println("Done with directories setup.");


What Can Happen? — July 11, 2014

What Can Happen?

Consider what can happen when a request whether a command or query is executed.

Command: void execute(Process process)

Query: Result execute(Criteria)


  1. Successful execution
  2. Failure occurs because of workflow, rules, logic
  3. Exception occurs and the application should recover


Given the situation above, always log ….

What The Error? — July 8, 2014

What The Error?

A public place where developers, architects and designers can post error conditions and exceptions with explanations interpretations of the opaque error displayed and possible solutions for the error / exception.

For example,


The variable or statement in question does not point to
an instance of anything.

Ensure variable or statement is pointing to an actual value.

Programming Paradigms for Dummies: What Every Programmer Should Know —
Key Value Pairs General User Story —
Excellent Paper on Dealing with Complexity and Understanding Systems —
Concepts (Reactive Progamming, Domain Driven Design, Concurrency, Hexagonal) — July 3, 2014

Concepts (Reactive Progamming, Domain Driven Design, Concurrency, Hexagonal)

1. Understanding Domain Driven Design – is an approach to software development for complex needs by connecting the implementation to an evolving model.

2. Reactive Programming – is a programming paradigm oriented around data flows and the propagation of change.

3. Concurrency in Java – is a form of computing in which several computations are executing during overlapping time periods –concurrently – instead of sequentially (one completing before the next starts). This is a property of a system – this may be an individual program, a computer, or a network – and there is a separate execution point or “thread of control” for each computation (“process”).


4. Hexagonal Architecture – Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.

As events arrive from the outside world at a port, a technology-specific adapter converts it into a usable procedure call or message and passes it to the application. The application is blissfully ignorant of the nature of the input device. When the application has something to send out, it sends it out through a port to an adapter, which creates the appropriate signals needed by the receiving technology (human or automated). The application has a semantically sound interaction with the adapters on all sides of it, without actually knowing the nature of the things on the other side of the adapters.

5. Programming in the Large

It’s Time To Create A Method — June 26, 2014

It’s Time To Create A Method

When writing code, it is possible that you will have to do the same thing from more than one location.

If this is true, then it’s time to write a method that is reachable from both locations.

The variations of this include a class where two of its methods need the same functionality, so create a private method that will be called by each method.