1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools.deploy;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
20  
21  import java.io.File;
22  
23  import javax.enterprise.deploy.shared.ModuleType;
24  import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
25  import javax.enterprise.deploy.spi.DeploymentManager;
26  import javax.enterprise.deploy.spi.TargetModuleID;
27  import javax.enterprise.deploy.spi.factories.DeploymentFactory;
28  import javax.enterprise.deploy.spi.status.ProgressObject;
29  
30  /**
31   * <a href="DeploymentHandler.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Sandeep Soni
34   * @author Brian Wing Shun Chan
35   */
36  public class DeploymentHandler {
37  
38      public DeploymentHandler(
39          String dmId, String dmUser, String dmPassword, String dfClassName) {
40  
41          try {
42              ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();
43  
44              DeploymentFactoryManager deploymentFactoryManager =
45                  DeploymentFactoryManager.getInstance();
46  
47              DeploymentFactory deploymentFactory =
48                  (DeploymentFactory)classLoader.loadClass(
49                      dfClassName).newInstance();
50  
51              deploymentFactoryManager.registerDeploymentFactory(
52                  deploymentFactory);
53  
54              _deploymentManager = deploymentFactoryManager.getDeploymentManager(
55                  dmId, dmUser, dmPassword);
56          }
57          catch (Exception e) {
58              _log.error(e, e);
59          }
60      }
61  
62      public DeploymentManager getDeploymentManager() {
63          return _deploymentManager;
64      }
65  
66      public void deploy(File warDir, String warContext) throws Exception {
67          setStarted(false);
68  
69          ProgressObject deployProgress = null;
70  
71          TargetModuleID[] targetModuleIDs =
72              _deploymentManager.getAvailableModules(
73                  ModuleType.WAR, _deploymentManager.getTargets());
74  
75          for (TargetModuleID targetModuleID : targetModuleIDs) {
76              if (!targetModuleID.getModuleID().equals(warContext)) {
77                  continue;
78              }
79  
80              deployProgress = _deploymentManager.redeploy(
81                  new TargetModuleID[] {targetModuleID}, warDir, null);
82  
83              break;
84          }
85  
86          if (deployProgress == null) {
87              deployProgress = _deploymentManager.distribute(
88                  _deploymentManager.getTargets(), warDir, null);
89          }
90  
91          deployProgress.addProgressListener(
92              new DeploymentProgressListener(this, warContext));
93  
94          waitForStart(warContext);
95  
96          if (_error) {
97              throw new Exception("Failed to deploy " + warDir);
98          }
99      }
100 
101     public void releaseDeploymentManager() {
102         _deploymentManager.release();
103     }
104 
105     public synchronized void setError(boolean error) {
106         _error = error;
107     }
108 
109     public synchronized void setStarted(boolean started) {
110         _started = started;
111 
112         notifyAll();
113     }
114 
115     protected synchronized void waitForStart(String warContext)
116         throws Exception {
117 
118         while (!_error && !_started) {
119             wait();
120         }
121 
122         if (_error) {
123             return;
124         }
125     }
126 
127     private static Log _log = LogFactoryUtil.getLog(DeploymentHandler.class);
128 
129     private DeploymentManager _deploymentManager;
130     private boolean _error;
131     private boolean _started;
132 
133 }