// Path: src/main/java/net/imagej/server/ImageJServer.java
// public class ImageJServer extends Application<ImageJServerConfiguration> {
// 
// 	private final Context ctx;
// 
// 	private final ObjectService objectService;
// 
// 	private final JsonService jsonService;
// 
// 	private Environment env;
// 
// 	public ImageJServer(final Context ctx) {
// 		this.ctx = ctx;
// 		objectService = new DefaultObjectService();
// 		jsonService = new DefaultJsonService(ctx, objectService);
// 	}
// 
// 	@Override
// 	public String getName() {
// 		return "ImageJ";
// 	}
// 
// 	@Override
// 	public void initialize(final Bootstrap<ImageJServerConfiguration> bootstrap) {
// 		jsonService.addDeserializerTo(bootstrap.getObjectMapper());
// 	}
// 
// 	@Override
// 	public void run(final ImageJServerConfiguration configuration,
// 		final Environment environment)
// 	{
// 		// Enable CORS headers
// 		final FilterRegistration.Dynamic cors = environment.servlets().addFilter(
// 			"CORS", CrossOriginFilter.class);
// 
// 		// Configure CORS parameters
// 		cors.setInitParameter("allowedOrigins", "*");
// 		cors.setInitParameter("allowedHeaders",
// 			"X-Requested-With,Content-Type,Accept,Origin");
// 		cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
// 
// 		// Add URL mapping
// 		cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true,
// 			"/*");
// 
// 		env = environment;
// 
// 		// NB: not implemented yet
// 		final ImageJServerHealthCheck healthCheck = new ImageJServerHealthCheck();
// 		environment.healthChecks().register("imagej-server", healthCheck);
// 
// 		environment.jersey().register(MultiPartFeature.class);
// 
// 		// -- resources --
// 
// 		environment.jersey().register(AdminResource.class);
// 
// 		environment.jersey().register(ModulesResource.class);
// 
// 		environment.jersey().register(ObjectsResource.class);
// 
// 		// -- context dependencies injection --
// 
// 		environment.jersey().register(new AbstractBinder() {
// 
// 			@Override
// 			protected void configure() {
// 				bind(ctx).to(Context.class);
// 				bind(env).to(Environment.class);
// 				bind(objectService).to(ObjectService.class);
// 				bind(jsonService).to(JsonService.class);
// 			}
// 
// 		});
// 	}
// 
// 	public void stop() throws Exception {
// 		if (env == null) return;
// 		env.getApplicationContext().getServer().stop();
// 	}
// 
// 	public void join() throws InterruptedException {
// 		if (env == null) return;
// 		env.getApplicationContext().getServer().join();
// 	}
// }


import org.scijava.ui.UIService;
import java.util.List;
import net.imagej.server.ImageJServer;
import org.scijava.command.Command;
import org.scijava.log.LogService;
import org.scijava.object.ObjectService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
/*
 * #%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.commands;




/**
 * Stops the ImageJ Server, if one is running.
 *
 * @author Curtis Rueden
 */
@Plugin(type = Command.class, menuPath = "Plugins > Utilities > Stop Server")
public class StopServer implements Command {

	@Parameter
	private ObjectService objectService;

	@Parameter(required = false)
	private UIService ui;

	@Parameter
	private LogService log;

	@Override
	public void run() {
		final List<ImageJServer> servers = //
			objectService.getObjects(ImageJServer.class);
		if (!servers.isEmpty()) {
			try {
// Path: src/main/java/net/imagej/server/ImageJServer.java
// public class ImageJServer extends Application<ImageJServerConfiguration> {
// 
// 	private final Context ctx;
// 
// 	private final ObjectService objectService;
// 
// 	private final JsonService jsonService;
// 
// 	private Environment env;
// 
// 	public ImageJServer(final Context ctx) {
// 		this.ctx = ctx;
// 		objectService = new DefaultObjectService();
// 		jsonService = new DefaultJsonService(ctx, objectService);
// 	}
// 
// 	@Override
// 	public String getName() {
// 		return "ImageJ";
// 	}
// 
// 	@Override
// 	public void initialize(final Bootstrap<ImageJServerConfiguration> bootstrap) {
// 		jsonService.addDeserializerTo(bootstrap.getObjectMapper());
// 	}
// 
// 	@Override
// 	public void run(final ImageJServerConfiguration configuration,
// 		final Environment environment)
// 	{
// 		// Enable CORS headers
// 		final FilterRegistration.Dynamic cors = environment.servlets().addFilter(
// 			"CORS", CrossOriginFilter.class);
// 
// 		// Configure CORS parameters
// 		cors.setInitParameter("allowedOrigins", "*");
// 		cors.setInitParameter("allowedHeaders",
// 			"X-Requested-With,Content-Type,Accept,Origin");
// 		cors.setInitParameter("allowedMethods", "OPTIONS,GET,PUT,POST,DELETE,HEAD");
// 
// 		// Add URL mapping
// 		cors.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true,
// 			"/*");
// 
// 		env = environment;
// 
// 		// NB: not implemented yet
// 		final ImageJServerHealthCheck healthCheck = new ImageJServerHealthCheck();
// 		environment.healthChecks().register("imagej-server", healthCheck);
// 
// 		environment.jersey().register(MultiPartFeature.class);
// 
// 		// -- resources --
// 
// 		environment.jersey().register(AdminResource.class);
// 
// 		environment.jersey().register(ModulesResource.class);
// 
// 		environment.jersey().register(ObjectsResource.class);
// 
// 		// -- context dependencies injection --
// 
// 		environment.jersey().register(new AbstractBinder() {
// 
// 			@Override
// 			protected void configure() {
// 				bind(ctx).to(Context.class);
// 				bind(env).to(Environment.class);
// 				bind(objectService).to(ObjectService.class);
// 				bind(jsonService).to(JsonService.class);
// 			}
// 
// 		});
// 	}
// 
// 	public void stop() throws Exception {
// 		if (env == null) return;
// 		env.getApplicationContext().getServer().stop();
// 	}
// 
// 	public void join() throws InterruptedException {
// 		if (env == null) return;
// 		env.getApplicationContext().getServer().join();
// 	}
// }


// Path: src/main/java/net/imagej/server/commands/StopServer.java
import org.scijava.ui.UIService;
import java.util.List;
import net.imagej.server.ImageJServer;
import org.scijava.command.Command;
import org.scijava.log.LogService;
import org.scijava.object.ObjectService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/*
 * #%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.commands;




/**
 * Stops the ImageJ Server, if one is running.
 *
 * @author Curtis Rueden
 */
@Plugin(type = Command.class, menuPath = "Plugins > Utilities > Stop Server")
public class StopServer implements Command {

	@Parameter
	private ObjectService objectService;

	@Parameter(required = false)
	private UIService ui;

	@Parameter
	private LogService log;

	@Override
	public void run() {
		final List<ImageJServer> servers = //
			objectService.getObjects(ImageJServer.class);
		if (!servers.isEmpty()) {
			try {
				servers.get(0).stop();