1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.pluto.descriptors.services.impl;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.net.URL;
23
24 import org.apache.pluto.descriptors.portlet.PortletAppDD;
25 import org.apache.pluto.descriptors.services.Constants;
26 import org.apache.pluto.descriptors.services.PortletAppDescriptorService;
27 import org.exolab.castor.mapping.Mapping;
28 import org.exolab.castor.mapping.MappingException;
29
30 /***
31 * Abstract Implementation of the Web Application Deployment
32 * Descriptor service. Provides default implementation of
33 * the service; requiring only that subclasses provide the
34 * input streams to/from the actual descriptor.
35 *
36 * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
37 * @version $Id: AbstractPortletAppDescriptorService.java 156743 2005-03-10 05:50:30Z ddewolf $
38 * @since Mar 5, 2005
39 */
40 public abstract class AbstractPortletAppDescriptorService
41 extends AbstractCastorDescriptorService
42 implements PortletAppDescriptorService {
43
44 public static final String PORTLET_XML_MAPPING =
45 "castor-portlet-xml-mapping.xml";
46
47 /***
48 * Flag to determine if an empty deployment descriptor
49 * should be created (in memory) if the actual descriptor
50 * does not exist.
51 */
52 private boolean create;
53
54 /***
55 * Default Constructor.
56 */
57 protected AbstractPortletAppDescriptorService(String contextPath) {
58 super(contextPath);
59 this.create = true;
60 }
61
62 /***
63 * Secondary Constructor. Create an instance
64 * and specify whether or not a default/empty instance
65 * should be returned if no descriptor is available.
66 * @param create
67 */
68 protected AbstractPortletAppDescriptorService(String contextPath, boolean create) {
69 super(contextPath);
70 this.create = create;
71 }
72
73 /***
74 * Read the Web Application Deployment Descriptor.
75 *
76 * @return WebAppDD instance representing the descriptor.
77 * @throws java.io.IOException
78 */
79 public PortletAppDD read() throws IOException {
80 PortletAppDD portlet =
81 (PortletAppDD) readInternal();
82 if(portlet == null && create) {
83 portlet = new PortletAppDD();
84 }
85 return portlet;
86 }
87
88 /***
89 * Write the deployment descriptor.
90 * @param portlet
91 * @throws java.io.IOException
92 */
93 public void write(PortletAppDD portlet) throws IOException {
94 writeInternal(portlet);
95 }
96
97 /***
98 * Retrieve the Web Application Deployment
99 * descriptor's public Id.
100 * @return
101 */
102 protected String getPublicId() {
103 return Constants.PORLTET_XML_PUBLIC_ID;
104 }
105
106 /***
107 * Retrieve the Web Application Deployment
108 * descriptor's DTD uri.
109 * @return
110 */
111 protected String getDTDUri() {
112 return Constants.PORTLET_XML_DTD;
113 }
114
115 /***
116 * Read and Retrieve the Web Application's Castor Mapping
117 * resource.
118 *
119 * @return
120 * @throws java.io.IOException
121 * @throws org.exolab.castor.mapping.MappingException
122 */
123 protected Mapping getCastorMapping()
124 throws IOException, MappingException {
125 URL url = PortletAppDD.class.getResource(PORTLET_XML_MAPPING);
126
127 Mapping mapping = new Mapping();
128 mapping.loadMapping(url);
129 return mapping;
130 }
131
132 protected boolean getIgnoreExtraElements() {
133 return true;
134 }
135
136 private void dump(URL url) {
137 try {
138 InputStream is = url.openStream();
139 BufferedReader in = new BufferedReader(new InputStreamReader(is));
140 String s;
141 while( (s = in.readLine())!=null) {
142 System.out.println(s);
143 }
144 }
145 catch(IOException io) {
146 io.printStackTrace();
147 }
148 }
149
150 }
151