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  public class DeploymentProgressListener implements ProgressListener {
43  
44      public DeploymentProgressListener(
45          DeploymentHandler deploymentHandler, String warContext) {
46  
47          _deploymentHandler = deploymentHandler;
48          _warContext = warContext;
49          _deploymentManager = _deploymentHandler.getDeploymentManager();
50      }
51  
52      public void handleProgressEvent(ProgressEvent progressEvent) {
53          DeploymentStatus deploymentStatus = progressEvent.getDeploymentStatus();
54  
55          if (_log.isInfoEnabled()) {
56              _log.info(deploymentStatus.getMessage());
57          }
58  
59          if (deploymentStatus.isCompleted()) {
60              try {
61                  TargetModuleID[] targetModuleIDs =
62                      _deploymentManager.getNonRunningModules(
63                          ModuleType.WAR, _deploymentManager.getTargets());
64  
65                  if ((targetModuleIDs != null) && (targetModuleIDs.length > 0)) {
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                  else {
84                      targetModuleIDs = _deploymentManager.getAvailableModules(
85                          ModuleType.WAR, _deploymentManager.getTargets());
86  
87                      for (TargetModuleID targetModuleID : targetModuleIDs) {
88                          if (!_warContext.equals(targetModuleID.getModuleID())) {
89                              continue;
90                          }
91  
92                          ProgressObject startProgress = _deploymentManager.start(
93                              new TargetModuleID[] {targetModuleID});
94  
95                          startProgress.addProgressListener(
96                              new StartProgressListener(_deploymentHandler));
97  
98                          _deploymentHandler.setError(false);
99                          _deploymentHandler.setStarted(true);
100 
101                         break;
102                     }
103                 }
104             }
105             catch (Exception e) {
106                 _log.error(e, e);
107 
108                 _deploymentHandler.setError(true);
109                 _deploymentHandler.setStarted(false);
110             }
111         }
112         else if (deploymentStatus.isFailed()) {
113             _deploymentHandler.setError(true);
114             _deploymentHandler.setStarted(false);
115         }
116     }
117 
118     private static Log _log =
119         LogFactoryUtil.getLog(DeploymentProgressListener.class);
120 
121     private DeploymentHandler _deploymentHandler;
122     private String _warContext;
123     private DeploymentManager _deploymentManager;
124 
125 }