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