/**
   * Attempts to compile the given InputStream into a Groovy script using the given name
   *
   * @param in The InputStream to read the Groovy code from
   * @param name The name of the class to use
   * @param pageName The page name
   * @param metaInfo
   * @return The compiled java.lang.Class, which is an instance of groovy.lang.Script
   */
  private Class<?> compileGroovyPage(
      InputStream in, String name, String pageName, GroovyPageMetaInfo metaInfo) {
    GroovyClassLoader groovyClassLoader = findOrInitGroovyClassLoader();

    // Compile the script into an object
    Class<?> scriptClass;
    try {
      scriptClass = groovyClassLoader.parseClass(DefaultGroovyMethods.getText(in), name);
    } catch (CompilationFailedException e) {
      LOG.error("Compilation error compiling GSP [" + name + "]:" + e.getMessage(), e);

      int lineNumber = GrailsExceptionResolver.extractLineNumber(e);

      final int[] lineMappings = metaInfo.getLineNumbers();
      if (lineNumber > 0 && lineNumber < lineMappings.length) {
        lineNumber = lineMappings[lineNumber - 1];
      }
      throw new GroovyPagesException(
          "Could not parse script [" + name + "]: " + e.getMessage(), e, lineNumber, pageName);
    } catch (IOException e) {
      throw new GroovyPagesException(
          "IO exception parsing script [" + name + "]: " + e.getMessage(), e);
    }
    return scriptClass;
  }
 /**
  * Parses the groovy script into a class. We store the Class instead of the script proper so that
  * it doesn't invoke race conditions on multiple executions of the script.
  */
 private void createGroovyClass() {
   try {
     GroovyClassLoader loader = new GroovyClassLoader(getClassLoader());
     InputStream scriptIs = getScriptInputStream();
     GroovyCodeSource groovyCodeSource =
         new GroovyCodeSource(scriptIs, "nanocontainer.groovy", "groovyGeneratedForNanoContainer");
     scriptClass = loader.parseClass(groovyCodeSource);
   } catch (CompilationFailedException e) {
     throw new GroovyCompilationException("Compilation Failed '" + e.getMessage() + "'", e);
   } catch (IOException e) {
     throw new NanoContainerMarkupException(e);
   }
 }
Esempio n. 3
0
  // TODO Cache these...though this will hurt hot-reloading. Perhaps a debug mode configuration?
  private Template getTemplate(final String acceptHeader, final String templateKey)
      throws AproxGroovyException {
    final String accept = (acceptHeader == null ? "" : acceptHeader.replace('/', '_') + "/");
    try {
      final String filename = accept + templateKey + ".groovy";
      final DataFile templateFile = manager.getDataFile(TEMPLATES, filename);
      logger.info(
          "Looking for template: {} for ACCEPT header: {} in: {}",
          templateKey,
          acceptHeader,
          templateFile);

      Template template;
      if (templateFile.exists() && !templateFile.isDirectory()) {
        template = engine.createTemplate(templateFile.readString());
      } else {
        final String urlpath = TEMPLATES + "/" + accept + templateKey + ".groovy";
        logger.info(
            "Looking for template: {} for ACCEPT header: {} in: {}",
            templateKey,
            acceptHeader,
            urlpath);

        final URL u = Thread.currentThread().getContextClassLoader().getResource(urlpath);

        template = u == null ? null : engine.createTemplate(u);
      }

      if (template == null) {
        throw new AproxGroovyException(
            "Failed to locate template: %s (with ACCEPT header: %s)", templateKey, acceptHeader);
      }

      return template;
    } catch (final CompilationFailedException e) {
      throw new AproxGroovyException(
          "Failed to compile template: %s. Reason: %s", e, templateKey, e.getMessage());
    } catch (final ClassNotFoundException e) {
      throw new AproxGroovyException(
          "Failed to compile template: %s. Reason: %s", e, templateKey, e.getMessage());
    } catch (final IOException e) {
      throw new AproxGroovyException(
          "Failed to read template: %s. Reason: %s", e, templateKey, e.getMessage());
    }
  }
Esempio n. 4
0
 /**
  * Parses a script
  *
  * @param clazzName
  * @param sourceCode
  * @return
  */
 public String parseScript(String clazzName, String sourceCode) {
   String compilationError = null;
   int lastIndexOf = clazzName.lastIndexOf(".");
   String codeBase = clazzName;
   if (lastIndexOf != -1) {
     codeBase = clazzName.substring(0, lastIndexOf);
   }
   GroovyCodeSource groovyCodeSource = new GroovyCodeSource(sourceCode, clazzName, codeBase);
   try {
     Class<?> parsedClass = groovyClassLoader.parseClass(groovyCodeSource, false);
     availableClasses.put(clazzName, parsedClass);
   } catch (CompilationFailedException e) {
     compilationError = "Compilation for " + clazzName + " failed with " + e.getMessage();
     LOGGER.warn(compilationError);
   } catch (Exception ex) {
     compilationError = "Parsing class " + clazzName + " failed with " + ex.getMessage();
     LOGGER.warn(compilationError);
   }
   return compilationError;
 }
Esempio n. 5
0
  @Override
  public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    PollStatus serviceStatus = PollStatus.unavailable("Poll not completed yet");
    TimeoutTracker tracker =
        new TimeoutTracker(parameters, DEFAULT_SEQUENCE_RETRY, DEFAULT_TIMEOUT);

    for (tracker.reset();
        tracker.shouldRetry() && !serviceStatus.isAvailable();
        tracker.nextAttempt()) {
      String seleniumTestFilename = getGroovyFilename(parameters);
      try {

        Map<String, Number> responseTimes = new HashMap<String, Number>();
        responseTimes.put(PollStatus.PROPERTY_RESPONSE_TIME, Double.NaN);

        tracker.startAttempt();
        Result result =
            runTest(
                getBaseUrl(parameters, svc),
                getTimeout(parameters),
                createGroovyClass(seleniumTestFilename));
        double responseTime = tracker.elapsedTimeInMillis();
        responseTimes.put(PollStatus.PROPERTY_RESPONSE_TIME, responseTime);

        if (result.wasSuccessful()) {
          serviceStatus = PollStatus.available();
          serviceStatus.setProperties(responseTimes);
        } else {
          serviceStatus = PollStatus.unavailable(getFailureMessage(result, svc));
        }
      } catch (CompilationFailedException e) {
        serviceStatus =
            PollStatus.unavailable(
                "Selenium page sequence attempt on:"
                    + svc.getIpAddr()
                    + " failed : selenium-test compilation error "
                    + e.getMessage());
        String reason = "Selenium sequence failed: CompilationFailedException" + e.getMessage();
        SeleniumMonitor.LOG.debug(reason);
        PollStatus.unavailable(reason);
      } catch (IOException e) {
        serviceStatus =
            PollStatus.unavailable(
                "Selenium page sequence attempt on "
                    + svc.getIpAddr()
                    + " failed: IOException occurred, failed to find selenium-test: "
                    + seleniumTestFilename);
        String reason = "Selenium sequence failed: IOException: " + e.getMessage();
        SeleniumMonitor.LOG.debug(reason);
        PollStatus.unavailable(reason);
      } catch (Exception e) {
        serviceStatus =
            PollStatus.unavailable(
                "Selenium page sequence attempt on "
                    + svc.getIpAddr()
                    + " failed:\n"
                    + e.getMessage());
        String reason = "Selenium sequence failed: Exception: " + e.getMessage();
        SeleniumMonitor.LOG.debug(reason);
        PollStatus.unavailable(reason);
      }
    }

    return serviceStatus;
  }