1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.rpad.portlet.deployer.impl;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.Calendar;
29
30 import javax.faces.context.FacesContext;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.jetspeed.CommonPortletServices;
35 import org.apache.jetspeed.deployment.DeploymentException;
36 import org.apache.jetspeed.deployment.DeploymentManager;
37 import org.apache.jetspeed.deployment.DeploymentStatus;
38 import org.apache.jetspeed.portlets.rpad.PortletApplication;
39 import org.apache.jetspeed.portlets.rpad.portlet.deployer.PortletDeployer;
40 import org.apache.jetspeed.portlets.rpad.portlet.util.FacesMessageUtil;
41
42 public class JetspeedPortletDeployer implements PortletDeployer
43 {
44 /***
45 * Logger for this class
46 */
47 private static final Log log = LogFactory
48 .getLog(JetspeedPortletDeployer.class);
49
50 private int status;
51
52 private long startTime = 0;
53
54 public JetspeedPortletDeployer()
55 {
56 status = READY;
57 }
58
59 public int getStatus()
60 {
61 return status;
62 }
63
64 synchronized public void deploy(PortletApplication portlet)
65 {
66 if (status != READY)
67 {
68
69
70
71 FacesMessageUtil
72 .addWarnMessage("Other deployment process is running.");
73 return;
74 }
75 DeployerThread deployer = new DeployerThread();
76 deployer.setDeploymentManager((DeploymentManager) FacesContext
77 .getCurrentInstance().getExternalContext().getApplicationMap()
78 .get(CommonPortletServices.CPS_DEPLOYMENT_MANAGER_COMPONENT));
79 deployer.setPortletApplication(portlet);
80 try
81 {
82 deployer.start();
83
84 FacesMessageUtil.addInfoMessage("Started a deployment process.");
85 }
86 catch (Exception e)
87 {
88
89 FacesMessageUtil
90 .addErrorMessage("Could not start deployment process.");
91 log.error("Could not start deployment process.", e);
92 }
93 }
94
95 public class DeployerThread extends Thread
96 {
97 private DeploymentManager deploymentManager;
98
99 private PortletApplication portletApplication;
100
101
102
103
104 public void run()
105 {
106 status = DEPLOYING;
107 try
108 {
109 startTime = Calendar.getInstance().getTimeInMillis();
110 if (getDeploymentManager() != null)
111 {
112 String binaryUrl = portletApplication.getBinaryUrl();
113 if (binaryUrl != null && !binaryUrl.equals(""))
114 {
115 File targetFile = null;
116 try
117 {
118 File tempFile = File.createTempFile("rpad_", "."
119 + portletApplication.getPackaging());
120 FileOutputStream out = new FileOutputStream(
121 tempFile);
122 drain(getInputStream(portletApplication
123 .getBinaryUrl()), out);
124 try
125 {
126 targetFile = new File(tempFile.getParentFile(),
127 portletApplication.getArtifactId()
128 + "."
129 + portletApplication
130 .getPackaging());
131 tempFile.renameTo(targetFile);
132 }
133 catch (Exception e)
134 {
135 targetFile = tempFile;
136 }
137 if (getDeploymentManager().deploy(targetFile)
138 .getStatus() == DeploymentStatus.STATUS_OKAY)
139 {
140 log.info(portletApplication.getName()
141 + " was deployed.");
142 }
143 else
144 {
145 log.error("Could not deploy "
146 + portletApplication.getName());
147 }
148 }
149 catch (FileNotFoundException e)
150 {
151 log.error(e);
152 }
153 catch (IOException e)
154 {
155 log.error(e);
156 }
157 catch (DeploymentException e)
158 {
159 log.error(e);
160 }
161 if (targetFile != null && targetFile.exists())
162 {
163 targetFile.delete();
164 }
165 }
166 else
167 {
168 log.error("The target url is invalid. The path is "
169 + binaryUrl);
170 }
171 }
172 else
173 {
174 log.error("Could not find the deployment manager.");
175 }
176 }
177 catch (Exception e)
178 {
179 log.error("Unexpected exception.", e);
180 }
181 finally
182 {
183 status = READY;
184 }
185 }
186
187 /***
188 * @return the portletApplication
189 */
190 public PortletApplication getPortletApplication()
191 {
192 return portletApplication;
193 }
194
195 /***
196 * @param portletApplication the portletApplication to set
197 */
198 public void setPortletApplication(PortletApplication portletApplication)
199 {
200 this.portletApplication = portletApplication;
201 }
202
203 /***
204 * @return the startTime
205 */
206 public long getStartTime()
207 {
208 return startTime;
209 }
210
211 /***
212 * @return the deploymentManager
213 */
214 public DeploymentManager getDeploymentManager()
215 {
216 return deploymentManager;
217 }
218
219 /***
220 * @param deploymentManager the deploymentManager to set
221 */
222 public void setDeploymentManager(DeploymentManager deploymentManager)
223 {
224 this.deploymentManager = deploymentManager;
225 }
226
227 }
228
229 protected void drain(InputStream in, OutputStream out) throws IOException
230 {
231 try
232 {
233 byte[] buf = new byte[8192];
234 int len = in.read(buf);
235
236 while (len != -1)
237 {
238 out.write(buf, 0, len);
239 len = in.read(buf);
240 }
241 out.flush();
242 }
243 catch (IOException e)
244 {
245 throw e;
246 }
247 finally
248 {
249 try
250 {
251 out.close();
252 }
253 catch (IOException e)
254 {
255 }
256 try
257 {
258 in.close();
259 }
260 catch (IOException e)
261 {
262 }
263 }
264 }
265
266 protected InputStream getInputStream(String path)
267 {
268 if (path.startsWith("http:") || path.startsWith("https:"))
269 {
270 try
271 {
272 URL url = new URL(path);
273 return url.openStream();
274 }
275 catch (MalformedURLException e)
276 {
277 log.error("Wrong url: " + path, e);
278 }
279 catch (IOException e)
280 {
281 log.error("Could not load " + path, e);
282 }
283 }
284 else if (path.startsWith("file:"))
285 {
286 try
287 {
288 return new FileInputStream(new File(path.substring(5)));
289 }
290 catch (FileNotFoundException e)
291 {
292 log.error("Could not load " + path, e);
293 }
294 }
295 return null;
296 }
297
298 }