Skip to content

Commit

Permalink
JENKINS-9302, JENKINS-9130 colorize groovy script areas
Browse files Browse the repository at this point in the history
  • Loading branch information
imod committed Apr 9, 2011
1 parent 0b303ef commit bc3f719
Show file tree
Hide file tree
Showing 18 changed files with 2,572 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@
/.settings
/.classpath
/.project
/target
/work
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.340</version>
<version>1.380</version>
<!-- which version of Hudson is this plugin built against? -->
<relativePath>../pom.xml</relativePath>
</parent>
Expand Down
Expand Up @@ -24,41 +24,36 @@
package org.jvnet.hudson.plugins.scriptler;

import hudson.Extension;
import hudson.PluginWrapper;
import hudson.Util;
import hudson.model.ManagementLink;
import hudson.model.Computer;
import hudson.model.ComputerSet;
import hudson.model.Hudson;
import hudson.model.Hudson.MasterComputer;
import hudson.security.Permission;
import hudson.util.RemotingDiagnostics;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jvnet.hudson.plugins.scriptler.config.Script;
import org.jvnet.hudson.plugins.scriptler.config.ScriptlerConfiguration;
import org.jvnet.hudson.plugins.scriptler.share.Catalog;
import org.jvnet.hudson.plugins.scriptler.share.CatalogEntry;
import org.jvnet.hudson.plugins.scriptler.share.CatalogInfo;
import org.jvnet.hudson.plugins.scriptler.share.CatalogManager;
import org.jvnet.hudson.plugins.scriptler.util.ScriptHelper;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -77,9 +72,6 @@ public class ScriptlerManagment extends ManagementLink {

private final static Logger LOGGER = Logger.getLogger(ScriptlerManagment.class.getName());

// always retrieve via getter
private ScriptlerConfiguration cfg = null;

/*
* (non-Javadoc)
*
Expand All @@ -100,6 +92,10 @@ public String getUrlName() {
return "scriptler";
}

public boolean disableRemoteCatalog() {
return getConfiguration().isDisbableRemoteCatalog();
}

/*
* (non-Javadoc)
*
Expand All @@ -119,14 +115,23 @@ public ScriptlerManagment getScriptler() {
}

public ScriptlerConfiguration getConfiguration() {
if (cfg == null) {
try {
cfg = ScriptlerConfiguration.load();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to load scriptler configuration", e);
}
}
return cfg;
return ScriptlerConfiguration.getConfiguration();
}

public String getPluginResourcePath() {
PluginWrapper wrapper = Hudson.getInstance().getPluginManager().getPlugin(ScritplerPluginImpl.class);
return Hudson.getInstance().getRootUrl() + "plugin/" + wrapper.getShortName() + "/";
}

public HttpResponse doScriptlerSettings(StaplerRequest res, StaplerResponse rsp,
@QueryParameter("disableRemoteCatalog") boolean disableRemoteCatalog) throws IOException {
checkPermission(Hudson.ADMINISTER);

ScriptlerConfiguration cfg = getConfiguration();
cfg.setDisbableRemoteCatalog(disableRemoteCatalog);
cfg.save();

return new HttpRedirect("settings");
}

/**
Expand All @@ -143,11 +148,16 @@ public ScriptlerConfiguration getConfiguration() {
* @return same forward as from <code>doScriptAdd</code>
* @throws IOException
*/
public HttpResponse doDownloadScript(StaplerRequest res, StaplerResponse rsp, @QueryParameter("id") String id, @QueryParameter("catalog") String catalogName)
throws IOException {
public HttpResponse doDownloadScript(StaplerRequest res, StaplerResponse rsp, @QueryParameter("id") String id,
@QueryParameter("catalog") String catalogName) throws IOException {
checkPermission(Hudson.ADMINISTER);

CatalogInfo catInfo = getConfiguration().getCatalogInfo(catalogName);
ScriptlerConfiguration c = getConfiguration();
if (c.isDisbableRemoteCatalog()) {
return new HttpRedirect("index");
}

CatalogInfo catInfo = c.getCatalogInfo(catalogName);
CatalogManager catalogManager = new CatalogManager(catInfo);
Catalog catalog = catalogManager.loadCatalog();
CatalogEntry entry = catalog.getEntryById(id);
Expand Down Expand Up @@ -178,8 +188,9 @@ public HttpResponse doDownloadScript(StaplerRequest res, StaplerResponse rsp, @Q
* @return forward to 'index'
* @throws IOException
*/
public HttpResponse doScriptAdd(StaplerRequest res, StaplerResponse rsp, @QueryParameter("name") String name, @QueryParameter("comment") String comment,
@QueryParameter("script") String script, String originCatalogName, String originId) throws IOException {
public HttpResponse doScriptAdd(StaplerRequest res, StaplerResponse rsp, @QueryParameter("name") String name,
@QueryParameter("comment") String comment, @QueryParameter("script") String script, String originCatalogName, String originId)
throws IOException {
checkPermission(Hudson.ADMINISTER);

if (StringUtils.isEmpty(script) || StringUtils.isEmpty(name)) {
Expand All @@ -195,7 +206,8 @@ public HttpResponse doScriptAdd(StaplerRequest res, StaplerResponse rsp, @QueryP

Script newScript = null;
if (!StringUtils.isEmpty(originId)) {
newScript = new Script(name, comment, true, originCatalogName, originId, new SimpleDateFormat("dd MMM yyyy HH:mm:ss a").format(new Date()));
newScript = new Script(name, comment, true, originCatalogName, originId,
new SimpleDateFormat("dd MMM yyyy HH:mm:ss a").format(new Date()));
} else {
// save (overwrite) the meta information
newScript = new Script(name, comment);
Expand Down Expand Up @@ -261,7 +273,7 @@ public HttpResponse doUploadScript(StaplerRequest req) throws IOException, Servl

fileItem.write(new File(rootDir, fileName));

Script script = getScript(fileName, false);
Script script = ScriptHelper.getScript(fileName, false);
if (script == null) {
script = new Script(fileName, "uploaded");
}
Expand All @@ -277,30 +289,6 @@ public HttpResponse doUploadScript(StaplerRequest req) throws IOException, Servl
}
}

/**
* Loads the script information.
*
* @param scriptName
* the name of the script
* @param withSrc
* should the script sources be loaded too?
* @return the script
*/
protected Script getScript(String scriptName, boolean withSrc) {
Script s = getConfiguration().getScriptByName(scriptName);
File scriptSrc = new File(getScriptDirectory(), scriptName);
if (withSrc) {
try {
Reader reader = new FileReader(scriptSrc);
String src = IOUtils.toString(reader);
s.setScript(src);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "not able to load sources for script [" + scriptName + "]", e);
}
}
return s;
}

/**
* Display the screen to trigger a script. The source of the script get
* loaded from the filesystem and placed in the request to display it on the
Expand All @@ -318,7 +306,7 @@ protected Script getScript(String scriptName, boolean withSrc) {
public void doRunScript(StaplerRequest req, StaplerResponse rsp, @QueryParameter("name") String scriptName) throws IOException, ServletException {
checkPermission(Hudson.ADMINISTER);

Script script = getScript(scriptName, true);
Script script = ScriptHelper.getScript(scriptName, true);
req.setAttribute("script", script);
// set default selection
req.setAttribute("currentNode", "(master)");
Expand Down Expand Up @@ -352,57 +340,16 @@ public void doTriggerScript(StaplerRequest req, StaplerResponse rsp, @QueryParam

// set the script info back to the request, to display it together with
// the output.
Script tempScript = getScript(scriptName, false);
Script tempScript = ScriptHelper.getScript(scriptName, false);
tempScript.setScript(script);
req.setAttribute("script", tempScript);
req.setAttribute("currentNode", node);

String output = doScript(node, script);
String output = ScriptHelper.doScript(node, script);
req.setAttribute("output", output);
req.getView(this, "runscript.jelly").forward(req, rsp);
}

/**
* Runs the execution on a given slave.
*
* @param node
* where to run the script.
* @param scriptTxt
* the script (groovy) to be executed.
* @return the output
* @throws IOException
* @throws ServletException
*/
private String doScript(String node, String scriptTxt) throws IOException, ServletException {

String output = "[no output]";
if (node != null && scriptTxt != null) {

try {

Computer comp = Hudson.getInstance().getComputer(node);
if (comp == null && "(master)".equals(node)) {
output = RemotingDiagnostics.executeGroovy(scriptTxt, MasterComputer.localChannel);
} else if (comp == null) {
output = Messages.node_not_found(node);
} else {
if (comp.getChannel() == null) {
output = Messages.node_not_online(node);
}

else {
output = RemotingDiagnostics.executeGroovy(scriptTxt, comp.getChannel());
}
}

} catch (InterruptedException e) {
throw new ServletException(e);
}
}
return output;

}

/**
* Loads the script by its name and forwards the request to "edit.jelly".
*
Expand All @@ -418,7 +365,7 @@ private String doScript(String node, String scriptTxt) throws IOException, Servl
public void doEditScript(StaplerRequest req, StaplerResponse rsp, @QueryParameter("name") String scriptName) throws IOException, ServletException {
checkPermission(Hudson.ADMINISTER);

Script script = getScript(scriptName, true);
Script script = ScriptHelper.getScript(scriptName, true);
req.setAttribute("script", script);
req.getView(this, "edit.jelly").forward(req, rsp);
}
Expand Down
Expand Up @@ -112,13 +112,15 @@ private void synchronizeConfig() throws IOException {
// remove the old catalog, we now rely on scriptlerweb!
for (Iterator<CatalogInfo> iterator = catalogInfos.iterator(); iterator.hasNext();) {
CatalogInfo catalogInfo = iterator.next();
if(catalogInfo.name.equals("default")){
if (catalogInfo.name.equals("default")) {
iterator.remove();
}
}
if (catalogInfos.size() == 0) {
// CatalogInfo defaultCat = new CatalogInfo("default", DEFAULT_CATALOG, DEFAULT_LOCATION + "/{0}", DEFAULT_LOCATION + "/{0}");
// catalogInfos.add(defaultCat);
// CatalogInfo defaultCat = new CatalogInfo("default",
// DEFAULT_CATALOG, DEFAULT_LOCATION + "/{0}", DEFAULT_LOCATION +
// "/{0}");
// catalogInfos.add(defaultCat);
CatalogInfo scriptlerWeb = new CatalogInfo("scriptlerweb", "http://scriptlerweb.appspot.com/catalog/xml",
"http://scriptlerweb.appspot.com/script/show/{1}", "http://scriptlerweb.appspot.com/script/download/{1}");
catalogInfos.add(scriptlerWeb);
Expand All @@ -130,16 +132,15 @@ private void synchronizeConfig() throws IOException {
/**
* search into the declared backup directory for backup archives
*/
@SuppressWarnings("unchecked")
public List<File> getAvailableScripts() throws IOException {
File scriptDirectory = ScriptlerManagment.getScriptDirectory();
LOGGER.log(Level.FINE, "Listing files of {0}", scriptDirectory.getAbsoluteFile());

File[] scriptFiles = scriptDirectory.listFiles();

List fileList;
List<File> fileList;
if (scriptFiles == null) {
fileList = new ArrayList();
fileList = new ArrayList<File>();
} else {
fileList = Arrays.asList(scriptFiles);
}
Expand Down
Expand Up @@ -35,6 +35,8 @@
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jvnet.hudson.plugins.scriptler.ScriptlerManagment;
import org.jvnet.hudson.plugins.scriptler.share.CatalogInfo;
Expand All @@ -46,8 +48,12 @@
*/
public final class ScriptlerConfiguration extends ScriptSet implements Saveable {

private final static Logger LOGGER = Logger.getLogger(ScriptlerConfiguration.class.getName());

private List<CatalogInfo> catalogInfos = new ArrayList<CatalogInfo>();

private boolean disbableRemoteCatalog = false;

public ScriptlerConfiguration(SortedSet<Script> scripts) {
if (scripts != null) {
this.scriptSet = scripts;
Expand Down Expand Up @@ -104,6 +110,20 @@ public static ScriptlerConfiguration load() throws IOException {
}
}

// always retrieve via getter
private static transient ScriptlerConfiguration cfg = null;

public static ScriptlerConfiguration getConfiguration() {
if (cfg == null) {
try {
cfg = ScriptlerConfiguration.load();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to load scriptler configuration", e);
}
}
return cfg;
}

private static final XStream XSTREAM = new XStream2();

static {
Expand All @@ -112,4 +132,12 @@ public static ScriptlerConfiguration load() throws IOException {
XSTREAM.alias("catalog", CatalogInfo.class);
}

public boolean isDisbableRemoteCatalog() {
return disbableRemoteCatalog;
}

public void setDisbableRemoteCatalog(boolean disbableRemoteCatalog) {
this.disbableRemoteCatalog = disbableRemoteCatalog;
}

}

0 comments on commit bc3f719

Please sign in to comment.