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