Search in sources :

Example 31 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class tdbloader method exec.

@Override
protected void exec() {
    if (isVerbose()) {
        System.out.println("Java maximum memory: " + Runtime.getRuntime().maxMemory());
        System.out.println(ARQ.getContext());
    }
    if (isVerbose())
        showProgress = true;
    if (isQuiet())
        showProgress = false;
    if (super.contains(argStats)) {
        if (!hasValueOfTrue(argStats) && !hasValueOfFalse(argStats))
            throw new CmdException("Not a boolean value: " + getValue(argStats));
        generateStats = super.hasValueOfTrue(argStats);
    }
    if (super.contains(argNoStats))
        generateStats = false;
    List<String> urls = getPositional();
    if (urls.size() != 0)
        checkFiles(urls);
    if (graphName == null) {
        if (urls.size() == 0) {
            checkFiles(urls);
            loadQuadsStdin();
        } else {
            loadQuads(urls);
        }
        return;
    }
    for (String url : urls) {
        Lang lang = RDFLanguages.filenameToLang(url);
        if (lang != null && RDFLanguages.isQuads(lang)) {
            throw new CmdException("Warning: Quads format given - only the default graph is loaded into the graph for --graph");
        }
    }
    loadNamedGraph(urls);
}
Also used : CmdException(org.apache.jena.cmd.CmdException) Lang(org.apache.jena.riot.Lang)

Example 32 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class CmdLangParse method exec$.

protected void exec$() {
    if (modLangParse.getRDFSVocab() != null)
        setup = RDFSFactory.setupRDFS(modLangParse.getRDFSVocab().getGraph());
    if (modLangOutput.compressedOutput()) {
        try {
            outputWrite = new GZIPOutputStream(outputWrite, true);
        } catch (IOException e) {
            IO.exception(e);
        }
    }
    outputStream = null;
    PostParseHandler postParse = null;
    outputStream = createStreamSink();
    if (outputStream == null) {
        Pair<StreamRDF, PostParseHandler> p = createAccumulateSink();
        outputStream = p.getLeft();
        postParse = p.getRight();
    }
    try {
        // The actual parsing ...
        if (super.getPositional().isEmpty()) {
            ParseRecord parseRec = parseFile("-");
            outcome(parseRec);
        } else {
            boolean b = super.getPositional().size() > 1;
            for (String fn : super.getPositional()) {
                if (b && !super.isQuiet())
                    SysRIOT.getLogger().info("File: " + fn);
                ParseRecord parseRec = parseFile(fn);
                outcome(parseRec);
            }
        }
        if (postParse != null)
            postParse.postParse();
        // Total if more than one file.
        if (super.getPositional().size() > 1 && (modTime.timingEnabled() || modLangParse.outputCount())) {
            long totalMillis = 0;
            long totalTriples = 0;
            long totalQuads = 0;
            long totalTuples = 0;
            long totalErrors = 0;
            long totalWarnings = 0;
            boolean allSuccessful = true;
            for (ParseRecord pRec : outcomes) {
                if (pRec.timeMillis >= 0)
                    totalMillis += pRec.timeMillis;
                totalTriples += pRec.triples;
                totalQuads += pRec.quads;
                totalTuples += pRec.tuples;
                totalErrors += pRec.errHandler.getErrorCount();
                totalWarnings += pRec.errHandler.getWarningCount();
                allSuccessful = allSuccessful & pRec.success;
            }
            output("Total", true, totalTriples, totalQuads, totalTuples, totalMillis, totalErrors, totalWarnings);
        }
    } finally {
        if (outputWrite != System.out)
            IO.close(outputWrite);
        else
            IO.flush(outputWrite);
        System.err.flush();
    }
    // exit(1) if there were any errors.
    for (ParseRecord pr : outcomes) {
        if (!pr.success || pr.errHandler.hadIssues())
            throw new CmdException();
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) StreamRDF(org.apache.jena.riot.system.StreamRDF) CmdException(org.apache.jena.cmd.CmdException) IOException(java.io.IOException)

Example 33 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class shacl_validate method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    super.processModulesAndArgs();
    datafile = super.getValue(argData);
    shapesfile = super.getValue(argShapes);
    // No -- arguments, use act on single file of shapes and data.
    if (datafile == null && shapesfile == null) {
        if (positionals.size() == 1) {
            datafile = positionals.get(0);
            shapesfile = positionals.get(0);
        }
    }
    if (datafile == null)
        throw new CmdException("Usage: " + getSummary());
    if (shapesfile == null)
        shapesfile = datafile;
    textOutput = super.hasArg(argOutputText);
    if (contains(argTargetNode)) {
        targetNode = getValue(argTargetNode);
    }
}
Also used : CmdException(org.apache.jena.cmd.CmdException)

