Repository: jena
Updated Branches:
  refs/heads/master f6817af89 -> 821045666


Clean up configuration when adding a DataService.

Don't provide implicit manipulation of already registered DataService
but assume that the application sets up a DataService itself for
detailed configuration.

Add test of configuration by DataService.


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/82104566
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/82104566
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/82104566

Branch: refs/heads/master
Commit: 8210456666a74413b4946eb24e1a556304faeebb
Parents: f6817af
Author: Andy Seaborne <a...@apache.org>
Authored: Thu Oct 13 10:50:54 2016 +0100
Committer: Andy Seaborne <a...@apache.org>
Committed: Thu Oct 13 10:50:54 2016 +0100

----------------------------------------------------------------------
 .../apache/jena/fuseki/server/DataService.java  | 22 ++++--
 .../fuseki/embedded/FusekiEmbeddedServer.java   | 76 +++++++-------------
 .../fuseki/embedded/TestEmbeddedFuseki.java     | 28 ++++----
 3 files changed, 59 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/82104566/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/DataService.java
----------------------------------------------------------------------
diff --git 
a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/DataService.java
 
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/DataService.java
index b08649f..ac87b4b 100644
--- 
a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/DataService.java
+++ 
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/server/DataService.java
@@ -41,14 +41,15 @@ public class DataService { //implements DatasetMXBean {
         return dummy ; 
     }
     
-    public static DataService dummy = new DataService(null) ;
+    public static final DataService dummy ;
     static {
-        dummy.dataset = new DatasetGraphReadOnly(DatasetGraphFactory.create()) 
;
+        DatasetGraph dsg = new 
DatasetGraphReadOnly(DatasetGraphFactory.create()) ;
+        dummy = new DataService(dsg) ;
         dummy.addEndpoint(OperationName.Query, DEF.ServiceQuery) ;
         dummy.addEndpoint(OperationName.Query, DEF.ServiceQueryAlt) ;
     }
     
-    private DatasetGraph dataset = null ;              // Only valid if active.
+    private DatasetGraph dataset ;
 
     private ListMultimap<OperationName, Endpoint> operations    = 
ArrayListMultimap.create() ;
     private Map<String, Endpoint> endpoints                     = new 
