Services

From CyberCore
Jump to: navigation, search

Services are plain Java objects (POJOs), which are registered under certain name.

The Component Container reads all registered classes from a configuration file and instantiate a singleton instance of each one.

The configuration file is in Turtle format and usually resides in the installation directory of the server.

By default, the server looks in the INSTALL_DIR/config folder.

Java

Below is a sample of the RandomStringService from the Tutorials section:


..................................

// the service for the "create simple service" tutorial
@Service(name="randomStrings")
public class RandomStringService implements RandomStringServiceMBean {
	private static Logger log = LoggerFactory.getLogger(RandomStringService.class);
	
	static final URI PARAM_NS = new URIImpl("http://cybercore-project.net/tutorials#");
	static final URI PARAM_PREFIX = new URIImpl("http://cybercore-project.net/tutorials#prefix");
	
	Random random;
	int count;
	String defaultPrefix;
	
	@ServiceMethod
	@Description("generates a random string (with complex input)")
	public String complexString( @PName("input")
								 @Description("some complex input")
								 ComplexInput input) {
		return randomString(input.getPrefix());
	}
	
	@ServiceMethod
	@Description("generates a random string")	
	public String randomString( @PName("prefix")
								@Description("some known part of the random string..")
								String prefix) {
		assertState();
		
		if(prefix == null) {
			prefix = defaultPrefix;
		}
		
		String result = prefix + String.valueOf(random.nextInt());		
		log.debug("generated: "+result);
		
		count++;
		
		return result; 
	}
	
	private void loadConfig() {
                // read RDF
		RdfQuery query; // reference to the system repository
		try {
			query = (RdfQuery) LocalServer.getComponents().getComponent(
					new java.net.URI(CoreServiceNames.SystemRepository));
		} catch (URISyntaxException e) {
			throw new RuntimeException(e);
		}
			
		ConfigHelper config = new ConfigHelper(query);
		defaultPrefix =	config.getString(PARAM_NS, PARAM_PREFIX, "");		
	}
	
	@Startup
	public void start() throws ComponentBindException {
		// load logic here

                ..................................
	}
	
	@Shutdown
	public void stop() {
		// cleanup logic here
		
		..................................
	}
		
	..........................
}

In the example above:

  1. Service methods are marked with @ServiceMethod attribute
  2. There start and stop methods, annotated with the @Startup and @Shutdown attributes respectively.
  3. The service is not dealing with marshalling / unmarshalling data. This is done by the various connectors (which are also services).

TTL

service:randomStrings		rdf:type	entity:service ;
				entity:implementation	"net.ccil.platform.tutorials.RandomStringService" ;
				entity:classpath		"" ;
				entity:arguments		"" .

HTTP

Sample GET:

http://localhost:8080/randomString/randomString?json=["some_prefix_"]

More information about the various way to invoke CyberCore services is available at the Connectors section.