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.deploy.auto;
016    
017    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
018    import com.liferay.portal.kernel.deploy.auto.context.AutoDeploymentContext;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.messaging.DestinationNames;
022    import com.liferay.portal.kernel.messaging.Message;
023    import com.liferay.portal.kernel.messaging.MessageBusUtil;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.util.PrefsPropsUtil;
029    import com.liferay.portal.util.PropsValues;
030    
031    import java.io.File;
032    import java.io.IOException;
033    import java.io.InputStream;
034    
035    import java.util.ArrayList;
036    import java.util.Enumeration;
037    import java.util.List;
038    import java.util.zip.ZipEntry;
039    import java.util.zip.ZipFile;
040    
041    /**
042     * @author Ryan Park
043     */
044    public class LiferayPackageAutoDeployer implements AutoDeployer {
045    
046            public LiferayPackageAutoDeployer() {
047                    try {
048                            _baseDir = PrefsPropsUtil.getString(
049                                    PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
050                                    PropsValues.AUTO_DEPLOY_DEPLOY_DIR);
051                    }
052                    catch (Exception e) {
053                            _log.error(e, e);
054                    }
055            }
056    
057            public int autoDeploy(AutoDeploymentContext autoDeploymentContext)
058                    throws AutoDeployException {
059    
060                    ZipFile zipFile = null;
061    
062                    try {
063                            File file = autoDeploymentContext.getFile();
064    
065                            zipFile = new ZipFile(file);
066    
067                            List<String> fileNames = new ArrayList<String>(zipFile.size());
068                            String propertiesString = null;
069    
070                            Enumeration<? extends ZipEntry> enu = zipFile.entries();
071    
072                            while (enu.hasMoreElements()) {
073                                    ZipEntry zipEntry = enu.nextElement();
074    
075                                    String zipEntryFileName = zipEntry.getName();
076    
077                                    if (!zipEntryFileName.endsWith(".war") &&
078                                            !zipEntryFileName.endsWith(".xml") &&
079                                            !zipEntryFileName.endsWith(".zip") &&
080                                            !zipEntryFileName.equals(
081                                                    "liferay-marketplace.properties")) {
082    
083                                            continue;
084                                    }
085    
086                                    if (_log.isInfoEnabled()) {
087                                            _log.info(
088                                                    "Extracting " + zipEntryFileName + " from " +
089                                                            file.getName());
090                                    }
091    
092                                    InputStream inputStream = zipFile.getInputStream(zipEntry);
093    
094                                    if (zipEntryFileName.equals("liferay-marketplace.properties")) {
095                                            inputStream = zipFile.getInputStream(zipEntry);
096    
097                                            propertiesString = StringUtil.read(inputStream);
098                                    }
099                                    else {
100                                            fileNames.add(zipEntryFileName);
101    
102                                            FileUtil.write(
103                                                    _baseDir + StringPool.SLASH + zipEntryFileName,
104                                                    inputStream);
105                                    }
106                            }
107    
108                            if (propertiesString != null) {
109                                    Message message = new Message();
110    
111                                    message.put("command", "deploy");
112                                    message.put("fileNames", fileNames);
113                                    message.put("properties", propertiesString);
114    
115                                    MessageBusUtil.sendMessage(
116                                            DestinationNames.MARKETPLACE, message);
117                            }
118    
119                            return AutoDeployer.CODE_DEFAULT;
120                    }
121                    catch (Exception e) {
122                            throw new AutoDeployException(e);
123                    }
124                    finally {
125                            if (zipFile != null) {
126                                    try {
127                                            zipFile.close();
128                                    }
129                                    catch (IOException ioe) {
130                                    }
131                            }
132                    }
133            }
134    
135            public AutoDeployer cloneAutoDeployer() {
136                    return new LiferayPackageAutoDeployer();
137            }
138    
139            private static Log _log = LogFactoryUtil.getLog(
140                    LiferayPackageAutoDeployer.class);
141    
142            private String _baseDir;
143    
144    }