Implemented REST API and server for executing scans

This commit is contained in:
2013-10-23 15:16:49 +02:00
parent e483f90837
commit f4d5102ea3
6 changed files with 257 additions and 3 deletions

View File

@@ -6,6 +6,25 @@
<version>1.1.41</version>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>

View File

@@ -341,9 +341,9 @@ public class Acquisition {
// Clear global variables Jython
jVariableDictionary.clear();
// Destroy the CA context
cservice.destroy();
logger.fine("ChannelService destroyed");
// // Destroy the CA context
// cservice.destroy();
// logger.fine("ChannelService destroyed");
// Remove log handler
if(logHandler!=null){

View File

@@ -0,0 +1,74 @@
/**
*
* Copyright 2013 Paul Scherrer Institute. All rights reserved.
*
* This code is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This code is distributed in the hope that it will be useful, but without any
* warranty; without even the implied warranty of merchantability or fitness for
* a particular purpose. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ch.psi.fda.rest;
import java.util.logging.Logger;
import com.google.common.eventbus.EventBus;
import ch.psi.fda.aq.Acquisition;
import ch.psi.fda.model.v1.Configuration;
import ch.psi.jcae.ChannelService;
/**
* @author ebner
*
*/
public class AcquisitionEngine {
private static final Logger logger = Logger.getLogger(AcquisitionEngine.class.getName());
private Acquisition acquisition;
private ChannelService cservice;
public AcquisitionEngine(ChannelService cservice){
this.cservice=cservice;
}
public void scan(Configuration configuration){
if(acquisition!=null){
throw new RuntimeException("There is already and acquisition running");
}
try{
EventBus ebus = new EventBus();
acquisition = new Acquisition(cservice);
acquisition.initalize(ebus, configuration);
acquisition.execute();
}
catch(InterruptedException e){
throw new RuntimeException(e);
}
finally{
acquisition.destroy();
acquisition=null;
}
}
public void stop(){
if(acquisition==null){
return;
}
logger.info("Stop acquisition");
acquisition.abort();
// acquisition.destroy();
}
}

View File

@@ -0,0 +1,14 @@
package ch.psi.fda.rest;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import ch.psi.jcae.impl.DefaultChannelService;
public class ResourceBinder extends AbstractBinder {
@Override
protected void configure() {
bind(new AcquisitionEngine(new DefaultChannelService())).to(AcquisitionEngine.class);
}
}

View File

@@ -0,0 +1,98 @@
package ch.psi.fda.rest;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Logger;
import javax.ws.rs.core.UriBuilder;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;
import sun.misc.Signal;
import sun.misc.SignalHandler;
@SuppressWarnings("restriction")
public class RestServer {
private static final Logger logger = Logger.getLogger(RestServer.class.getName());
public static void main(String[] args) throws IOException, ParseException {
// Option handling
int port = 8080;
Options options = new Options();
options.addOption("h", false, "Help");
options.addOption("p", true, "Server port (default: "+port+")");
GnuParser parser = new GnuParser();
CommandLine line = parser.parse(options, args);
if (line.hasOption("p")) {
port = Integer.parseInt(line.getOptionValue("p"));
}
if (line.hasOption("h")) {
HelpFormatter f = new HelpFormatter();
f.printHelp("broker", options);
return;
}
URI baseUri = UriBuilder.fromUri("http://" + InetAddress.getLocalHost().getHostName() + "/").port(port).build();
// Broker broker = createBroker(config);
ResourceBinder binder = new ResourceBinder();
ResourceConfig resourceConfig = new ResourceConfig(JacksonFeature.class);
resourceConfig.packages(RestServer.class.getPackage().getName()+".services"); // Services are located in services package
resourceConfig.register(binder);
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
// Static content
// String home = System.getenv("BROKER_BASE");
// if (home == null) {
// home = "src/main/assembly";
// }
// home = home + "/www";
// server.getServerConfiguration().addHttpHandler(new StaticHttpHandler(home), "/static");
logger.info("Broker started");
// logger.info(String.format("Management interface available at %sstatic/", baseUri));
logger.info("Use ctrl+c to stop ...");
// Signal handling
final CountDownLatch latch = new CountDownLatch(1);
Signal.handle(new Signal("INT"), new SignalHandler() {
public void handle(Signal sig) {
if(latch.getCount()==0){
logger.info("Terminate broker by System.exit()");
System.exit(1); // Terminate program after 2 ctrl+c
}
latch.countDown();
}
});
// Wait for termination, i.e. wait for ctrl+c
try {
latch.await();
} catch (InterruptedException e) {
}
server.stop();
// broker.terminate();
}
}

View File

@@ -0,0 +1,49 @@
/**
*
* Copyright 2013 Paul Scherrer Institute. All rights reserved.
*
* This code is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This code is distributed in the hope that it will be useful, but without any
* warranty; without even the implied warranty of merchantability or fitness for
* a particular purpose. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ch.psi.fda.rest.services;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import ch.psi.fda.model.v1.Configuration;
import ch.psi.fda.rest.AcquisitionEngine;
@Path("fda")
public class ScanService {
@Inject
private AcquisitionEngine aengine;
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
// @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public void execute(Configuration configuration) throws InterruptedException{
aengine.scan(configuration);
}
@DELETE
public void stop(){
aengine.stop();
}
}