KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > ServletData


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.wizards;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.openide.util.NbBundle;
27
28 import org.netbeans.modules.j2ee.dd.api.common.InitParam;
29 import org.netbeans.modules.j2ee.dd.api.web.Filter;
30 import org.netbeans.modules.j2ee.dd.api.web.FilterMapping;
31 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
32 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping;
33
34 // PENDING - it would be better to have a FilterData which extends
35
// ServletData, and keep the filter specific code in that class.
36

37 /**
38  * Deployment data validator object for servlets.
39  * @author ana.von.klopp@sun.com
40  */

41 class ServletData extends DeployData {
42
43     private String JavaDoc errorMessage = null;
44     private String JavaDoc name = null;
45     // These are URL mappings - they're used by both Servlets and Filters
46
private String JavaDoc[] urlMappings = null;
47     // These are mappings to servlet names - used by Filters only
48
private ArrayList JavaDoc filterMappings = null;
49     private String JavaDoc[][] initparams = null;
50     private boolean paramOK = true;
51     private FileType fileType = null;
52     private static final boolean debug = false;
53     private boolean addToDD=true;
54
55     ServletData(FileType fileType) {
56     this.fileType = fileType;
57     }
58
59     String JavaDoc getName() {
60     if(name == null) return "";
61     return name;
62     }
63
64     void setName(String JavaDoc name) {
65     if(name != this.name) {
66         if(fileType == FileType.FILTER)
67         updateFilterMappings(getName(), name);
68         this.name = name;
69     }
70     }
71
72     String JavaDoc[] getServletNames() {
73     if(webApp == null) return new String JavaDoc[0];
74     Servlet[] ss = webApp.getServlet();
75     String JavaDoc[] names = new String JavaDoc[ss.length];
76     for (int i=0; i<ss.length; i++) {
77         try {
78         names[i] = ss[i].getServletName();
79         }
80         catch(Exception JavaDoc e) {
81         names[i] = "";
82         }
83     }
84     return names;
85     }
86
87     java.util.List JavaDoc getUrlPatterns () {
88     if(webApp == null) {
89         if(debug) log("\tNo web app, return null"); //NOI18N
90
return new ArrayList JavaDoc();
91     }
92         ServletMapping[] maps = webApp.getServletMapping();
93         java.util.List JavaDoc l = new ArrayList JavaDoc();
94         for (int i=0;i<maps.length;i++) {
95             l.add(maps[i].getUrlPattern());
96         }
97         return l;
98     }
99     
100     ArrayList JavaDoc getFilterMappings() {
101
102     if(debug) log("::getFilterMappings()"); //NOI18N
103
if(webApp == null) {
104         if(debug) log("\tNo web app, return null"); //NOI18N
105
return new ArrayList JavaDoc();
106     }
107     if(filterMappings != null) {
108         if(debug) log("\tFilter mappings already exist"); //NOI18N
109
return filterMappings;
110     }
111
112     if(debug) log("\tCreating the filter mapping list"); //NOI18N
113
FilterMapping[] fm = webApp.getFilterMapping();
114     if(debug) {
115         log("\tOrder of mappings according to DD APIs"); //NOI18N
116
for(int i=0; i<fm.length; ++i)
117         log("\tServlet name: " + fm[i].getFilterName()); //NOI18N
118
}
119
120     filterMappings = new ArrayList JavaDoc();
121     filterMappings.add(new FilterMappingData(getName()));
122
123     String JavaDoc string;
124     String JavaDoc[] d = null;
125     FilterMappingData fmd;
126     FilterMappingData.Dispatcher[] dispatchList;
127
128     for (int i=0; i<fm.length; i++) {
129         fmd = new FilterMappingData();
130         fmd.setName(fm[i].getFilterName());
131         
132         string = fm[i].getUrlPattern();
133         if(string == null || string.length() == 0) {
134         fmd.setType(FilterMappingData.Type.SERVLET);
135         fmd.setPattern(fm[i].getServletName());
136         }
137         else {
138         fmd.setType(FilterMappingData.Type.URL);
139         fmd.setPattern(string);
140         }
141         
142         try {
143         if(fm[i].sizeDispatcher() == 0) {
144             filterMappings.add(fmd);
145             continue;
146         }
147         }
148         catch(Exception JavaDoc ex) {
149         // Not supported
150
filterMappings.add(fmd);
151         continue;
152         }
153
154         try {
155         d = fm[i].getDispatcher();
156         }
157         catch(Exception JavaDoc ex) {
158         if(debug) {
159             log(ex.toString());
160             ex.printStackTrace();
161         }
162         // PENDING ...
163
// Servlet 2.3
164
}
165         if(d == null)
166         
167         dispatchList = new FilterMappingData.Dispatcher[0];
168         else {
169         dispatchList = new FilterMappingData.Dispatcher[d.length];
170         for(int j=0; j<d.length; ++j) {
171             dispatchList[j] = FilterMappingData.Dispatcher.findDispatcher(d[j]);
172             if(debug) log("\tDispatch: " + dispatchList[j]);//NOI18N
173
}
174         }
175         fmd.setDispatcher(dispatchList);
176         filterMappings.add(fmd);
177     }
178     return filterMappings;
179     }
180
181     void setFilterMappings(ArrayList JavaDoc fmds) {
182     if(debug) log("::setFilterMappings()");
183     filterMappings = fmds;
184     }
185     
186     void updateFilterMappings(String JavaDoc oldName, String JavaDoc newName) {
187     if(debug)
188         log("::updateFilterMappings("+oldName+", " + newName + "),"); //NOI18N
189
Iterator JavaDoc i = getFilterMappings().iterator();
190     // No web app
191
if(i == null) return;
192     FilterMappingData fmd;
193     while(i.hasNext()) {
194         fmd = (FilterMappingData)i.next();
195         if(fmd.getName().equals(oldName)) fmd.setName(newName);
196     }
197     }
198
199     boolean isNameUnique() {
200     if(webApp == null) return true;
201     Servlet[] ss = webApp.getServlet();
202     for (int i=0; i<ss.length; i++) {
203         if(name.equals(ss[i].getServletName())) {
204         return false;
205         }
206     }
207
208     Filter[] ff = webApp.getFilter();
209     for (int i=0; i<ff.length; i++) {
210         if(name.equals(ff[i].getFilterName())) {
211         return false;
212         }
213     }
214     return true;
215     }
216
217     String JavaDoc[] getUrlMappings() {
218     if(urlMappings == null) return new String JavaDoc[0];
219     return urlMappings;
220     }
221     
222     String JavaDoc createDDServletName(String JavaDoc className) {
223         if (webApp==null) return null;
224         String JavaDoc result = className;
225         Servlet servlet = (Servlet) webApp.findBeanByName("Servlet","ServletName",result); //NOI18N
226
while (servlet!=null) {
227             result = findNextId(result);
228             servlet = (Servlet) webApp.findBeanByName("Servlet","ServletName",result); //NOI18N
229
}
230         setName(result);
231         return result;
232     }
233     
234     void createDDServletMapping(String JavaDoc servletName) {
235         if (webApp==null) return;
236         String JavaDoc result = "/"+servletName;
237         ServletMapping mapping = (ServletMapping) webApp.findBeanByName("ServletMapping","UrlPattern",result); //NOI18N
238
while (mapping!=null) {
239             result = findNextId(result);
240             mapping = (ServletMapping) webApp.findBeanByName("ServletMapping","UrlPattern",result); //NOI18N
241
}
242         urlMappings = new String JavaDoc[]{result};
243     }
244     
245     /** Compute the next proper value for the id
246      */

247     private String JavaDoc findNextId(String JavaDoc id) {
248         char ch = id.charAt(id.length()-1);
249         String JavaDoc result=null;
250         if (Character.isDigit(ch)) {
251             String JavaDoc lastDigit = id.substring(id.length()-1);
252             int num = new Integer JavaDoc(lastDigit).intValue()+1;
253             result=id.substring(0,id.length()-1)+new Integer JavaDoc(num).toString();
254         } else {
255             return result=id+"_1"; //NOI18N
256
}
257         return result;
258     }
259     
260
261     String JavaDoc getUrlMappingsAsString() {
262
263     if(urlMappings == null || urlMappings.length == 0 ) return ""; //NOI18N
264
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
265     int index = 0;
266     while(index<urlMappings.length-1) {
267         buf.append(urlMappings[index]);
268         buf.append(", "); //NOI18N
269
index++;
270     }
271
272     buf.append(urlMappings[index]);
273     return buf.toString();
274     }
275
276     void parseUrlMappingString(String JavaDoc raw) {
277
278     urlMappings = null;
279     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(raw, ",");
280     ArrayList JavaDoc list = new ArrayList JavaDoc();
281     String JavaDoc mapping;
282     String JavaDoc[] names = getServletNames();
283
284     while(st.hasMoreTokens()) {
285         mapping = st.nextToken().trim();
286         if(mapping.length() == 0)
287         continue;
288         list.add(mapping);
289     }
290
291     urlMappings = new String JavaDoc[list.size()];
292         list.toArray(urlMappings);
293     }
294
295     String JavaDoc[][] getInitParams() {
296     if(initparams == null) return new String JavaDoc[0][2];
297     return initparams;
298     }
299
300     void setInitParams(String JavaDoc[][] initparams, boolean paramOK) {
301     this.initparams = initparams;
302     this.paramOK = paramOK;
303     }
304
305     int getNumInitParams() {
306     if(initparams == null) return 0;
307     return initparams.length;
308     }
309
310     boolean isParamOK() {
311     return paramOK;
312     }
313
314     void setParamOK(boolean paramOK) {
315     this.paramOK = paramOK;
316     }
317
318
319     boolean isValid() {
320     if(debug) log("::isValid()"); //NOI18N
321
errorMessage = new String JavaDoc();
322     if(webApp == null) return true;
323         if (!isAddToDD()) return true;
324
325     if(getName().length() == 0) {
326         errorMessage = NbBundle.getMessage(ServletData.class,
327                            "MSG_no_name");
328         return false;
329     }
330
331     if(!isNameUnique()) {
332         errorMessage = NbBundle.getMessage(ServletData.class,
333                            "MSG_name_unique");
334         return false;
335     }
336
337     if(debug) log("\tname is fine"); //NOI18N
338

339     if(fileType == FileType.SERVLET) {
340         if(!checkMappingsForServlet()) return false;
341             if(!checkServletDuplicitMappings()) return false;
342     }
343     else if(fileType == FileType.FILTER) {
344         if(!checkMappingsForFilter()) return false;
345     }
346     
347     if(!isParamOK()) {
348         errorMessage = NbBundle.getMessage(ServletData.class,
349                            "MSG_invalid_param");
350         return false;
351     }
352
353     if(debug) log("\tparams are fine"); //NOI18N
354
return true;
355     }
356
357
358     boolean checkMappingsForServlet() {
359
360     errorMessage = new String JavaDoc();
361         String JavaDoc[] mappings = getUrlMappings();
362     if(mappings == null || mappings.length == 0) {
363
364         if(debug) log("\tNo URL mappings"); //NOI18N
365
errorMessage = NbBundle.getMessage(ServletData.class,
366                            "MSG_no_mapping");
367         return false;
368     }
369         for (int i=0;i<mappings.length;i++) {
370             String JavaDoc errMessage = checkServletMappig(mappings[i]);
371             if (errMessage!=null) {
372                 errorMessage = errMessage;
373                 return false;
374             }
375         }
376     if(debug) log("\tmappings are fine"); //NOI18N
377
return true;
378     }
379     
380     boolean checkServletDuplicitMappings() {
381     errorMessage = new String JavaDoc();
382         String JavaDoc[] newMappings = getUrlMappings();
383         java.util.List JavaDoc urlPatterns = getUrlPatterns();
384         for (int i=0;i<newMappings.length;i++) {
385             Iterator JavaDoc it = urlPatterns.iterator();
386             while(it.hasNext()) {
387                 String JavaDoc urlPattern = (String JavaDoc)it.next();
388                 if (newMappings[i].equals(urlPattern)) {
389                     if(debug) log("\tDuplicit URL mappings"); //NOI18N
390
errorMessage = NbBundle.getMessage(ServletData.class,
391                                                        "MSG_url_pattern_unique");
392                     return false;
393                 }
394             }
395             // new Url Patterns need to be compare to each other
396
urlPatterns.add(newMappings[i]);
397         }
398     if(debug) log("\tmappings- duplicity - is fine"); //NOI18N
399
return true;
400     }
401
402     boolean checkMappingsForFilter() {
403     errorMessage = new String JavaDoc();
404     if(filterMappings == null || filterMappings.size() == 0) {
405         if(debug) log("\tNo mappings"); //NOI18N
406
errorMessage = NbBundle.getMessage(ServletData.class,
407                         "MSG_no_mapping");
408         return false;
409     }
410     Iterator JavaDoc i = getFilterMappings().iterator();
411     boolean found = false;
412     FilterMappingData fmd;
413     while(i.hasNext()) {
414         fmd = (FilterMappingData)(i.next());
415         if(fmd.getName().equals(getName())) {
416         found = true;
417         break;
418         }
419     }
420     if(!found) {
421         errorMessage = NbBundle.getMessage(ServletData.class,
422                            "MSG_no_mapping");
423         return false;
424     }
425     return true;
426     }
427
428     void createDDEntries() {
429     if(debug) log("::createDDEntries()");
430     if(webApp == null) return;
431     if(debug) log("\t...adding");
432
433     if(fileType == FileType.SERVLET) {
434         boolean added = addServlet();
435         if(added) {
436         addUrlMappings();
437         if(debug) log("\t...writing changes");
438         try {
439             writeChanges();
440         } catch (java.io.IOException JavaDoc ex) {
441                     if (debug) ex.printStackTrace();
442         }
443         }
444     }
445     else if(fileType == FileType.FILTER) {
446         boolean added = addFilter();
447         if(added) {
448         addFilterMappings();
449         if(debug) log("\t...writing changes");
450         try {
451             writeChanges();
452                 } catch (java.io.IOException JavaDoc ex) {
453                     if (debug) ex.printStackTrace();
454         }
455         }
456     }
457     }
458
459
460     private boolean addServlet() {
461
462     if(debug) log("::addServlet()"); //NOI18N
463
if(webApp == null) return false;
464     Servlet s;
465     try {
466         s = (Servlet)webApp.createBean("Servlet"); //NOI18N
467
if(debug) log("\tCreated servlet"); //NOI18N
468
}
469     
470     catch(ClassNotFoundException JavaDoc cnfe) {
471         if(debug) cnfe.printStackTrace();
472         return false;
473     }
474
475     s.setServletName(name);
476     s.setServletClass(className);
477
478     int numInitParams = getInitParams().length;
479
480     if(debug)
481         log("\tnum params " + String.valueOf(numInitParams));//NOI18N
482

483
484     for(int i=0; i<numInitParams; ++i) {
485         InitParam param;
486         try {
487         param = (InitParam)s.createBean("InitParam"); //NOI18N
488
if(debug) log("\tCreated initparam"); //NOI18N
489
}
490
491         catch(ClassNotFoundException JavaDoc cnfe) {
492         if(debug) cnfe.printStackTrace();
493         continue;
494         }
495
496         param.setParamName(initparams[i][0]);
497         param.setParamValue(initparams[i][1]);
498         s.addInitParam(param);
499     }
500
501     if(debug)
502         log("\tnum params " + String.valueOf(s.sizeInitParam())); //NOI18N
503

504     webApp.addServlet(s);
505     return true;
506     }
507
508
509     private boolean addFilter() {
510
511     if(debug) log("::addFilter()"); //NOI18N
512
if(webApp == null) return false;
513     Filter f;
514     try {
515         f = (Filter)webApp.createBean("Filter"); //NOI18N
516
if(debug) log("\tCreated filter"); //NOI18N
517
}
518
519     catch(ClassNotFoundException JavaDoc cnfe) {
520         if(debug) cnfe.printStackTrace();
521         return false;
522     }
523
524     f.setFilterName(name);
525     f.setFilterClass(className);
526
527     int numInitParams = getInitParams().length;
528
529     if(debug)
530         log("\tnum params " + String.valueOf(numInitParams));//NOI18N
531

532
533     for(int i=0; i<numInitParams; ++i) {
534         InitParam param;
535         try {
536         param = (InitParam)f.createBean("InitParam"); //NOI18N
537
if(debug) log("\tCreated initparam"); //NOI18N
538
}
539
540         catch(ClassNotFoundException JavaDoc cnfe) {
541         if(debug) cnfe.printStackTrace();
542         continue;
543         }
544
545         param.setParamName(initparams[i][0]);
546         param.setParamValue(initparams[i][1]);
547         f.addInitParam(param);
548     }
549
550     if(debug)
551         log("\tnum params " + String.valueOf(f.sizeInitParam())); //NOI18N
552

553     webApp.addFilter(f);
554     return true;
555     }
556
557
558     private void addUrlMappings() {
559
560     if(webApp == null) return;
561     int numMappings = getUrlMappings().length;
562     for(int i=0; i<numMappings; ++i) {
563         ServletMapping m;
564         try {
565         m = (ServletMapping)webApp.createBean("ServletMapping"); //NOI18N
566
}
567         catch(ClassNotFoundException JavaDoc cnfe) {
568         return;
569         }
570         m.setServletName(name);
571         m.setUrlPattern(urlMappings[i]);
572         webApp.addServletMapping(m);
573     }
574     }
575
576     private void addFilterMappings() {
577
578     if(debug) log("::addFilterMappings()");
579     if(webApp == null) return;
580
581     // filterMappings cannot be null, or of size zero
582
int numFilterMappings = filterMappings.size();
583     Iterator JavaDoc iterator = filterMappings.iterator();
584
585     FilterMapping[] fm = new FilterMapping[numFilterMappings];
586
587     FilterMappingData fmd;
588     for(int i=0; i<numFilterMappings; ++i) {
589
590         fmd = (FilterMappingData)(iterator.next());
591
592         try {
593         fm[i] = (FilterMapping)webApp.createBean("FilterMapping"); //NOI18N
594
if(debug) log("\tCreated filter mapping"); //NOI18N
595
}
596         catch(ClassNotFoundException JavaDoc cnfe) {
597         return;
598         }
599
600         fm[i].setFilterName(fmd.getName());
601         if(debug) log("\tFilter name: " + fmd.getName()); //NOI18N
602
if(fmd.getType() == FilterMappingData.Type.URL) {
603         fm[i].setUrlPattern(fmd.getPattern());
604         if(debug) log("URL pattern " + fmd.getPattern()); //NOI18N
605
}
606         else {
607         fm[i].setServletName(fmd.getPattern());
608         if(debug) log("Servlet pattern " + fmd.getPattern()); //NOI18N
609
}
610
611         int length = fmd.getDispatcher().length;
612         if(length == 0) {
613         if(debug) log("\tNo dispatcher, continue"); //NOI18N
614
continue;
615         }
616
617         String JavaDoc[] s = new String JavaDoc[length];
618         FilterMappingData.Dispatcher[] d = fmd.getDispatcher();
619         for(int j=0; j<length; ++j) {
620         if(debug) log("\tDispatcher: " + d[j].toString()); //NOI18N
621
s[j] = d[j].toString();
622         }
623         try {
624         fm[i].setDispatcher(s);
625         }
626         catch(Exception JavaDoc e) {
627         if(debug) log("\tFailed to set dispatcher"); //NOI18N
628
// do nothing, wrong version
629
}
630         if(debug) log(fm[i].toString());
631     }
632     webApp.setFilterMapping(fm);
633     }
634     
635     
636     void setAddToDD(boolean addToDD){
637         this.addToDD=addToDD;
638     }
639     
640     boolean isAddToDD() {
641         return addToDD;
642     }
643
644     String JavaDoc getErrorMessage() {
645     return errorMessage;
646     }
647
648     void log(String JavaDoc s) {
649     System.out.println("ServletData" + s);
650     }
651     
652     private String JavaDoc checkServletMappig(String JavaDoc uri) {
653         if (!uri.matches("[\\*/].*")) { //NOI18N
654
return NbBundle.getMessage(ServletData.class,"MSG_WrongUriStart");
655         } else if (uri.length()>1 && uri.endsWith("/")) {
656             return NbBundle.getMessage(ServletData.class,"MSG_WrongUriEnd");
657         } else if (uri.matches(".*\\*.*\\*.*")) { //NOI18N
658
return NbBundle.getMessage(ServletData.class,"MSG_TwoAsterisks");
659         } else if (uri.matches("..*\\*..*")) { //NOI18N
660
return NbBundle.getMessage(ServletData.class,"MSG_AsteriskInTheMiddle");
661         }
662         return null;
663     }
664 }
665
666
Popular Tags