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.deploy.auto;
016    
017    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StreamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.util.PrefsPropsUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    import java.io.File;
028    import java.io.IOException;
029    import java.io.InputStream;
030    
031    import java.util.Enumeration;
032    import java.util.zip.ZipEntry;
033    import java.util.zip.ZipFile;
034    
035    /**
036     * @author Ryan Park
037     */
038    public class LiferayPackageAutoDeployer implements AutoDeployer {
039    
040            public LiferayPackageAutoDeployer() {
041                    try {
042                            baseDir = PrefsPropsUtil.getString(
043                                    PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
044                                    PropsValues.AUTO_DEPLOY_DEPLOY_DIR);
045                    }
046                    catch (Exception e) {
047                            _log.error(e);
048                    }
049            }
050    
051            public void autoDeploy(File file, String context)
052                    throws AutoDeployException {
053    
054                    ZipFile zipFile = null;
055    
056                    try {
057                            zipFile = new ZipFile(file);
058    
059                            Enumeration<? extends ZipEntry> enu = zipFile.entries();
060    
061                            while (enu.hasMoreElements()) {
062                                    ZipEntry zipEntry = enu.nextElement();
063    
064                                    String zipEntryFileName = zipEntry.getName();
065    
066                                    if (!zipEntryFileName.endsWith(".war") &&
067                                            !zipEntryFileName.endsWith(".xml") &&
068                                            !zipEntryFileName.endsWith(".zip")) {
069    
070                                            continue;
071                                    }
072    
073                                    if (_log.isInfoEnabled()) {
074                                            _log.info(
075                                                    "Extracting " + zipEntryFileName + " from " +
076                                                            file.getName());
077                                    }
078    
079                                    InputStream inputStream = null;
080    
081                                    try {
082                                            inputStream = zipFile.getInputStream(zipEntry);
083    
084                                            FileUtil.write(
085                                                    baseDir + StringPool.SLASH + zipEntryFileName,
086                                                    inputStream);
087                                    }
088                                    finally {
089                                            StreamUtil.cleanUp(inputStream);
090                                    }
091                            }
092                    }
093                    catch (Exception e) {
094                            throw new AutoDeployException(e);
095                    }
096                    finally {
097                            if (zipFile != null) {
098                                    try {
099                                            zipFile.close();
100                                    }
101                                    catch (IOException ioe) {
102                                    }
103                            }
104                    }
105            }
106    
107            protected String baseDir;
108    
109            private static Log _log = LogFactoryUtil.getLog(
110                    LiferayPackageAutoDeployer.class);
111    
112    }