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.deploy;
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.util.NamedThreadFactory;
023    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
024    import com.liferay.portal.kernel.util.StreamUtil;
025    import com.liferay.portal.kernel.util.Time;
026    
027    import java.io.FileOutputStream;
028    import java.io.IOException;
029    import java.io.InputStream;
030    
031    import java.util.List;
032    import java.util.concurrent.Executors;
033    import java.util.concurrent.ScheduledExecutorService;
034    import java.util.concurrent.TimeUnit;
035    
036    /**
037     * <p>
038     * See https://issues.liferay.com/browse/LPS-39277.
039     * </p>
040     *
041     * @author Brian Wing Shun Chan
042     * @author Shuyang Zhou
043     */
044    public class RequiredPluginsUtil {
045    
046            public static synchronized void startCheckingRequiredPlugins() {
047                    if (_scheduledExecutorService != null) {
048                            return;
049                    }
050    
051                    _scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(
052                            new NamedThreadFactory(
053                                    RequiredPluginsUtil.class.getName(), Thread.NORM_PRIORITY,
054                                    null));
055    
056                    _scheduledExecutorService.scheduleWithFixedDelay(
057                            new Runnable() {
058    
059                                    @Override
060                                    public void run() {
061                                            checkRequiredPlugins();
062                                    }
063    
064                            },
065                            Time.MINUTE, Time.MINUTE, TimeUnit.MILLISECONDS);
066            }
067    
068            public static synchronized void stopCheckingRequiredPlugins() {
069                    _unschedule(true);
070            }
071    
072            protected synchronized static void checkRequiredPlugins() {
073                    List<String[]> levelsRequiredDeploymentContexts =
074                            DeployManagerUtil.getLevelsRequiredDeploymentContexts();
075                    List<String[]> levelsRequiredDeploymentWARFileNames =
076                            DeployManagerUtil.getLevelsRequiredDeploymentWARFileNames();
077    
078                    boolean deployed = false;
079    
080                    for (int i = 0; i < levelsRequiredDeploymentContexts.size(); i++) {
081                            String[] levelRequiredDeploymentContexts =
082                                    levelsRequiredDeploymentContexts.get(i);
083                            String[] levelRequiredDeploymentWARFileNames =
084                                    levelsRequiredDeploymentWARFileNames.get(i);
085    
086                            for (int j = 0; j < levelRequiredDeploymentContexts.length; j++) {
087                                    String levelRequiredDeploymentContext =
088                                            levelRequiredDeploymentContexts[j];
089    
090                                    if (DeployManagerUtil.isDeployed(
091                                                    levelRequiredDeploymentContext)) {
092    
093                                            continue;
094                                    }
095    
096                                    deployed = true;
097    
098                                    String levelRequiredDeploymentWARFileName =
099                                            levelRequiredDeploymentWARFileNames[j];
100    
101                                    if (_log.isDebugEnabled()) {
102                                            _log.debug(
103                                                    "Automatically deploying the required plugin " +
104                                                            levelRequiredDeploymentWARFileName);
105                                    }
106    
107                                    ClassLoader classLoader =
108                                            PortalClassLoaderUtil.getClassLoader();
109    
110                                    InputStream inputStream = classLoader.getResourceAsStream(
111                                            "com/liferay/portal/deploy/dependencies/plugins" +
112                                                    (i + 1) + "/" + levelRequiredDeploymentWARFileNames[j]);
113    
114                                    AutoDeployDir autoDeployDir = AutoDeployUtil.getDir(
115                                            AutoDeployDir.DEFAULT_NAME);
116    
117                                    if (autoDeployDir == null) {
118    
119                                            // This should never happen except during shutdown
120    
121                                            if (_log.isDebugEnabled()) {
122                                                    _log.debug(
123                                                            "The autodeploy directory " +
124                                                                    AutoDeployDir.DEFAULT_NAME + " is null");
125                                            }
126    
127                                            continue;
128                                    }
129    
130                                    try {
131                                            StreamUtil.transfer(
132                                                    inputStream,
133                                                    new FileOutputStream(
134                                                            autoDeployDir.getDeployDir() + "/" +
135                                                                    levelRequiredDeploymentWARFileNames[j]));
136                                    }
137                                    catch (IOException ioe) {
138                                            _log.error("Unable to write file", ioe);
139                                    }
140                            }
141                    }
142    
143                    if (!deployed) {
144    
145                            // If all the required plugins were already in place, then we can
146                            // safely unschedule our required plugins check to conserve
147                            // processing power. Removal of plugins would trigger an undeploy
148                            // event which would restart the scheduled required plugins check.
149    
150                            _unschedule(false);
151                    }
152            }
153    
154            private static void _unschedule(boolean awaitTermination) {
155                    if (_scheduledExecutorService != null) {
156                            _scheduledExecutorService.shutdownNow();
157    
158                            if (awaitTermination) {
159                                    try {
160                                            _scheduledExecutorService.awaitTermination(
161                                                    1, TimeUnit.MINUTES);
162                                    }
163                                    catch (InterruptedException ie) {
164                                            _log.error(ie, ie);
165                                    }
166                            }
167    
168                            _scheduledExecutorService = null;
169                    }
170            }
171    
172            private static final Log _log = LogFactoryUtil.getLog(
173                    RequiredPluginsUtil.class);
174    
175            private static ScheduledExecutorService _scheduledExecutorService;
176    
177    }