Esempio n. 1
0
  /**
   * @param aConfiguration
   * @throws Exception
   */
  public ExtProcessPlugIn(PluginConfiguration aConfiguration) throws Exception {
    super(aConfiguration);
    if (mLog.isDebugEnabled()) {
      mLog.debug("Instantiating External Process plug-in...");
    }
    // specify default name space for file system plugin
    this.setNamespace(NS_EXTPROCESS);

    try {
      mBeanFactory = getConfigBeanFactory();
      if (null == mBeanFactory) {
        mLog.error(
            "No or invalid spring configuration for external process plug-in, some features may not be available.");
      } else {
        mSettings = (Settings) mBeanFactory.getBean("org.jwebsocket.plugins.extprocess.settings");
        // replace all alias values with environment variables
        Map<String, String> lAllowedProgs = mSettings.getAllowedProgs();
        for (Entry<String, String> lEntry : lAllowedProgs.entrySet()) {
          lEntry.setValue(Tools.expandEnvVarsAndProps(lEntry.getValue()));
        }
        if (mLog.isInfoEnabled()) {
          mLog.info("External Process plug-in successfully instantiated.");
        }
      }
    } catch (BeansException lEx) {
      mLog.error(Logging.getSimpleExceptionMessage(lEx, "instantiating ExtProcess plug-in"));
      throw lEx;
    }
  }
Esempio n. 2
0
  private void call(WebSocketConnector aConnector, Token aToken) {
    TokenServer lServer = getServer();

    Token lResponse = createResponse(aToken);

    // check if user is allowed to run 'exists' command
    /*
    if (!hasAuthority(aConnector, NS_EXTPROCESS + ".call")) {
    if (mLog.isDebugEnabled()) {
    mLog.debug("Returning 'Access denied'...");
    }
    lServer.sendToken(aConnector, lServer.createAccessDenied(aToken));
    return;
    }
    */

    String lAlias = aToken.getString("alias");
    if (null == lAlias) {
      lResponse.setInteger("code", -1);
      lResponse.setString("msg", "No alias passed.");
      lServer.sendToken(aConnector, lResponse);
      return;
    }
    String lCmdLine = mSettings.getAllowedProgs().get(lAlias);
    if (null == lCmdLine) {
      lResponse.setInteger("code", -1);
      lResponse.setString("msg", "Alias '" + lAlias + "' not found.");
      lServer.sendToken(aConnector, lResponse);
      return;
    }
    List<?> lArgs = aToken.getList("args");

    if (mLog.isDebugEnabled()) {
      mLog.debug(
          "Processing 'call'"
              + " (alias: "
              + lAlias
              + ", args: "
              + StringUtils.collectionToDelimitedString(lArgs, ", ")
              + ")...");
    }
    String[] lCmdTokens = StringUtils.tokenizeToStringArray(lCmdLine, " ", true, false);
    List<String> lCmd = new ArrayList<String>();
    for (String lCmdToken : lCmdTokens) {
      for (int lArgIdx = 0; lArgIdx < lArgs.size(); lArgIdx++) {
        lCmdToken = lCmdToken.replace("${" + (lArgIdx + 1) + "}", lArgs.get(lArgIdx).toString());
      }
      lCmd.add(lCmdToken);
    }

    ProcessBuilder lProcBuilder = new ProcessBuilder(lCmd);
    // Map<String, String> lEnv = lProcBuilder.environment();
    lProcBuilder.directory(new File(System.getenv("temp")));

    try {
      if (mLog.isDebugEnabled()) {
        mLog.debug("Directory: " + System.getenv("temp"));
      }
      final Process process = lProcBuilder.start();
      InputStream lIS = process.getInputStream();
      InputStreamReader lISR = new InputStreamReader(lIS);
      BufferedReader lBR = new BufferedReader(lISR);
      String lLine;
      StringBuilder lStrBuf = new StringBuilder();
      while ((lLine = lBR.readLine()) != null) {
        Token lEventToken = TokenFactory.createToken(getNamespace(), "event");
        lEventToken.setString("line", lLine);
        lStrBuf.append(lLine).append("\n");
        lServer.sendToken(aConnector, lEventToken);
      }
      lResponse.setInteger("exitCode", process.exitValue());
      if (mLog.isDebugEnabled()) {
        mLog.debug("Sent '" + lStrBuf.toString().replace("\n", "\\n") + "'.");
      }
    } catch (IOException lEx) {
      lResponse.setInteger("code", -1);
      String lMsg = Logging.getSimpleExceptionMessage(lEx, "calling external process");
      lResponse.setString("msg", lMsg);
      mLog.error(lMsg);
    }

    lServer.sendToken(aConnector, lResponse);
  }