KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > common > DataAccessProviderStack


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script.common;
19
20 import java.util.Stack JavaDoc;
21 import javax.servlet.jsp.JspContext JavaDoc;
22
23 public class DataAccessProviderStack {
24
25     private static final String JavaDoc KEY = "org.apache.beehive.netui.script.common.DataAccessProviderStack";
26
27     private Stack JavaDoc _stack = null;
28
29     public static final void addDataAccessProvider(IDataAccessProvider provider, JspContext JavaDoc jspContext) {
30         assert jspContext != null;
31
32         DataAccessProviderBean bean = new DataAccessProviderBean(provider);
33
34         Object JavaDoc val = jspContext.getAttribute(KEY);
35         DataAccessProviderStack curStack = null;
36         if(val == null) {
37             curStack = new DataAccessProviderStack();
38
39             jspContext.setAttribute(KEY, curStack);
40         } else
41             curStack = (DataAccessProviderStack)val;
42
43         curStack.push(bean);
44
45         jspContext.setAttribute("container", bean);
46
47         return;
48     }
49
50     public static final DataAccessProviderBean removeDataAccessProvider(JspContext JavaDoc jspContext) {
51         assert jspContext != null;
52
53         Object JavaDoc val = jspContext.getAttribute(KEY);
54         if(val != null) {
55             DataAccessProviderStack curStack = (DataAccessProviderStack)val;
56             DataAccessProviderBean lastTop = curStack.pop();
57
58             if(!curStack.isEmpty())
59                 jspContext.setAttribute("container", curStack.peek());
60             else
61                 jspContext.removeAttribute("container");
62
63             return lastTop;
64         }
65
66         // todo: should this thrown an IllegalStateException?
67

68         return null;
69     }
70
71     public DataAccessProviderStack() {
72         _stack = new Stack JavaDoc();
73     }
74
75     public boolean isEmpty() {
76         return _stack.empty();
77     }
78
79     public DataAccessProviderBean peek() {
80         return (DataAccessProviderBean)_stack.peek();
81     }
82
83     public DataAccessProviderBean pop() {
84         return (DataAccessProviderBean)_stack.pop();
85     }
86
87     public void push(DataAccessProviderBean bean) {
88         _stack.push(bean);
89     }
90
91 }
92
Popular Tags