private GroovyShell createEngine(AbstractBuild<?, ?> build, TaskListener listener) {

    ClassLoader cl = Jenkins.getInstance().getPluginManager().uberClassLoader;
    ScriptSandbox sandbox = null;
    CompilerConfiguration cc = new CompilerConfiguration();
    cc.addCompilationCustomizers(
        new ImportCustomizer()
            .addStarImports("jenkins", "jenkins.model", "hudson", "hudson.model"));

    ExtendedEmailPublisher publisher =
        build.getProject().getPublishersList().get(ExtendedEmailPublisher.class);
    if (publisher.getDescriptor().isSecurityEnabled()) {
      cc.addCompilationCustomizers(new SandboxTransformer());
      sandbox = new ScriptSandbox();
    }

    Binding binding = new Binding();
    binding.setVariable("build", build);
    binding.setVariable("project", build.getParent());
    binding.setVariable("rooturl", publisher.getDescriptor().getHudsonUrl());
    binding.setVariable("out", listener.getLogger());

    GroovyShell shell = new GroovyShell(cl, binding, cc);

    if (sandbox != null) {
      sandbox.register();
    }

    return shell;
  }
  private boolean executePresendScript(
      AbstractBuild<?, ?> build,
      BuildListener listener,
      MimeMessage msg,
      EmailTrigger trigger,
      Map<String, EmailTrigger> triggered)
      throws RuntimeException {
    boolean cancel = false;
    presendScript = new ContentBuilder().transformText(presendScript, this, build, listener);
    if (StringUtils.isNotBlank(presendScript)) {
      listener.getLogger().println("Executing pre-send script");
      ClassLoader cl = Jenkins.getInstance().getPluginManager().uberClassLoader;
      ScriptSandbox sandbox = null;
      CompilerConfiguration cc = new CompilerConfiguration();
      cc.addCompilationCustomizers(
          new ImportCustomizer()
              .addStarImports("jenkins", "jenkins.model", "hudson", "hudson.model"));

      if (ExtendedEmailPublisher.DESCRIPTOR.isSecurityEnabled()) {
        debug(listener.getLogger(), "Setting up sandbox for pre-send script");
        cc.addCompilationCustomizers(new SandboxTransformer());
        sandbox = new ScriptSandbox();
      }

      Binding binding = new Binding();
      binding.setVariable("build", build);
      binding.setVariable("msg", msg);
      binding.setVariable("logger", listener.getLogger());
      binding.setVariable("cancel", cancel);
      binding.setVariable("trigger", trigger);
      binding.setVariable("triggered", Collections.unmodifiableMap(triggered));

      GroovyShell shell = new GroovyShell(cl, binding, cc);
      StringWriter out = new StringWriter();
      PrintWriter pw = new PrintWriter(out);

      if (sandbox != null) {
        sandbox.register();
      }

      try {
        Object output = shell.evaluate(presendScript);
        if (output != null) {
          pw.println("Result: " + output);
          cancel = ((Boolean) shell.getVariable("cancel")).booleanValue();
          debug(listener.getLogger(), "Pre-send script set cancel to %b", cancel);
        }
      } catch (SecurityException e) {
        listener
            .getLogger()
            .println("Pre-send script tried to access secured objects: " + e.getMessage());
      } catch (Throwable t) {
        t.printStackTrace(pw);
        listener.getLogger().println(out.toString());
        // should we cancel the sending of the email???
      }
      debug(listener.getLogger(), out.toString());
    }
    return !cancel;
  }
 @Inject
 public GroovyScriptEngineService(Settings settings) {
   super(settings);
   ImportCustomizer imports = new ImportCustomizer();
   imports.addStarImports("org.joda.time");
   imports.addStaticStars("java.lang.Math");
   CompilerConfiguration config = new CompilerConfiguration();
   config.addCompilationCustomizers(imports);
   this.sandboxed = settings.getAsBoolean(GROOVY_SCRIPT_SANDBOX_ENABLED, true);
   if (this.sandboxed) {
     config.addCompilationCustomizers(
         GroovySandboxExpressionChecker.getSecureASTCustomizer(settings));
   }
   this.loader = new GroovyClassLoader(settings.getClassLoader(), config);
 }
Esempio n. 4
0
  public static CompilerConfiguration generateCompilerConfigurationFromOptions(CommandLine cli)
      throws IOException {
    //
    // Setup the configuration data

    CompilerConfiguration configuration = new CompilerConfiguration();

    if (cli.hasOption("classpath")) {
      configuration.setClasspath(cli.getOptionValue("classpath"));
    }

    if (cli.hasOption('d')) {
      configuration.setTargetDirectory(cli.getOptionValue('d'));
    }

    if (cli.hasOption("encoding")) {
      configuration.setSourceEncoding(cli.getOptionValue("encoding"));
    }

    if (cli.hasOption("basescript")) {
      configuration.setScriptBaseClass(cli.getOptionValue("basescript"));
    }

    // joint compilation parameters
    if (cli.hasOption('j')) {
      Map<String, Object> compilerOptions = new HashMap<String, Object>();

      String[] opts = cli.getOptionValues("J");
      compilerOptions.put("namedValues", opts);

      opts = cli.getOptionValues("F");
      compilerOptions.put("flags", opts);

      configuration.setJointCompilationOptions(compilerOptions);
    }

    if (cli.hasOption("indy")) {
      configuration.getOptimizationOptions().put("int", false);
      configuration.getOptimizationOptions().put("indy", true);
    }

    if (cli.hasOption("configscript")) {
      String path = cli.getOptionValue("configscript");
      File groovyConfigurator = new File(path);
      Binding binding = new Binding();
      binding.setVariable("configuration", configuration);

      CompilerConfiguration configuratorConfig = new CompilerConfiguration();
      ImportCustomizer customizer = new ImportCustomizer();
      customizer.addStaticStars(
          "org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
      configuratorConfig.addCompilationCustomizers(customizer);

      GroovyShell shell = new GroovyShell(binding, configuratorConfig);
      shell.evaluate(groovyConfigurator);
    }

    return configuration;
  }
Esempio n. 5
0
 private GroovyClassLoader createClassLoader(
     ClassLoader parentLoader,
     final boolean staticCompile,
     Class<? extends Script> scriptBaseClass) {
   final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
   if (!scriptBaseClass.equals(Script.class)) {
     compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
   }
   compilerConfiguration.addCompilationCustomizers(
       new CompilationCustomizer(CompilePhase.CONVERSION) {
         @Override
         public void call(SourceUnit source, GeneratorContext context, ClassNode classNode)
             throws org.codehaus.groovy.control.CompilationFailedException {
           if (staticCompile) {
             classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
           }
           classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
         }
       });
   return new GroovyClassLoader(parentLoader, compilerConfiguration);
 }
Esempio n. 6
0
    public String call() throws RuntimeException {
      // if we run locally, cl!=null. Otherwise the delegating classloader will be available as
      // context classloader.
      if (cl == null) cl = Thread.currentThread().getContextClassLoader();
      CompilerConfiguration cc = new CompilerConfiguration();
      cc.addCompilationCustomizers(
          new ImportCustomizer()
              .addStarImports("jenkins", "jenkins.model", "hudson", "hudson.model"));
      GroovyShell shell = new GroovyShell(cl, new Binding(), cc);

      StringWriter out = new StringWriter();
      PrintWriter pw = new PrintWriter(out);
      shell.setVariable("out", pw);
      try {
        Object output = shell.evaluate(script);
        if (output != null) pw.println("Result: " + output);
      } catch (Throwable t) {
        t.printStackTrace(pw);
      }
      return out.toString();
    }