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