1 23 26 package org.infoglue.deliver.portal.services; 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.util.List ; 32 33 import javax.servlet.ServletConfig ; 34 import javax.servlet.ServletContext ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.apache.pluto.om.common.ObjectID; 39 import org.apache.pluto.om.entity.PortletApplicationEntity; 40 import org.apache.pluto.om.entity.PortletApplicationEntityList; 41 import org.apache.pluto.om.entity.PortletEntity; 42 import org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService; 43 import org.apache.pluto.portalImpl.util.Properties; 44 import org.exolab.castor.jdo.Database; 45 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService; 46 import org.infoglue.cms.controllers.kernel.impl.simple.PortletAssetController; 47 import org.infoglue.cms.entities.content.DigitalAsset; 48 import org.infoglue.cms.entities.content.DigitalAssetVO; 49 import org.infoglue.cms.util.CmsPropertyHandler; 50 import org.infoglue.deliver.portal.OmBuilder; 51 import org.infoglue.deliver.portal.OmBuilderXStreamImpl; 52 import org.infoglue.deliver.portal.om.PortletApplicationEntityListImpl; 53 54 61 public class PortletEntityRegistryServiceDBImpl extends PortletEntityRegistryService 62 { 63 private static final Log LOG = LogFactory.getLog(PortletEntityRegistryServiceDBImpl.class); 64 65 public static final String PORTLET_REGISTRY_CONTENT_NAME = "portletentityregistry.xml"; 66 67 private ServletContext aContext; 68 69 private OmBuilder builder = new OmBuilderXStreamImpl(); 70 71 private PortletApplicationEntityListImpl applications; 72 73 private boolean needRefresh = true; 74 75 80 public PortletApplicationEntityList getPortletApplicationEntityList() { 81 if (needRefresh) { 82 try { 83 load(); 84 } catch (IOException e) { 85 LOG.error("Failed to load PortletEntityRegistry", e); 86 } 87 } 88 return applications; 89 } 90 91 96 public PortletEntity getPortletEntity(ObjectID id) { 97 if (needRefresh) { 98 try { 99 load(); 100 } catch (IOException e) { 101 LOG.error("Failed to load PortletEntityRegistry", e); 102 return null; 103 } 104 } 105 106 String oid = id.toString(); 107 int dot = oid.lastIndexOf("."); 108 if (dot < 0) { 109 LOG.warn("ID does not contain '.' to separate application- and portlet-id: " + id); 110 return null; 111 } 112 113 ObjectID appID = org.apache.pluto.portalImpl.util.ObjectID.createFromString(oid.substring( 114 0, dot)); 115 116 PortletApplicationEntity appEntity = applications.get(appID); 117 if (appEntity == null) { 118 LOG.warn("Application not found: " + appID); 119 LOG.warn(toString()); 120 return null; 121 } 122 PortletEntity portletEntity = appEntity.getPortletEntityList().get(id); 123 if (portletEntity == null) { 124 LOG.warn("Portlet not found: " + id); 125 LOG.warn(toString()); 126 } 127 128 return portletEntity; 129 } 130 131 136 public void store() throws IOException { 137 String xml = builder.toXML(applications); 138 if (LOG.isDebugEnabled()) { 139 LOG.debug("Storing PortletEntityRegistry...\n" + xml); 140 } 141 142 try { 143 Database db = CastorDatabaseService.getDatabase(); 144 db.begin(); 145 146 byte[] serial = xml.getBytes(); 147 InputStream is = new ByteArrayInputStream (serial); 148 149 DigitalAsset da = getPortletRegistry(); 150 if (da == null) { 151 LOG.info("Creating new " + PORTLET_REGISTRY_CONTENT_NAME); 152 153 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 154 DigitalAssetVO newAsset = new DigitalAssetVO(); 155 newAsset.setAssetContentType("text/xml"); 156 newAsset.setAssetKey(PORTLET_REGISTRY_CONTENT_NAME); 157 newAsset.setAssetFileName(PORTLET_REGISTRY_CONTENT_NAME); 158 newAsset.setAssetFilePath(filePath); 159 newAsset.setAssetFileSize(new Integer (serial.length)); 160 161 da = PortletAssetController.create(newAsset, is); 162 LOG.warn(PORTLET_REGISTRY_CONTENT_NAME + " stored as id=" + da.getId()); 163 } else { 164 LOG.info("Updating " + PORTLET_REGISTRY_CONTENT_NAME); 165 166 DigitalAssetVO daVO = da.getValueObject(); 167 daVO.setAssetFileSize(new Integer (serial.length)); 168 169 PortletAssetController.update(daVO, is); 170 } 171 is.close(); 172 173 db.commit(); 174 db.close(); 175 LOG.debug("Stored PortletEntityRegistry successfully"); 176 } catch (Throwable e) { 177 LOG.error("Failed to store PortletEntityRegistry", e); 178 } 179 } 180 181 186 public void load() throws IOException { 187 LOG.warn("Loading PortletEntityRegistry..."); 188 try { 189 DigitalAsset da = getPortletRegistry(); 190 if (da == null) { 191 applications = new PortletApplicationEntityListImpl(); 192 } else { 193 InputStream is = da.getAssetBlob(); 194 applications = builder.getPortletApplicationEntityList(is); 195 is.close(); 196 } 197 needRefresh = false; 198 if (LOG.isDebugEnabled()) { 199 LOG.debug("Applications: " + toString()); 200 } 201 } catch (Throwable e) { 202 LOG.error("Failed to load PortletEntityRegistry", e); 203 } 204 } 205 206 211 public void refresh(PortletEntity entity) { 212 } 214 215 public String toString() { 216 return builder.toXML(getPortletApplicationEntityList()); 217 } 218 219 private DigitalAsset getPortletRegistry() throws Exception { 220 List das = PortletAssetController.getDigitalAssetByName(PORTLET_REGISTRY_CONTENT_NAME); 221 if (das != null && das.size() > 0) { 222 DigitalAsset da = (DigitalAsset) das.get(0); 223 LOG.debug("Registry located as id=" + da.getId()); 224 return da; 225 } else { 226 LOG.info("Portlet Registry not found"); 227 } 228 return null; 229 } 230 231 237 protected void init(ServletConfig conf, Properties props) throws Exception { 238 LOG.debug("Calling init()"); 239 super.init(conf, props); 240 needRefresh = true; 241 } 242 243 248 protected void postInit(ServletConfig conf) throws Exception { 249 LOG.debug("Calling postInit()"); 250 super.postInit(conf); 251 needRefresh = true; 252 } 253 } | Popular Tags |