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 aQute.bnd.header.OSGiHeader;
018    import aQute.bnd.header.Parameters;
019    import aQute.bnd.osgi.Constants;
020    
021    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
022    import com.liferay.portal.kernel.deploy.auto.AutoDeployer;
023    import com.liferay.portal.kernel.deploy.auto.BaseAutoDeployListener;
024    import com.liferay.portal.kernel.deploy.auto.context.AutoDeploymentContext;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.util.StreamUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    
030    import java.io.File;
031    import java.io.FileInputStream;
032    import java.io.IOException;
033    
034    import java.util.Iterator;
035    import java.util.Set;
036    import java.util.jar.Attributes;
037    import java.util.jar.JarInputStream;
038    import java.util.jar.Manifest;
039    
040    /**
041     * @author Miguel Pastor
042     */
043    public class ModuleAutoDeployListener extends BaseAutoDeployListener {
044    
045            public ModuleAutoDeployListener() {
046                    _autoDeployer = new ThreadSafeAutoDeployer(new ModuleAutoDeployer());
047            }
048    
049            @Override
050            public int deploy(AutoDeploymentContext autoDeploymentContext)
051                    throws AutoDeployException {
052    
053                    File file = autoDeploymentContext.getFile();
054    
055                    if (_log.isDebugEnabled()) {
056                            _log.debug("Invoking deploy for " + file.getPath());
057                    }
058    
059                    if (!isModule(file)) {
060                            return AutoDeployer.CODE_NOT_APPLICABLE;
061                    }
062    
063                    if (_log.isInfoEnabled()) {
064                            _log.info("Copied module for " + file.getPath());
065                    }
066    
067                    int code = _autoDeployer.autoDeploy(autoDeploymentContext);
068    
069                    if ((code == AutoDeployer.CODE_DEFAULT) && _log.isInfoEnabled()) {
070                            _log.info(
071                                    "Module for " + file.getPath() + " copied successfully. " +
072                                            "Deployment will start in a few seconds.");
073                    }
074    
075                    return code;
076            }
077    
078            protected boolean isModule(File file) throws AutoDeployException {
079                    if (!isJarFile(file)) {
080                            return false;
081                    }
082    
083                    JarInputStream jarInputStream = null;
084    
085                    Manifest manifest = null;
086    
087                    try {
088                            jarInputStream = new JarInputStream(new FileInputStream(file));
089    
090                            manifest = jarInputStream.getManifest();
091                    }
092                    catch (IOException ioe) {
093                            throw new AutoDeployException(ioe);
094                    }
095                    finally {
096                            StreamUtil.cleanUp(jarInputStream);
097                    }
098    
099                    if (manifest == null) {
100                            return false;
101                    }
102    
103                    Attributes attributes = manifest.getMainAttributes();
104    
105                    String bundleSymbolicNameAttributeValue = attributes.getValue(
106                            Constants.BUNDLE_SYMBOLICNAME);
107    
108                    Parameters bundleSymbolicNameMap = OSGiHeader.parseHeader(
109                            bundleSymbolicNameAttributeValue);
110    
111                    Set<String> bundleSymbolicNameSet = bundleSymbolicNameMap.keySet();
112    
113                    if (bundleSymbolicNameSet.isEmpty()) {
114                            return false;
115                    }
116    
117                    Iterator<String> bundleSymbolicNameIterator =
118                            bundleSymbolicNameSet.iterator();
119    
120                    String bundleSymbolicName = bundleSymbolicNameIterator.next();
121    
122                    return Validator.isNotNull(bundleSymbolicName);
123            }
124    
125            private static Log _log = LogFactoryUtil.getLog(
126                    ModuleAutoDeployListener.class);
127    
128            private AutoDeployer _autoDeployer;
129    
130    }