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