001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.rpad.portlet.deployer.impl;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.Calendar;
029:
030: import javax.faces.context.FacesContext;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.jetspeed.CommonPortletServices;
035: import org.apache.jetspeed.deployment.DeploymentException;
036: import org.apache.jetspeed.deployment.DeploymentManager;
037: import org.apache.jetspeed.deployment.DeploymentStatus;
038: import org.apache.jetspeed.portlets.rpad.PortletApplication;
039: import org.apache.jetspeed.portlets.rpad.portlet.deployer.PortletDeployer;
040: import org.apache.jetspeed.portlets.rpad.portlet.util.FacesMessageUtil;
041:
042: public class JetspeedPortletDeployer implements PortletDeployer {
043: /**
044: * Logger for this class
045: */
046: private static final Log log = LogFactory
047: .getLog(JetspeedPortletDeployer.class);
048:
049: private int status;
050:
051: private long startTime = 0;
052:
053: public JetspeedPortletDeployer() {
054: status = READY;
055: }
056:
057: public int getStatus() {
058: return status;
059: }
060:
061: synchronized public void deploy(PortletApplication portlet) {
062: if (status != READY) {
063: //TODO check timeout
064:
065: //TODO i18n
066: FacesMessageUtil
067: .addWarnMessage("Other deployment process is running.");
068: return;
069: }
070: DeployerThread deployer = new DeployerThread();
071: deployer
072: .setDeploymentManager((DeploymentManager) FacesContext
073: .getCurrentInstance()
074: .getExternalContext()
075: .getApplicationMap()
076: .get(
077: CommonPortletServices.CPS_DEPLOYMENT_MANAGER_COMPONENT));
078: deployer.setPortletApplication(portlet);
079: try {
080: deployer.start();
081: //TODO i18n
082: FacesMessageUtil
083: .addInfoMessage("Started a deployment process.");
084: } catch (Exception e) {
085: //TODO i18n
086: FacesMessageUtil
087: .addErrorMessage("Could not start deployment process.");
088: log.error("Could not start deployment process.", e);
089: }
090: }
091:
092: public class DeployerThread extends Thread {
093: private DeploymentManager deploymentManager;
094:
095: private PortletApplication portletApplication;
096:
097: /* (non-Javadoc)
098: * @see java.lang.Thread#run()
099: */
100: public void run() {
101: status = DEPLOYING;
102: try {
103: startTime = Calendar.getInstance().getTimeInMillis();
104: if (getDeploymentManager() != null) {
105: String binaryUrl = portletApplication
106: .getBinaryUrl();
107: if (binaryUrl != null && !binaryUrl.equals("")) {
108: File targetFile = null;
109: try {
110: File tempFile = File.createTempFile(
111: "rpad_", "."
112: + portletApplication
113: .getPackaging());
114: FileOutputStream out = new FileOutputStream(
115: tempFile);
116: drain(getInputStream(portletApplication
117: .getBinaryUrl()), out);
118: try {
119: targetFile = new File(tempFile
120: .getParentFile(),
121: portletApplication
122: .getArtifactId()
123: + "."
124: + portletApplication
125: .getPackaging());
126: tempFile.renameTo(targetFile);
127: } catch (Exception e) {
128: targetFile = tempFile;
129: }
130: if (getDeploymentManager().deploy(
131: targetFile).getStatus() == DeploymentStatus.STATUS_OKAY) {
132: log.info(portletApplication.getName()
133: + " was deployed.");
134: } else {
135: log.error("Could not deploy "
136: + portletApplication.getName());
137: }
138: } catch (FileNotFoundException e) {
139: log.error(e);
140: } catch (IOException e) {
141: log.error(e);
142: } catch (DeploymentException e) {
143: log.error(e);
144: }
145: if (targetFile != null && targetFile.exists()) {
146: targetFile.delete();
147: }
148: } else {
149: log
150: .error("The target url is invalid. The path is "
151: + binaryUrl);
152: }
153: } else {
154: log.error("Could not find the deployment manager.");
155: }
156: } catch (Exception e) {
157: log.error("Unexpected exception.", e);
158: } finally {
159: status = READY;
160: }
161: }
162:
163: /**
164: * @return the portletApplication
165: */
166: public PortletApplication getPortletApplication() {
167: return portletApplication;
168: }
169:
170: /**
171: * @param portletApplication the portletApplication to set
172: */
173: public void setPortletApplication(
174: PortletApplication portletApplication) {
175: this .portletApplication = portletApplication;
176: }
177:
178: /**
179: * @return the startTime
180: */
181: public long getStartTime() {
182: return startTime;
183: }
184:
185: /**
186: * @return the deploymentManager
187: */
188: public DeploymentManager getDeploymentManager() {
189: return deploymentManager;
190: }
191:
192: /**
193: * @param deploymentManager the deploymentManager to set
194: */
195: public void setDeploymentManager(
196: DeploymentManager deploymentManager) {
197: this .deploymentManager = deploymentManager;
198: }
199:
200: }
201:
202: protected void drain(InputStream in, OutputStream out)
203: throws IOException {
204: try {
205: byte[] buf = new byte[8192];
206: int len = in.read(buf);
207:
208: while (len != -1) {
209: out.write(buf, 0, len);
210: len = in.read(buf);
211: }
212: out.flush();
213: } catch (IOException e) {
214: throw e;
215: } finally {
216: try {
217: out.close();
218: } catch (IOException e) {
219: }
220: try {
221: in.close();
222: } catch (IOException e) {
223: }
224: }
225: }
226:
227: protected InputStream getInputStream(String path) {
228: if (path.startsWith("http:") || path.startsWith("https:")) {
229: try {
230: URL url = new URL(path);
231: return url.openStream();
232: } catch (MalformedURLException e) {
233: log.error("Wrong url: " + path, e);
234: } catch (IOException e) {
235: log.error("Could not load " + path, e);
236: }
237: } else if (path.startsWith("file:")) {
238: try {
239: return new FileInputStream(new File(path.substring(5)));
240: } catch (FileNotFoundException e) {
241: log.error("Could not load " + path, e);
242: }
243: }
244: return null;
245: }
246:
247: }
|