KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > StrutsGlobalAttribute


1 package org.jahia.services.applications;
2
3 import javax.servlet.ServletException JavaDoc;
4 import java.util.Enumeration JavaDoc;
5
6 /**
7  * Struts Global Attribute
8  * <p>Title: </p>
9  * <p>Description: </p>
10  * <p>Copyright: Copyright (c) 2002</p>
11  * <p>Company: </p>
12  *
13  * @author Khue Nguyen
14  * @version 1.0
15  */

16 public class StrutsGlobalAttribute {
17
18     public static String JavaDoc REQUEST_SCOPE = "REQUEST";
19     public static String JavaDoc SESSION_SCOPE = "SESSION";
20
21     private String JavaDoc attributeName;
22     private String JavaDoc attributeScope;
23     private Object JavaDoc attributeValue;
24     private boolean attributeExist = false;
25
26     /**
27      * @param attributeName
28      * @param attributeScope REQUEST_SCOPE, SESSION_SCOPE
29      */

30     public StrutsGlobalAttribute (String JavaDoc attributeName, String JavaDoc attributeScope) {
31         this.attributeName = attributeName;
32         this.attributeScope = attributeScope;
33     }
34
35     /**
36      * @param request
37      * @param response
38      *
39      * @throws ServletException
40      * @throws java.io.IOException
41      */

42     public void backupAttribute (ServletIncludeRequestWrapper request,
43                                  ServletIncludeResponseWrapper response)
44             throws ServletException JavaDoc, java.io.IOException JavaDoc {
45         if (isRequestScope ()) {
46             if (checkAttribute (request.getAttributeNames ())) {
47                 this.attributeValue = request.getAttribute (this.attributeName);
48                 request.removeAttribute (this.attributeName);
49             }
50         } else {
51             if (checkAttribute (request.getAttributeNames ())) {
52                 this.attributeValue = request.getSession ()
53                         .getAttribute (this.attributeName);
54                 request.getSession ().removeAttribute (this.attributeName);
55             }
56         }
57     }
58
59     /**
60      * @param request
61      * @param response
62      *
63      * @throws ServletException
64      * @throws java.io.IOException
65      */

66     public void restoreAttribute (ServletIncludeRequestWrapper request,
67                                   ServletIncludeResponseWrapper response)
68             throws ServletException JavaDoc, java.io.IOException JavaDoc {
69         if (this.attributeExist) {
70             if (isRequestScope ()) {
71                 request.setAttribute (this.attributeName, this.attributeValue);
72             } else {
73                 request.getSession ().setAttribute (this.attributeName,
74                         this.attributeValue);
75             }
76         }
77     }
78
79     private boolean isRequestScope () {
80         return REQUEST_SCOPE.equals (this.attributeScope);
81     }
82
83     private boolean checkAttribute (Enumeration JavaDoc names) {
84         while (names.hasMoreElements ()) {
85             String JavaDoc name = (String JavaDoc) names.nextElement ();
86             if (name.equals (this.attributeName)) {
87                 this.attributeExist = true;
88                 break;
89             }
90         }
91         return this.attributeExist;
92     }
93
94 }
95
Popular Tags