2012/07/20

she sells sea shells on the shore

At the moment I'm spending some time at the Belgian coast, catching my breath after my most recent adventures in Minneapolis (I even - image that - joined an "Happy Hour" where I - for the record - drank Pepsi Cola ... some things will never change).

However there is only so much watching the waves and potty training his son a man can take before he is drawn back to slightly less chaotic things ... NetKernel for example.

Below is a snippet of code that I first wrote for a specific implementation. Then I realised it could be made more general (that's when I added the argument) and right now I'm deciding in which general library it should go.

The snippet is provided as is, without any guarantee. I'm also not getting into any fights over whether or not flags have a place in coding. A long time ago I was taught that using gotos and/or flags was not done by good developers. I guess I'm not a good developer.

// Author: Tom Geudens
// Date  : 2012/07/20


// The usual suspects for an accessor.
import org.netkernel.layer0.meta.impl.SourcedArgumentMetaImpl;
import org.netkernel.layer0.nkf.*;
import org.netkernel.module.standard.endpoint.StandardAccessorImpl;


// Processing
import java.io.File;


public class FlagAccessor extends StandardAccessorImpl {
public FlagAccessor() {
this.declareThreadSafe();
this.declareArgument(new SourcedArgumentMetaImpl("flagname",null,null,new Class[] {String.class}));
}

private String getHome() {
String vHome=System.getProperty("user.home");

// Windows requires a reversal of the slash for a proper file:/ url
// as well as an extra slash in front.
if(vHome.contains("\\")) {
vHome = "/" + vHome.replace(File.separator,"/");
}
return "file:" + vHome;
}


public void onNew(INKFRequestContext aContext) throws Exception {
// One mandatory argument for the onNew function.
String aFlagname = aContext.getThisRequest().getArgumentValue("flagname");


String vFlagDirectory = getHome() + "/.flags/";
String vFlagFile = vFlagDirectory + aFlagname;
Boolean vCreateResponse = false;

if (! (aContext.exists(vFlagDirectory)) ) {
INKFRequest vCreateRequest = aContext.createRequest(vFlagDirectory);
vCreateRequest.setVerb(INKFRequestReadOnly.VERB_NEW);
vCreateResponse = (Boolean)aContext.issueRequest(vCreateRequest);

if (! vCreateResponse) {
throw new NKFException("can not create directoy " + vFlagDirectory);
}
}

if (! (aContext.exists(vFlagFile)) ) {
INKFRequest vCreateRequest = aContext.createRequest(vFlagFile);
vCreateRequest.setVerb(INKFRequestReadOnly.VERB_NEW);
vCreateResponse = (Boolean)aContext.issueRequest(vCreateRequest);

if (! vCreateResponse) {
throw new NKFException("can not create file " + vFlagFile);
}
}
aContext.createResponseFrom(vFlagFile);
}

public void onDelete(INKFRequestContext aContext) throws Exception {
// One mandatory argument for the onDelete function.
String aFlagname = aContext.getThisRequest().getArgumentValue("flagname");


String vFlagDirectory = getHome() + "/.flags/";
String vFlagFile = vFlagDirectory + aFlagname;
Boolean vDeleteResponse = true;


if (aContext.exists(vFlagFile) ) {
INKFRequest vDeleteRequest = aContext.createRequest(vFlagFile);
vDeleteRequest.setVerb(INKFRequestReadOnly.VERB_DELETE);
vDeleteResponse = (Boolean)aContext.issueRequest(vDeleteRequest);
}
aContext.createResponseFrom(vDeleteResponse);
}


public void onExists(INKFRequestContext aContext) throws Exception {
// One mandatory argument for the onExists function.
String aFlagname = aContext.getThisRequest().getArgumentValue("flagname");


String vFlagDirectory = getHome() + "/.flags/";
String vFlagFile = vFlagDirectory + aFlagname;

aContext.createResponseFrom(aContext.exists(vFlagFile));
}
}

Have fun !