KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > ListMessagesTag


1 /*
2  * @(#)ListMessagesTag.java 1.4 02/04/04
3  *
4  * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  *
38  */

39
40 package demo;
41
42 import java.io.*;
43 import java.util.*;
44 import javax.mail.*;
45 import javax.mail.internet.*;
46 import javax.mail.search.*;
47 import javax.servlet.jsp.*;
48 import javax.servlet.jsp.tagext.*;
49
50 /**
51  * Custom tag for listing messages. The scripting variable is only
52  * within the body of the tag.
53  */

54 public class ListMessagesTag extends BodyTagSupport {
55     private String JavaDoc folder;
56     private String JavaDoc session;
57     private int msgNum = 0;
58     private int messageCount = 0;
59     private Message message;
60     private Message[] messages;
61     private MessageInfo messageinfo;
62     
63     /**
64      * folder attribute getter method.
65      */

66     public String JavaDoc getFolder() {
67         return folder;
68     }
69     
70     /**
71      * session attribute getter method.
72      */

73     public String JavaDoc getSession() {
74         return session;
75     }
76     
77     /**
78      * folder setter method.
79      */

80     public void setFolder(String JavaDoc folder) {
81         this.folder = folder;
82     }
83
84     /**
85      * session attribute setter method.
86      */

87     public void setSession(String JavaDoc session) {
88         this.session = session;
89     }
90
91     /**
92      * Method for processing the start of the tag.
93      */

94     public int doStartTag() throws JspException {
95         messageinfo = new MessageInfo();
96        
97         try {
98             Folder folder = (Folder)pageContext.getAttribute(
99         getFolder(), PageContext.SESSION_SCOPE);
100             FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.DELETED), false);
101             messages = folder.search(ft);
102             messageCount = messages.length;
103         } catch (Exception JavaDoc ex) {
104             throw new JspException(ex.getMessage());
105         }
106
107         if (messageCount > 0) {
108             getMessage();
109             return BodyTag.EVAL_BODY_TAG;
110         } else
111             return BodyTag.SKIP_BODY;
112     }
113    
114     /**
115      * Method for processing the body content of the tag.
116      */

117     public int doAfterBody() throws JspException {
118         
119         BodyContent body = getBodyContent();
120         try {
121             body.writeOut(getPreviousOut());
122         } catch (IOException e) {
123             throw new JspTagException("IterationTag: " + e.getMessage());
124         }
125         
126         // clear up so the next time the body content is empty
127
body.clearBody();
128        
129         if (msgNum < messageCount) {
130             getMessage();
131             return BodyTag.EVAL_BODY_TAG;
132         } else {
133             return BodyTag.SKIP_BODY;
134         }
135     }
136     
137     /**
138      * Helper method for retrieving messages.
139      */

140     private void getMessage() throws JspException {
141         message = messages[msgNum++];
142         messageinfo.setMessage(message);
143         pageContext.setAttribute(getId(), messageinfo);
144     }
145 }
146
147
Popular Tags