001    /**
002     * Copyright (c) 2000-present 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.util.StreamUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    
027    import java.io.File;
028    import java.io.FileInputStream;
029    import java.io.IOException;
030    
031    import java.util.Iterator;
032    import java.util.Set;
033    import java.util.jar.Attributes;
034    import java.util.jar.JarInputStream;
035    import java.util.jar.Manifest;
036    
037    /**
038     * @author Miguel Pastor
039     * @author Manuel de la Pe??a
040     */
041    public class ModuleAutoDeployListener extends BaseAutoDeployListener {
042    
043            @Override
044            protected AutoDeployer buildAutoDeployer() {
045                    return new ThreadSafeAutoDeployer(new ModuleAutoDeployer());
046            }
047    
048            @Override
049            protected String getPluginPathInfoMessage(File file) {
050                    return "Copied module for " + file.getPath();
051            }
052    
053            @Override
054            protected String getSuccessMessage(File file) {
055                    return "Module for " + file.getPath() + " copied successfully";
056            }
057    
058            @Override
059            protected boolean isDeployable(File file) throws AutoDeployException {
060                    return isModule(file);
061            }
062    
063            protected boolean isModule(File file) throws AutoDeployException {
064                    PluginAutoDeployListenerHelper pluginAutoDeployListenerHelper =
065                            new PluginAutoDeployListenerHelper(file);
066    
067                    if (!pluginAutoDeployListenerHelper.isJarFile()) {
068                            return false;
069                    }
070    
071                    JarInputStream jarInputStream = null;
072    
073                    Manifest manifest = null;
074    
075                    try {
076                            jarInputStream = new JarInputStream(new FileInputStream(file));
077    
078                            manifest = jarInputStream.getManifest();
079                    }
080                    catch (IOException ioe) {
081                            throw new AutoDeployException(ioe);
082                    }
083                    finally {
084                            StreamUtil.cleanUp(jarInputStream);
085                    }
086    
087                    if (manifest == null) {
088                            return false;
089                    }
090    
091                    Attributes attributes = manifest.getMainAttributes();
092    
093                    String bundleSymbolicNameAttributeValue = attributes.getValue(
094                            Constants.BUNDLE_SYMBOLICNAME);
095    
096                    Parameters bundleSymbolicNameMap = OSGiHeader.parseHeader(
097                            bundleSymbolicNameAttributeValue);
098    
099                    Set<String> bundleSymbolicNameSet = bundleSymbolicNameMap.keySet();
100    
101                    if (bundleSymbolicNameSet.isEmpty()) {
102                            return false;
103                    }
104    
105                    Iterator<String> bundleSymbolicNameIterator =
106                            bundleSymbolicNameSet.iterator();
107    
108                    String bundleSymbolicName = bundleSymbolicNameIterator.next();
109    
110                    return Validator.isNotNull(bundleSymbolicName);
111            }
112    
113    }