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