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

org.daisy.pipeline.pipeline1.impl.Pipeline1ScriptProvider_SPI Maven / Gradle / Ivy

The newest version!
/***************************************************************/
/* This class was generated by ds-to-spi-annotations-processor */
/***************************************************************/

package org.daisy.pipeline.pipeline1.impl;

public class Pipeline1ScriptProvider_SPI
       implements org.daisy.common.spi.ServiceWithProperties
                , org.daisy.pipeline.script.ScriptServiceProvider
{
	
	private static final org.slf4j.Logger spi_log = org.slf4j.LoggerFactory.getLogger(Pipeline1ScriptProvider_SPI.class);
	
	private final java.util.Map spi_props;
	private boolean spi_deactivated = false;
	
	private final Object delegate;

	public Pipeline1ScriptProvider_SPI() {
		try {
			// FIXME: use same classloader if multiple components have the same @LoadWith
			ClassLoader classLoader = (ClassLoader)Class.forName("org.daisy.pipeline.pipeline1.impl.Pipeline1ClassLoader").newInstance();
			delegate = classLoader.loadClass("org.daisy.pipeline.pipeline1.impl.Pipeline1ScriptProvider").newInstance();
		} catch (ClassNotFoundException |
		         InstantiationException |
		         IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		spi_log.trace("Creating Pipeline1ScriptProvider");
		spi_props = new java.util.HashMap();
		spi_props.put("component.name", "Pipeline1ScriptProvider");
		{
			spi_log.trace("Binding org.daisy.pipeline.datatypes.DatatypeRegistry services...");
			// cardinality: 1..1
			int minRef = 1;
			int maxRef = 1;
			int refCount = 0;
			try {
				java.util.Iterator services
					= org.daisy.common.spi.ServiceLoader.load(org.daisy.pipeline.datatypes.DatatypeRegistry.class).iterator();
				while (services.hasNext()) {
					try {
						org.daisy.pipeline.datatypes.DatatypeRegistry service = services.next();
						try {
							java.lang.reflect.Method m = delegate.getClass().getDeclaredMethod(
								"setDatatypeRegistry",
								org.daisy.pipeline.datatypes.DatatypeRegistry.class
							);
							m.setAccessible(true);
							m.invoke(delegate, service);
						} catch (NoSuchMethodException |
						         IllegalAccessException |
						         java.lang.reflect.InvocationTargetException e) {
							throw new RuntimeException(e);
						}
						spi_log.trace("Bound org.daisy.pipeline.datatypes.DatatypeRegistry service: "
						              + service.getClass().getName() + "@"
						              + Integer.toHexString(System.identityHashCode(service)));
						refCount++;
						if (maxRef > 0 && refCount == maxRef)
							break;
					} catch (Throwable e) {
						spi_log.error("Error while binding org.daisy.pipeline.datatypes.DatatypeRegistry service", e);
					}
				}
			} catch (Throwable e) {
				spi_log.error("Error while binding org.daisy.pipeline.datatypes.DatatypeRegistry services", e);
			}
			if (refCount < minRef) {
				spi_log.warn("No org.daisy.pipeline.datatypes.DatatypeRegistry found");
				throw new RuntimeException("No org.daisy.pipeline.datatypes.DatatypeRegistry found");
			} else {
				spi_log.trace("Bound " + refCount + " org.daisy.pipeline.datatypes.DatatypeRegistry services.");
			}
		}
		spi_log.trace("Activating Pipeline1ScriptProvider");
		try {
			java.lang.reflect.Method m = delegate.getClass().getDeclaredMethod("init");
			m.setAccessible(true);
			m.invoke(delegate);
		} catch (NoSuchMethodException |
		         IllegalAccessException  e) {
			throw new RuntimeException(e);
		} catch (java.lang.reflect.InvocationTargetException e) {
			if (e.getCause() instanceof org.daisy.common.spi.ActivationException)
				throw (org.daisy.common.spi.ActivationException)e.getCause();
			else
				throw new RuntimeException(e);
		}
	}
	
	@Override
	protected void finalize() throws Throwable {
		spi_deactivate();
		super.finalize();
	}
	
	public void spi_deactivate() {
		if (!spi_deactivated) {
			spi_log.trace("Deactivating Pipeline1ScriptProvider");
			try {
				try {
					java.lang.reflect.Method m = delegate.getClass().getDeclaredMethod("close");
					m.setAccessible(true);
					m.invoke(delegate);
				} catch (NoSuchMethodException |
				         IllegalAccessException |
				         java.lang.reflect.InvocationTargetException e) {
					throw new RuntimeException(e);
				}
			} catch (Throwable e) {
				spi_log.error("Caught exception while deactivating Pipeline1ScriptProvider", e);
			} finally {
				spi_deactivated = true;
			}
		}
	}
	
	public java.util.Map spi_getProperties() {
		return spi_props;
	}

	@Override
	public java.lang.Iterable> getScripts() {
		try {
			return (java.lang.Iterable>)delegate.getClass().getMethod("getScripts").invoke(
				delegate
			);
		} catch (NoSuchMethodException |
		         IllegalAccessException e) {
			throw new RuntimeException(e);
		} catch (java.lang.reflect.InvocationTargetException e) {
			throw new RuntimeException(e);
		} 
	}

	@Override
	public String toString() {
		return delegate.toString();
	}

	@Override
	public int hashCode() {
		return delegate.hashCode();
	}

	@Override
	public boolean equals(Object o) {
		return delegate.equals(o);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy