diff --git a/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoFrameWriter.java b/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoFrameWriter.java index 59303273f4..f07fe1eac0 100644 --- a/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoFrameWriter.java +++ b/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoFrameWriter.java @@ -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; /** @@ -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){ diff --git a/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoToHipoWriter.java b/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoToHipoWriter.java index caf29c4616..df83b1c069 100644 --- a/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoToHipoWriter.java +++ b/common-tools/clara-io/src/main/java/org/jlab/io/clara/HipoToHipoWriter.java @@ -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; /** @@ -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(); @@ -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); } diff --git a/common-tools/clas-io/src/main/java/org/jlab/utils/ClaraYaml.java b/common-tools/clas-io/src/main/java/org/jlab/utils/ClaraYaml.java index 957894fd3c..b6525bfb3b 100644 --- a/common-tools/clas-io/src/main/java/org/jlab/utils/ClaraYaml.java +++ b/common-tools/clas-io/src/main/java/org/jlab/utils/ClaraYaml.java @@ -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; @@ -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 yamlConf = (Map) yaml.load(input); this.json = new JSONObject(yamlConf); } catch (FileNotFoundException ex) { Logger.getLogger(ClaraYaml.class.getName()).log(Level.SEVERE, null, ex); } + setStockSchemaDirectory(); } /** @@ -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 @@ -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(); } }