View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.tools.deploy;
18  
19  import org.jdom.Document;
20  import org.jdom.Element;
21  import org.jdom.Namespace;
22  
23  /***
24   * Utilities for manipulating the web.xml deployment descriptor version 2.4
25   * 
26   * @author Nicolas Dutertry
27   * @version $Id: JetspeedWebApplicationRewriter2_4.java 517121 2007-03-12 07:45:49Z ate $
28   */
29  class JetspeedWebApplicationRewriter2_4 extends JetspeedWebApplicationRewriter
30  {
31      public static final String JETSPEED_SERVLET_XPATH = "/js:web-app/js:servlet/js:servlet-name[contains(child::text(), \"JetspeedContainer\")]";
32      public static final String JETSPEED_SERVLET_MAPPING_XPATH = "/js:web-app/js:servlet-mapping/js:servlet-name[contains(child::text(), \"JetspeedContainer\")]";
33      public static final String JSP_CONFIG_XPATH = "/js:web-app/js:jsp-config";
34      public static final String PORTLET_TAGLIB_XPATH = "/js:web-app/js:jsp-config/js:taglib/js:taglib-uri[contains(child::text(), \"http://java.sun.com/portlet\")]";
35      
36      protected static final String[] ELEMENTS_BEFORE_SERVLET = new String[]{"description", "display-name", "icon", 
37              "distributable", "context-param", "filter", "filter-mapping", "listener", "servlet"};
38      protected static final String[] ELEMENTS_BEFORE_SERVLET_MAPPING = new String[]{"description", "display-name", "icon", 
39              "distributable", "context-param", "filter", "filter-mapping", "listener", "servlet",
40              "servlet-mapping"};
41      
42      protected static final String[] ELEMENTS_BEFORE_JSP_CONFIG = new String[]{"description", "display-name", "icon", 
43          "distributable", "context-param", "filter", "filter-mapping", "listener", "servlet",
44          "servlet-mapping", "session-config", "mime-mapping", "welcome-file-list", "error-page", "jsp-config"};
45      
46      protected static final String[] ELEMENTS_BEFORE_TAGLIB_MAPPING = new String[]{"taglib"};
47        
48      public JetspeedWebApplicationRewriter2_4(Document doc, String portletApplication)
49      {
50          super(doc, portletApplication);
51      }
52  
53      public JetspeedWebApplicationRewriter2_4(Document doc)
54      {
55          super(doc);
56      }
57      
58      /***
59       * Returns the jetspeed servlet xpath.
60       * 
61       * @return jetspeed servlet xpath
62       */
63      protected String getJetspeedServletXPath()
64      {
65          return JETSPEED_SERVLET_XPATH;
66      }
67      
68      /***
69       * Returns the jetspeed servlet mapping xpath.
70       * 
71       * @return jetspeed servlet mapping xpath
72       */
73      protected String getJetspeedServletMappingXPath()
74      {
75          return JETSPEED_SERVLET_MAPPING_XPATH;
76      }
77  
78      /***
79       * Returns the portlet taglib xpath.
80       * 
81       * @return portlet taglib xpath
82       */
83      protected String getPortletTagLibXPath()
84      {
85          return PORTLET_TAGLIB_XPATH;
86      }
87  
88      /***
89       * Inserts the jetspeed servlet into web.xml
90       * 
91       * @param root
92       * @throws Exception
93       */
94      protected void insertJetspeedServlet(Element root) throws Exception
95      {
96          Namespace namespace = root.getNamespace();
97          Element jetspeedServletElement = new Element("servlet", namespace);
98          Element servletName = new Element("servlet-name", namespace).addContent(JETSPEED_CONTAINER);
99          Element servletDspName = new Element("display-name", namespace).addContent(JETSPEED_SERVLET_DISPLAY_NAME);
100         Element servletDesc = new Element("description", namespace)
101                 .addContent(JETSPEED_SERVLET_DESCRIPTION);
102         Element servletClass = new Element("servlet-class", namespace)
103                 .addContent(JETSPEED_SERVLET_CLASS);
104         // order is important 
105         jetspeedServletElement.addContent(servletDesc);
106         jetspeedServletElement.addContent(servletDspName);
107         jetspeedServletElement.addContent(servletName);
108         jetspeedServletElement.addContent(servletClass);
109         insertContextNameParam(jetspeedServletElement);
110         insertLoadOnStartup(jetspeedServletElement);
111         insertElementCorrectly(root, jetspeedServletElement, ELEMENTS_BEFORE_SERVLET);
112     }
113 
114     /***
115      * Inserts the jetspeed servlet mapping into web.xml
116      * 
117      * @param root
118      * @throws Exception
119      */
120     protected void insertJetspeedServletMapping(Element root) throws Exception
121     {
122         Namespace namespace = root.getNamespace();
123         Element jetspeedServletMappingElement = new Element("servlet-mapping", namespace);
124         
125         Element servletMapName = new Element("servlet-name", namespace).addContent(JETSPEED_CONTAINER);
126         Element servletUrlPattern = new Element("url-pattern", namespace).addContent("/container/*");
127 
128         jetspeedServletMappingElement.addContent(servletMapName);
129         jetspeedServletMappingElement.addContent(servletUrlPattern);
130 
131         insertElementCorrectly(root, jetspeedServletMappingElement, ELEMENTS_BEFORE_SERVLET_MAPPING);
132     }
133 
134     /***
135      * Inserts the portlet taglib into web.xml
136      * 
137      * @param root
138      * @throws Exception
139      */
140     protected void insertPortletTagLib(Element root) throws Exception
141     {
142         Namespace namespace = root.getNamespace();
143         Element jspConfig = (Element)getXPath(JSP_CONFIG_XPATH).selectSingleNode(root.getDocument());
144         if(jspConfig == null)
145         {
146             jspConfig = new Element("jsp-config", namespace);
147             insertElementCorrectly(root, jspConfig, ELEMENTS_BEFORE_JSP_CONFIG);
148         }
149         Element taglib = new Element ("taglib", namespace);
150         Element taguri = new Element("taglib-uri", namespace).addContent("http://java.sun.com/portlet");
151         Element taglocation = new Element("taglib-location", namespace).addContent("/WEB-INF/tld/portlet.tld");
152         
153         taglib.addContent(taguri);
154         taglib.addContent(taglocation);
155         
156         insertElementCorrectly(jspConfig, taglib, ELEMENTS_BEFORE_TAGLIB_MAPPING);
157     }
158 }