001
014
015 package com.liferay.portal.deploy.auto;
016
017 import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
018 import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
019 import com.liferay.portal.kernel.util.Validator;
020 import com.liferay.portal.kernel.zip.ZipReader;
021 import com.liferay.portal.kernel.zip.ZipReaderFactoryUtil;
022 import com.liferay.portal.osgi.service.OSGiServiceUtil;
023
024 import java.io.File;
025 import java.io.FileInputStream;
026 import java.io.InputStream;
027
028 import java.net.URI;
029
030 import java.util.jar.Attributes;
031 import java.util.jar.Manifest;
032
033 import org.osgi.framework.Bundle;
034 import org.osgi.framework.BundleContext;
035 import org.osgi.framework.Constants;
036 import org.osgi.framework.launch.Framework;
037
038
041 public class OSGiAutoDeployListener implements AutoDeployListener {
042
043 public void deploy(File file, String context) throws AutoDeployException {
044 try {
045 doDeploy(file, context);
046 }
047 catch (Exception e) {
048 throw new AutoDeployException(e);
049 }
050 }
051
052 protected void cleanUp(ZipReader zipReader, InputStream inputStream) {
053 if (inputStream != null) {
054 try {
055 inputStream.close();
056 }
057 catch (Exception e) {
058 }
059
060 inputStream = null;
061 }
062
063 if (zipReader != null) {
064 try {
065 zipReader.close();
066 }
067 catch (Exception e) {
068 }
069 }
070 }
071
072 protected void doDeploy(File file, String context) throws Exception {
073 Framework framework = OSGiServiceUtil.getFramework();
074
075 if (framework == null) {
076 return;
077 }
078
079 String fileName = file.getName();
080
081 fileName = fileName.toLowerCase();
082
083 if (file.isDirectory() ||
084 (!fileName.endsWith(".jar") && !fileName.endsWith(".war"))) {
085
086 return;
087 }
088
089 ZipReader zipReader = null;
090
091 InputStream inputStream = null;
092
093 try {
094 zipReader = ZipReaderFactoryUtil.getZipReader(file);
095
096 inputStream = zipReader.getEntryAsInputStream(
097 "/META-INF/MANIFEST.MF");
098
099 if (inputStream == null) {
100 return;
101 }
102
103 Manifest manifest = new Manifest(inputStream);
104
105 Attributes attributes = manifest.getMainAttributes();
106
107 String bundleSymbolicName = attributes.getValue(
108 Constants.BUNDLE_SYMBOLICNAME);
109
110 if (Validator.isNotNull(bundleSymbolicName)) {
111 installBundle(framework, file, manifest);
112 }
113 }
114 finally {
115 cleanUp(zipReader, inputStream);
116 }
117 }
118
119 protected void installBundle(
120 Framework framework, File file, Manifest manifest)
121 throws Exception {
122
123 BundleContext bundleContext = framework.getBundleContext();
124
125 URI uri = file.toURI();
126
127 Bundle bundle = bundleContext.getBundle(uri.toString());
128
129 InputStream inputStream = new FileInputStream(file);
130
131 if (bundle != null) {
132 bundle.update(inputStream);
133 }
134 else {
135 bundle = bundleContext.installBundle(uri.toString(), inputStream);
136 }
137
138 bundle.start();
139 }
140
141 }