コード例 #1
0
  public GraphConfigurationContainer(final List<HierarchicalConfiguration> configurations)
      throws GraphConfigurationException {

    if (configurations == null) {
      throw new GraphConfigurationException("No graph configurations");
    }

    // create one graph for each configuration for each <graph> element
    final Iterator<HierarchicalConfiguration> it = configurations.iterator();
    while (it.hasNext()) {
      final HierarchicalConfiguration graphConfig = it.next();
      final String graphName = graphConfig.getString(Tokens.REXSTER_GRAPH_NAME, "");

      if (graphName.equals("")) {
        // all graphs must have a graph name
        logger.warn("Could not load graph " + graphName + ".  The graph-name element was not set.");
        this.failedConfigurations.add(graphConfig);
      } else {
        // check for duplicate graph configuration
        if (!this.graphs.containsKey(graphName)) {

          if (graphConfig.getBoolean(Tokens.REXSTER_GRAPH_ENABLED, true)) {

            // one graph failing initialization will not prevent the rest in
            // their attempt to be created
            try {
              final Graph graph = getGraphFromConfiguration(graphConfig);
              final RexsterApplicationGraph rag = new RexsterApplicationGraph(graphName, graph);

              // loads extensions that are allowed to be served for this graph
              final List extensionConfigs =
                  graphConfig.getList(Tokens.REXSTER_GRAPH_EXTENSIONS_ALLOWS_PATH);
              rag.loadAllowableExtensions(extensionConfigs);

              // loads extension configuration for this graph
              final List<HierarchicalConfiguration> extensionConfigurations =
                  graphConfig.configurationsAt(Tokens.REXSTER_GRAPH_EXTENSIONS_PATH);
              rag.loadExtensionsConfigurations(extensionConfigurations);

              this.graphs.put(rag.getGraphName(), rag);

              logger.info("Graph " + graphName + " - " + graph + " loaded");
            } catch (GraphConfigurationException gce) {
              logger.warn(
                  "Could not load graph " + graphName + ". Please check the XML configuration.");
              logger.warn(gce.getMessage());

              if (gce.getCause() != null) {
                logger.warn(gce.getCause().getMessage());
              }

              failedConfigurations.add(graphConfig);
            } catch (Exception e) {
              logger.warn("Could not load graph " + graphName + ".", e);

              failedConfigurations.add(graphConfig);
            }
          } else {
            logger.info("Graph " + graphName + " - " + " not enabled and not loaded.");
          }
        } else {
          logger.warn(
              "A graph with the name "
                  + graphName
                  + " was already configured.  Please check the XML configuration.");

          failedConfigurations.add(graphConfig);
        }
      }
    }
  }
コード例 #2
0
  private ExtensionResponse tryExecuteGremlinScript(
      final RexsterResourceContext rexsterResourceContext,
      final Graph graph,
      final Vertex vertex,
      final Edge edge,
      final String script) {

    final MetricRegistry metricRegistry = rexsterResourceContext.getMetricRegistry();
    final Timer scriptTimer = metricRegistry.timer(MetricRegistry.name("http", "script-engine"));
    final Counter successfulExecutions =
        metricRegistry.counter(MetricRegistry.name("http", "script-engine", "success"));
    final Counter failedExecutions =
        metricRegistry.counter(MetricRegistry.name("http", "script-engine", "fail"));

    ExtensionResponse extensionResponse;

    final JSONObject requestObject = rexsterResourceContext.getRequestObject();

    // can't initialize this statically because the configure() method won't get called before it.
    // need to think a bit on how to best initialized the controller.
    final EngineController engineController = EngineController.getInstance();

    final boolean showTypes = RequestObjectHelper.getShowTypes(requestObject);
    final long offsetStart = RequestObjectHelper.getStartOffset(requestObject);
    final long offsetEnd = RequestObjectHelper.getEndOffset(requestObject);

    final GraphSONMode mode = showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL;
    final Set<String> returnKeys = RequestObjectHelper.getReturnKeys(requestObject, WILDCARD);

    final String languageToExecuteWith = getLanguageToExecuteWith(requestObject);
    final EngineHolder engineHolder;
    final ScriptEngine scriptEngine;
    try {
      if (!engineController.isEngineAvailable(languageToExecuteWith)) {
        return ExtensionResponse.error("language requested is not available on the server");
      }

      engineHolder = engineController.getEngineByLanguageName(languageToExecuteWith);
      scriptEngine = engineHolder.getEngine();
    } catch (ScriptException se) {
      return ExtensionResponse.error("could not get request script engine");
    }

    final Bindings bindings = createBindings(graph, vertex, edge, scriptEngine);

    // add all keys not defined by this request as bindings to the script engine
    placeParametersOnBinding(requestObject, bindings, showTypes);

    // get the list of "stored procedures" to run
    final RexsterApplicationGraph rag = rexsterResourceContext.getRexsterApplicationGraph();

    final ExtensionMethod extensionMethod = rexsterResourceContext.getExtensionMethod();
    Map configurationMap = null;

    Iterator<String> scriptsToRun = null;
    try {
      final ExtensionConfiguration extensionConfiguration =
          rag != null ? rag.findExtensionConfiguration(EXTENSION_NAMESPACE, EXTENSION_NAME) : null;
      if (extensionConfiguration != null) {
        configurationMap = extensionConfiguration.tryGetMapFromConfiguration();
        scriptsToRun = getScriptsToRun(requestObject, configurationMap);
      }
    } catch (IOException ioe) {
      return ExtensionResponse.error(
          ioe, generateErrorJson(extensionMethod.getExtensionApiAsJson()));
    }

    if ((script == null || script.isEmpty()) && scriptsToRun == null) {
      return ExtensionResponse.badRequest(
          "no scripts provided", generateErrorJson(extensionMethod.getExtensionApiAsJson()));
    }

    final Timer.Context context = scriptTimer.time();
    try {
      // result is either the ad-hoc script on the query string or the last "stored procedure"
      Object result = null;
      if (scriptsToRun != null) {
        while (scriptsToRun.hasNext()) {
          result = engineHolder.getEngine().eval(scriptsToRun.next(), bindings);
        }
      }

      if (isClientScriptAllowed(configurationMap) && script != null && !script.isEmpty()) {
        result = engineHolder.getEngine().eval(script, bindings);
      }

      final JSONArray results =
          new JSONResultConverter(mode, offsetStart, offsetEnd, returnKeys).convert(result);

      final HashMap<String, Object> resultMap = new HashMap<String, Object>();
      resultMap.put(Tokens.SUCCESS, true);
      resultMap.put(Tokens.RESULTS, results);

      final JSONObject resultObject = new JSONObject(resultMap);
      extensionResponse = ExtensionResponse.ok(resultObject);

      successfulExecutions.inc();

    } catch (Exception e) {
      logger.error(String.format("Gremlin Extension: %s", e.getMessage()), e);
      extensionResponse =
          ExtensionResponse.error(e, generateErrorJson(extensionMethod.getExtensionApiAsJson()));

      failedExecutions.inc();
    } finally {
      context.stop();
    }

    return extensionResponse;
  }