1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools.deploy;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import javax.enterprise.deploy.shared.ModuleType;
29  import javax.enterprise.deploy.spi.DeploymentManager;
30  import javax.enterprise.deploy.spi.TargetModuleID;
31  import javax.enterprise.deploy.spi.status.DeploymentStatus;
32  import javax.enterprise.deploy.spi.status.ProgressEvent;
33  import javax.enterprise.deploy.spi.status.ProgressListener;
34  import javax.enterprise.deploy.spi.status.ProgressObject;
35  
36  /**
37   * <a href="DeploymentProgressListener.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Sandeep Soni
41   *
42   */
43  public class DeploymentProgressListener implements ProgressListener {
44  
45      public DeploymentProgressListener(
46          DeploymentHandler deploymentHandler, String warContext) {
47  
48          _deploymentHandler = deploymentHandler;
49          _warContext = warContext;
50          _deploymentManager = _deploymentHandler.getDeploymentManager();
51      }
52  
53      public void handleProgressEvent(ProgressEvent progressEvent) {
54          DeploymentStatus deploymentStatus = progressEvent.getDeploymentStatus();
55  
56          if (_log.isInfoEnabled()) {
57              _log.info(deploymentStatus.getMessage());
58          }
59  
60          if (deploymentStatus.isCompleted()) {
61              try {
62                  TargetModuleID[] targetModuleIDs =
63                      _deploymentManager.getNonRunningModules(
64                          ModuleType.WAR, _deploymentManager.getTargets());
65  
66                  for (TargetModuleID targetModuleID : targetModuleIDs) {
67                      if (!_warContext.equals(targetModuleID.getModuleID())) {
68                          continue;
69                      }
70  
71                      ProgressObject startProgress = _deploymentManager.start(
72                          new TargetModuleID[] {targetModuleID});
73  
74                      startProgress.addProgressListener(
75                          new StartProgressListener(_deploymentHandler));
76  
77                      _deploymentHandler.setError(false);
78                      _deploymentHandler.setStarted(true);
79  
80                      break;
81                  }
82              }
83              catch (Exception e) {
84                  _log.error(e, e);
85  
86                  _deploymentHandler.setError(true);
87                  _deploymentHandler.setStarted(false);
88              }
89          }
90          else if (deploymentStatus.isFailed()) {
91              _deploymentHandler.setError(true);
92              _deploymentHandler.setStarted(false);
93          }
94      }
95  
96      private static Log _log =
97          LogFactoryUtil.getLog(DeploymentProgressListener.class);
98  
99      private DeploymentHandler _deploymentHandler;
100     private String _warContext;
101     private DeploymentManager _deploymentManager;
102 
103 }