@Restricted(DoNotUse.class) // accessed via REST API
 public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String json)
     throws Exception {
   // TODO JENKINS-31458 is there not an easier way to do this?
   JSONObject jsonO = JSONObject.fromObject(json);
   Jenkins j = Jenkins.getActiveInstance();
   Class<?> c = j.getPluginManager().uberClassLoader.loadClass(jsonO.getString("stapler-class"));
   StepDescriptor descriptor = (StepDescriptor) j.getDescriptor(c.asSubclass(Step.class));
   Object o;
   try {
     o = descriptor.newInstance(req, jsonO);
   } catch (RuntimeException x) { // e.g. IllegalArgumentException
     return HttpResponses.plainText(Functions.printThrowable(x));
   }
   try {
     String groovy = object2Groovy(o);
     if (descriptor.isAdvanced()) {
       String warning = Messages.Snippetizer_this_step_should_not_normally_be_used_in();
       groovy = "// " + warning + "\n" + groovy;
     }
     return HttpResponses.plainText(groovy);
   } catch (UnsupportedOperationException x) {
     Logger.getLogger(CpsFlowExecution.class.getName())
         .log(Level.WARNING, "failed to render " + json, x);
     return HttpResponses.plainText(x.getMessage());
   }
 }
 @Override
 public Builder newInstance(StaplerRequest req, JSONObject formData) throws FormException {
   // TODO f:dropdownDescriptorSelector does not seem to work sensibly: the super method uses
   // RequestImpl.bindJSON and ignores any StepDescriptor.newInstance override.
   // Cf. Snippetizer.doGenerateSnippet, which also seems to lack a standard way of parsing
   // part of a form using databinding.
   JSONObject s = formData.getJSONObject("s");
   Jenkins j = Jenkins.getActiveInstance();
   Class<?> c;
   try {
     c = j.getPluginManager().uberClassLoader.loadClass(s.getString("stapler-class"));
   } catch (ClassNotFoundException x) {
     throw new FormException(x, "s");
   }
   Descriptor<?> descriptor = j.getDescriptor(c.asSubclass(Step.class));
   return new StepBuilder((Step) descriptor.newInstance(req, s));
 }
 private String obtainClassOwnership() {
   if (this.classOwnership != null) {
     return this.classOwnership;
   }
   if (pm == null) {
     Jenkins j = Jenkins.getInstance();
     if (j != null) {
       pm = j.getPluginManager();
     }
   }
   if (pm == null) {
     return null;
   }
   // TODO: possibly recursively scan super class to discover dependencies
   PluginWrapper p = pm.whichPlugin(hudson.plugins.promoted_builds.PromotionProcess.class);
   this.classOwnership = p != null ? p.getShortName() + '@' + trimVersion(p.getVersion()) : null;
   return this.classOwnership;
 }
Example #4
0
  static {
    // register option handlers that are defined
    ClassLoaders cls = new ClassLoaders();
    Jenkins j = Jenkins.getInstance();
    if (j != null) { // only when running on the master
      cls.put(j.getPluginManager().uberClassLoader);

      ResourceNameIterator servicesIter =
          new DiscoverServiceNames(cls).findResourceNames(OptionHandler.class.getName());
      final ResourceClassIterator itr = new DiscoverClasses(cls).findResourceClasses(servicesIter);

      while (itr.hasNext()) {
        Class h = itr.nextResourceClass().loadClass();
        Class c =
            Types.erasure(Types.getTypeArgument(Types.getBaseClass(h, OptionHandler.class), 0));
        CmdLineParser.registerHandler(c, h);
      }
    }
  }
Example #5
0
  /** Gets the encrypted usage stat data to be sent to the Hudson server. */
  public String getStatData() throws IOException {
    Jenkins j = Jenkins.getInstance();

    JSONObject o = new JSONObject();
    o.put("stat", 1);
    o.put("install", j.getLegacyInstanceId());
    o.put("servletContainer", j.servletContext.getServerInfo());
    o.put("version", Jenkins.VERSION);

    List<JSONObject> nodes = new ArrayList<JSONObject>();
    for (Computer c : j.getComputers()) {
      JSONObject n = new JSONObject();
      if (c.getNode() == j) {
        n.put("master", true);
        n.put("jvm-vendor", System.getProperty("java.vm.vendor"));
        n.put("jvm-name", System.getProperty("java.vm.name"));
        n.put("jvm-version", System.getProperty("java.version"));
      }
      n.put("executors", c.getNumExecutors());
      DescriptorImpl descriptor = j.getDescriptorByType(DescriptorImpl.class);
      n.put("os", descriptor.get(c));
      nodes.add(n);
    }
    o.put("nodes", nodes);

    List<JSONObject> plugins = new ArrayList<JSONObject>();
    for (PluginWrapper pw : j.getPluginManager().getPlugins()) {
      if (!pw.isActive()) continue; // treat disabled plugins as if they are uninstalled
      JSONObject p = new JSONObject();
      p.put("name", pw.getShortName());
      p.put("version", pw.getVersion());
      plugins.add(p);
    }
    o.put("plugins", plugins);

    JSONObject jobs = new JSONObject();
    List<TopLevelItem> items = j.getAllItems(TopLevelItem.class);
    for (TopLevelItemDescriptor d : Items.all()) {
      int cnt = 0;
      for (TopLevelItem item : items) {
        if (item.getDescriptor() == d) cnt++;
      }
      jobs.put(d.getJsonSafeClassName(), cnt);
    }
    o.put("jobs", jobs);

    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      // json -> UTF-8 encode -> gzip -> encrypt -> base64 -> string
      OutputStreamWriter w =
          new OutputStreamWriter(
              new GZIPOutputStream(new CombinedCipherOutputStream(baos, getKey(), "AES")), "UTF-8");
      try {
        o.write(w);
      } finally {
        IOUtils.closeQuietly(w);
      }

      return new String(Base64.encode(baos.toByteArray()));
    } catch (GeneralSecurityException e) {
      throw new Error(e); // impossible
    }
  }