001    /**
002     * Copyright (c) 2000-present 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.extension;
016    
017    import com.liferay.portal.deploy.DeployUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.OSDetector;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.ServerDetector;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.tools.deploy.BaseDeployer;
030    import com.liferay.portal.util.PropsValues;
031    
032    import java.io.File;
033    import java.io.InputStream;
034    
035    import java.util.ArrayList;
036    import java.util.List;
037    
038    /**
039     * @author Shuyang Zhou
040     */
041    public class WebsphereDeploymentExtension implements DeploymentExtension {
042    
043            @Override
044            public void copyXmls(BaseDeployer baseDeployer, File srcFile)
045                    throws Exception {
046    
047                    baseDeployer.copyDependencyXml("ibm-web-ext.xmi", srcFile + "/WEB-INF");
048            }
049    
050            @Override
051            public String getServerId() {
052                    return ServerDetector.WEBSPHERE_ID;
053            }
054    
055            @Override
056            public void postDeploy(String destDir, String deployDir) throws Exception {
057                    if (Validator.isNull(
058                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_QUERY)) {
059    
060                            if (_log.isInfoEnabled()) {
061                                    StringBundler sb = new StringBundler();
062    
063                                    sb.append("Do not install the plugin with wsadmin since the ");
064                                    sb.append("property \"");
065                                    sb.append(
066                                            PropsKeys.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_QUERY);
067                                    sb.append("\"is not configured");
068    
069                                    _log.info(sb.toString());
070                            }
071    
072                            return;
073                    }
074    
075                    String wsadminContent = FileUtil.read(
076                            DeployUtil.getResourcePath("wsadmin.py"));
077    
078                    String adminAppListOptions =
079                            PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_LIST_OPTIONS;
080    
081                    if (Validator.isNotNull(adminAppListOptions)) {
082                            adminAppListOptions =
083                                    StringPool.APOSTROPHE + adminAppListOptions +
084                                            StringPool.APOSTROPHE;
085                    }
086    
087                    wsadminContent = StringUtil.replace(
088                            wsadminContent,
089                            new String[] {
090                                    "${auto.deploy.websphere.wsadmin.app.manager.install.options}",
091                                    "${auto.deploy.websphere.wsadmin.app.manager.list.options}",
092                                    "${auto.deploy.websphere.wsadmin.app.manager.query}",
093                                    "${auto.deploy.websphere.wsadmin.app.manager.update.options}"
094                            },
095                            new String[] {
096                                    PropsValues.
097                                            AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_INSTALL_OPTIONS,
098                                    adminAppListOptions,
099                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_QUERY,
100                                    PropsValues.
101                                            AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_MANAGER_UPDATE_OPTIONS
102                            });
103    
104                    String pluginServletContextName = deployDir.substring(
105                            0, deployDir.length() - 4);
106    
107                    String pluginApplicationName = pluginServletContextName;
108    
109                    if (Validator.isNotNull(
110                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_NAME_SUFFIX)) {
111    
112                            pluginApplicationName +=
113                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_APP_NAME_SUFFIX;
114                    }
115    
116                    wsadminContent = StringUtil.replace(
117                            wsadminContent,
118                            new String[] {
119                                    "${auto.deploy.dest.dir}",
120                                    "${auto.deploy.websphere.wsadmin.app.name}",
121                                    "${plugin.servlet.context.name}"
122                            },
123                            new String[] {
124                                    destDir, pluginApplicationName, pluginServletContextName
125                            });
126    
127                    String wsadminFileName = FileUtil.createTempFileName("py");
128    
129                    FileUtil.write(wsadminFileName, wsadminContent);
130    
131                    String webSphereHome = System.getenv("WAS_HOME");
132    
133                    List<String> commands = new ArrayList<>();
134    
135                    if (OSDetector.isWindows()) {
136                            commands.add(webSphereHome + "\\bin\\wsadmin.bat");
137                    }
138                    else {
139                            commands.add(webSphereHome + "/bin/wsadmin.sh");
140                    }
141    
142                    if (Validator.isNotNull(
143                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_PROPERTIES_FILE)) {
144    
145                            commands.add("-p");
146                            commands.add(
147                                    PropsValues.AUTO_DEPLOY_WEBSPHERE_WSADMIN_PROPERTIES_FILE);
148                    }
149    
150                    commands.add("-f");
151                    commands.add(wsadminFileName);
152    
153                    if (_log.isInfoEnabled()) {
154                            StringBundler sb = new StringBundler(commands.size() * 2 + 1);
155    
156                            sb.append("Installing plugin by executing");
157    
158                            for (String command : commands) {
159                                    sb.append(StringPool.SPACE);
160                                    sb.append(command);
161                            }
162    
163                            _log.info(sb.toString());
164                    }
165    
166                    ProcessBuilder processBuilder = new ProcessBuilder(commands);
167    
168                    processBuilder.redirectErrorStream(true);
169    
170                    Process process = processBuilder.start();
171    
172                    if (_log.isInfoEnabled()) {
173                            InputStream inputStream = process.getInputStream();
174    
175                            String output = StringUtil.read(inputStream);
176    
177                            for (String line : StringUtil.split(output, CharPool.NEW_LINE)) {
178                                    _log.info("Process output: " + line);
179                            }
180    
181                            try {
182                                    int exitValue = process.exitValue();
183    
184                                    if (exitValue == 0) {
185                                            _log.info(
186                                                    "Successfully executed command with an exit value of " +
187                                                            exitValue);
188                                    }
189                                    else {
190                                            _log.info(
191                                                    "Unsuccessfully executed command with an exit value " +
192                                                            "of " + exitValue);
193                                    }
194                            }
195                            catch (IllegalThreadStateException itse) {
196                                    _log.info("Process did not terminate");
197                            }
198                    }
199    
200                    FileUtil.delete(wsadminFileName);
201            }
202    
203            private static final Log _log = LogFactoryUtil.getLog(
204                    WebsphereDeploymentExtension.class);
205    
206    }