Example 34 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class infer method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    if (!contains(argRDFS))
        throw new CmdException("Required argument missing: --" + argRDFS.getKeyName());
    String fn = getValue(argRDFS);
    vocab = RDFDataMgr.loadGraph(fn);
}
Also used : CmdException(org.apache.jena.cmd.CmdException)

Example 35 with CmdException

use of org.apache.jena.cmd.CmdException in project jena by apache.

the class FusekiMain method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    Logger log = Fuseki.serverLog;
    serverConfig.verboseLogging = super.isVerbose();
    boolean allowEmpty = contains(argEmpty) || contains(argSparqler);
    // ---- Checking consistency
    int numDefinitions = 0;
    if (contains(argMem))
        numDefinitions++;
    if (contains(argFile))
        numDefinitions++;
    if (contains(assemblerDescDecl))
        numDefinitions++;
    if (contains(argTDB))
        numDefinitions++;
    if (contains(argMemTDB))
        numDefinitions++;
    if (contains(argConfig))
        numDefinitions++;
    if (numDefinitions == 0 && !allowEmpty)
        throw new CmdException("No dataset specified on the command line.");
    if (numDefinitions > 1)
        throw new CmdException("Multiple ways providing a dataset. Only one of --mem, --file, --loc or --conf");
    if (numDefinitions > 0 && allowEmpty)
        throw new CmdException("Dataset provided but 'no dataset' flag given");
    // ---- check: Invalid: --conf + service name.
    if (contains(argConfig)) {
        if (getPositional().size() != 0)
            throw new CmdException("Can't have both a configutation file and a service name");
        if (contains(argRDFS))
            throw new CmdException("Need to define RDFS setup in the configuration file.");
    } else {
        if (!allowEmpty && getPositional().size() == 0)
            throw new CmdException("Missing service name");
        if (getPositional().size() > 1)
            throw new CmdException("Multiple dataset path names given");
        if (getPositional().size() != 0)
            serverConfig.datasetPath = DataAccessPoint.canonical(getPositionalArg(0));
    }
    serverConfig.datasetDescription = "<unset>";
    // ---- check: Invalid: --update + --conf
    if (contains(argUpdate) && contains(argConfig))
        throw new CmdException("--update and a configuration file does not make sense (control using the configuration file only)");
    boolean allowUpdate = contains(argUpdate);
    serverConfig.allowUpdate = allowUpdate;
    boolean hasJettyConfigFile = contains(argJettyConfig);
    // ---- Port
    serverConfig.port = defaultPort;
    if (contains(argPort)) {
        if (hasJettyConfigFile)
            throw new CmdException("Can't specify the port and also provide a Jetty configuration file");
        serverConfig.port = portNumber(argPort);
    }
    if (contains(argLocalhost)) {
        if (hasJettyConfigFile)
            throw new CmdException("Can't specify 'localhost' and also provide a Jetty configuration file");
        serverConfig.loopback = true;
    }
    // Which TDB to use to create a command line TDB database.
    if (contains(argTDB1mode))
        useTDB2 = false;
    if (contains(argTDB2mode))
        useTDB2 = true;
    if (allowEmpty) {
        serverConfig.empty = true;
        serverConfig.datasetDescription = "No dataset";
    }
    // Fuseki config file
    if (contains(argConfig)) {
        String file = getValue(argConfig);
        if (file.startsWith("file:"))
            file = file.substring("file:".length());
        Path path = Path.of(file);
        if (!Files.exists(path))
            throw new CmdException("File not found: " + file);
        if (Files.isDirectory(path))
            throw new CmdException("Is a directory: " + file);
        serverConfig.datasetDescription = "Configuration: " + path.toAbsolutePath();
        serverConfig.serverConfig = getValue(argConfig);
    }
    // Ways to setup a dataset.
    if (contains(argMem)) {
        serverConfig.datasetDescription = "in-memory";
        // Only one setup should be called by the test above but to be safe
        // and in case of future changes, clear the configuration.
        serverConfig.dsg = DatasetGraphFactory.createTxnMem();
        // Always allow, else you can't do very much!
        serverConfig.allowUpdate = true;
    }
    if (contains(argFile)) {
        List<String> filenames = getValues(argFile);
        serverConfig.datasetDescription = "in-memory, with files loaded";
        serverConfig.dsg = DatasetGraphFactory.createTxnMem();
        for (String filename : filenames) {
            String pathname = filename;
            if (filename.startsWith("file:"))
                pathname = filename.substring("file:".length());
            if (!FileOps.exists(pathname))
                throw new CmdException("File not found: " + filename);
            // INITIAL DATA.
            Lang language = RDFLanguages.filenameToLang(filename);
            if (language == null)
                throw new CmdException("Can't guess language for file: " + filename);
            Txn.executeWrite(serverConfig.dsg, () -> {
                try {
                    log.info("Dataset: in-memory: load file: " + filename);
                    RDFDataMgr.read(serverConfig.dsg, filename);
                } catch (RiotException ex) {
                    throw new CmdException("Failed to load file: " + filename);
                }
            });
        }
    }
    if (contains(argMemTDB)) {
        DSGSetup.setupMemTDB(useTDB2, serverConfig);
    }
    if (contains(argTDB)) {
        String directory = getValue(argTDB);
        DSGSetup.setupTDB(directory, useTDB2, serverConfig);
    }
    if (contains(assemblerDescDecl)) {
        serverConfig.datasetDescription = "Assembler: " + getValue(assemblerDescDecl);
        // Need to add service details.
        Dataset ds = modDataset.createDataset();
        serverConfig.dsg = ds.asDatasetGraph();
    }
    if (contains(argRDFS)) {
        String rdfsVocab = getValue(argRDFS);
        if (!FileOps.exists(rdfsVocab))
            throw new CmdException("No such file for RDFS: " + rdfsVocab);
        serverConfig.rdfsGraph = RDFDataMgr.loadGraph(rdfsVocab);
        serverConfig.datasetDescription = serverConfig.datasetDescription + " (with RDFS)";
        serverConfig.dsg = RDFSFactory.datasetRDFS(serverConfig.dsg, serverConfig.rdfsGraph);
    }
    // ---- Misc features.
    if (contains(argTimeout)) {
        String str = getValue(argTimeout);
        ARQ.getContext().set(ARQ.queryTimeout, str);
    }
    if (contains(argSparqler)) {
        String filebase = getValue(argSparqler);
        if (!FileOps.exists(filebase))
            throw new CmdException("File area not found: " + filebase);
        serverConfig.contentDirectory = filebase;
        serverConfig.addGeneral = "/sparql";
        serverConfig.empty = true;
        serverConfig.validators = true;
    }
    if (contains(argGeneralQuerySvc)) {
        String z = getValue(argGeneralQuerySvc);
        if (!z.startsWith("/"))
            z = "/" + z;
        serverConfig.addGeneral = z;
    }
    if (contains(argValidators)) {
        serverConfig.validators = true;
    }
    if (contains(argBase)) {
        // Static files.
        String filebase = getValue(argBase);
        if (!FileOps.exists(filebase)) {
            throw new CmdException("File area not found: " + filebase);
        // FmtLog.warn(Fuseki.configLog, "File area not found: "+filebase);
        }
        serverConfig.contentDirectory = filebase;
    }
    if (contains(argPasswdFile)) {
        if (hasJettyConfigFile)
            throw new CmdException("Can't specify a password file and also provide a Jetty configuration file");
        serverConfig.passwdFile = getValue(argPasswdFile);
    }
    if (contains(argRealm))
        serverConfig.realm = getValue(argRealm);
    if (contains(argHttpsPort) && !contains(argHttps))
        throw new CmdException("https port given but not certificate details via --" + argHttps.getKeyName());
    if (contains(argHttps)) {
        if (hasJettyConfigFile)
            throw new CmdException("Can't specify \"https\" and also provide a Jetty configuration file");
        serverConfig.httpsPort = defaultHttpsPort;
        if (contains(argHttpsPort))
            serverConfig.httpsPort = portNumber(argHttpsPort);
        String httpsSetup = getValue(argHttps);
        // The details go in a separate file that can be secured.
        serverConfig.httpsKeysDetails = httpsSetup;
    }
    if (contains(argAuth)) {
        if (hasJettyConfigFile)
            throw new CmdException("Can't specify authentication and also provide a Jetty configuration file");
        String schemeStr = getValue(argAuth);
        serverConfig.authScheme = AuthScheme.scheme(schemeStr);
    }
    // Jetty server : this will be the server configuration regardless of other settings.
    if (contains(argJettyConfig)) {
        String jettyConfigFile = getValue(argJettyConfig);
        if (!FileOps.exists(jettyConfigFile))
            throw new CmdException("Jetty config file not found: " + jettyConfigFile);
        serverConfig.jettyConfigFile = jettyConfigFile;
    }
    // 2020-10: Ignore argCORS - CORS is now on by default in Fuseki Main cmd
    serverConfig.withCORS = !contains(argNoCORS);
    serverConfig.withPing = contains(argWithPing);
    serverConfig.withStats = contains(argWithStats);
    serverConfig.withMetrics = contains(argWithMetrics);
    serverConfig.withCompact = contains(argWithCompact);
