001    /**
002     * Copyright (c) 2000-present 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                    String baseDir = null;
049    
050                    try {
051                            baseDir = PrefsPropsUtil.getString(
052                                    PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
053                                    PropsValues.AUTO_DEPLOY_DEPLOY_DIR);
054                    }
055                    catch (Exception e) {
056                            _log.error(e, e);
057                    }
058    
059                    _baseDir = baseDir;
060            }
061    
062            @Override
063            public int autoDeploy(AutoDeploymentContext autoDeploymentContext)
064                    throws AutoDeployException {
065    
066                    ZipFile zipFile = null;
067    
068                    try {
069                            File file = autoDeploymentContext.getFile();
070    
071                            zipFile = new ZipFile(file);
072    
073                            List<String> fileNames = new ArrayList<>(zipFile.size());
074                            String propertiesString = null;
075    
076                            Enumeration<? extends ZipEntry> enu = zipFile.entries();
077    
078                            while (enu.hasMoreElements()) {
079                                    ZipEntry zipEntry = enu.nextElement();
080    
081                                    String zipEntryFileName = zipEntry.getName();
082    
083                                    if (_log.isInfoEnabled()) {
084                                            _log.info(
085                                                    "Extracting " + zipEntryFileName + " from " +
086                                                            file.getName());
087                                    }
088    
089                                    InputStream inputStream = zipFile.getInputStream(zipEntry);
090    
091                                    if (zipEntryFileName.equals("liferay-marketplace.properties")) {
092                                            inputStream = zipFile.getInputStream(zipEntry);
093    
094                                            propertiesString = StringUtil.read(inputStream);
095                                    }
096                                    else {
097                                            fileNames.add(zipEntryFileName);
098    
099                                            FileUtil.write(
100                                                    _baseDir + StringPool.SLASH + zipEntryFileName,
101                                                    inputStream);
102                                    }
103                            }
104    
105                            if (propertiesString != null) {
106                                    Message message = new Message();
107    
108                                    message.put("command", "deploy");
109                                    message.put("fileNames", fileNames);
110                                    message.put("properties", propertiesString);
111    
112                                    MessageBusUtil.sendMessage(
113                                            DestinationNames.MARKETPLACE, message);
114                            }
115    
116                            return AutoDeployer.CODE_DEFAULT;
117                    }
118                    catch (Exception e) {
119                            throw new AutoDeployException(e);
120                    }
121                    finally {
122                            if (zipFile != null) {
123                                    try {
124                                            zipFile.close();
125                                    }
126                                    catch (IOException ioe) {
127                                    }
128                            }
129                    }
130            }
131    
132            @Override
133            public AutoDeployer cloneAutoDeployer() {
134                    return new LiferayPackageAutoDeployer();
135            }
136    
137            private static final Log _log = LogFactoryUtil.getLog(
138                    LiferayPackageAutoDeployer.class);
139    
140            private final String _baseDir;
141    
142    }