KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > tags > AccessTag


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.tags;
27
28 import net.killingar.forum.internal.AccessLevel;
29 import net.killingar.forum.internal.managers.ForumManager;
30 import webwork.util.ServletValueStack;
31
32 import javax.servlet.jsp.JspException JavaDoc;
33 import javax.servlet.jsp.JspTagException JavaDoc;
34 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
35
36 public class AccessTag extends BodyTagSupport JavaDoc
37 {
38     protected ServletValueStack stack;
39
40     private long access = 0;
41     private long ownerID = -1;
42
43    protected ServletValueStack getStack()
44    {
45       if (stack == null) {
46          stack = ServletValueStack.getStack(pageContext);
47       }
48       else
49       {
50         // Set the pageContext in the stack to make sure that the context
51
// is the correct one, since the same stack is used for the whole
52
// request, which can mean several different pageContexts
53
stack.setContext(pageContext);
54       }
55       return stack;
56    }
57
58    protected Object JavaDoc findValue(String JavaDoc aName)
59    {
60       return getStack().findValue(aName);
61    }
62
63     public void setOwnerID(String JavaDoc attr)
64     {
65         ownerID = ((Long JavaDoc)findValue(attr)).longValue();
66     }
67
68   public int doStartTag() throws JspException JavaDoc
69   {
70         try
71         {
72             ForumManager manager = (ForumManager)pageContext.getSession().getAttribute("manager");
73             if (manager.hasAccess(manager.getUserID(), access))
74                 return EVAL_BODY_TAG;
75             else if (ownerID != -1 && ownerID == manager.getUserID())
76                 return EVAL_BODY_TAG;
77             else
78                 return SKIP_BODY;
79         }
80         catch (Exception JavaDoc ex)
81         {
82             //ex.printStackTrace(new java.io.PrintWriter(pageContext.getOut()));
83
throw new JspTagException JavaDoc("accessTag: " + ex);
84             //return SKIP_BODY;
85
}
86   }
87
88     public void setOwner(String JavaDoc s)
89     {
90         Object JavaDoc o = findValue(s);
91         if (o != null)
92             ownerID = ((Long JavaDoc)findValue(s)).longValue();
93     }
94
95     public void setAccess(String JavaDoc s)
96     {
97         try
98         {
99             access = Long.parseLong(s);
100         }
101         catch (NumberFormatException JavaDoc e)
102         {
103             access = AccessLevel.parseAccessLevel(s);
104         }
105     }
106
107   public int doEndTag() throws JspTagException JavaDoc
108   {
109     try
110         {
111             if (bodyContent != null)
112                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
113
114             bodyContent = null;
115         }
116         catch (java.io.IOException JavaDoc e)
117         {
118             throw new JspTagException JavaDoc(e.toString());
119         }
120         return EVAL_PAGE;
121   }
122
123     public int doAfterBody() throws JspTagException JavaDoc
124     {
125         return SKIP_BODY;
126     }
127 }
128
Popular Tags