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                  if ((targetModuleIDs != null) && (targetModuleIDs.length > 0)) {
67                      for (TargetModuleID targetModuleID : targetModuleIDs) {
68                          if (!_warContext.equals(targetModuleID.getModuleID())) {
69                              continue;
70                          }
71  
72                          ProgressObject startProgress = _deploymentManager.start(
73                              new TargetModuleID[] {targetModuleID});
74  
75                          startProgress.addProgressListener(
76                              new StartProgressListener(_deploymentHandler));
77  
78                          _deploymentHandler.setError(false);
79                          _deploymentHandler.setStarted(true);
80  
81                          break;
82                      }
83                  }
84                  else {
85                      targetModuleIDs = _deploymentManager.getAvailableModules(
86                          ModuleType.WAR, _deploymentManager.getTargets());
87  
88                      for (TargetModuleID targetModuleID : targetModuleIDs) {
89                          if (!_warContext.equals(targetModuleID.getModuleID())) {
90                              continue;
91                          }
92  
93                          ProgressObject startProgress = _deploymentManager.start(
94                              new TargetModuleID[] {targetModuleID});
95  
96                          startProgress.addProgressListener(
97                              new StartProgressListener(_deploymentHandler));
98  
99                          _deploymentHandler.setError(false);
100                         _deploymentHandler.setStarted(true);
101 
102                         break;
103                     }
104                 }
105             }
106             catch (Exception e) {
107                 _log.error(e, e);
108 
109                 _deploymentHandler.setError(true);
110                 _deploymentHandler.setStarted(false);
111             }
112         }
113         else if (deploymentStatus.isFailed()) {
114             _deploymentHandler.setError(true);
115             _deploymentHandler.setStarted(false);
116         }
117     }
118 
119     private static Log _log =
120         LogFactoryUtil.getLog(DeploymentProgressListener.class);
121 
122     private DeploymentHandler _deploymentHandler;
123     private String _warContext;
124     private DeploymentManager _deploymentManager;
125 
126 }