1 16 17 package org.infoglue.deliver.portal.services; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Vector ; 25 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletContext ; 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 ; 45 46 53 54 public class PortletDefinitionRegistryServiceFileImplIG extends PortletDefinitionRegistryService 55 { 56 57 private static String fileSeparator = System.getProperty("file.separator"); 58 59 62 private final static String INITIAL_TMP_PREFIX = "tmp"; 63 64 68 private final static int FULL_TMP_PREFIX_LEN = INITIAL_TMP_PREFIX.length() + 5; 69 70 74 private final static String WAR_FILE_EXT = ".war"; 75 76 public final static String DEFAULT_MAPPING_PORTLETXML = "WEB-INF/data/xml/portletdefinitionmapping.xml"; 78 public final static String DEFAULT_MAPPING_WEBXML = "WEB-INF/data/xml/servletdefinitionmapping.xml"; 79 private final static String CONFIG_MAPPING_PORTLETXML = "mapping.portletxml.configfile"; 81 private final static String CONFIG_MAPPING_WEBXML = "mapping.webxml.configfile"; 82 83 private Mapping mappingPortletXml = null; 85 private Mapping mappingWebXml = null; 86 private ServletContext servletContext = null; 88 private String baseWMDir = null; 90 private Logger log = null; 91 92 protected static PortletApplicationDefinitionListImpl registry = new PortletApplicationDefinitionListImpl(); 95 protected static Map portletsKeyObjectId = new HashMap (); 96 97 public void init (ServletConfig config, Properties properties) throws Exception 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 root = config.getServletContext().getRealPath("/"); baseWMDir = root + fileSeparator + 107 "WEB-INF" + fileSeparator + 108 "portletapps" + fileSeparator; if(log.isDebugEnabled()) 110 log.debug("baseWMDir = " + baseWMDir + " fileSeparator = " + fileSeparator); 111 } 112 else 113 { 114 this.baseWMDir = this.servletContext.getRealPath(""); 115 if (this.baseWMDir.endsWith(fileSeparator)) { 117 this.baseWMDir = this.baseWMDir.substring(0, this.baseWMDir.length()-1); 118 } 119 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 String _mapping = properties.getString(CONFIG_MAPPING_PORTLETXML, DEFAULT_MAPPING_PORTLETXML); 131 log.debug("_mapping = " + _mapping); 132 File f = new File (_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 e) 140 { 141 log.error("Failed to load mapping file "+_mapping,e); 142 throw e; 143 } 144 _mapping = properties.getString(CONFIG_MAPPING_WEBXML, DEFAULT_MAPPING_WEBXML); 146 f = new File (_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 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 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 190 { 191 log.debug("baseWMDir in load(): " + baseWMDir); 192 193 File f = new File (baseWMDir); 194 String [] entries = f.list(); 195 for (int i=0; i<entries.length; i++) 196 { 197 File entry = new File (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 216 private String resolveURI(String webModule) 217 { 218 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(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 return webModule; 234 } 235 236 private void load(String baseDir, String webModule) throws Exception 237 { 238 String directory = baseDir+webModule+fileSeparator+"WEB-INF"+fileSeparator; 239 240 File portletXml = new File (directory+"portlet.xml"); 241 File webXml = new File (directory+"web.xml"); 242 243 if (portletXml.exists()) { 247 if (log.isDebugEnabled()) 248 { 249 log.debug("Loading the following Portlet Applications XML files..."+portletXml+", "+webXml); 250 } 251 252 InputSource source = new InputSource (new FileInputStream (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 webDocument = 265 XmlParser.parseWebXml(new FileInputStream (webXml)); 266 267 unmarshaller = new Unmarshaller(this.mappingWebXml); 268 unmarshaller.setIgnoreExtraElements(true); 269 webApp = (WebApplicationDefinitionImpl)unmarshaller.unmarshal(webDocument); 270 271 Vector structure = new Vector (); 272 structure.add(portletApp); 273 structure.add("/" + resolveURI(webModule)); 274 275 webApp.postLoad(structure); 276 277 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 structure = new Vector (); 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 iterator = registry.iterator(); 332 while (iterator.hasNext()) 333 { 334 PortletApplicationDefinition papp = (PortletApplicationDefinition)iterator.next(); 335 337 Iterator portlets = papp.getPortletDefinitionList().iterator(); 339 while (portlets.hasNext()) 340 { 341 PortletDefinition portlet = (PortletDefinition)portlets.next(); 342 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 public void postInit() throws Exception 354 { 355 PortletDefinitionRegistry.setPortletDefinitionRegistryService(); 356 } 357 358 } 359 | Popular Tags |