/*
 * #%L
 * SciJava Common shared library for SciJava software.
 * %%
 * Copyright (C) 2009 - 2014 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package org.scijava;

import org.scijava.app.App;
import org.scijava.app.AppService;
import org.scijava.app.SciJavaApp;
import org.scijava.app.StatusService;
import org.scijava.event.EventHistory;
import org.scijava.event.EventService;
import org.scijava.log.LogService;
import org.scijava.object.ObjectService;
import org.scijava.plugin.PluginService;
import org.scijava.service.Service;
import org.scijava.thread.ThreadService;

/**
 * Abstract superclass for {@link Gateway} implementations.
 * 
 * @author Mark Hiner
 * @author Curtis Rueden
 */
public abstract class AbstractGateway extends AbstractContextual implements
	Gateway
{

	private final String appName;

	// -- Constructor --

	public AbstractGateway() {
		this(SciJavaApp.NAME, null);
	}

	public AbstractGateway(String appName, Context context) {
		this.appName = appName;
		if (context != null) setContext(context);
	}

	// -- Gateway methods --

	@Override
	public <S extends Service> S get(final Class<S> serviceClass) {
		final S service = getContext().getService(serviceClass);
		checkNull(service, serviceClass.getName());
		return service;
	}

	@Override
	public Service get(final String serviceClassName) {
		final Service service = getContext().getService(serviceClassName);
		checkNull(service, serviceClassName);
		return service;
	}

	// -- Gateway methods - services --

	@Override
	public AppService app() {
		return get(AppService.class);
	}

	@Override
	public EventHistory eventHistory() {
		return get(EventHistory.class);
	}

	@Override
	public EventService event() {
		return get(EventService.class);
	}

	@Override
	public LogService log() {
		return get(LogService.class);
	}

	@Override
	public ObjectService object() {
		return get(ObjectService.class);
	}

	@Override
	public PluginService plugin() {
		return get(PluginService.class);
	}

	@Override
	public StatusService status() {
		return get(StatusService.class);
	}

	@Override
	public ThreadService thread() {
		return get(ThreadService.class);
	}

	// -- Gateway methods - application --

	@Override
	public App getApp() {
		return app().getApp(appName);
	}

	@Override
	public String getTitle() {
		return getApp().getTitle();
	}

	@Override
	public String getVersion() {
		return getApp().getVersion();
	}

	@Override
	public String getInfo(boolean mem) {
		return getApp().getInfo(mem);
	}

	// -- Helper methods --

	/** Throws {@link NoSuchServiceException} if the service wasn't found. */
	private void checkNull(final Service service, final String name) {
		if (service == null) {
			throw new NoSuchServiceException("Service " + name + " not found.");
		}
	}

}

@@ -2578,322 +2578,160 @@
 %7B%0A%09%09
-final S service = getContext().getService(serviceClass);%0A%09%09checkNull(service, serviceClass.getName());%0A%09%09return service;%0A%09%7D%0A%0A%09@Override%0A%09public Service get(final String serviceClassName) %7B%0A%09%09final Service service = getContext().getService(serviceClassName);%0A%09%09checkNull(service, serviceClassName);%0A%09%09return service
+return context().service(serviceClass);%0A%09%7D%0A%0A%09@Override%0A%09public Service get(final String serviceClassName) %7B%0A%09%09return context().service(serviceClassName)
 ;%0A%09%7D
@@ -3767,280 +3767,6 @@
 %09%7D%0A%0A
-%09// -- Helper methods --%0A%0A%09/** Throws %7B@link NoSuchServiceException%7D if the service wasn't found. */%0A%09private void checkNull(final Service service, final String name) %7B%0A%09%09if (service == null) %7B%0A%09%09%09throw new NoSuchServiceException(%22Service %22 + name + %22 not found.%22);%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

use new Context#service methods