001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }