001
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
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 }