// if ( contains(argGZip) ) {
// if ( !hasValueOfTrue(argGZip) && !hasValueOfFalse(argGZip) )
// throw new CmdException(argGZip.getNames().get(0) + ": Not understood: " + getValue(argGZip));
// jettyServerConfig.enableCompression = super.hasValueOfTrue(argGZip);
// }
}
Also used : Path(java.nio.file.Path) RiotException(org.apache.jena.riot.RiotException) CmdException(org.apache.jena.cmd.CmdException) Dataset(org.apache.jena.query.Dataset) Lang(org.apache.jena.riot.Lang) Logger(org.slf4j.Logger) DataAccessPoint(org.apache.jena.fuseki.server.DataAccessPoint)

Aggregations

CmdException (org.apache.jena.cmd.CmdException)35 Lang (org.apache.jena.riot.Lang)8 JenaException (org.apache.jena.shared.JenaException)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)4 RiotException (org.apache.jena.riot.RiotException)4 ArgDecl (org.apache.jena.cmd.ArgDecl)3 Graph (org.apache.jena.graph.Graph)3 Node (org.apache.jena.graph.Node)3 ModDataset (arq.cmdline.ModDataset)2 PrintStream (java.io.PrintStream)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Function (java.util.function.Function)2 Stream (java.util.stream.Stream)2 StreamOps (org.apache.jena.atlas.lib.StreamOps)2 LogCtl (org.apache.jena.atlas.logging.LogCtl)2 CmdGeneral (org.apache.jena.cmd.CmdGeneral)2 TerminationException (org.apache.jena.cmd.TerminationException)2 DataAccessPoint (org.apache.jena.fuseki.server.DataAccessPoint)2