From: <tho...@us...> - 2014-01-15 13:17:28
|
Revision: 7801 http://bigdata.svn.sourceforge.net/bigdata/?rev=7801&view=rev Author: thompsonbry Date: 2014-01-15 13:17:18 +0000 (Wed, 15 Jan 2014) Log Message: ----------- Added an example to start and run the NSS from embedded code. Added Paths: ----------- branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/samples/com/bigdata/samples/NSSEmbeddedExample.java Added: branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/samples/com/bigdata/samples/NSSEmbeddedExample.java =================================================================== --- branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/samples/com/bigdata/samples/NSSEmbeddedExample.java (rev 0) +++ branches/BIGDATA_RELEASE_1_3_0/bigdata-sails/src/samples/com/bigdata/samples/NSSEmbeddedExample.java 2014-01-15 13:17:18 UTC (rev 7801) @@ -0,0 +1,148 @@ +package com.bigdata.samples; + +import java.net.URL; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.log4j.Logger; +import org.eclipse.jetty.server.Server; + +import com.bigdata.journal.IIndexManager; +import com.bigdata.rdf.sail.BigdataSail; +import com.bigdata.rdf.sail.webapp.NanoSparqlServer; +import com.bigdata.util.config.NicUtil; + +/** + * Class demonstrates how to start the {@link NanoSparqlServer} from within + * embedded code. + * + * @author <a href="mailto:tho...@us...">Bryan Thompson</a> + */ +public class NSSEmbeddedExample implements Runnable { + + private static final Logger log = Logger + .getLogger(NSSEmbeddedExample.class); + + private int port; + private final IIndexManager indexManager; + private final Map<String, String> initParams; + + /** + * + * @param port + * The desired port -or- ZERO (0) to use a random open port. + */ + public NSSEmbeddedExample(final int port, final IIndexManager indexManager, + final Map<String, String> initParams) { + + if (indexManager == null) + throw new IllegalArgumentException(); + + if (initParams == null) + throw new IllegalArgumentException(); + + this.port = port; + this.indexManager = indexManager; + this.initParams = initParams; + + } + + @Override + public void run() { + + Server server = null; + try { + + server = NanoSparqlServer.newInstance(port, indexManager, + initParams); + + server.start(); + + final int actualPort = server.getConnectors()[0] + .getLocalPort(); + + String hostAddr = NicUtil.getIpAddress("default.nic", + "default", true/* loopbackOk */); + + if (hostAddr == null) { + + hostAddr = "localhost"; + + } + + final String serviceURL = new URL("http", hostAddr, actualPort, ""/* file */) + .toExternalForm(); + + System.out.println("serviceURL: " + serviceURL); + + // Block and wait. The NSS is running. + server.join(); + + } catch (Throwable t) { + + log.error(t, t); + + } finally { + + if (server != null) { + + try { + + server.stop(); + + } catch (Exception e) { + + log.error(e, e); + + } + + server = null; + + System.out.println("Halted."); + + } + + } + + } + + /** + * Start and run an {@link NanoSparqlServer} instance from embedded code. + * + * @param args + * ignored. + * + * @throws Exception + */ + public static void main(final String[] args) throws Exception { + + final int port = 0; // random port. + + /* + * Create or re-open a durable database instance using default + * configuration properties. There are other constructors that allow you + * to take more control over this process. + */ + final BigdataSail sail = new BigdataSail(); + + sail.initialize(); + + try { + + final IIndexManager indexManager = sail.getDatabase() + .getIndexManager(); + + final Map<String, String> initParams = new LinkedHashMap<String, String>(); + + new Thread(new NSSEmbeddedExample(port, indexManager, initParams)) + .run(); + + } finally { + + sail.shutDown(); + + } + + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |