/*
 * #%L
 * ImageJ server for RESTful access to ImageJ.
 * %%
 * Copyright (C) 2013 - 2016 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

package net.imagej.server;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.scijava.io.ByteArrayByteBank;
import org.scijava.io.ByteBank;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.service.AbstractService;
import org.scijava.service.Service;
import org.scijava.util.FileUtils;

/**
 * Default implementation of {@link ImageJServerService}.
 *
 * @author Curtis Rueden
 */
@Plugin(type = Service.class)
public class DefaultImageJServerService extends AbstractService implements
	ImageJServerService
{

	@Parameter
	private LogService log;

	private String configFilePath;

	@Override
	public ImageJServer start(final String... args) {
		final String[] arguments = args == null || args.length == 0 ? //
			new String[] { "server", configFilePath() } : args;
		final ImageJServer app = new ImageJServer(context());
		try {
			app.run(arguments);
		}
		catch (final Exception exc) {
			throw new RuntimeException(exc);
		}
		return app;
	}

	// -- Helper methods --

	private String configFilePath() {
		if (configFilePath == null) initConfigFilePath();
		return configFilePath;
	}

	private synchronized void initConfigFilePath() {
		if (configFilePath != null) return;

		try {
			final InputStream in = getClass().getResourceAsStream("imagej-server.yml");
			final byte[] bytes = readStreamFully(in);

			final File configFile = File.createTempFile("imagej-server", ".yml");
			FileUtils.writeFile(configFile, bytes);
			configFile.deleteOnExit();
			configFilePath = configFile.getAbsolutePath();
		}
		catch (final IOException exc) {
			log.error(exc);
		}
	}

	// TODO: Move this to SciJava Common.
	private byte[] readStreamFully(final InputStream in) throws IOException {
		final ByteBank bank = new ByteArrayByteBank();
		byte[] buf = new byte[256 * 1024];
		while (true) {
			final int r = in.read(buf);
			if (r <= 0) break;
			bank.appendBytes(buf, r);
		}
		return bank.toByteArray();
	}
}

@@ -825,16 +825,64 @@
 tStream;
+%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -1393,16 +1393,211 @@
 vice%0A%7B%0A%0A
+%09/**%0A%09 * Collection of servers that have been doled out.%0A%09 * %3Cp%3E%0A%09 * We keep these here so we can stop them all upon dispose.%0A%09 * %3C/p%3E%0A%09 */%0A%09private Set%3CImageJServer%3E servers = new HashSet%3C%3E();%0A%0A
 %09@Parame
@@ -1897,16 +1897,125 @@
 ntext())
+ %7B%0A%09%09%09@Override%0A%09%09%09public void stop() throws Exception %7B%0A%09%09%09%09servers.remove(this);%0A%09%09%09%09super.stop();%0A%09%09%09%7D%0A%09%09%7D
 ;%0A%09%09try 
@@ -2121,19 +2121,220 @@
 %7D%0A%09%09
-return app;
+servers.add(app);%0A%09%09return app;%0A%09%7D%0A%0A%09@Override%0A%09public void dispose() %7B%0A%09%09for (final ImageJServer server : servers) %7B%0A%09%09%09try %7B%0A%09%09%09%09server.stop();%0A%09%09%09%7D%0A%09%09%09catch (final Exception exc) %7B%0A%09%09%09%09log.error(exc);%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A

Stop all servers when the context disposes