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  
22  /***
23   * Utilities for manipulating the context.xml deployment descriptor
24   * 
25   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
26   * @version $Id: JetspeedContextRewriter.java 517124 2007-03-12 08:10:25Z ate $
27   */
28  public class JetspeedContextRewriter
29  {
30      private Document document;
31      private String portletApplication;
32      public JetspeedContextRewriter(Document doc, String portletApplication)
33      {
34          this.document = doc;
35          this.portletApplication = portletApplication;
36      }
37  
38      public void processContextXML()
39          throws Exception
40      {
41          if (document != null)
42          {
43              try
44              {
45                  // get root Context
46                  Element root = null;
47                  if (!document.hasRootElement())
48                  {
49                      root = new Element("Context");
50                      document.setRootElement(root);
51                  }
52                  else
53                  {
54                      root = document.getRootElement();
55                  }   
56                  
57                  // set Context path
58                  String pathAttribute = root.getAttributeValue("path");
59                  if ((pathAttribute == null) || !pathAttribute.equals("/" + portletApplication))
60                  {
61                      root.setAttribute("path", "/" + portletApplication);
62                  }
63                  
64                  // set Context docBase
65                  String docBaseAttribute = root.getAttributeValue("docBase");
66                  if ((docBaseAttribute == null) || !docBaseAttribute.equals(portletApplication))
67                  {
68                      root.setAttribute("docBase", portletApplication);
69                  }
70              }
71              catch (Exception e)
72              {
73                  throw new Exception("Unable to process context.xml for infusion " + e.toString(), e);
74              }
75          }
76      }
77  }