KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > managementtool > actions > UploadPortletAction


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 package org.infoglue.cms.applications.managementtool.actions;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import org.apache.commons.httpclient.HttpClient;
35 import org.apache.commons.httpclient.HttpMethod;
36 import org.apache.commons.httpclient.StatusLine;
37 import org.apache.commons.httpclient.methods.GetMethod;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.pluto.om.common.Preference;
41 import org.apache.pluto.om.common.PreferenceSet;
42 import org.apache.pluto.om.entity.PortletApplicationEntityList;
43 import org.apache.pluto.om.portlet.PortletApplicationDefinition;
44 import org.apache.pluto.om.portlet.PortletDefinition;
45 import org.apache.pluto.om.portlet.PortletDefinitionList;
46 import org.apache.pluto.portalImpl.services.portletentityregistry.PortletEntityRegistry;
47 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
48 import org.infoglue.cms.controllers.kernel.impl.simple.PortletAssetController;
49 import org.infoglue.cms.entities.content.DigitalAsset;
50 import org.infoglue.cms.entities.content.DigitalAssetVO;
51 import org.infoglue.cms.exception.SystemException;
52 import org.infoglue.cms.util.CmsPropertyHandler;
53 import org.infoglue.cms.util.NotificationMessage;
54 import org.infoglue.cms.util.RemoteCacheUpdater;
55 import org.infoglue.deliver.portal.deploy.Deploy;
56 import org.infoglue.deliver.portal.om.PortletApplicationEntityImpl;
57 import org.infoglue.deliver.portal.om.PortletApplicationEntityListImpl;
58 import org.infoglue.deliver.portal.om.PortletEntityImpl;
59 import org.infoglue.deliver.portal.om.PreferenceImpl;
60 import org.infoglue.deliver.portal.om.PreferenceSetImpl;
61
62 import webwork.action.ActionContext;
63 import webwork.multipart.MultiPartRequestWrapper;
64
65 /**
66  * Upload a portlet-war. The war-file is stored as a digital asset.
67  *
68  * @author jand
69  *
70  */

