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

se.jbee.inject.bootstrap.BootstrapperBundle Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
/*
 *  Copyright (c) 2012, Jan Bernitt 
 *			
 *  Licensed under the Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
 */
package se.jbee.inject.bootstrap;

import se.jbee.inject.util.Metaclass;

/**
 * The default utility {@link Bundle} that is a {@link Bootstrap} as well so that bindings can be
 * declared nicer.
 * 
 * @author Jan Bernitt ([email protected])
 */
public abstract class BootstrapperBundle
		implements Bundle, Bootstrapper {

	private Bootstrapper bootstrap;

	@Override
	public final void bootstrap( Bootstrapper bootstrap ) {
		Bootstrap.nonnullThrowsReentranceException( this.bootstrap );
		this.bootstrap = bootstrap;
		bootstrap();
	}

	@Override
	public final void install( Class bundle ) {
		bootstrap.install( bundle );
	}

	@Override
	public final void install( Module module ) {
		bootstrap.install( module );
	}

	@Override
	public final  void install( PresetModule module ) {
		bootstrap.install( module );
	}

	@Override
	public final void uninstall( Class bundle ) {
		bootstrap.uninstall( bundle );
	}

	@Override
	public final  & ModularBundle> void install( M... modules ) {
		bootstrap.install( modules );
	}

	@Override
	public final > void install( Class> bundle,
			Class property ) {
		bootstrap.install( bundle, property );
	}

	@Override
	public final  & ModularBundle> void uninstall( M... modules ) {
		bootstrap.uninstall( modules );
	}

	protected final  & ModularBundle> void installAll( Class modules ) {
		install( modules.getEnumConstants() );
	}

	protected final  & ModularBundle> void uninstallAll( Class modules ) {
		uninstall( modules.getEnumConstants() );
	}

	/**
	 * Installs the given {@link Module} using the given {@link Inspector} when declaring binds.
	 */
	protected final void install( Module module, Inspector inspector ) {
		install( new InspectorModule( module, inspector ) );
	}

	protected final void install( Class module, Inspector inspector ) {
		install( newInstance( module ), inspector );
	}

	protected static Module newInstance( Class module ) {
		return Invoke.constructor( Metaclass.accessible( Inspect.noArgsConstructor( module ) ) );
	}

	@Override
	public String toString() {
		return "bundle " + getClass().getSimpleName();
	}

	protected abstract void bootstrap();

	private static final class InspectorModule
			implements Module {

		private final Module module;
		private final Inspector inspector;

		InspectorModule( Module module, Inspector inspector ) {
			super();
			this.module = module;
			this.inspector = inspector;
		}

		@Override
		public void declare( Bindings bindings, Inspector inspector ) {
			module.declare( bindings, this.inspector );
		}

		@Override
		public String toString() {
			return module + "[" + inspector + "]";
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy