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.patcher;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.patcher.Patcher;
020    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.StreamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    
027    import java.io.File;
028    import java.io.IOException;
029    import java.io.InputStream;
030    
031    import java.util.Properties;
032    
033    /**
034     * @author Zsolt Balogh
035     * @author Brian Wing Shun Chan
036     */
037    @DoPrivileged
038    public class PatcherImpl implements Patcher {
039    
040            @Override
041            public boolean applyPatch(File patchFile) {
042                    File patchDirectory = getPatchDirectory();
043    
044                    if (patchDirectory == null) {
045                            return false;
046                    }
047    
048                    try {
049                            FileUtil.copyFile(
050                                    patchFile,
051                                    new File(
052                                            patchDirectory + StringPool.SLASH + patchFile.getName()));
053    
054                            return true;
055                    }
056                    catch (Exception e) {
057                            _log.error(
058                                    "Unable to copy " + patchFile.getAbsolutePath() + " to " +
059                                            patchDirectory.getAbsolutePath());
060    
061                            return false;
062                    }
063            }
064    
065            @Override
066            public String[] getFixedIssues() {
067                    if (_fixedIssueKeys != null) {
068                            return _fixedIssueKeys;
069                    }
070    
071                    Properties properties = getProperties();
072    
073                    _fixedIssueKeys = StringUtil.split(
074                            properties.getProperty(PROPERTY_FIXED_ISSUES));
075    
076                    return _fixedIssueKeys;
077            }
078    
079            @Override
080            public String[] getInstalledPatches() {
081                    if (_installedPatchNames != null) {
082                            return _installedPatchNames;
083                    }
084    
085                    Properties properties = getProperties();
086    
087                    _installedPatchNames = StringUtil.split(
088                            properties.getProperty(PROPERTY_INSTALLED_PATCHES));
089    
090                    return _installedPatchNames;
091            }
092    
093            @Override
094            public File getPatchDirectory() {
095                    if (_patchDirectory != null) {
096                            return _patchDirectory;
097                    }
098    
099                    Properties properties = getProperties();
100    
101                    String patchDirectoryName = properties.getProperty(
102                            PROPERTY_PATCH_DIRECTORY);
103    
104                    if (Validator.isNotNull(patchDirectoryName)) {
105                            _patchDirectory = new File(patchDirectoryName);
106    
107                            if (!_patchDirectory.exists()) {
108                                    _log.error("The patch directory does not exist");
109                            }
110                    }
111                    else {
112                            _log.error("The patch directory is not specified");
113                    }
114    
115                    return _patchDirectory;
116            }
117    
118            @Override
119            public String[] getPatchLevels() {
120                    if (_patchLevels != null) {
121                            return _patchLevels;
122                    }
123    
124                    Properties properties = getProperties();
125    
126                    _patchLevels = StringUtil.split(
127                            properties.getProperty(PROPERTY_PATCH_LEVELS));
128    
129                    return _patchLevels;
130            }
131    
132            @Override
133            public Properties getProperties() {
134                    if (_properties != null) {
135                            return _properties;
136                    }
137    
138                    Properties properties = new Properties();
139    
140                    Class<?> clazz = getClass();
141    
142                    ClassLoader classLoader = clazz.getClassLoader();
143    
144                    InputStream inputStream = classLoader.getResourceAsStream(
145                            PATCHER_PROPERTIES);
146    
147                    if (inputStream == null) {
148                            if (_log.isDebugEnabled()) {
149                                    _log.debug("Unable to load " + PATCHER_PROPERTIES);
150                            }
151                    }
152                    else {
153                            try {
154                                    properties.load(inputStream);
155    
156                                    _configured = true;
157                            }
158                            catch (IOException ioe) {
159                                    _log.error(ioe, ioe);
160                            }
161                            finally {
162                                    StreamUtil.cleanUp(inputStream);
163                            }
164                    }
165    
166                    _properties = properties;
167    
168                    return _properties;
169            }
170    
171            @Override
172            public boolean isConfigured() {
173                    return _configured;
174            }
175    
176            private static Log _log = LogFactoryUtil.getLog(PatcherImpl.class);
177    
178            private static boolean _configured;
179            private static String[] _fixedIssueKeys;
180            private static String[] _installedPatchNames;
181            private static File _patchDirectory;
182            private static String[] _patchLevels;
183            private static Properties _properties;
184    
185    }