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 (_log.isInfoEnabled()) {
080                                            _log.info(
081                                                    "Extracting " + zipEntryFileName + " from " +
082                                                            file.getName());
083                                    }
084    
085                                    InputStream inputStream = zipFile.getInputStream(zipEntry);
086    
087                                    if (zipEntryFileName.equals("liferay-marketplace.properties")) {
088                                            inputStream = zipFile.getInputStream(zipEntry);
089    
090                                            propertiesString = StringUtil.read(inputStream);
091                                    }
092                                    else {
093                                            fileNames.add(zipEntryFileName);
094    
095                                            FileUtil.write(
096                                                    _baseDir + StringPool.SLASH + zipEntryFileName,
097                                                    inputStream);
098                                    }
099                            }
100    
101                            if (propertiesString != null) {
102                                    Message message = new Message();
103    
104                                    message.put("command", "deploy");
105                                    message.put("fileNames", fileNames);
106                                    message.put("properties", propertiesString);
107    
108                                    MessageBusUtil.sendMessage(
109                                            DestinationNames.MARKETPLACE, message);
110                            }
111    
112                            return AutoDeployer.CODE_DEFAULT;
113                    }
114                    catch (Exception e) {
115                            throw new AutoDeployException(e);
116                    }
117                    finally {
118                            if (zipFile != null) {
119                                    try {
120                                            zipFile.close();
121                                    }
122                                    catch (IOException ioe) {
123                                    }
124                            }
125                    }
126            }
127    
128            @Override
129            public AutoDeployer cloneAutoDeployer() {
130                    return new LiferayPackageAutoDeployer();
131            }
132    
133            private static Log _log = LogFactoryUtil.getLog(
134                    LiferayPackageAutoDeployer.class);
135    
136            private String _baseDir;
137    
138    }