KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > services > portletdefinitionregistry > PortletDefinitionRegistryServiceFileImpl


1 /*
2  * Copyright 2003,2004 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 /*
17
18  */

19
20 package org.apache.pluto.portalImpl.services.portletdefinitionregistry;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31
32 import org.apache.pluto.om.common.ObjectID;
33 import org.apache.pluto.om.portlet.PortletApplicationDefinition;
34 import org.apache.pluto.om.portlet.PortletApplicationDefinitionList;
35 import org.apache.pluto.om.portlet.PortletDefinition;
36 import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionImpl;
37 import org.apache.pluto.portalImpl.om.portlet.impl.PortletApplicationDefinitionListImpl;
38 import org.apache.pluto.portalImpl.om.servlet.impl.WebApplicationDefinitionImpl;
39 import org.apache.pluto.portalImpl.services.log.Log;
40 import org.apache.pluto.portalImpl.util.Properties;
41 import org.apache.pluto.portalImpl.xml.XmlParser;
42 import org.apache.pluto.services.log.Logger;
43 import org.exolab.castor.mapping.Mapping;
44 import org.exolab.castor.xml.Unmarshaller;
45 import org.xml.sax.InputSource JavaDoc;
46
47 /**
48  * A simple XML Castor file based implementation of the
49  * <code>PortletRegistryService</config>.
50  *
51  * <p>This store persit the PortletRegistry informations.</p>
52  *
53  */

