KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23 /*
24  * Created on 2005-mar-10
25  */

26 package org.infoglue.deliver.portal.services;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.servlet.ServletConfig JavaDoc;
34 import javax.servlet.ServletContext JavaDoc;
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 /**
55  * Infoglue native portlet entity registry service. The registry is
56  * loaded/stored in the infoglue database as a digital asset named
57  * "portletentityregistry.xml".
58  *
59  * @author jand
60  */

61 public class PortletEntityRegistryServiceDBImpl extends PortletEntityRegistryService
62 {
63     private static final Log LOG = LogFactory.getLog(PortletEntityRegistryServiceDBImpl.class);
64
65     public static final String JavaDoc PORTLET_REGISTRY_CONTENT_NAME = "portletentityregistry.xml";
66
67     private ServletContext JavaDoc aContext;
68
69     private OmBuilder builder = new OmBuilderXStreamImpl();
70
71     private PortletApplicationEntityListImpl applications;
72
73     private boolean needRefresh = true;
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#getPortletApplicationEntityList()
79      */

80     public PortletApplicationEntityList getPortletApplicationEntityList() {
81         if (needRefresh) {
82             try {
83                 load();
84             } catch (IOException JavaDoc e) {
85                 LOG.error("Failed to load PortletEntityRegistry", e);
86             }
87         }
88         return applications;
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#getPortletEntity(org.apache.pluto.om.common.ObjectID)
95      */

96     public PortletEntity getPortletEntity(ObjectID id) {
97         if (needRefresh) {
98             try {
99                 load();
100             } catch (IOException JavaDoc e) {
101                 LOG.error("Failed to load PortletEntityRegistry", e);
102                 return null;
103             }
104         }
105
106         String JavaDoc 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     /*
132      * (non-Javadoc)
133      *
134      * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#store()
135      */

136     public void store() throws IOException JavaDoc {
137         String JavaDoc 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 JavaDoc is = new ByteArrayInputStream JavaDoc(serial);
148
149             DigitalAsset da = getPortletRegistry();
150             if (da == null) {
151                 LOG.info("Creating new " + PORTLET_REGISTRY_CONTENT_NAME);
152
153                 String JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc e) {
177             LOG.error("Failed to store PortletEntityRegistry", e);
178         }
179     }
180
181     /*
182      * (non-Javadoc)
183      *
184      * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#load()
185      */

186     public void load() throws IOException JavaDoc {
187         LOG.warn("Loading PortletEntityRegistry...");
188         try {
189             DigitalAsset da = getPortletRegistry();
190             if (da == null) {
191                 applications = new PortletApplicationEntityListImpl();
192             } else {
193                 InputStream JavaDoc 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 JavaDoc e) {
202             LOG.error("Failed to load PortletEntityRegistry", e);
203         }
204     }
205
206     /*
207      * (non-Javadoc)
208      *
209      * @see org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistryService#refresh(org.apache.pluto.om.entity.PortletEntity)
210      */

211     public void refresh(PortletEntity entity) {
212         // TODO Auto-generated method stub
213
}
214
215     public String JavaDoc toString() {
216         return builder.toXML(getPortletApplicationEntityList());
217     }
218
219     private DigitalAsset getPortletRegistry() throws Exception JavaDoc {
220         List JavaDoc 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     /*
232      * (non-Javadoc)
233      *
234      * @see org.apache.pluto.portalImpl.services.Service#init(javax.servlet.ServletConfig,
235      * org.apache.pluto.portalImpl.util.Properties)
236      */

237     protected void init(ServletConfig JavaDoc conf, Properties props) throws Exception JavaDoc {
238         LOG.debug("Calling init()");
239         super.init(conf, props);
240         needRefresh = true;
241     }
242
243     /*
244      * (non-Javadoc)
245      *
246      * @see org.apache.pluto.portalImpl.services.Service#postInit(javax.servlet.ServletConfig)
247      */

248     protected void postInit(ServletConfig JavaDoc conf) throws Exception JavaDoc {
249         LOG.debug("Calling postInit()");
250         super.postInit(conf);
251         needRefresh = true;
252     }
253 }
Popular Tags