com.github.dynamicextensionsalfresco.osgi.spring.WebApplicationSystemPackageFactoryBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alfresco-integration Show documentation
Show all versions of alfresco-integration Show documentation
Adds an OSGi container to alfresco repository supporting dynamic code reloading, classpath isolation and a bunch of other useful features
package com.github.dynamicextensionsalfresco.osgi.spring;
import com.github.dynamicextensionsalfresco.osgi.Configuration;
import com.github.dynamicextensionsalfresco.osgi.JavaPackageScanner;
import com.github.dynamicextensionsalfresco.osgi.PackageCacheMode;
import com.github.dynamicextensionsalfresco.osgi.SystemPackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Provides {@link SystemPackage}s by scanning the web application for Java packages.
*
* @author Laurens Fridael
*
*/
public class WebApplicationSystemPackageFactoryBean implements FactoryBean> {
private final Logger logger = LoggerFactory.getLogger(getClass());
/* Dependencies */
private ObjectFactory javaPackageScanner;
private Configuration configuration;
/* Main operations */
@Override
public boolean isSingleton() {
return true;
}
@SuppressWarnings("unchecked")
@Override
public Class extends Set> getObjectType() {
return (Class extends Set>) (Class>) Set.class;
}
@Override
public Set getObject() {
final long start = System.currentTimeMillis();
Set systemPackages = createSystemPackages();
final long finish = System.currentTimeMillis();
if (finish - start > 1500L) {
logger.info("System Package scanning took '{}' milliseconds. Possible improvement: enable System Package "
+ "cache by adding 'osgi.container.system-package-cache.mode:ENABLE' "
+ "to 'osgi-container.properties'", (finish - start));
}
return systemPackages;
}
/* Utility operations */
protected Set createSystemPackages() {
final JavaPackageScanner packageScanner = javaPackageScanner.getObject();
final boolean validCache = packageScanner.isCacheValid(configuration.getSystemPackageCache());
Set packages;
if (validCache) {
packages = getCachedPackages();
} else {
packages = Collections.emptySet();
}
final boolean cacheDoesNotExist = CollectionUtils.isEmpty(packages);
final PackageCacheMode packageCacheMode = configuration.getSystemPackageCacheMode();
if (!packageCacheMode.isReadFromCache() || cacheDoesNotExist || !validCache) {
packages = packageScanner.scanWebApplicationPackages();
}
if (packageCacheMode.isForceWriteToCache() || (cacheDoesNotExist && packageCacheMode.isWriteToCache())) {
writeCachedPackages(packages);
}
if (!packageCacheMode.isWriteToCache()) {
final File cacheFile = configuration.getSystemPackageCache();
if (cacheFile.isFile()) {
cacheFile.delete();
}
}
return packages;
}
private Set getCachedPackages() {
File packageCache = configuration.getSystemPackageCache();
if (packageCache.isFile()) {
try {
final LineNumberReader in = new LineNumberReader(new InputStreamReader(new FileInputStream(packageCache)));
try {
final Set systemPackages = new LinkedHashSet(4500, 0.1f);
for (String line; (line = in.readLine()) != null;) {
line = line.trim();
if (!line.isEmpty()) {
systemPackages.add(SystemPackage.fromString(line));
}
}
return systemPackages;
} catch (final IOException e) {
if (logger.isWarnEnabled()) {
logger.warn("Error reading cached system package configuration from repository: {}.",
e.getMessage());
}
} finally {
IOUtils.closeQuietly(in);
}
} catch (IOException e) {
logger.warn("Failed to open Java packages cache reader", e);
}
}
return null;
}
private void writeCachedPackages(final Set packages) {
final File packageCache = configuration.getSystemPackageCache();
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileOutputStream(packageCache));
for (final SystemPackage systemPackage : packages) {
writer.println(systemPackage.toString());
}
logger.debug("Wrote system package list to {}.", packageCache.getAbsolutePath());
} catch (FileNotFoundException e) {
logger.warn("Failed t");
} finally {
IOUtils.closeQuietly(writer);
}
}
/* Dependencies */
public void setJavaPackageScanner(final ObjectFactory javaPackageScanner) {
Assert.notNull(javaPackageScanner);
this.javaPackageScanner = javaPackageScanner;
}
/* Configuration */
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy