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