All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dynamicextensionsalfresco.osgi.spring.SystemPackageConfigurationFactoryBean Maven / Gradle / Ivy

Go to download

Adds an OSGi container to alfresco repository supporting dynamic code reloading, classpath isolation and a bunch of other useful features

There is a newer version: 3.1.0
Show newest version
package com.github.dynamicextensionsalfresco.osgi.spring;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.LinkedHashSet;
import java.util.Set;

import com.github.dynamicextensionsalfresco.osgi.SystemPackage;
import com.github.dynamicextensionsalfresco.osgi.SystemPackageEditor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

/**
 * {@link FactoryBean} for creating a List of {@link SystemPackage}s from a text file.
 * 
 * @author Laurens Fridael
 * 
 */
public class SystemPackageConfigurationFactoryBean extends AbstractConfigurationFileFactoryBean> {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	/* Configuration */

	private String defaultVersion;

	/* State */

	private Set systemPackages;

	/* Operations */

	@SuppressWarnings("unchecked")
	@Override
	public Class> getObjectType() {
		return (Class>) (Class) Set.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	@Override
	public Set getObject() throws IOException {
		if (systemPackages == null) {
			systemPackages = createSystemPackages();
		}
		return systemPackages;
	}

	/* Utility operations */

	protected Set createSystemPackages() throws IOException {
		final Set systemPackages = new LinkedHashSet();
		final SystemPackageEditor systemPackageEditor = new SystemPackageEditor();
		systemPackageEditor.setDefaultVersion(getDefaultVersion());
		for (final Resource configuration : resolveConfigurations()) {
			LineNumberReader in = null;
			try {
				in = new LineNumberReader(new InputStreamReader(configuration.getInputStream()));
				for (String line; (line = in.readLine()) != null;) {
					line = line.trim();
					if (line.isEmpty() || line.startsWith("#")) {
						// Skip empty lines and comments.
						continue;
					}
					try {
						systemPackageEditor.setAsText(line);
						final SystemPackage systemPackage = (SystemPackage) systemPackageEditor.getValue();
						systemPackages.add(systemPackage);
					} catch (final IllegalArgumentException e) {
						logger.warn("Could not parse SystemPackage configuration line: {}", e.getMessage());
					}
				}
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (final IOException ignore) {}
				}
			}
		}
		return systemPackages;
	}

	/* Configuration */

	public void setDefaultVersion(final String defaultVersion) {
		Assert.hasText(defaultVersion);
		this.defaultVersion = defaultVersion;
	}

	public String getDefaultVersion() {
		return defaultVersion;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy