001    /**
002     * Copyright (c) 2000-2012 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            public boolean applyPatch(File patchFile) {
039                    File patchDirectory = getPatchDirectory();
040    
041                    if (patchDirectory == null) {
042                            return false;
043                    }
044    
045                    try {
046                            FileUtil.copyFile(
047                                    patchFile,
048                                    new File(
049                                            patchDirectory + StringPool.SLASH + patchFile.getName()));
050    
051                            return true;
052                    }
053                    catch (Exception e) {
054                            _log.error(
055                                    "Unable to copy " + patchFile.getAbsolutePath() + " to " +
056                                            patchDirectory.getAbsolutePath());
057    
058                            return false;
059                    }
060            }
061    
062            public String[] getFixedIssues() {
063                    if (_fixedIssueKeys != null) {
064                            return _fixedIssueKeys;
065                    }
066    
067                    Properties properties = getProperties();
068    
069                    _fixedIssueKeys = StringUtil.split(
070                            properties.getProperty(PROPERTY_FIXED_ISSUES));
071    
072                    return _fixedIssueKeys;
073            }
074    
075            public String[] getInstalledPatches() {
076                    if (_installedPatchNames != null) {
077                            return _installedPatchNames;
078                    }
079    
080                    Properties properties = getProperties();
081    
082                    _installedPatchNames = StringUtil.split(
083                            properties.getProperty(PROPERTY_INSTALLED_PATCHES));
084    
085                    return _installedPatchNames;
086            }
087    
088            public File getPatchDirectory() {
089                    if (_patchDirectory != null) {
090                            return _patchDirectory;
091                    }
092    
093                    Properties properties = getProperties();
094    
095                    String patchDirectoryName = properties.getProperty(
096                            PROPERTY_PATCH_DIRECTORY);
097    
098                    if (Validator.isNotNull(patchDirectoryName)) {
099                            _patchDirectory = new File(patchDirectoryName);
100    
101                            if (!_patchDirectory.exists()) {
102                                    _log.error("The patch directory does not exist");
103                            }
104                    }
105                    else {
106                            _log.error("The patch directory is not specified");
107                    }
108    
109                    return _patchDirectory;
110            }
111    
112            public Properties getProperties() {
113                    if (_properties != null) {
114                            return _properties;
115                    }
116    
117                    Properties properties = new Properties();
118    
119                    Class<?> clazz = getClass();
120    
121                    ClassLoader classLoader = clazz.getClassLoader();
122    
123                    InputStream inputStream = classLoader.getResourceAsStream(
124                            PATCHER_PROPERTIES);
125    
126                    if (inputStream == null) {
127                            if (_log.isDebugEnabled()) {
128                                    _log.debug("Unable to load " + PATCHER_PROPERTIES);
129                            }
130                    }
131                    else {
132                            try {
133                                    properties.load(inputStream);
134    
135                                    _configured = true;
136                            }
137                            catch (IOException ioe) {
138                                    _log.error(ioe, ioe);
139                            }
140                            finally {
141                                    StreamUtil.cleanUp(inputStream);
142                            }
143                    }
144    
145                    _properties = properties;
146    
147                    return _properties;
148            }
149    
150            public boolean isConfigured() {
151                    return _configured;
152            }
153    
154            private static Log _log = LogFactoryUtil.getLog(PatcherImpl.class);
155    
156            private static boolean _configured;
157            private static String[] _fixedIssueKeys;
158            private static String[] _installedPatchNames;
159            private static File _patchDirectory;
160            private static Properties _properties;
161    
162    }