forked from jenkinsci/persona-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kohsuke
committed
Oct 19, 2010
1 parent
4c23ab1
commit 9440e95
Showing
4 changed files
with
176 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/hudson/plugins/persona/xml/XmlBasedPersona.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package hudson.plugins.persona.xml; | ||
|
||
import hudson.model.AbstractBuild; | ||
import hudson.model.Result; | ||
import hudson.plugins.persona.simple.Image; | ||
import hudson.plugins.persona.simple.SimplePersona; | ||
import org.dom4j.DocumentException; | ||
import org.dom4j.Element; | ||
import org.dom4j.io.SAXReader; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link SimplePersona} implementation based on XML file. | ||
* | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
public class XmlBasedPersona extends SimplePersona { | ||
public final URL xml; | ||
private final URL imageBase; | ||
private final String imageBasePath; | ||
|
||
private String icon; | ||
private String success; | ||
private String failure; | ||
private String other; | ||
private String displayName; | ||
|
||
/** | ||
* Parses a persona from an XML file. | ||
* | ||
* @param xml | ||
* Location of the XML file. | ||
* @param imageBase | ||
* Base URL to find images like icon.png, success.jpg, and so on. | ||
*/ | ||
public static XmlBasedPersona create(URL xml, URL imageBase, String imageBasePath) throws DocumentException, IOException { | ||
Element r = new SAXReader().read(xml).getRootElement(); | ||
return new XmlBasedPersona(r,xml,imageBase,imageBasePath); | ||
} | ||
|
||
private XmlBasedPersona(Element r, URL xml, URL imageBase, String imageBasePath) throws IOException, DocumentException { | ||
super(r.attributeValue("id"),new ArrayList<String>()); | ||
|
||
this.xml = xml; | ||
this.imageBase = imageBase; | ||
this.imageBasePath = imageBasePath; | ||
|
||
reload(); | ||
} | ||
|
||
/** | ||
* Finds an image in ${imageBase}/${baseName}.* for some known extension | ||
*/ | ||
private String findImage(URL imageBase, String imageBasePath, String baseName) throws IOException { | ||
for (String ext : EXTENSIONS) { | ||
try { | ||
new URL(imageBase,baseName+ext).openStream().close(); | ||
// found it. | ||
return imageBasePath+'/'+baseName+ext; | ||
} catch (IOException e) { | ||
// not found. try next | ||
} | ||
} | ||
throw new IOException("No image found that matches "+imageBase+"/"+baseName+".*"); | ||
} | ||
|
||
/** | ||
* Reloads the configuration of this persona from its original XML. | ||
*/ | ||
public void reload() throws IOException, DocumentException { | ||
this.icon = findImage(imageBase, imageBasePath, "icon"); | ||
this.success = findImage(imageBase, imageBasePath, "success"); | ||
this.failure = findImage(imageBase, imageBasePath, "failure"); | ||
this.other = findImage(imageBase, imageBasePath, "other"); | ||
|
||
Element r = new SAXReader().read(xml).getRootElement(); | ||
this.displayName = r.attributeValue("displayName"); | ||
|
||
List<String> quotes = new ArrayList<String>(); | ||
for (Element e : (List<Element>)r.elements("quote")) { | ||
quotes.add(e.getTextTrim()); | ||
} | ||
setQuotes(quotes); | ||
} | ||
|
||
@Override | ||
public Image getImage(AbstractBuild<?,?> build) { | ||
Result r = build.getResult(); | ||
if (r== Result.SUCCESS) | ||
return new Image(icon, success); | ||
if (r== Result.FAILURE) | ||
return new Image(icon, failure); | ||
return new Image(icon, other); | ||
} | ||
|
||
@Override | ||
public Image getDefaultImage() { | ||
return new Image(icon, success); | ||
} | ||
|
||
public String getDisplayName() { | ||
return displayName; | ||
} | ||
|
||
private static final String[] EXTENSIONS = {".jpg",".jpeg",".png",".gif", | ||
".JPG",".JPEG",".PNG",".GIF"}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/hudson/plugins/persona/xml/XmlPersonaReloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package hudson.plugins.persona.xml; | ||
|
||
import hudson.Extension; | ||
import hudson.ExtensionComponent; | ||
import hudson.ExtensionList; | ||
import hudson.Util; | ||
import hudson.model.Hudson; | ||
import hudson.model.RootAction; | ||
import hudson.plugins.persona.Persona; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
/** | ||
* Exposes an URL to reload XML persona. | ||
* | ||
* @author Kohsuke Kawaguchi | ||
*/ | ||
@Extension | ||
public class XmlPersonaReloader implements RootAction { | ||
public String getIconFileName() { | ||
return null; | ||
} | ||
|
||
public String getDisplayName() { | ||
return null; | ||
} | ||
|
||
public String getUrlName() { | ||
return "reload-persona"; | ||
} | ||
|
||
public void doIndex(StaplerResponse rsp) throws IOException { | ||
Hudson.getInstance().checkPermission(Hudson.ADMINISTER); | ||
rsp.setContentType("text/plain"); | ||
PrintWriter w = rsp.getWriter(); | ||
|
||
ExtensionList<Persona> all = Persona.all(); | ||
|
||
for (XmlBasedPersona p : Util.filter(all,XmlBasedPersona.class)) { | ||
w.println("Reloaded "+p.xml); | ||
} | ||
|
||
// find new personas | ||
for (ExtensionComponent<Persona> c : new XmlPersonaFinder().find(Persona.class, Hudson.getInstance())) { | ||
XmlBasedPersona p = (XmlBasedPersona)c.getInstance(); | ||
if (Persona.byId(p.id)==null) | ||
all.add(all.size(),p); | ||
} | ||
} | ||
} |