KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > server > FiltersEdit


1 package org.enhydra.server;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import org.enhydra.server.util.WebAppXML;
7
8 /**
9  * <p>Title: </p>
10  * <p>Description: </p>
11  * <p>Copyright: Copyright (c) 2002</p>
12  * <p>Company: </p>
13  * @author Damir Milovic
14  * @version 1.0
15  */

16
17 public class FiltersEdit {
18     private EnhydraServer es = null;
19     private String JavaDoc appName = null;
20     private WebAppXML webXML = null;
21     private HashMap JavaDoc filters = null;
22
23     public FiltersEdit(String JavaDoc appName) {
24         es = EnhydraServer.getInstance();
25         this.appName = appName;
26         AppInfo appInfo = es.getAppInfo(appName);
27         webXML = appInfo.getWebAppXML();
28         reloadFilters();
29     }
30     public void reloadFilters(){
31         
32         //refresh document from web.xml file
33
webXML.reloadDocument();
34         refreshFilters();
35         
36     }
37     
38     private void refreshFilters(){
39         String JavaDoc[] filterNames = webXML.getFilterNames();
40         filters = new HashMap JavaDoc();
41         if(filterNames != null){
42             for(int i=0 ;i<filterNames.length ; i++){
43                 FilterData filterData = createFilterData(filterNames[i]);
44                 filters.put(filterNames[i], filterData);
45             }
46         }
47     }
48
49     public WebAppXML getWebAppXML(){
50         return webXML;
51     }
52
53
54
55     public String JavaDoc[] getFilterNames(){
56         return webXML.getFilterNames();
57     }
58
59     public FilterData getFilterData(String JavaDoc filterName){
60        return (FilterData) filters.get(filterName);
61     }
62
63     public void addFilter(FilterData filterData){
64         HashMap JavaDoc params = new HashMap JavaDoc();
65         if(webXML.getFilterParameters(filterData.getName()).containsKey("filter-class")){
66             // it is not empty HashMap, filters with given name allready exist,
67
// so remove it first.
68
webXML.removeFilter(filterData.getName());
69         }
70         
71         
72         // add filter properties
73
String JavaDoc name, className, description, displayName, smallIcon, largeIcon,urlPattern, servletName;
74         ArrayList JavaDoc initParamsArray = filterData.getInitParams();
75         
76         name = filterData.getName().trim();
77         className = filterData.getFilterClass().trim();
78         
79         description = filterData.getDescription();
80         //if empty string set null
81
if(description!=null && description.trim().equalsIgnoreCase("")){
82             description = null;
83         }
84         
85         displayName = filterData.getDisplayName();
86         if(displayName!=null && displayName.trim().equalsIgnoreCase("")){
87             displayName = null;
88         }
89         
90         smallIcon = filterData.getSmallIcon();
91         if(smallIcon!=null && smallIcon.trim().equalsIgnoreCase("")){
92             smallIcon = null;
93         }
94         
95         largeIcon = filterData.getLargeIcon();
96         if(largeIcon!=null && largeIcon.trim().equalsIgnoreCase("")){
97             largeIcon = null;
98         }
99         
100         urlPattern = filterData.getMappingURLPattern();
101         if(urlPattern!=null && urlPattern.trim().equalsIgnoreCase("")){
102             urlPattern = null;
103         }
104         
105         servletName = filterData.getMappingServletName();
106
107         //if urlPattern is set then servlet name must be null
108
if(urlPattern!=null){
109             servletName = null;
110         }else if(servletName!=null && servletName.trim().equalsIgnoreCase("")){
111             servletName = null;
112         }
113         
114         String JavaDoc[] paramNames = null;
115         String JavaDoc[] paramValues = null;
116         String JavaDoc[] paramDescriptions = null;
117             
118         //fill init params
119
if(initParamsArray!=null){
120             paramNames = new String JavaDoc[initParamsArray.size()];
121             paramValues = new String JavaDoc[initParamsArray.size()];
122             paramDescriptions = new String JavaDoc[initParamsArray.size()];
123             
124             for(int i=0; i<initParamsArray.size(); i++){
125                 paramNames[i] = (String JavaDoc) ((ArrayList JavaDoc)initParamsArray.get(i)).get(0);
126                 paramValues[i] = (String JavaDoc) ((ArrayList JavaDoc)initParamsArray.get(i)).get(1);
127                 paramDescriptions[i] = (String JavaDoc) ((ArrayList JavaDoc)initParamsArray.get(i)).get(2);
128             }
129         }
130         
131         webXML.addFilter(name, className, smallIcon, largeIcon, displayName, description,paramNames, paramValues, paramDescriptions, urlPattern, servletName);
132         refreshFilters();
133     }
134
135     public FilterData createFilterData(String JavaDoc filterName){
136
137         FilterData filterData = new FilterData(filterName);
138         HashMap JavaDoc params = webXML.getFilterParameters(filterName);
139         if(params !=null){
140             if(params.containsKey("description")){
141                filterData.setDescription((String JavaDoc)params.get("description"));
142             }
143             if(params.containsKey("display-name")){
144                filterData.setDisplayName((String JavaDoc)params.get("display-name"));
145             }
146             if(params.containsKey("filter-class")){
147                filterData.setFilterClass((String JavaDoc)params.get("filter-class"));
148             }
149             if(params.containsKey("large-icon")){
150                filterData.setLargeIcon((String JavaDoc)params.get("large-icon"));
151             }
152             if(params.containsKey("small-icon")){
153                filterData.setSmallIcon((String JavaDoc)params.get("small-icon"));
154             }
155             if(params.containsKey("url-pattern")){
156                filterData.setMappingURLPattern((String JavaDoc)params.get("url-pattern"));
157             }
158     
159             if(params.containsKey("servlet-name")){
160                filterData.setMappingServletName((String JavaDoc)params.get("servlet-name"));
161             }
162             //init-params
163
if(params.containsKey("init-param")){
164                 filterData.setInitParams((ArrayList JavaDoc)params.get("init-param"));
165             }
166         }
167
168         return filterData;
169     }
170     public void removeFilter(String JavaDoc filterName){
171         webXML.removeFilter(filterName);
172         refreshFilters();
173     }
174     public void save(){
175         webXML.saveDocument();
176     }
177 }
Popular Tags