001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.deploy;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.ClassLoaderUtil;
020    
021    import java.io.File;
022    
023    import javax.enterprise.deploy.shared.ModuleType;
024    import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
025    import javax.enterprise.deploy.spi.DeploymentManager;
026    import javax.enterprise.deploy.spi.TargetModuleID;
027    import javax.enterprise.deploy.spi.factories.DeploymentFactory;
028    import javax.enterprise.deploy.spi.status.ProgressObject;
029    
030    /**
031     * @author Sandeep Soni
032     * @author Brian Wing Shun Chan
033     */
034    public class DeploymentHandler {
035    
036            public DeploymentHandler(
037                    String dmId, String dmUser, String dmPassword, String dfClassName) {
038    
039                    try {
040                            ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
041    
042                            DeploymentFactoryManager deploymentFactoryManager =
043                                    DeploymentFactoryManager.getInstance();
044    
045                            Class<?> clazz = classLoader.loadClass(dfClassName);
046    
047                            DeploymentFactory deploymentFactory =
048                                    (DeploymentFactory)clazz.newInstance();
049    
050                            deploymentFactoryManager.registerDeploymentFactory(
051                                    deploymentFactory);
052    
053                            _deploymentManager = deploymentFactoryManager.getDeploymentManager(
054                                    dmId, dmUser, dmPassword);
055                    }
056                    catch (Exception e) {
057                            _log.error(e, e);
058                    }
059            }
060    
061            public void deploy(File warDir, String warContext) throws Exception {
062                    setStarted(false);
063    
064                    ProgressObject deployProgress = null;
065    
066                    TargetModuleID[] targetModuleIDs =
067                            _deploymentManager.getAvailableModules(
068                                    ModuleType.WAR, _deploymentManager.getTargets());
069    
070                    for (TargetModuleID targetModuleID : targetModuleIDs) {
071                            if (!targetModuleID.getModuleID().equals(warContext)) {
072                                    continue;
073                            }
074    
075                            deployProgress = _deploymentManager.redeploy(
076                                    new TargetModuleID[] {targetModuleID}, warDir, null);
077    
078                            break;
079                    }
080    
081                    if (deployProgress == null) {
082                            deployProgress = _deploymentManager.distribute(
083                                    _deploymentManager.getTargets(), warDir, null);
084                    }
085    
086                    deployProgress.addProgressListener(
087                            new DeploymentProgressListener(this, warContext));
088    
089                    waitForStart(warContext);
090    
091                    if (_error) {
092                            throw new Exception("Failed to deploy " + warDir);
093                    }
094            }
095    
096            public DeploymentManager getDeploymentManager() {
097                    return _deploymentManager;
098            }
099    
100            public void releaseDeploymentManager() {
101                    _deploymentManager.release();
102            }
103    
104            public synchronized void setError(boolean error) {
105                    _error = error;
106            }
107    
108            public synchronized void setStarted(boolean started) {
109                    _started = started;
110    
111                    notifyAll();
112            }
113    
114            protected synchronized void waitForStart(String warContext)
115                    throws Exception {
116    
117                    while (!_error && !_started) {
118                            wait();
119                    }
120    
121                    if (_error) {
122                            return;
123                    }
124            }
125    
126            private static Log _log = LogFactoryUtil.getLog(DeploymentHandler.class);
127    
128            private DeploymentManager _deploymentManager;
129            private boolean _error;
130            private boolean _started;
131    
132    }