1 16 19 20 package org.apache.pluto.portalImpl.services.portletdefinitionregistry; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.Vector ; 28 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.ServletContext ; 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 ; 46 47 54 55 public class PortletDefinitionRegistryServiceFileImpl extends PortletDefinitionRegistryService 56 { 57 58 private static String fileSeparator = System.getProperty("file.separator"); 59 60 63 private final static String INITIAL_TMP_PREFIX = "tmp"; 64 65 69 private final static int FULL_TMP_PREFIX_LEN = 70 INITIAL_TMP_PREFIX.length() + 5; 71 72 76 private final static String WAR_FILE_EXT = ".war"; 77 78 public final static String DEFAULT_MAPPING_PORTLETXML = "WEB-INF/data/xml/portletdefinitionmapping.xml"; 80 public final static String DEFAULT_MAPPING_WEBXML = "WEB-INF/data/xml/servletdefinitionmapping.xml"; 81 private final static String CONFIG_MAPPING_PORTLETXML = "mapping.portletxml.configfile"; 83 private final static String CONFIG_MAPPING_WEBXML = "mapping.webxml.configfile"; 84 85 private Mapping mappingPortletXml = null; 87 private Mapping mappingWebXml = null; 88 private ServletContext servletContext = null; 90 private String baseWMDir = null; 92 private Logger log = null; 93 94 protected PortletApplicationDefinitionListImpl registry = 97 new PortletApplicationDefinitionListImpl(); 98 protected Map portletsKeyObjectId = new HashMap (); 99 100 public void init (ServletConfig config, Properties properties) throws Exception 101 { 102 log = Log.getService().getLogger(getClass()); 103 servletContext = config.getServletContext(); 104 105 if (properties.getBoolean("non-servlet")==Boolean.TRUE) 106 { 107 String root = config.getServletContext().getRealPath("/"); baseWMDir = root + fileSeparator + 109 "WEB-INF" + fileSeparator + 110 "portletapps" + fileSeparator; if(log.isDebugEnabled()) 112 log.debug("baseWMDir = " + baseWMDir + " fileSeparator = " + fileSeparator); 113 } 114 else 115 { 116 this.baseWMDir = this.servletContext.getRealPath(""); 117 if (this.baseWMDir.endsWith(fileSeparator)) { 119 this.baseWMDir = this.baseWMDir.substring(0, this.baseWMDir.length()-1); 120 } 121 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 String _mapping = properties.getString(CONFIG_MAPPING_PORTLETXML, DEFAULT_MAPPING_PORTLETXML); 134 File f = new File (_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 e) 142 { 143 log.error("Failed to load mapping file "+_mapping,e); 144 throw e; 145 } 146 _mapping = properties.getString(CONFIG_MAPPING_WEBXML, DEFAULT_MAPPING_WEBXML); 148 f = new File (_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 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 177 { 178 File f = new File (baseWMDir); 179 String [] entries = f.list(); 180 for (int i=0; i<entries.length; i++) 181 { 182 File entry = new File (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 201 private String resolveURI(String webModule) 202 { 203 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 return webModule; 214 } 215 216 private void load(String baseDir, String webModule) throws Exception 217 { 218 String directory = baseDir+webModule+fileSeparator+"WEB-INF"+fileSeparator; 219 220 File portletXml = new File (directory+"portlet.xml"); 221 File webXml = new File (directory+"web.xml"); 222 223 if (portletXml.exists()) { 227 if (log.isDebugEnabled()) 228 { 229 log.debug("Loading the following Portlet Applications XML files..."+portletXml+", "+webXml); 230 } 231 232 InputSource source = new InputSource (new FileInputStream (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 webDocument = 245 XmlParser.parseWebXml(new FileInputStream (webXml)); 246 247 unmarshaller = new Unmarshaller(this.mappingWebXml); 248 unmarshaller.setIgnoreExtraElements(true); 249 webApp = 250 (WebApplicationDefinitionImpl)unmarshaller.unmarshal(webDocument); 251 252 Vector structure = new Vector (); 253 structure.add(portletApp); 254 structure.add("/" + resolveURI(webModule)); 255 256 webApp.postLoad(structure); 257 258 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 structure = new Vector (); 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 iterator = registry.iterator(); 306 while (iterator.hasNext()) 307 { 308 PortletApplicationDefinition papp = (PortletApplicationDefinition)iterator.next(); 309 310 Iterator 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 |