71 public class UploadPortletAction extends InfoGlueAbstractAction
72 {
73     private static final Log log = LogFactory.getLog(UploadPortletAction.class);
74
75     private static final String JavaDoc PORTLET_DEPLOY_PREFIX = "portlet.deploy";
76
77     private DigitalAsset digitalAsset;
78
79     public String JavaDoc doExecute() //throws Exception
80
{
81         try
82         {
83             MultiPartRequestWrapper mpr = ActionContext.getMultiPartRequest();
84             if (mpr == null)
85             {
86                 return "input";
87             }
88
89             log.debug("Handling upload...");
90             Enumeration JavaDoc names = mpr.getFileNames();
91             if (names.hasMoreElements())
92             {
93                 String JavaDoc name = (String JavaDoc) names.nextElement();
94                 log.debug("name:" + name);
95                 File JavaDoc uploadedFile = mpr.getFile(name);
96                 if (uploadedFile == null || uploadedFile.length() == 0)
97                 {
98                     log.error("No file found in multipart request");
99                     return "input";
100                 }
101
102                 String JavaDoc contentType = mpr.getContentType(name);
103                 String JavaDoc fileName = mpr.getFilesystemName(name);
104                 String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
105                 log.debug("fileName:" + fileName);
106                 
107                 // Pluto prepare portlet-war
108
String JavaDoc appName = fileName;
109                 int dot = appName.lastIndexOf(".");
110                 if (dot > 0)
111                 {
112                     appName = appName.substring(0, dot);
113                 }
114
115                 log.info("appName:" + appName);
116                 
117                 // Create file where Deployer will write updated
118
// (pluto-prepared) .war
119
File JavaDoc file = new File JavaDoc(uploadedFile.getParentFile(), "tmp" + System.currentTimeMillis());
120                 log.info("file:" + file.getAbsolutePath());
121                 PortletApplicationDefinition pad = Deploy.prepareArchive(uploadedFile, file, appName);
122
123                 // Extract portlet application information to be added to
124
// portlet entity registry
125
log.info("Adding portlet application to registry: " + appName);
126                 PortletApplicationEntityImpl pae = new PortletApplicationEntityImpl();
127                 pae.setId(appName);
128
129                 PortletDefinitionList pdl = pad.getPortletDefinitionList();
130                 for (Iterator JavaDoc it = pdl.iterator(); it.hasNext();)
131                 {
132                     PortletDefinition pd = (PortletDefinition) it.next();
133                     log.debug("Adding portlet: " + pd.getName());
134                     PortletEntityImpl pe = new PortletEntityImpl();
135                     pe.setId(pd.getName());
136
137                     // Copy preferences
138
ArrayList JavaDoc destPrefs = new ArrayList JavaDoc();
139                     PreferenceSet prefSet = pd.getPreferenceSet();
140                     for (Iterator JavaDoc prefs = prefSet.iterator(); prefs.hasNext();)
141                     {
142                         Preference src = (Preference) prefs.next();
143                         ArrayList JavaDoc destValues = new ArrayList JavaDoc();
144                         for (Iterator JavaDoc values = src.getValues(); values.hasNext();)
145                         {
146                             destValues.add(values.next());
147                         }
148                         destPrefs.add(new PreferenceImpl(src.getName(), destValues));
149                     }
150                     pe.setPreferenceSet(new PreferenceSetImpl(destPrefs));
151                     pae.addPortletEntity(pe);
152                 }
153
154                 // Create Digital Asset
155
log.debug("Creating Digital Asset...");
156                 DigitalAssetVO newAsset = new DigitalAssetVO();
157                 newAsset.setAssetContentType(contentType);
158                 newAsset.setAssetKey(fileName);
159                 newAsset.setAssetFileName(fileName);
160                 newAsset.setAssetFilePath(filePath);
161                 newAsset.setAssetFileSize(new Integer JavaDoc(new Long JavaDoc(file.length()).intValue()));
162
163                 // Check existance of portlet and remove old ones
164
List JavaDoc assets = PortletAssetController.getDigitalAssetByName(fileName);
165                 if (assets != null && assets.size() > 0)
166                 {
167                     log.info("Removing old instance of " + fileName);
168                     for (Iterator JavaDoc it = assets.iterator(); it.hasNext();)
169                     {
170                         DigitalAsset oldAsset = (DigitalAsset) it.next();
171                         PortletAssetController.delete(oldAsset.getId());
172                     }
173                 }
174
175                 log.info("Storing Digital Asset (portlet) " + fileName);
176                 InputStream JavaDoc is = new FileInputStream JavaDoc(file);
177                 digitalAsset = PortletAssetController.create(newAsset, is);
178                 is.close();
179                 log.debug("Digital Asset stored as id=" + digitalAsset.getId());
180
181                 // Cleanup
182
uploadedFile.delete();
183                 file.delete();
184
185                 // Add the new application to portlet registry
186
// Update persisted portlet registry
187
// TODO check existance first?
188
PortletApplicationEntityList pael = PortletEntityRegistry.getPortletApplicationEntityList();
189                 if (pael instanceof PortletApplicationEntityListImpl)
190                 {
191                     ((PortletApplicationEntityListImpl) pael).add(pae);
192                     log.debug("Portlet application successfully added to registry");
193                 } else
194                 {
195                     log.error("Unknown implementation of PortletApplicationEntityList, "
196                             + "cannot add portlet application!");
197                     return "error";
198                 }
199                 PortletEntityRegistry.store();
200
201                 // Refresh deliver-engines
202
updateDeliverEngines(digitalAsset.getId());
203             }
204             else
205             {
206                 throw new SystemException("No file was uploaded...");
207             }
208         }
209         catch (Throwable JavaDoc e)
210         {
211             log.error("ERROR", e);
212             return "error";
213         }
214
215         return "success";
216     }
217
218     /**
219      * Report to deliver engines that a portlet has been uploaded
220      *
221      * @param contentId
222      * contentId of portlet
223      */

224     private void updateDeliverEngines(Integer JavaDoc digitalAssetId)
225     {
226         List JavaDoc allUrls = CmsPropertyHandler.getInternalDeliveryUrls();
227         allUrls.addAll(CmsPropertyHandler.getPublicDeliveryUrls());
228         
229         Iterator JavaDoc urlIterator = allUrls.iterator();
230         while(urlIterator.hasNext())
231         {
232             String JavaDoc url = (String JavaDoc)urlIterator.next() + "/DeployPortlet.action";
233             
234             try
235             {
236                 HttpClient client = new HttpClient();
237     
238                 // establish a connection within 5 seconds
239
client.setConnectionTimeout(5000);
240     
241                 // set the default credentials
242
HttpMethod method = new GetMethod(url);
243                 method.setQueryString("digitalAssetId=" + digitalAssetId);
244                 method.setFollowRedirects(true);
245     
246                 // execute the method
247
client.executeMethod(method);
248                 StatusLine status = method.getStatusLine();
249                 if (status != null && status.getStatusCode() == 200) {
250                     log.info("Successfully deployed portlet at " + url);
251                 } else {
252                     log.warn("Failed to deploy portlet at " + url + ": " + status);
253                 }
254     
255                 //clean up the connection resources
256
method.releaseConnection();
257             }
258             catch(Exception JavaDoc e)
259             {
260                 e.printStackTrace();
261             }
262         }
263         
264         /*
265         Properties props = CmsPropertyHandler.getProperties();
266         for (Enumeration keys = props.keys(); keys.hasMoreElements();) {
267             String key = (String) keys.nextElement();
268             if (key.startsWith(PORTLET_DEPLOY_PREFIX)) {
269                 String url = props.getProperty(key);
270                 try {
271                     HttpClient client = new HttpClient();
272
273                     //establish a connection within 5 seconds
274                     client.setConnectionTimeout(5000);
275
276                     //set the default credentials
277                     HttpMethod method = new GetMethod(url);
278                     method.setQueryString("digitalAssetId=" + digitalAssetId);
279                     method.setFollowRedirects(true);
280
281                     //execute the method
282                     client.executeMethod(method);
283                     StatusLine status = method.getStatusLine();
284                     if (status != null && status.getStatusCode() == 200) {
285                         log.info("Successfully deployed portlet at " + url);
286                     } else {
287                         log.warn("Failed to deploy portlet at " + url + ": " + status);
288                     }
289
290                     //clean up the connection resources
291                     method.releaseConnection();
292                 } catch (Throwable e) {
293                     log.error(e.getMessage(), e);
294                 }
295             }
296         }
297         */

298
299     }
300
301     /**
302      * @return
303      */

304     public DigitalAsset getDigitalAsset() {
305         return digitalAsset;
306     }
307
308 }
Popular Tags