KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > portal > services > PortletDefinitionRegistryServiceFileImplIG


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

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

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

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

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

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

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

337             // fill portletsKeyObjectId
338
Iterator JavaDoc portlets = papp.getPortletDefinitionList().iterator();
339             while (portlets.hasNext())
340             {
341                 PortletDefinition portlet = (PortletDefinition)portlets.next();
342                 //System.out.println("portlet: " + portlet.toString());
343

344                 portletsKeyObjectId.put(portlet.getId(), portlet);
345                 log.debug("Putting in portletsKeyObjectId[" + portletsKeyObjectId.hashCode() + "]:" + portlet.getId() + "=" + portlet.toString() + "[" + papp.getId() + "]");
346             }
347
348         }
349
350     }
351
352     //method added for hot deploy
353
public void postInit() throws Exception JavaDoc
354      {
355         PortletDefinitionRegistry.setPortletDefinitionRegistryService();
356      }
357     
358 }
359
Popular Tags