HashMap<>() ;
@@ -56,17 +57,30 @@ public class DataService { //implements DatasetMXBean {
     private volatile DatasetStatus state = UNINITIALIZED ;
 
     // DataService-level counters.
-    private final CounterSet counters                   = new CounterSet() ;
+    private final CounterSet    counters                = new CounterSet() ;
     private final AtomicLong    requestCounter          = new AtomicLong(0) ;  
 
     private final AtomicBoolean offlineInProgress       = new 
AtomicBoolean(false) ;
     private final AtomicBoolean acceptingRequests       = new 
AtomicBoolean(true) ;
 
+    /** Create a {@code DataService} for the given dataset. */
     public DataService(DatasetGraph dataset) {
         this.dataset = dataset ;
         counters.add(CounterName.Requests) ;
         counters.add(CounterName.RequestsGood) ;
         counters.add(CounterName.RequestsBad) ;
     }
+
+    /**
+     * Create a {@code DataService} that has the same dataset, same operations 
and
+     * endpoints as another {@code DataService}. Counters are not copied.
+     */
+    public DataService(DataService other) {
+        // Copy non-counter state of 'other'.
+        this.dataset = other.dataset ;
+        this.operations = ArrayListMultimap.create(other.operations) ;
+        this.endpoints = new HashMap<>(other.endpoints) ;
+    }
+
     
     public DatasetGraph getDataset() {
         return dataset ; 

http://git-wip-us.apache.org/repos/asf/jena/blob/82104566/jena-fuseki2/jena-fuseki-embedded/src/main/java/org/apache/jena/fuseki/embedded/FusekiEmbeddedServer.java
----------------------------------------------------------------------
diff --git 
a/jena-fuseki2/jena-fuseki-embedded/src/main/java/org/apache/jena/fuseki/embedded/FusekiEmbeddedServer.java
 
b/jena-fuseki2/jena-fuseki-embedded/src/main/java/org/apache/jena/fuseki/embedded/FusekiEmbeddedServer.java
index aa206fa..452761b 100644
--- 
a/jena-fuseki2/jena-fuseki-embedded/src/main/java/org/apache/jena/fuseki/embedded/FusekiEmbeddedServer.java
+++ 
b/jena-fuseki2/jena-fuseki-embedded/src/main/java/org/apache/jena/fuseki/embedded/FusekiEmbeddedServer.java
@@ -34,7 +34,6 @@ import org.apache.jena.fuseki.mgt.ActionStats ;
 import org.apache.jena.fuseki.server.DataAccessPoint ;
 import org.apache.jena.fuseki.server.DataAccessPointRegistry ;
 import org.apache.jena.fuseki.server.DataService ;
-import org.apache.jena.fuseki.server.OperationName ;
 import org.apache.jena.fuseki.servlets.FusekiFilter ;
 import org.apache.jena.query.Dataset ;
 import org.apache.jena.sparql.core.DatasetGraph ;
@@ -95,9 +94,11 @@ public class FusekiEmbeddedServer {
     public final Server server ;
     private int port ;
     
-    public FusekiEmbeddedServer(Server server) {
+    private FusekiEmbeddedServer(int port, Server server) {
         this.server = server ;
-        port = ((ServerConnector)server.getConnectors()[0]).getPort() ;
+        // This should be the same.
+        //this.port = ((ServerConnector)server.getConnectors()[0]).getPort() ;
+        this.port = port ;
     }
     
     /** Get the underlying Jetty server which has also been set up. */ 
@@ -138,20 +139,22 @@ public class FusekiEmbeddedServer {
     
     /** FusekiEmbeddedServer.Builder */
     public static class Builder {
-        // Keeping this allows accumulation of data access points for one 
name.  
-        private Map<String, DataService> map = new HashMap<>() ;
-        private int port = 3330 ;
-        private boolean loopback = false ;
-        private boolean withStats = false ;
-        private String contextPath = "/" ;
-        
-        /* Set the port to run on */ 
+        private Map<String, DataService> map           = new HashMap<>() ;
+        // Default values.
+        private int                      port          = 3330 ;
+        private boolean                  loopback      = false ;
+        private boolean                  withStats     = false ;
+        private String                   contextPath   = "/" ;
+
+        /** Set the port to run on. */ 
         public Builder setPort(int port) {
+            if ( port <= 0 )
+                throw new IllegalArgumentException("port="+port+" : Port must 
be greater than zero.") ;
             this.port = port ;
             return this ;
         }
         
-        /* Context path to Fuseki.  If it's "/" then Fuseki URL look like
+        /** Context path to Fuseki.  If it's "/" then Fuseki URL look like
          * "http://host:port/dataset/query"; else 
"http://host:port/path/dataset/query"; 
          */
         public Builder setContextPath(String path) {
@@ -173,72 +176,43 @@ public class FusekiEmbeddedServer {
             return this ;
         }
 
-        /* Add the dataset with given name and a default set of services 
including update */  
+        /** Add the dataset with given name and a default set of services 
including update */  
         public Builder add(String name, Dataset ds) {
             return add(name, ds.asDatasetGraph()) ;
         }
 
-        /* Add the dataset with given name and a default set of services 
including update */  
+        /** Add the dataset with given name and a default set of services 
including update */  
         public Builder add(String name, DatasetGraph dsg) {
             return add(name, dsg, true) ;
         }
 
-        /* Add the dataset with given name and a default set of services. */  
+        /** Add the dataset with given name and a default set of services. */  
         public Builder add(String name, Dataset ds, boolean allowUpdate) {
             return add(name, ds.asDatasetGraph(), allowUpdate) ;
         }
             
         
-        /* Add the dataset with given name and a default set of services. */  
+        /** Add the dataset with given name and a default set of services. */  
         public Builder add(String name, DatasetGraph dsg, boolean allowUpdate) 
{
             DataService dSrv = FusekiBuilder.buildDataService(dsg, 
allowUpdate) ; 
             return add(name, dSrv) ;
         }
         
-        /* Add a data service that includes dataset and service names.*/  
+        /** Add a data service that includes dataset and service names.
+         * A {@link DataService} allows for choices of the various endpoint 
names.
+         */  
         public Builder add(String name, DataService dataService) {
             return add$(name, dataService) ; 
         }
         
-        /* Add an operation, specifing it's endpoint name.
-         * This adds endpoints to any existing data service already setup by 
the builder.   
-         */
-        public Builder add(String name, Dataset ds, OperationName opName, 
String epName) {
-            return add(name, ds.asDatasetGraph(), opName, epName) ; 
-        }
-
-            /* Add an operation, specifing it's endpoint name.
-         * This adds endpoints to any existing data service already setup by 
the builder.   
-         */
-        public Builder add(String name, DatasetGraph dsg, OperationName 
opName, String epName) {
-            DataService dSrv = map.get(name) ;
-            if ( dSrv == null ) {
-                dSrv = new DataService(dsg) ;
-                map.put(name, dSrv) ;
-            }
-            dSrv.addEndpoint(opName, epName);
-            return this ; 
-        }
-
         private Builder add$(String name, DataService dataService) {
             name = DataAccessPoint.canonical(name) ;
             if ( map.containsKey(name) )
-                throw new FusekiConfigException("Attempt to add a DataService 
for a different dataset: "+name) ;
+                throw new FusekiConfigException("Data service name already 
registered: "+name) ;
             map.put(name, dataService) ;
-            
-            // Merge endpoints : too complicated 
-//            DataService dSrv = map.get(name) ;
-//            if ( dSrv != null ) {
-//                DatasetGraph dsg1 = dSrv.getDataset() ;
-//                DatasetGraph dsg2 = dataService.getDataset() ;
-//                if ( dsg1 != dsg2 ) // Object identity
-//                    throw new FusekiConfigException("Attempt to add a 
DataService for a different dataset: "+name) ;
-//                dSrv.getOperations() ;
-//            } else
-//                map.put(name, dataService) ;
             return this ;
         }
-        
+
         /** Read and parse a Fuseki services/datasets file.
          *  <p>
          *  The application is responsible for ensuring a correct classpath. 
For example,
@@ -265,7 +239,7 @@ public class FusekiEmbeddedServer {
             DataAccessPointRegistry.set(handler.getServletContext(), registry) 
;
             Server server = jettyServer(port, loopback) ;
             server.setHandler(handler);
-            return new FusekiEmbeddedServer(server) ;
+            return new FusekiEmbeddedServer(port, server) ;
         }
 
         /** Build a ServletContextHandler with the Fuseki router : {@link 
FusekiFilter} */

http://git-wip-us.apache.org/repos/asf/jena/blob/82104566/jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/TestEmbeddedFuseki.java
----------------------------------------------------------------------
diff --git 
a/jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/TestEmbeddedFuseki.java
 
b/jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/TestEmbeddedFuseki.java
index 581edaa..6926fa6 100644
--- 
a/jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/TestEmbeddedFuseki.java
+++ 
b/jena-fuseki2/jena-fuseki-embedded/src/test/java/org/apache/jena/fuseki/embedded/TestEmbeddedFuseki.java
@@ -70,18 +70,6 @@ public class TestEmbeddedFuseki {
         server.stop() ;
     }
     
-    @Test public void embedded_01a() {
-        Dataset ds = DatasetFactory.createTxnMem() ;
-        FusekiEmbeddedServer server = FusekiEmbeddedServer.create().add("/ds", 
ds).build() ;
-        assertTrue(DataAccessPointRegistry.get().isRegistered("/ds")) ;
-        server.start() ;
-        query("http://localhost:3330/ds/query";, "SELECT * { ?s ?p ?o}", 
qExec-> {
-            ResultSet rs = qExec.execSelect() ; 
-            assertFalse(rs.hasNext()) ;
-        }) ;
-        server.stop() ;
-    }
-
     @Test public void embedded_02() {
         DatasetGraph dsg = dataset() ;
         FusekiEmbeddedServer server = FusekiEmbeddedServer.make(3330, "/ds2", 
dsg) ;
@@ -242,6 +230,22 @@ public class TestEmbeddedFuseki {
         } finally { server.stop() ; } 
     }
 
+    @Test public void embedded_20() {
+        DatasetGraph dsg = dataset() ;
+        DataService dSrv = new DataService(dsg) ;
+        dSrv.addEndpoint(OperationName.Query, "q") ;
+        dSrv.addEndpoint(OperationName.GSP_R, "gsp") ;
+        FusekiEmbeddedServer server = FusekiEmbeddedServer.create()
+            .add("/dsrv1", dSrv)
+            .build() ;
+        server.start() ;
+        try {
+            query("http://localhost:3330/dsrv1/q","ASK{}",x->{}) ;
+            String x1 = 
HttpOp.execHttpGetString("http://localhost:3330/dsrv1/gsp";) ;
+            assertNotNull(x1) ;
+        } finally { server.stop() ; } 
+    }
+    
     /** Create an HttpEntity for the graph */  
     protected static HttpEntity graphToHttpEntity(final Graph graph) {
         final RDFFormat syntax = RDFFormat.TURTLE_BLOCKS ;

Reply via email to