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