KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > tiles > InsertTag


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

18
19 package org.apache.struts.taglib.tiles;
20
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.struts.Globals;
35 import org.apache.struts.taglib.tiles.util.TagUtils;
36 import org.apache.struts.tiles.AttributeDefinition;
37 import org.apache.struts.tiles.ComponentContext;
38 import org.apache.struts.tiles.ComponentDefinition;
39 import org.apache.struts.tiles.Controller;
40 import org.apache.struts.tiles.DefinitionAttribute;
41 import org.apache.struts.tiles.DefinitionNameAttribute;
42 import org.apache.struts.tiles.DefinitionsFactoryException;
43 import org.apache.struts.tiles.DirectStringAttribute;
44 import org.apache.struts.tiles.FactoryNotFoundException;
45 import org.apache.struts.tiles.NoSuchDefinitionException;
46 import org.apache.struts.tiles.TilesUtil;
47
48 /**
49  * This is the tag handler for <tiles:insert>, which includes
50  * a template. The tag's body content consists of <tiles:put>
51  * tags, which are accessed by <tiles:get> in the template.
52  *
53  * @version $Rev: 165160 $ $Date: 2005-04-28 17:29:58 +0100 (Thu, 28 Apr 2005) $
54  */

55 public class InsertTag
56     extends DefinitionTagSupport
57     implements PutTagParent, ComponentConstants, PutListTagParent {
58
59     /**
60      * The role delimiter.
61      * @deprecated This will be removed in a release after Struts 1.2.
62      */

63     public static final String JavaDoc ROLE_DELIMITER = ",";
64
65     /**
66      * Commons Logging instance.
67      */

68     protected static Log log = LogFactory.getLog(InsertTag.class);
69
70     /* JSP Tag attributes */
71
72     /**
73      * Flush attribute value.
74      */

75     protected boolean flush = true;
76
77     /**
78      * Name to insert.
79      */

80     protected String JavaDoc name = null;
81
82     /**
83      * Name of attribute from which to read page name to include.
84      */

85     protected String JavaDoc attribute = null;
86
87     /**
88      * Name of bean used as entity to include.
89      */

90     protected String JavaDoc beanName = null;
91
92     /**
93      * Name of bean property, if any.
94      */

95     protected String JavaDoc beanProperty = null;
96
97     /**
98      * Scope of bean, if any.
99      */

100     protected String JavaDoc beanScope = null;
101
102     /**
103      * Are errors ignored. This is the property for attribute 'ignore'.
104      * Default value is false, which throw an exception.
105      * Only 'attribute not found' errors are ignored.
106      */

107     protected boolean isErrorIgnored = false;
108
109     /**
110      * Name of component instance to include.
111      */

112     protected String JavaDoc definitionName = null;
113
114     /* Internal properties */
115     /**
116      * Does the end tag need to be processed.
117      * Default value is true. Boolean set in case of ignored errors.
118      */

119     protected boolean processEndTag = true;
120
121     /**
122      * Current component context.
123      */

124     protected ComponentContext cachedCurrentContext;
125
126     /**
127      * Final handler of tag methods.
128      */

129     protected TagHandler tagHandler = null;
130
131     /**
132      * Trick to allows inner classes to access pageContext.
133      */

134     protected PageContext JavaDoc pageContext = null;
135
136     /**
137      * Reset member values for reuse. This method calls super.release(),
138      * which invokes TagSupport.release(), which typically does nothing.
139      */

140     public void release() {
141
142         super.release();
143         attribute = null;
144         beanName = null;
145         beanProperty = null;
146         beanScope = null;
147
148         definitionName = null;
149         flush = true;
150         name = null;
151         page = null;
152         role = null;
153         isErrorIgnored = false;
154
155         releaseInternal();
156     }
157
158     /**
159      * Reset internal member values for reuse.
160      */

161     protected void releaseInternal() {
162         cachedCurrentContext = null;
163         processEndTag = true;
164         // pageContext = null; // orion doesn't set it between two tags
165
tagHandler = null;
166     }
167
168     /**
169      * Set the current page context.
170      * Called by the page implementation prior to doStartTag().
171      * <p>
172      * Needed to allow inner classes to access pageContext.
173      */

174     public void setPageContext(PageContext JavaDoc pc) {
175         this.pageContext = pc;
176         super.setPageContext(pc);
177     }
178
179     /**
180      * Get the pageContext property.
181      */

182     public PageContext JavaDoc getPageContext() {
183         return pageContext;
184     }
185
186     /**
187      * Set name.
188      */

189     public void setName(String JavaDoc value) {
190         this.name = value;
191     }
192
193     /**
194      * Get name.
195      */

196     public String JavaDoc getName() {
197         return name;
198     }
199
200     /**
201      * Set component.
202      */

203     public void setComponent(String JavaDoc name) {
204         this.page = name;
205     }
206
207     /**
208      * Set definition.
209      */

210     public void setDefinition(String JavaDoc name) {
211         this.definitionName = name;
212     }
213
214     /**
215      * Get definition name.
216      */

217     public String JavaDoc getDefinitionName() {
218         return definitionName;
219     }
220
221     /**
222      * Set attribute.
223      */

224     public void setAttribute(String JavaDoc value) {
225         this.attribute = value;
226     }
227
228     /**
229      * Set bean name.
230      */

231     public void setBeanName(String JavaDoc value) {
232         this.beanName = value;
233     }
234
235     /**
236      * Get bean name.
237      */

238     public String JavaDoc getBeanName() {
239         return beanName;
240     }
241
242     /**
243      * Set bean property.
244      */

245     public void setBeanProperty(String JavaDoc value) {
246         this.beanProperty = value;
247     }
248
249     /**
250      * Get bean property.
251      */

252     public String JavaDoc getBeanProperty() {
253         return beanProperty;
254     }
255
256     /**
257      * Set bean scope.
258      */

259     public void setBeanScope(String JavaDoc value) {
260         this.beanScope = value;
261     }
262
263     /**
264      * Get bean scope.
265      */

266     public String JavaDoc getBeanScope() {
267         return beanScope;
268     }
269
270     /**
271      * Set flush.
272      */

273     public void setFlush(boolean flush) {
274         this.flush = flush;
275     }
276
277     /**
278      * Get flush.
279      */

280     public boolean getFlush() {
281         return flush;
282     }
283
284     /**
285      * Set flush.
286      * Method added for compatibility with JSP1.1
287      */

288     public void setFlush(String JavaDoc flush) {
289         this.flush = (Boolean.valueOf(flush).booleanValue());
290     }
291
292     /**
293      * Set ignore.
294      */

295     public void setIgnore(boolean ignore) {
296         this.isErrorIgnored = ignore;
297     }
298
299     /**
300      * Get ignore.
301      */

302     public boolean getIgnore() {
303         return isErrorIgnored;
304     }
305
306     /////////////////////////////////////////////////////////////////////////
307

308     /**
309      * Add a body attribute.
310      * Erase any attribute with same name previously set.
311      */

312     public void putAttribute(String JavaDoc name, Object JavaDoc value) {
313         tagHandler.putAttribute(name, value);
314     }
315
316     /**
317      * Process nested &lg;put&gt; tag.
318      * Method calls by nested &lg;put&gt; tags.
319      * Nested list is added to current list.
320      * If role is defined, it is checked immediately.
321      */

322     public void processNestedTag(PutTag nestedTag) throws JspException JavaDoc {
323         // Check role
324
HttpServletRequest JavaDoc request =
325             (HttpServletRequest JavaDoc) pageContext.getRequest();
326         String JavaDoc role = nestedTag.getRole();
327         if (role != null && !request.isUserInRole(role)) {
328             // not allowed : skip attribute
329
return;
330         }
331
332         putAttribute(nestedTag.getName(), nestedTag.getRealValue());
333     }
334
335     /**
336      * Process nested &lg;putList&gt; tag.
337      * Method calls by nested &lg;putList&gt; tags.
338      * Nested list is added to sub-component attributes
339      * If role is defined, it is checked immediately.
340      */

341     public void processNestedTag(PutListTag nestedTag) throws JspException JavaDoc {
342         // Check role
343
HttpServletRequest JavaDoc request =
344             (HttpServletRequest JavaDoc) pageContext.getRequest();
345         String JavaDoc role = nestedTag.getRole();
346         if (role != null && !request.isUserInRole(role)) {
347             // not allowed : skip attribute
348
return;
349         }
350
351         // Check if a name is defined
352
if (nestedTag.getName() == null) {
353             throw new JspException JavaDoc("Error - PutList : attribute name is not defined. It is mandatory as the list is added as attribute of 'insert'.");
354         }
355
356         // now add attribute to enclosing parent (i.e. : this object).
357
putAttribute(nestedTag.getName(), nestedTag.getList());
358     }
359
360     /**
361      * Method calls by nested &lg;putList&gt; tags.
362      * A new list is added to current insert object.
363      */

364     public void putAttribute(PutListTag nestedTag) throws JspException JavaDoc {
365         // Check role
366
HttpServletRequest JavaDoc request =
367             (HttpServletRequest JavaDoc) pageContext.getRequest();
368         String JavaDoc role = nestedTag.getRole();
369         if (role != null && !request.isUserInRole(role)) {
370             // not allowed : skip attribute
371
return;
372         }
373
374         putAttribute(nestedTag.getName(), nestedTag.getList());
375     }
376
377     /**
378      * Get current component context.
379      */

380     private ComponentContext getCurrentContext() {
381         if (cachedCurrentContext == null) {
382             cachedCurrentContext =
383                 (ComponentContext) pageContext.getAttribute(
384                     ComponentConstants.COMPONENT_CONTEXT,
385                     PageContext.REQUEST_SCOPE);
386         }
387
388         return cachedCurrentContext;
389     }
390
391     /**
392      * Get instantiated Controller.
393      * Return controller denoted by controllerType, or <code>null</code> if controllerType
394      * is null.
395      * @throws JspException If controller can't be created.
396      */

397     private Controller getController() throws JspException JavaDoc {
398         if (controllerType == null) {
399             return null;
400         }
401
402         try {
403             return ComponentDefinition.createController(
404                 controllerName,
405                 controllerType);
406
407         } catch (InstantiationException JavaDoc ex) {
408             throw new JspException JavaDoc(ex.getMessage());
409         }
410     }
411
412     /**
413      * Process the start tag by checking tag's attributes and creating appropriate handler.
414      * Possible handlers :
415      * <ul>
416      * <li> URL
417      * <li> definition
418      * <li> direct String
419      * </ul>
420      * Handlers also contain sub-component context.
421      */

422     public int doStartTag() throws JspException JavaDoc {
423
424             // Additional fix for Bug 20034 (2005-04-28)
425
cachedCurrentContext = null;
426
427         // Check role immediatly to avoid useless stuff.
428
// In case of insertion of a "definition", definition's role still checked later.
429
// This lead to a double check of "role" ;-(
430
HttpServletRequest JavaDoc request =
431             (HttpServletRequest JavaDoc) pageContext.getRequest();
432         if (role != null && !request.isUserInRole(role)) {
433             processEndTag = false;
434             return SKIP_BODY;
435         }
436
437         try {
438             tagHandler = createTagHandler();
439
440         } catch (JspException JavaDoc e) {
441             if (isErrorIgnored) {
442                 processEndTag = false;
443                 return SKIP_BODY;
444             } else {
445                 throw e;
446             }
447         }
448
449         return tagHandler.doStartTag();
450     }
451
452     /**
453      * Process the end tag by including the template.
454      * Simply call the handler doEndTag
455      */

456     public int doEndTag() throws JspException JavaDoc {
457         if (!processEndTag) {
458             releaseInternal();
459             return EVAL_PAGE;
460         }
461
462         int res = tagHandler.doEndTag();
463         // Reset properties used by object, in order to be able to reuse object.
464
releaseInternal();
465         return res;
466     }
467
468     /**
469      * Process tag attribute and create corresponding tag handler.
470      */

471     public TagHandler createTagHandler() throws JspException JavaDoc {
472         // Check each tag attribute.
473
// page Url attribute must be the last checked because it can appears concurrently
474
// with others attributes.
475
if (definitionName != null) {
476             return processDefinitionName(definitionName);
477         } else if (attribute != null) {
478             return processAttribute(attribute);
479         } else if (beanName != null) {
480             return processBean(beanName, beanProperty, beanScope);
481         } else if (name != null) {
482             return processName(name);
483         } else if (page != null) {
484             return processUrl(page);
485         } else {
486             throw new JspException JavaDoc("Error - Tag Insert : At least one of the following attribute must be defined : template|page|attribute|definition|name|beanName. Check tag syntax");
487         }
488     }
489
490     /**
491      * Process an object retrieved as a bean or attribute.
492      * Object can be a typed attribute, a String, or anything else.
493      * If typed attribute, use associated type.
494      * Otherwise, apply toString() on object, and use returned string as a name.
495      * @throws JspException - Throws by underlying nested call to
496      * processDefinitionName()
497      */

498     public TagHandler processObjectValue(Object JavaDoc value) throws JspException JavaDoc {
499         // First, check if value is one of the Typed Attribute
500
if (value instanceof AttributeDefinition) {
501             // We have a type => return appropriate IncludeType
502
return processTypedAttribute((AttributeDefinition) value);
503
504         } else if (value instanceof ComponentDefinition) {
505             return processDefinition((ComponentDefinition) value);
506         }
507
508         // Value must denote a valid String
509
return processAsDefinitionOrURL(value.toString());
510     }
511
512     /**
513      * Process name.
514      * Search in following order :
515      * <ul>
516      * <li>Component context - if found, process it as value.</li>
517      * <li>definitions factory</li>
518      * <li>URL</li>
519      * <li></li>
520      * </ul>
521      *
522      * @return appropriate tag handler.
523      * @throws JspException - Throws by underlying nested call to
524      * processDefinitionName()
525      */

526     public TagHandler processName(String JavaDoc name) throws JspException JavaDoc {
527         Object JavaDoc attrValue = getCurrentContext().getAttribute(name);
528
529         if (attrValue != null) {
530             return processObjectValue(attrValue);
531         }
532
533         return processAsDefinitionOrURL(name);
534     }
535
536     /**
537      * Process the url.
538      * @throws JspException If failed to create controller
539      */

540     public TagHandler processUrl(String JavaDoc url) throws JspException JavaDoc {
541         return new InsertHandler(url, role, getController());
542     }
543
544     /**
545      * Process tag attribute "definition".
546      * First, search definition in the factory, then create handler from this definition.
547      * @param name Name of the definition.
548      * @return Appropriate TagHandler.
549      * @throws JspException- NoSuchDefinitionException No Definition found for name.
550      * @throws JspException- FactoryNotFoundException Can't find Definitions factory.
551      * @throws JspException- DefinedComponentFactoryException General error in factory.
552      * @throws JspException InstantiationException Can't create requested controller
553      */

554     protected TagHandler processDefinitionName(String JavaDoc name)
555         throws JspException JavaDoc {
556
557         try {
558             ComponentDefinition definition =
559                 TilesUtil.getDefinition(
560                     name,
561                     (HttpServletRequest JavaDoc) pageContext.getRequest(),
562                     pageContext.getServletContext());
563
564             if (definition == null) { // is it possible ?
565
throw new NoSuchDefinitionException();
566             }
567
568             return processDefinition(definition);
569
570         } catch (NoSuchDefinitionException ex) {
571             throw new JspException JavaDoc(
572                 "Error - Tag Insert : Can't get definition '"
573                     + definitionName
574                     + "'. Check if this name exist in definitions factory.");
575
576         } catch (FactoryNotFoundException ex) {
577             throw new JspException JavaDoc(ex.getMessage());
578
579         } catch (DefinitionsFactoryException ex) {
580             if (log.isDebugEnabled()) {
581                 ex.printStackTrace();
582             }
583
584             // Save exception to be able to show it later
585
pageContext.setAttribute(
586                 Globals.EXCEPTION_KEY,
587                 ex,
588                 PageContext.REQUEST_SCOPE);
589             throw new JspException JavaDoc(ex.getMessage());
590         }
591     }
592
593     /**
594      * End of Process tag attribute "definition".
595      * Overload definition with tag attributes "template" and "role".
596      * Then, create appropriate tag handler.
597      * @param definition Definition to process.
598      * @return Appropriate TagHandler.
599      * @throws JspException InstantiationException Can't create requested controller
600      */

601     protected TagHandler processDefinition(ComponentDefinition definition)
602         throws JspException JavaDoc {
603         // Declare local variable in order to not change Tag attribute values.
604
String JavaDoc role = this.role;
605         String JavaDoc page = this.page;
606         Controller controller = null;
607
608         try {
609             controller = definition.getOrCreateController();
610
611             // Overload definition with tag's template and role.
612
if (role == null) {
613                 role = definition.getRole();
614             }
615
616             if (page == null) {
617                 page = definition.getTemplate();
618             }
619
620             if (controllerName != null) {
621                 controller =
622                     ComponentDefinition.createController(
623                         controllerName,
624                         controllerType);
625             }
626
627             // Can check if page is set
628
return new InsertHandler(
629                 definition.getAttributes(),
630                 page,
631                 role,
632                 controller);
633
634         } catch (InstantiationException JavaDoc ex) {
635             throw new JspException JavaDoc(ex.getMessage());
636         }
637     }
638
639     /**
640      * Process a bean.
641      * Get bean value, eventually using property and scope. Found value is process by processObjectValue().
642      * @param beanName Name of the bean
643      * @param beanProperty Property in the bean, or null.
644      * @param beanScope bean scope, or null.
645      * @return Appropriate TagHandler.
646      * @throws JspException - NoSuchDefinitionException No value associated to bean.
647      * @throws JspException an error occur while reading bean, or no definition found.
648      * @throws JspException - Throws by underlying nested call to processDefinitionName()
649      */

650     protected TagHandler processBean(
651         String JavaDoc beanName,
652         String JavaDoc beanProperty,
653         String JavaDoc beanScope)
654         throws JspException JavaDoc {
655
656         Object JavaDoc beanValue =
657             TagUtils.getRealValueFromBean(
658                 beanName,
659                 beanProperty,
660                 beanScope,
661                 pageContext);
662
663         if (beanValue == null) {
664             throw new JspException JavaDoc(
665                 "Error - Tag Insert : No value defined for bean '"
666                     + beanName
667                     + "' with property '"
668                     + beanProperty
669                     + "' in scope '"
670                     + beanScope
671                     + "'.");
672         }
673
674         return processObjectValue(beanValue);
675     }
676
677     /**
678      * Process tag attribute "attribute".
679      * Get value from component attribute.
680      * Found value is process by processObjectValue().
681      * @param name Name of the attribute.
682      * @return Appropriate TagHandler.
683      * @throws JspException - NoSuchDefinitionException No Definition found for name.
684      * @throws JspException - Throws by underlying nested call to processDefinitionName()
685      */

686     public TagHandler processAttribute(String JavaDoc name) throws JspException JavaDoc {
687         Object JavaDoc attrValue = getCurrentContext().getAttribute(name);
688
689         if (attrValue == null) {
690             throw new JspException JavaDoc(
691                 "Error - Tag Insert : No value found for attribute '"
692                     + name
693                     + "'.");
694         }
695
696         return processObjectValue(attrValue);
697     }
698
699     /**
700      * Try to process name as a definition, or as an URL if not found.
701      * @param name Name to process.
702      * @return appropriate TagHandler
703      * @throws JspException InstantiationException Can't create requested controller
704      */

705     public TagHandler processAsDefinitionOrURL(String JavaDoc name)
706         throws JspException JavaDoc {
707         try {
708             ComponentDefinition definition =
709                 TilesUtil.getDefinition(
710                     name,
711                     pageContext.getRequest(),
712                     pageContext.getServletContext());
713
714             if (definition != null) {
715                 return processDefinition(definition);
716             }
717
718         } catch (DefinitionsFactoryException ex) {
719             // silently failed, because we can choose to not define a factory.
720
}
721
722         // no definition found, try as url
723
return processUrl(name);
724     }
725
726     /**
727      * Process typed attribute according to its type.
728      * @param value Typed attribute to process.
729      * @return appropriate TagHandler.
730      * @throws JspException - Throws by underlying nested call to processDefinitionName()
731      */

732     public TagHandler processTypedAttribute(AttributeDefinition value)
733         throws JspException JavaDoc {
734         if (value instanceof DirectStringAttribute) {
735             return new DirectStringHandler((String JavaDoc) value.getValue());
736
737         } else if (value instanceof DefinitionAttribute) {
738             return processDefinition((ComponentDefinition) value.getValue());
739
740         } else if (value instanceof DefinitionNameAttribute) {
741             return processDefinitionName((String JavaDoc) value.getValue());
742         }
743
744         return new InsertHandler(
745             (String JavaDoc) value.getValue(),
746             role,
747             getController());
748     }
749
750     /**
751      * Do an include of specified page.
752      * This method is used internally to do all includes from this class. It delegates
753      * the include call to the TilesUtil.doInclude().
754      * @param page The page that will be included
755      * @throws ServletException - Thrown by call to pageContext.include()
756      * @throws IOException - Thrown by call to pageContext.include()
757      */

758     protected void doInclude(String JavaDoc page)
759         throws ServletException JavaDoc, IOException JavaDoc {
760         TilesUtil.doInclude(page, pageContext);
761     }
762
763     /////////////////////////////////////////////////////////////////////////////
764

765     /**
766      * Inner Interface.
767      * Sub handler for tag.
768      */

769     protected interface TagHandler {
770         /**
771          * Create ComponentContext for type depicted by implementation class.
772          */

773         public int doStartTag() throws JspException JavaDoc;
774         /**
775          * Do include for type depicted by implementation class.
776          */

777         public int doEndTag() throws JspException JavaDoc;
778         /**
779          * Add a component parameter (attribute) to subContext.
780          */

781         public void putAttribute(String JavaDoc name, Object JavaDoc value);
782     } // end inner interface
783

784     /////////////////////////////////////////////////////////////////////////////
785

786     /**
787      * Real handler, after attribute resolution.
788      * Handle include sub-component.
789      */

790     protected class InsertHandler implements TagHandler {
791         protected String JavaDoc page;
792         protected ComponentContext currentContext;
793         protected ComponentContext subCompContext;
794         protected String JavaDoc role;
795         protected Controller controller;
796
797         /**
798          * Constructor.
799          * Create insert handler using Component definition.
800          */

801         public InsertHandler(
802             Map JavaDoc attributes,
803             String JavaDoc page,
804             String JavaDoc role,
805             Controller controller) {
806
807             this.page = page;
808             this.role = role;
809             this.controller = controller;
810             subCompContext = new ComponentContext(attributes);
811         }
812
813         /**
814          * Constructor.
815          * Create insert handler to insert page at specified location.
816          */

817         public InsertHandler(String JavaDoc page, String JavaDoc role, Controller controller) {
818             this.page = page;
819             this.role = role;
820             this.controller = controller;
821             subCompContext = new ComponentContext();
822         }
823
824         /**
825          * Create a new empty context.
826          */

827         public int doStartTag() throws JspException JavaDoc {
828             // Check role
829
HttpServletRequest JavaDoc request =
830                 (HttpServletRequest JavaDoc) pageContext.getRequest();
831
832             if (role != null && !request.isUserInRole(role)) {
833                 return SKIP_BODY;
834             }
835
836             // save current context
837
this.currentContext = getCurrentContext();
838             return EVAL_BODY_INCLUDE;
839         }
840
841         /**
842          * Add attribute to sub context.
843          * Do nothing.
844          */

845         public void putAttribute(String JavaDoc name, Object JavaDoc value) {
846             subCompContext.putAttribute(name, value);
847         }
848
849         /**
850          * Include requested page.
851          */

852         public int doEndTag() throws JspException JavaDoc {
853             // Check role
854
HttpServletRequest JavaDoc request =
855                 (HttpServletRequest JavaDoc) pageContext.getRequest();
856
857             if (role != null && !request.isUserInRole(role)) {
858                 return EVAL_PAGE;
859             }
860
861             try {
862                 if (log.isDebugEnabled()) {
863                     log.debug("insert page='" + page + "'.");
864                 }
865
866                 // set new context for included component.
867
pageContext.setAttribute(
868                     ComponentConstants.COMPONENT_CONTEXT,
869                     subCompContext,
870                     PageContext.REQUEST_SCOPE);
871
872                 // Call controller if any
873
if (controller != null) {
874                     try {
875                         controller.execute(
876                             subCompContext,
877                             (HttpServletRequest JavaDoc) pageContext.getRequest(),
878                             (HttpServletResponse JavaDoc) pageContext.getResponse(),
879                             pageContext.getServletContext());
880                             
881                     } catch (Exception JavaDoc e) {
882                         throw new ServletException JavaDoc(e);
883                     }
884
885                 }
886
887                 // include requested component.
888
if (flush) {
889                     pageContext.getOut().flush();
890                 }
891
892                 doInclude(page);
893
894             } catch (IOException JavaDoc e) {
895                 String JavaDoc msg =
896                     "Can't insert page '" + page + "' : " + e.getMessage();
897                 log.error(msg, e);
898                 throw new JspException JavaDoc(msg);
899
900             } catch (IllegalArgumentException JavaDoc e) {
901                 // Can't resolve page uri, should we ignore it?
902
if (!(page == null && isErrorIgnored)) {
903                     String JavaDoc msg =
904                         "Can't insert page '"
905                             + page
906                             + "'. Check if it exists.\n"
907                             + e.getMessage();
908
909                     log.error(msg, e);
910                     throw new JspException JavaDoc(msg);
911                 }
912
913             } catch (ServletException JavaDoc e) {
914                 Throwable JavaDoc cause = e;
915                 if (e.getRootCause() != null) {
916                     cause = e.getRootCause();
917                 }
918
919                 String JavaDoc msg =
920                     "ServletException in '" + page + "': " + cause.getMessage();
921
922                 log.error(msg, e);
923                 throw new JspException JavaDoc(msg);
924
925             } finally {
926                 // restore old context only if currentContext not null
927
// (bug with Silverstream ?; related by Arvindra Sehmi 20010712)
928
if (currentContext != null) {
929                     pageContext.setAttribute(
930                         ComponentConstants.COMPONENT_CONTEXT,
931                         currentContext,
932                         PageContext.REQUEST_SCOPE);
933                 }
934             }
935
936             return EVAL_PAGE;
937         }
938
939         /**
940          * Process an exception.
941          * Depending of debug attribute, print full exception trace or only
942          * its message in output page.
943          * @param ex Exception
944          * @param msg An additional message to show in console and to propagate if we can't output exception.
945          * @deprecated This method will be removed in a release after Struts 1.2.
946          */

947         protected void processException(Throwable JavaDoc ex, String JavaDoc msg)
948             throws JspException JavaDoc {
949
950             try {
951                 if (msg == null) {
952                     msg = ex.getMessage();
953                 }
954
955                 if (log.isDebugEnabled()) { // show full trace
956
log.debug(msg, ex);
957                     pageContext.getOut().println(msg);
958                     ex.printStackTrace(
959                         new PrintWriter JavaDoc(pageContext.getOut(), true));
960                 } else { // show only message
961
pageContext.getOut().println(msg);
962                 }
963
964             } catch (IOException JavaDoc ioex) { // problems. Propagate original exception
965
pageContext.setAttribute(
966                     ComponentConstants.EXCEPTION_KEY,
967                     ex,
968                     PageContext.REQUEST_SCOPE);
969                 throw new JspException JavaDoc(msg);
970             }
971         }
972     }
973
974     /**
975      * Parse the list of roles and return <code>true</code> or <code>false</code> based on whether
976      * the user has that role or not.
977      * @param role Comma-delimited list of roles.
978      * @param request The request.
979      */

980     static public boolean userHasRole(
981         HttpServletRequest JavaDoc request,
982         String JavaDoc role) {
983         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(role, ",");
984         while (st.hasMoreTokens()) {
985             if (request.isUserInRole(st.nextToken())) {
986                 return true;
987             }
988         }
989
990         return false;
991     }
992
993     /////////////////////////////////////////////////////////////////////////////
994

995     /**
996      * Handle insert direct string.
997      */

998     protected class DirectStringHandler implements TagHandler {
999         /** Object to print as a direct string */
1000        private Object JavaDoc value;
1001
1002        /**
1003         * Constructor.
1004         */

1005        public DirectStringHandler(Object JavaDoc value) {
1006            this.value = value;
1007        }
1008
1009        /**
1010         * Do nothing, there is no context for a direct string.
1011         */

1012        public int doStartTag() throws JspException JavaDoc {
1013            return SKIP_BODY;
1014        }
1015
1016        /**
1017         * Add attribute to sub context.
1018         * Do nothing.
1019         */

1020        public void putAttribute(String JavaDoc name, Object JavaDoc value) {
1021        }
1022
1023        /**
1024         * Print String in page output stream.
1025         */

1026        public int doEndTag() throws JspException JavaDoc {
1027            try {
1028                if (flush) {
1029                    pageContext.getOut().flush();
1030                }
1031
1032                pageContext.getOut().print(value);
1033
1034            } catch (IOException JavaDoc ex) {
1035                if (log.isDebugEnabled()) {
1036                    log.debug("Can't write string '" + value + "' : ", ex);
1037                }
1038
1039                pageContext.setAttribute(
1040                    ComponentConstants.EXCEPTION_KEY,
1041                    ex,
1042                    PageContext.REQUEST_SCOPE);
1043
1044                throw new JspException JavaDoc(
1045                    "Can't write string '" + value + "' : " + ex.getMessage());
1046            }
1047
1048            return EVAL_PAGE;
1049        }
1050    }
1051}
1052
Popular Tags