001    /**
002     * Copyright (c) 2000-2010 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.kernel.util.PortalClassLoaderUtil;
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 = PortalClassLoaderUtil.getClassLoader();
041    
042                            DeploymentFactoryManager deploymentFactoryManager =
043                                    DeploymentFactoryManager.getInstance();
044    
045                            DeploymentFactory deploymentFactory =
046                                    (DeploymentFactory)classLoader.loadClass(
047                                            dfClassName).newInstance();
048    
049                            deploymentFactoryManager.registerDeploymentFactory(
050                                    deploymentFactory);
051    
052                            _deploymentManager = deploymentFactoryManager.getDeploymentManager(
053                                    dmId, dmUser, dmPassword);
054                    }
055                    catch (Exception e) {
056                            _log.error(e, e);
057                    }
058            }
059    
060            public DeploymentManager getDeploymentManager() {
061                    return _deploymentManager;
062            }
063    
064            public void deploy(File warDir, String warContext) throws Exception {
065                    setStarted(false);
066    
067                    ProgressObject deployProgress = null;
068    
069                    TargetModuleID[] targetModuleIDs =
070                            _deploymentManager.getAvailableModules(
071                                    ModuleType.WAR, _deploymentManager.getTargets());
072    
073                    for (TargetModuleID targetModuleID : targetModuleIDs) {
074                            if (!targetModuleID.getModuleID().equals(warContext)) {
075                                    continue;
076                            }
077    
078                            deployProgress = _deploymentManager.redeploy(
079                                    new TargetModuleID[] {targetModuleID}, warDir, null);
080    
081                            break;
082                    }
083    
084                    if (deployProgress == null) {
085                            deployProgress = _deploymentManager.distribute(
086                                    _deploymentManager.getTargets(), warDir, null);
087                    }
088    
089                    deployProgress.addProgressListener(
090                            new DeploymentProgressListener(this, warContext));
091    
092                    waitForStart(warContext);
093    
094                    if (_error) {
095                            throw new Exception("Failed to deploy " + warDir);
096                    }
097            }
098    
099            public void releaseDeploymentManager() {
100                    _deploymentManager.release();
101            }
102    
103            public synchronized void setError(boolean error) {
104                    _error = error;
105            }
106    
107            public synchronized void setStarted(boolean started) {
108                    _started = started;
109    
110                    notifyAll();
111            }
112    
113            protected synchronized void waitForStart(String warContext)
114                    throws Exception {
115    
116                    while (!_error && !_started) {
117                            wait();
118                    }
119    
120                    if (_error) {
121                            return;
122                    }
123            }
124    
125            private static Log _log = LogFactoryUtil.getLog(DeploymentHandler.class);
126    
127            private DeploymentManager _deploymentManager;
128            private boolean _error;
129            private boolean _started;
130    
131    }