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.deploy;
016    
017    import com.liferay.portal.events.GlobalStartupAction;
018    import com.liferay.portal.kernel.deploy.DeployManager;
019    import com.liferay.portal.kernel.deploy.auto.AutoDeployDir;
020    import com.liferay.portal.kernel.deploy.auto.context.AutoDeploymentContext;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.plugin.PluginPackage;
024    import com.liferay.portal.kernel.plugin.RequiredPluginPackageException;
025    import com.liferay.portal.kernel.plugin.Version;
026    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
027    import com.liferay.portal.kernel.util.ArrayUtil;
028    import com.liferay.portal.kernel.util.ReleaseInfo;
029    import com.liferay.portal.kernel.util.ServerDetector;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.StringUtil;
033    import com.liferay.portal.plugin.PluginPackageUtil;
034    
035    import java.io.File;
036    import java.io.InputStream;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    import java.util.Properties;
041    
042    /**
043     * @author Jonathan Potter
044     * @author Brian Wing Shun Chan
045     * @author Ryan Park
046     */
047    @DoPrivileged
048    public class DeployManagerImpl implements DeployManager {
049    
050            public DeployManagerImpl() {
051                    for (int i = 1; i < 9; i++) {
052                            String levelRequiredDeploymentWARFileNamesString = StringPool.BLANK;
053    
054                            try {
055                                    Class<?> clazz = getClass();
056    
057                                    InputStream inputStream = clazz.getResourceAsStream(
058                                            "dependencies/plugins" + i + "/wars.txt");
059    
060                                    if (inputStream == null) {
061                                            return;
062                                    }
063    
064                                    levelRequiredDeploymentWARFileNamesString = StringUtil.read(
065                                            inputStream);
066                            }
067                            catch (Exception e) {
068                                    _log.error(e, e);
069                            }
070    
071                            if (_log.isDebugEnabled()) {
072                                    _log.debug(
073                                            "Level " + i + " required deployment WAR file names " +
074                                                    levelRequiredDeploymentWARFileNamesString);
075                            }
076    
077                            String[] levelRequiredDeploymentWARFileNames = StringUtil.split(
078                                    levelRequiredDeploymentWARFileNamesString);
079    
080                            _levelsRequiredDeploymentWARFileNames.add(
081                                    levelRequiredDeploymentWARFileNames);
082    
083                            String[] levelRequiredDeploymentContexts =
084                                    new String[levelRequiredDeploymentWARFileNames.length];
085    
086                            _levelsRequiredDeploymentContexts.add(
087                                    levelRequiredDeploymentContexts);
088    
089                            for (int j = 0; j < levelRequiredDeploymentWARFileNames.length;
090                                            j++) {
091    
092                                    String warFileName = levelRequiredDeploymentWARFileNames[j];
093    
094                                    Version version = Version.getInstance(ReleaseInfo.getVersion());
095    
096                                    StringBundler sb = new StringBundler(4);
097    
098                                    sb.append(StringPool.DASH);
099                                    sb.append(version.getMajor());
100                                    sb.append(StringPool.PERIOD);
101                                    sb.append(version.getMinor());
102    
103                                    int index = warFileName.indexOf(sb.toString());
104    
105                                    levelRequiredDeploymentContexts[j] = warFileName.substring(
106                                            0, index);
107                            }
108    
109                            if (_log.isDebugEnabled()) {
110                                    _log.debug(
111                                            "Level " + i + " required deployment contexts " +
112                                                    StringUtil.merge(levelRequiredDeploymentContexts));
113                            }
114                    }
115            }
116    
117            @Override
118            public void deploy(AutoDeploymentContext autoDeploymentContext)
119                    throws Exception {
120    
121                    AutoDeployDir.deploy(
122                            autoDeploymentContext,
123                            GlobalStartupAction.getAutoDeployListeners(false));
124            }
125    
126            @Override
127            public String getDeployDir() throws Exception {
128                    return DeployUtil.getAutoDeployDestDir();
129            }
130    
131            @Override
132            public String getInstalledDir() throws Exception {
133                    if (ServerDetector.isGlassfish()) {
134                            File file = new File(
135                                    System.getProperty("com.sun.aas.instanceRoot"), "autodeploy");
136    
137                            return file.getAbsolutePath();
138                    }
139    
140                    return DeployUtil.getAutoDeployDestDir();
141            }
142    
143            @Override
144            public PluginPackage getInstalledPluginPackage(String context) {
145                    return PluginPackageUtil.getInstalledPluginPackage(context);
146            }
147    
148            @Override
149            public List<PluginPackage> getInstalledPluginPackages() {
150                    return PluginPackageUtil.getInstalledPluginPackages();
151            }
152    
153            @Override
154            public List<String[]> getLevelsRequiredDeploymentContexts() {
155                    return _levelsRequiredDeploymentContexts;
156            }
157    
158            @Override
159            public List<String[]> getLevelsRequiredDeploymentWARFileNames() {
160                    return _levelsRequiredDeploymentWARFileNames;
161            }
162    
163            @Override
164            public boolean isDeployed(String context) {
165                    return PluginPackageUtil.isInstalled(context);
166            }
167    
168            @Override
169            public boolean isRequiredDeploymentContext(String context) {
170                    for (String[] levelsRequiredDeploymentContexts :
171                                    _levelsRequiredDeploymentContexts) {
172    
173                            if (ArrayUtil.contains(levelsRequiredDeploymentContexts, context)) {
174                                    return true;
175                            }
176                    }
177    
178                    return false;
179            }
180    
181            @Override
182            public PluginPackage readPluginPackageProperties(
183                    String displayName, Properties properties) {
184    
185                    return PluginPackageUtil.readPluginPackageProperties(
186                            displayName, properties);
187            }
188    
189            @Override
190            public PluginPackage readPluginPackageXml(String xml) throws Exception {
191                    return PluginPackageUtil.readPluginPackageXml(xml);
192            }
193    
194            @Override
195            public void redeploy(String context) throws Exception {
196                    if (ServerDetector.isJetty()) {
197                            DeployUtil.redeployJetty(context);
198                    }
199                    else if (ServerDetector.isTomcat()) {
200                            DeployUtil.redeployTomcat(context);
201                    }
202            }
203    
204            @Override
205            public void undeploy(String context) throws Exception {
206                    if (isRequiredDeploymentContext(context)) {
207                            RequiredPluginPackageException rppe =
208                                    new RequiredPluginPackageException();
209    
210                            rppe.setContext(context);
211    
212                            throw rppe;
213                    }
214    
215                    File deployDir = new File(getDeployDir(), context);
216    
217                    DeployUtil.undeploy(ServerDetector.getServerId(), deployDir);
218            }
219    
220            private static Log _log = LogFactoryUtil.getLog(DeployManagerImpl.class);
221    
222            private List<String[]> _levelsRequiredDeploymentContexts =
223                    new ArrayList<String[]>();
224            private List<String[]> _levelsRequiredDeploymentWARFileNames =
225                    new ArrayList<String[]>();
226    
227    }