use of org.apache.jena.assembler.exceptions.AssemblerException in project jena by apache.
the class SecurityEvaluatorAssembler method open.
// initialization and registration is performed by SecuredAssembler
@Override
public SecurityEvaluator open(Assembler a, Resource root, Mode mode) {
Literal className = getUniqueLiteral(root, EVALUATOR_CLASS);
if (className == null) {
throw new AssemblerException(root, String.format(NO_X_PROVIDED, EVALUATOR_CLASS, root));
}
Class<?> clazz;
try {
clazz = Class.forName(className.getString());
} catch (ClassNotFoundException e1) {
throw new AssemblerException(root, String.format("Can not locate class %s as specified by %s in %s", className, EVALUATOR_CLASS, root));
}
if (!SecurityEvaluator.class.isAssignableFrom(clazz)) {
throw new AssemblerException(root, String.format("Class %s as specified by %s in %s does not implement SecurityEvaluator", className, EVALUATOR_CLASS, root));
}
// get the arguments as specified.
List<Object> args = new ArrayList<>();
Resource argRes = getUniqueResource(root, ARGUMENT_LIST);
if (argRes != null) {
Seq seq = argRes.as(Seq.class);
NodeIterator iter = seq.iterator();
RDFNode n = null;
while (iter.hasNext()) {
n = iter.next();
if (n.isLiteral()) {
args.add(n.asLiteral().getValue());
} else if (n.isResource()) {
args.add(a.open(a, n.asResource(), mode));
} else {
throw new AssemblerException(root, String.format("%s must be a literal or a resource", n));
}
}
}
for (Constructor<?> c : clazz.getConstructors()) {
if (c.getParameterTypes().length == args.size()) {
try {
if (args.size() == 0) {
return (SecurityEvaluator) c.newInstance();
} else {
return (SecurityEvaluator) c.newInstance(args.toArray());
}
} catch (InstantiationException e) {
throw new AssemblerException(root, e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new AssemblerException(root, e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new AssemblerException(root, e.getMessage(), e);
} catch (InvocationTargetException e) {
throw new AssemblerException(root, e.getMessage(), e);
}
}
}
throw new AssemblerException(root, String.format("Class %s does not have a %s argument constructor", className, args.size()));
}
use of org.apache.jena.assembler.exceptions.AssemblerException in project jena by apache.
the class TDBGraphAssembler method open.
@Override
public Model open(Assembler a, Resource root, Mode mode) {
// Make a model - the default model of the TDB dataset
// [] rdf:type tdb:GraphTDB ;
// tdb:location "dir" ;
// Make a named model.
// [] rdf:type tdb:GraphTDB ;
// tdb:location "dir" ;
// tdb:graphName <http://example/name> ;
// Location or dataset reference.
String locationDir = getStringValue(root, pLocation);
Resource dataset = getResourceValue(root, pDataset);
if (locationDir != null && dataset != null)
throw new AssemblerException(root, "Both location and dataset given: exactly one required");
if (locationDir == null && dataset == null)
throw new AssemblerException(root, "Must give location or refer to a dataset description");
String graphName = null;
if (root.hasProperty(pGraphName1))
graphName = getAsStringValue(root, pGraphName1);
if (root.hasProperty(pGraphName2))
graphName = getAsStringValue(root, pGraphName2);
if (root.hasProperty(pIndex))
Log.warn(this, "Custom indexes not implemented yet - ignored");
final Dataset ds;
if (locationDir != null) {
Location location = Location.create(locationDir);
ds = TDBFactory.createDataset(location);
} else
ds = DatasetAssemblerTDB.make(dataset);
try {
if (graphName != null)
return ds.getNamedModel(graphName);
else
return ds.getDefaultModel();
} catch (RuntimeException ex) {
ex.printStackTrace(System.err);
throw ex;
}
}
use of org.apache.jena.assembler.exceptions.AssemblerException in project jena by apache.
the class TestTDBAssembler method testGraph.
private static void testGraph(String assemblerFile, boolean named) {
Object thing = null;
try {
thing = AssemblerUtils.build(assemblerFile, VocabTDB.tGraphTDB);
} catch (AssemblerException e) {
e.getCause().printStackTrace(System.err);
throw e;
}
assertTrue(thing instanceof Model);
Graph graph = ((Model) thing).getGraph();
assertTrue(graph instanceof GraphTDB);
DatasetGraphTDB ds = ((GraphTDB) graph).getDatasetGraphTDB();
if (ds != null)
ds.close();
}
Aggregations