001
014
015 package com.liferay.portal.deploy.auto;
016
017 import aQute.lib.osgi.Constants;
018
019 import aQute.libg.header.OSGiHeader;
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.Map;
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
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 Manifest manifest = null;
084
085 try {
086 JarInputStream jarInputStream = new JarInputStream(
087 new FileInputStream(file));
088
089 manifest = jarInputStream.getManifest();
090 }
091 catch (IOException ioe) {
092 throw new AutoDeployException(ioe);
093 }
094
095 if (manifest == null) {
096 return false;
097 }
098
099 Attributes attributes = manifest.getMainAttributes();
100
101 String bundleSymbolicNameAttributeValue = attributes.getValue(
102 Constants.BUNDLE_SYMBOLICNAME);
103
104 Map<String, Map<String, String>> bundleSymbolicNameMap =
105 OSGiHeader.parseHeader(bundleSymbolicNameAttributeValue);
106
107 Set<String> bundleSymbolicNameSet = bundleSymbolicNameMap.keySet();
108
109 if (bundleSymbolicNameSet.isEmpty()) {
110 return false;
111 }
112
113 Iterator<String> bundleSymbolicNameIterator =
114 bundleSymbolicNameSet.iterator();
115
116 String bundleSymbolicName = bundleSymbolicNameIterator.next();
117
118 return Validator.isNotNull(bundleSymbolicName);
119 }
120
121 private static Log _log = LogFactoryUtil.getLog(
122 ModuleAutoDeployListener.class);
123
124 private AutoDeployer _autoDeployer;
125
126 }