54
55 public class PortletDefinitionRegistryServiceFileImpl extends PortletDefinitionRegistryService
56 {
57
58     private static String JavaDoc fileSeparator = System.getProperty("file.separator");
59
60     /**
61      * The initial portion of the web module prefix used by JBoss.
62      */

63     private final static String JavaDoc INITIAL_TMP_PREFIX = "tmp";
64
65     /**
66      * The length of the full web module prefix used by JBoss ({@link
67      * #INITIAL_TMP_PREFIX} plus numeric portion).
68      */

69     private final static int FULL_TMP_PREFIX_LEN =
70         INITIAL_TMP_PREFIX.length() + 5;
71
72     /**
73      * The file extension for web application archives (including the
74      * leading dot).
75      */

76     private final static String JavaDoc WAR_FILE_EXT = ".war";
77     
78     // default configuration values
79
public final static String JavaDoc DEFAULT_MAPPING_PORTLETXML = "WEB-INF/data/xml/portletdefinitionmapping.xml";
80     public final static String JavaDoc DEFAULT_MAPPING_WEBXML = "WEB-INF/data/xml/servletdefinitionmapping.xml";
81     // configuration keys
82
private final static String JavaDoc CONFIG_MAPPING_PORTLETXML = "mapping.portletxml.configfile";
83     private final static String JavaDoc CONFIG_MAPPING_WEBXML = "mapping.webxml.configfile";
84
85     // Castor mapping file
86
private Mapping mappingPortletXml = null;
87     private Mapping mappingWebXml = null;
88     // Servlet Context
89
private ServletContext JavaDoc servletContext = null;
90     // Base Dir where all web modules are located
91
private String JavaDoc baseWMDir = null;
92     private Logger log = null;
93
94     // Helper lists and hashtables to access the data as fast as possible
95
// List containing all portlet applications available in the system
96
protected PortletApplicationDefinitionListImpl registry =
97         new PortletApplicationDefinitionListImpl();
98     protected Map JavaDoc portletsKeyObjectId = new HashMap JavaDoc();
99
100     public void init (ServletConfig JavaDoc config, Properties properties) throws Exception JavaDoc
101     {
102         log = Log.getService().getLogger(getClass());
103         servletContext = config.getServletContext();
104
105         if (properties.getBoolean("non-servlet")==Boolean.TRUE)
106         {
107             String JavaDoc root = config.getServletContext().getRealPath("/"); //root
108
baseWMDir = root + fileSeparator +
109         "WEB-INF" + fileSeparator +
110         "portletapps" + fileSeparator; //org.apache.pluto.portalImpl.services.deploy.DeployServiceFileImpl.DEFAULT_PROTECTED;
111
if(log.isDebugEnabled())
112                 log.debug("baseWMDir = " + baseWMDir + " fileSeparator = " + fileSeparator);
113         }
114         else
115         {
116             this.baseWMDir = this.servletContext.getRealPath("");
117             // BEGIN PATCH for IBM WebSphere
118
if (this.baseWMDir.endsWith(fileSeparator)) {
119                 this.baseWMDir = this.baseWMDir.substring(0, this.baseWMDir.length()-1);
120             }
121             // END PATCH for IBM WebSphere
122

123             this.baseWMDir = this.baseWMDir.substring(0,
124                                             this.baseWMDir.lastIndexOf(fileSeparator))+fileSeparator;
125             if (log.isDebugEnabled())
126             {
127                 log.debug("servletContext.getRealPath('') =" + this.servletContext.getRealPath(""));
128                 log.debug("baseWMDir = " + this.baseWMDir);
129             }
130         }
131
132         // get portlet xml mapping file
133
String JavaDoc _mapping = properties.getString(CONFIG_MAPPING_PORTLETXML, DEFAULT_MAPPING_PORTLETXML);
134         File JavaDoc f = new File JavaDoc(_mapping);
135         if (!f.isAbsolute()) _mapping = servletContext.getRealPath(_mapping);
136         this.mappingPortletXml = new Mapping();
137         try
138         {
139             this.mappingPortletXml.loadMapping(_mapping);
140         }
141         catch (Exception JavaDoc e)
142         {
143             log.error("Failed to load mapping file "+_mapping,e);
144             throw e;
145         }
146         // get web xml mapping file
147
_mapping = properties.getString(CONFIG_MAPPING_WEBXML, DEFAULT_MAPPING_WEBXML);
148         f = new File JavaDoc(_mapping);
149         if (!f.isAbsolute()) _mapping = servletContext.getRealPath(_mapping);
150         this.mappingWebXml = new Mapping();
151         try
152         {
153             this.mappingWebXml.loadMapping(_mapping);
154         }
155         catch (Exception JavaDoc e)
156         {
157             log.error("Failed to load mapping file "+_mapping,e);
158             throw e;
159         }
160
161         load();
162
163         fill();
164     }
165
166     public PortletApplicationDefinitionList getPortletApplicationDefinitionList()
167     {
168         return registry;
169     }
170
171     public PortletDefinition getPortletDefinition(ObjectID id)
172     {
173         return (PortletDefinition)portletsKeyObjectId.get(id);
174     }
175
176     private void load() throws Exception JavaDoc
177     {
178         File JavaDoc f = new File JavaDoc(baseWMDir);
179         String JavaDoc[] entries = f.list();
180         for (int i=0; i<entries.length; i++)
181         {
182             File JavaDoc entry = new File JavaDoc(baseWMDir+entries[i]);
183             if (entry.isDirectory())
184             {
185                 if (log.isDebugEnabled())
186                 {
187                     log.debug("Searching in directory: " + entries[i]);
188                 }
189                 load(baseWMDir, entries[i]);
190             }
191         }
192     }
193
194     /**
195      * Handles resolution of a web module's file system name to its
196      * URI identifier.
197      *
198      * @param webModule The file system name.
199      * @return The URI part.
200      */

201     private String JavaDoc resolveURI(String JavaDoc webModule)
202     {
203         // For JBoss compatibility, change webModule from the form
204
// of "tmp12345foo.war" to "foo".
205
int len = webModule.length();
206         if (webModule.endsWith(WAR_FILE_EXT) &&
207             webModule.startsWith(INITIAL_TMP_PREFIX) &&
208             len > FULL_TMP_PREFIX_LEN + WAR_FILE_EXT.length()) {
209             webModule = webModule.substring(FULL_TMP_PREFIX_LEN,
210                                             len - WAR_FILE_EXT.length());
211         }
212         // else assumed literal.
213
return webModule;
214     }
215
216     private void load(String JavaDoc baseDir, String JavaDoc webModule) throws Exception JavaDoc
217     {
218         String JavaDoc directory = baseDir+webModule+fileSeparator+"WEB-INF"+fileSeparator;
219
220         File JavaDoc portletXml = new File JavaDoc(directory+"portlet.xml");
221         File JavaDoc webXml = new File JavaDoc(directory+"web.xml");
222
223         // check for the porlet.xml. If there is no portlet.xml this is not a
224
// portlet application web module
225
if (portletXml.exists()) // && (webXml.exists()))
226
{
227             if (log.isDebugEnabled())
228             {
229                 log.debug("Loading the following Portlet Applications XML files..."+portletXml+", "+webXml);
230             }
231
232             InputSource JavaDoc source = new InputSource JavaDoc(new FileInputStream JavaDoc(portletXml));
233             source.setSystemId(portletXml.toURL().toExternalForm());
234             
235             Unmarshaller unmarshaller = new Unmarshaller(this.mappingPortletXml);
236             unmarshaller.setIgnoreExtraElements(true);
237             PortletApplicationDefinitionImpl portletApp =
238                 (PortletApplicationDefinitionImpl)unmarshaller.unmarshal( source );
239
240             WebApplicationDefinitionImpl webApp = null;
241
242             if (webXml.exists())
243             {
244                 org.w3c.dom.Document JavaDoc webDocument =
245                 XmlParser.parseWebXml(new FileInputStream JavaDoc(webXml));
246
247                 unmarshaller = new Unmarshaller(this.mappingWebXml);
248                 unmarshaller.setIgnoreExtraElements(true);
249                 webApp =
250                     (WebApplicationDefinitionImpl)unmarshaller.unmarshal(webDocument);
251
252                 Vector JavaDoc structure = new Vector JavaDoc();
253                 structure.add(portletApp);
254                 structure.add("/" + resolveURI(webModule));
255
256                 webApp.postLoad(structure);
257
258                 // refill structure with necessary information
259
webApp.preBuild(structure);
260
261                 webApp.postBuild(structure);
262
263                 if (log.isDebugEnabled())
264                 {
265                     log.debug(webApp.toString());
266                 }
267             }
268             else
269             {
270                 if (log.isDebugEnabled())
271                 {
272                     log.debug("no web.xml...");
273                 }
274                 Vector JavaDoc structure = new Vector JavaDoc();
275                 structure.add("/" + resolveURI(webModule));
276                 structure.add(null);
277                 structure.add(null);
278
279                 portletApp.postLoad(structure);
280                 
281                 portletApp.preBuild(structure);
282                 
283                 portletApp.postBuild(structure);
284             }
285
286             registry.add( portletApp );
287
288             if (log.isDebugEnabled())
289             {
290                 if (webApp!=null)
291                 {
292                     log.debug("Dumping content of web.xml...");
293                     log.debug(webApp.toString());
294                 }
295                 log.debug("Dumping content of portlet.xml...");
296                 log.debug(portletApp.toString());
297             }
298         }
299
300     }
301
302     private void fill()
303     {
304
305         Iterator JavaDoc iterator = registry.iterator();
306         while (iterator.hasNext())
307         {
308             PortletApplicationDefinition papp = (PortletApplicationDefinition)iterator.next();
309
310             // fill portletsKeyObjectId
311
Iterator JavaDoc portlets = papp.getPortletDefinitionList().iterator();
312             while (portlets.hasNext())
313             {
314                 PortletDefinition portlet = (PortletDefinition)portlets.next();
315
316                 portletsKeyObjectId.put(portlet.getId(), portlet);
317
318             }
319
320         }
321
322     }
323 }
324
Popular Tags