Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jlab.jnp.hipo4.data.Event;
import org.jlab.jnp.hipo4.io.HipoWriterStream;
import org.jlab.jnp.utils.file.FileUtils;
import org.jlab.utils.ClaraYaml;
import org.json.JSONObject;

/**
Expand Down Expand Up @@ -47,9 +48,14 @@ private int getCompression(JSONObject opts) {
}

private String getSchemaDirectory(JSONObject opts) {
return opts.has(CONF_SCHEMA)
? opts.getString(CONF_SCHEMA)
: FileUtils.getEnvironmentPath("CLAS12DIR", "etc/bankdefs/hipo4");
String s = FileUtils.getEnvironmentPath("CLAS12DIR", "etc/bankdefs/hipo4");
if (opts.has(CONF_SCHEMA)) {
s = opts.getString(CONF_SCHEMA).trim();
// If it's not already an absolute path, assume it's the name of a
// stock schema that comes with COATJAVA and get the full path to it:
if (!s.startsWith("/")) s = ClaraYaml.getStockSchemaDirectory(opts.getString(s));
}
return s;
}

private String[] getFilterString(JSONObject opts){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jlab.jnp.hipo4.io.HipoWriter;
import org.jlab.jnp.hipo4.io.HipoWriterSorted;
import org.jlab.jnp.utils.file.FileUtils;
import org.jlab.utils.ClaraYaml;
import org.json.JSONObject;

/**
Expand Down Expand Up @@ -46,7 +47,6 @@ protected HipoWriterSorted createWriter(Path file, JSONObject opts) throws Event
throw new EventWriterException(e);
}
}


protected void configure(HipoWriterSorted writer, JSONObject opts) {
schemaBankList.clear();
Expand All @@ -58,8 +58,12 @@ protected void configure(HipoWriterSorted writer, JSONObject opts) {

String schemaDir = FileUtils.getEnvironmentPath("CLAS12DIR", "etc/bankdefs/hipo4");
if (opts.has(CONF_SCHEMA_DIR)) {
schemaDir = opts.getString(CONF_SCHEMA_DIR);
// Run YAML values throuh env-substitor:
schemaDir = opts.getString(CONF_SCHEMA_DIR).trim();
schemaDir = envSubstitutor.replace(schemaDir);
// If it's not already an absolute path, assume it's the name of a
// stock schema that comes with COATJAVA and get the full path to it:
if (!schemaDir.startsWith("/")) schemaDir = ClaraYaml.getStockSchemaDirectory(schemaDir);
System.out.printf("%s service: schema directory = %s%n", getName(), schemaDir);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jlab.utils.system.ClasUtilsFile;
import org.json.JSONException;
import org.json.JSONObject;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -21,19 +22,19 @@
*/
public class ClaraYaml {

private Yaml yaml = null;
private JSONObject json = null;

public ClaraYaml(String filename) {
InputStream input;
try {
input = new FileInputStream(filename);
this.yaml = new Yaml();
Yaml yaml = new Yaml();
Map<String, Object> yamlConf = (Map<String, Object>) yaml.load(input);
this.json = new JSONObject(yamlConf);
} catch (FileNotFoundException ex) {
Logger.getLogger(ClaraYaml.class.getName()).log(Level.SEVERE, null, ex);
}
setStockSchemaDirectory();
}

/**
Expand Down Expand Up @@ -93,6 +94,44 @@ public String schemaDirectory() {
return null;
}

public String getSchemaDirectory() {
return schemaDirectory();
}

/**
* Set the schema_dir directory.
* @param dir path to schema_dir directory
*/
public void setSchemaDirectory(String dir) {
if (!json.has("configuration"))
json.put("configuration",new JSONObject());
if (!json.getJSONObject("configuration").has("io_services"))
json.getJSONObject("configuration").put("io-services",new JSONObject());
if (!json.getJSONObject("configuration").getJSONObject("io-services").has("writer"))
json.getJSONObject("configuration").getJSONObject("io-services").put("writer",new JSONObject());
this.json.getJSONObject("configuration").getJSONObject("io-services").
getJSONObject("writer").put("schema_dir",dir);
}

/**
* Set the schema_dir, assuming it's currently the short (basename) of a
* "stock" schema, but only if it doesn't already look like an absolute path.
*/
public final void setStockSchemaDirectory() {
String s = getSchemaDirectory();
if (s != null && !s.trim().startsWith("/"))
setSchemaDirectory(getStockSchemaDirectory(s));
}

/**
* Convert short (base)name into absolute path, for "stock" schema.
* @param name
* @return
*/
public static String getStockSchemaDirectory(String name) {
return ClasUtilsFile.getResourceDir("CLAS12DIR", "etc/bankdefs/hipo4/singles/"+name);
}

/**
* Get the service's configuration as presented by CLARA.
* @param serviceName
Expand Down Expand Up @@ -135,10 +174,12 @@ public static JSONObject filter(JSONObject claraJson, String serviceName) {
}

public static void main(String[] args) {
ClaraYaml yaml = new ClaraYaml("/Users/baltzell/data-ai.yaml");
ClaraYaml yaml = new ClaraYaml(System.getenv("HOME")+"/sw/coatjava/github/etc/services/data-ai.yaml");
yaml.show();
yaml.showFiltered("DCCR");
yaml.showFiltered("EBTB");
yaml.setSchemaDirectory("dst");
yaml.setStockSchemaDirectory();
yaml.show();
}

}