1    package geekblog.tags;
2    
3    import java.io.IOException;
4    import java.util.Set;
5    import java.util.prefs.Preferences;
6    import javax.servlet.http.HttpServletRequest;
7    import javax.servlet.jsp.JspException;
8    import javax.servlet.jsp.JspWriter;
9    import javax.servlet.jsp.tagext.TagSupport;
10   
11   /**
12    * Created by IntelliJ IDEA.
13    * User: Kevin Jones
14    * Date: Jan 23, 2003
15    * Time: 10:43:37 PM
16    * To change this template use Options | File Templates.
17    */
18   public class RandomHeadShot extends TagSupport
19   {
20       public int doStartTag() throws JspException
21       {
22           String ctx = ((HttpServletRequest)(pageContext.getRequest())).getContextPath();
23           Preferences prefs = Preferences.systemRoot().node("net/sourceforge/geekblog" + ctx);
24   
25           String headshots = prefs.get("weblog.headshotsdir", "images");
26           Set headshotsDir = pageContext.getServletContext().getResourcePaths(headshots);
27           JspWriter out = pageContext.getOut();
28   
29           System.err.println(headshotsDir);
30           String[] fileList = (String[]) headshotsDir.toArray(new String[0]);
31   
32           try
33           {
34               if (fileList.length == 1)
35                   out.write(fileList[0]);
36               else
37               {
38                   int choice = (int) (Math.random() * fileList.length);
39                   out.write(fileList[choice]);
40               }
41           }
42           catch(IOException e)
43           {}
44           return EVAL_BODY_INCLUDE;
45       }
46   }
47