COSUImpl.java [src/csip] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package csip;
import csip.api.server.COSU;
import csip.cosu.ObjFunc;
import csip.utils.JSONUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* COSU support for services.
*
* @author od
*/
class COSUImpl implements COSU {
static final String KEY_COSU = "cosu";
Map<String, JSONObject> cosu;
COSUImpl(JSONObject metainfo) {
if (metainfo.has(KEY_COSU)) {
try {
// all cosu entries
cosu = JSONUtils.preprocess(metainfo.get(KEY_COSU));
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
}
}
JSONArray getCOSUMetainfo() {
if (!isSupported())
throw new UnsupportedOperationException();
JSONArray arr = new JSONArray();
SortedSet<String> keys = new TreeSet<>(cosu.keySet());
for (String key : keys) {
arr.put(cosu.get(key));
}
return arr;
}
@Override
public boolean isSupported() {
return cosu != null;
}
@Override
public List<String> getNames() {
if (!isSupported())
throw new UnsupportedOperationException();
return new ArrayList<>(cosu.keySet());
}
@Override
public ObjFunc getObjFunc(String name) {
if (!isSupported())
throw new UnsupportedOperationException();
JSONObject p = cosu.get(name);
return ObjFunc.of(p.optString("of", null));
}
@Override
public String[] getData(String name) {
if (!isSupported())
throw new UnsupportedOperationException();
JSONObject p = cosu.get(name);
try {
return JSONUtils.toStringArray(p.getJSONArray("data"));
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
}
@Override
public void setValue(String name, double value) {
if (!isSupported())
throw new UnsupportedOperationException();
JSONObject p = cosu.get(name);
try {
p.put("value", value);
} catch (JSONException ex) {
throw new RuntimeException(ex);
}
}
@Override
public String toString() {
return cosu == null ? "Not supported." : cosu.toString();
}
}