Search in sources :

Example 6 with AssemblerException

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()));
}
Also used : AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with AssemblerException

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;
    }
}
Also used : AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) VocabTDB.pDataset(org.apache.jena.tdb.assembler.VocabTDB.pDataset) Dataset(org.apache.jena.query.Dataset) VocabTDB.pLocation(org.apache.jena.tdb.assembler.VocabTDB.pLocation) Location(org.apache.jena.tdb.base.file.Location)

Example 8 with AssemblerException

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();
}
Also used : Graph(org.apache.jena.graph.Graph) AssemblerException(org.apache.jena.assembler.exceptions.AssemblerException) Model(org.apache.jena.rdf.model.Model)

Aggregations

AssemblerException (org.apache.jena.assembler.exceptions.AssemblerException)8 Model (org.apache.jena.rdf.model.Model)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Graph (org.apache.jena.graph.Graph)2 Resource (org.apache.jena.rdf.model.Resource)2 VocabTDB.pLocation (org.apache.jena.tdb.assembler.VocabTDB.pLocation)2 Location (org.apache.jena.tdb.base.file.Location)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 BaseTest (org.apache.jena.atlas.junit.BaseTest)1 Node (org.apache.jena.graph.Node)1 Dataset (org.apache.jena.query.Dataset)1 Literal (org.apache.jena.rdf.model.Literal)1 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)1 NodeValue (org.apache.jena.sparql.expr.NodeValue)1 ConfigTest (org.apache.jena.tdb.ConfigTest)1 VocabTDB.pDataset (org.apache.jena.tdb.assembler.VocabTDB.pDataset)1 Test (org.junit.Test)1