View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.descriptors.services.impl;
17  
18  import java.io.IOException;
19  import java.net.URL;
20  
21  import org.apache.pluto.descriptors.services.Constants;
22  import org.apache.pluto.descriptors.services.WebAppDescriptorService;
23  import org.apache.pluto.descriptors.servlet.WebAppDD;
24  import org.exolab.castor.mapping.Mapping;
25  import org.exolab.castor.mapping.MappingException;
26  
27  /***
28   * Abstract Implementation of the Web Application Deployment
29   * Descriptor service.  Provides default implementation of
30   * the service; requiring only that subclasses provide the
31   * input streams to/from the actual descriptor.
32   *
33   * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
34   * @version $Id: AbstractWebAppDescriptorService.java 156743 2005-03-10 05:50:30Z ddewolf $
35   * @since Mar 5, 2005
36   */
37  public abstract class AbstractWebAppDescriptorService
38      extends AbstractCastorDescriptorService
39      implements WebAppDescriptorService {
40  
41      public static final String WEB_XML_MAPPING = "castor-web-xml-mapping.xml";
42  
43      /***
44       * Flag to determine if an empty deployment descriptor
45       * should be created (in memory) if the actual descriptor
46       * does not exist.
47       */
48      private boolean create;
49  
50      /***
51       * Default Constructor.
52       */
53      protected AbstractWebAppDescriptorService(String contextPath) {
54          super(contextPath);
55          this.create = true;
56      }
57  
58      /***
59       * Secondary Constructor.  Create an instance
60       * and specify whether or not a default/empty instance
61       * should be returned if no descriptor is available.
62       * @param create
63       */
64      protected AbstractWebAppDescriptorService(String contextPath, boolean create) {
65          super(contextPath);
66          this.create = create;
67      }
68  
69      /***
70       * Read the Web Application Deployment Descriptor.
71       *
72       * @return WebAppDD instance representing the descriptor.
73       * @throws IOException
74       */
75      public WebAppDD read() throws IOException {
76          WebAppDD webApp = (WebAppDD) readInternal();
77          if(webApp == null && create) {
78              webApp = new WebAppDD();
79              webApp.setDisplayName("Portal Application");
80              webApp.setDescription("Auto Generated Portal Application Wrapper");
81          }
82          return webApp;
83      }
84  
85      /***
86       * Write the deployment descriptor.
87       * @param webApp
88       * @throws IOException
89       */
90      public void write(WebAppDD webApp) throws IOException {
91          writeInternal(webApp);
92      }
93  
94      /***
95       * Retrieve the Web Application Deployment
96       * descriptor's public Id.
97       * @return
98       */
99      protected String getPublicId() {
100         return Constants.WEB_XML_PUBLIC_ID;
101     }
102 
103     /***
104      * Retrieve the Web Application Deployment
105      * descriptor's DTD uri.
106      * @return
107      */
108     protected String getDTDUri() {
109         return Constants.WEB_XML_DTD;
110     }
111 
112     /***
113      * Read and Retrieve the Web Application's Castor Mapping
114      * resource.
115      *
116      * @return
117      * @throws IOException
118      * @throws MappingException
119      */
120     protected Mapping getCastorMapping()
121     throws IOException, MappingException {
122         URL url = WebAppDD.class.getResource(WEB_XML_MAPPING);
123         Mapping mapping = new Mapping();
124         mapping.loadMapping(url);
125         return mapping;
126     }
127 
128 }
129