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.messaging;
016    
017    import com.liferay.portal.kernel.deploy.DeployManagerUtil;
018    import com.liferay.portal.kernel.deploy.auto.AutoDeployDir;
019    import com.liferay.portal.kernel.deploy.auto.AutoDeployUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.messaging.BaseMessageListener;
023    import com.liferay.portal.kernel.messaging.Message;
024    import com.liferay.portal.kernel.util.StreamUtil;
025    
026    import java.io.FileOutputStream;
027    import java.io.InputStream;
028    
029    import java.util.List;
030    
031    /**
032     * <p>
033     * See http://issues.liferay.com/browse/LPS-39277.
034     * </p>
035     *
036     * @author Brian Wing Shun Chan
037     */
038    public class RequiredPluginsMessageListener extends BaseMessageListener {
039    
040            @Override
041            protected void doReceive(Message message) throws Exception {
042                    if (_firstMessage) {
043    
044                            // Ignore the first message to give the portal time to fully load
045    
046                            _firstMessage = false;
047    
048                            return;
049                    }
050    
051                    List<String[]> levelsRequiredDeploymentContexts =
052                            DeployManagerUtil.getLevelsRequiredDeploymentContexts();
053                    List<String[]> levelsRequiredDeploymentWARFileNames =
054                            DeployManagerUtil.getLevelsRequiredDeploymentWARFileNames();
055    
056                    for (int i = 0; i < levelsRequiredDeploymentContexts.size(); i++) {
057                            String[] levelRequiredDeploymentContexts =
058                                    levelsRequiredDeploymentContexts.get(i);
059                            String[] levelRequiredDeploymentWARFileNames =
060                                    levelsRequiredDeploymentWARFileNames.get(i);
061    
062                            for (int j = 0; j < levelRequiredDeploymentContexts.length; j++) {
063                                    String levelRequiredDeploymentContext =
064                                            levelRequiredDeploymentContexts[j];
065    
066                                    if (DeployManagerUtil.isDeployed(
067                                                    levelRequiredDeploymentContext)) {
068    
069                                            continue;
070                                    }
071    
072                                    String levelRequiredDeploymentWARFileName =
073                                            levelRequiredDeploymentWARFileNames[j];
074    
075                                    if (_log.isDebugEnabled()) {
076                                            _log.debug(
077                                                    "Automatically deploying the required plugin " +
078                                                            levelRequiredDeploymentWARFileName);
079                                    }
080    
081                                    Class<?> clazz = getClass();
082    
083                                    ClassLoader classLoader = clazz.getClassLoader();
084    
085                                    InputStream inputStream = classLoader.getResourceAsStream(
086                                            "com/liferay/portal/deploy/dependencies/plugins" +
087                                                    (i + 1) + "/" + levelRequiredDeploymentWARFileNames[j]);
088    
089                                    AutoDeployDir autoDeployDir = AutoDeployUtil.getDir(
090                                            AutoDeployDir.DEFAULT_NAME);
091    
092                                    if (autoDeployDir == null) {
093    
094                                            // This should never happen except during shutdown
095    
096                                            if (_log.isDebugEnabled()) {
097                                                    _log.debug(
098                                                            "The autodeploy directory " +
099                                                                    AutoDeployDir.DEFAULT_NAME + " is null");
100                                            }
101    
102                                            continue;
103                                    }
104    
105                                    StreamUtil.transfer(
106                                            inputStream,
107                                            new FileOutputStream(
108                                                    autoDeployDir.getDeployDir() + "/" +
109                                                            levelRequiredDeploymentWARFileNames[j]));
110                            }
111                    }
112            }
113    
114            private static Log _log = LogFactoryUtil.getLog(
115                    RequiredPluginsMessageListener.class);
116    
117            private boolean _firstMessage = true;
118    
119    }