Skip to content

Commit

Permalink
Schema registration as part of RestAPI registration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalohlava committed Mar 15, 2017
1 parent d07fd03 commit 9489431
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 38 deletions.
@@ -1 +1 @@
water.test.util.TestSchemaPojo
water.test.util.PojoUtilsTest$TestSchemaPojo
32 changes: 24 additions & 8 deletions h2o-core/src/main/java/water/H2O.java
Expand Up @@ -10,7 +10,15 @@
import org.reflections.Reflections;
import water.UDPRebooted.ShutdownTsk;
import water.api.AbstractRegister;
import water.api.Handler;
import water.api.HandlerFactory;
import water.api.RegisterRestApi;
import water.api.RequestServer;
import water.api.RestApiContext;
import water.api.RestApiHandler;
import water.api.Route;
import water.api.Schema;
import water.api.SchemaServer;
import water.exceptions.H2OFailException;
import water.exceptions.H2OIllegalArgumentException;
import water.init.*;
Expand Down Expand Up @@ -776,24 +784,30 @@ public static void registerRestApis(String relativeResourcePath) {
for (AbstractH2OExtension e : H2O.getExtensions()) {
e.printInitialized();
}
Log.info("Registered " + H2O.getExtensions().size() + " extensions in: " + registerExtensionsMillis + "mS");
Log.info("Registered " + H2O.getExtensions().size() + " extensions in: " + registerExtensionsMillis + "ms");

long before = System.currentTimeMillis();
ServiceLoader<AbstractRegister> extensionsLoader = ServiceLoader.load(AbstractRegister.class);
for (AbstractRegister r : extensionsLoader) {
RequestServer.DummyRestApiContext dummyRestApiContext = new RequestServer.DummyRestApiContext();
ServiceLoader<RegisterRestApi> restApiExtensionLoander = ServiceLoader.load(RegisterRestApi.class);
for (RegisterRestApi r : restApiExtensionLoander) {
try {
r.register(relativeResourcePath);
r.registerEndPoints(dummyRestApiContext);
r.registerSchemas(dummyRestApiContext);
} catch (Exception e) {
Log.info("Cannot register extension: " + r);
Log.info("Cannot register extension: " + r + ". Skipping it...");
}
}

apisRegistered = true;

long registerApisMillis = System.currentTimeMillis() - before;
Log.info("Registered: " + RequestServer.numRoutes() + " REST APIs in: " + registerApisMillis + "mS");
}
Log.info("Registered: " + RequestServer.numRoutes() + " REST APIs in: " + registerApisMillis + "ms");

// Register all schemas
SchemaServer.registerAllSchemasIfNecessary(dummyRestApiContext.getAllSchemas());
}

//-------------------------------------------------------------------------------------------------------------------

public static class AboutEntry {
Expand Down Expand Up @@ -1454,12 +1468,13 @@ private static void startNetworkServices() {
}

// Callbacks to add new Requests & menu items
// FIXME: remove me please
static private volatile boolean _doneRequests;

static public void register(
String method_url, Class<? extends water.api.Handler> hclass, String method, String apiName, String summary
) {
if (_doneRequests) throw new IllegalArgumentException("Cannot add more Requests once the list is finalized");
//if (_doneRequests) throw new IllegalArgumentException("Cannot add more Requests once the list is finalized");
RequestServer.registerEndpoint(apiName, method_url, hclass, method, summary);
}

Expand All @@ -1469,11 +1484,12 @@ public static void registerResourceRoot(File f) {

/** Start the web service; disallow future URL registration.
* Blocks until the server is up. */
@Deprecated
static public void finalizeRegistration() {
if (_doneRequests || H2O.ARGS.disable_web) return;
_doneRequests = true;

water.api.SchemaServer.registerAllSchemasIfNecessary();
//water.api.SchemaServer.registerAllSchemasIfNecessary();
jetty.acceptRequests();
}

Expand Down
25 changes: 1 addition & 24 deletions h2o-core/src/main/java/water/api/RegisterRestApi.java
Expand Up @@ -32,27 +32,4 @@ public interface RegisterRestApi {
* @param context
*/
void registerSchemas(RestApiContext context);
}

interface RestApiContext {

Route registerEndpoint(String api_name,
String method_uri,
Class<? extends Handler> handler_class,
String handler_method,
String summary);

Route registerEndpoint(String api_name,
String http_method,
String url,
Class<? extends Handler> handler_class,
String handler_method,
String summary,
HandlerFactory handler_factory);


Route registerEndpoint(String method_uri,
Class<? extends RestApiHandler> handler_clz);

void registerSchema(Schema ... schemas);
}
}
38 changes: 38 additions & 0 deletions h2o-core/src/main/java/water/api/RequestServer.java
Expand Up @@ -892,4 +892,42 @@ private static byte[] toByteArray(InputStream is) throws IOException {
}
}


/**
* Dummy Rest API context which is redirecting calls to static method API.
*/
public static class DummyRestApiContext implements RestApiContext {
@Override
public Route registerEndpoint(String apiName, String methodUri,
Class<? extends Handler> handlerClass, String handlerMethod,
String summary) {
return RequestServer.registerEndpoint(apiName, methodUri, handlerClass, handlerMethod, summary);
}

@Override
public Route registerEndpoint(String apiName, String httpMethod, String url,
Class<? extends Handler> handlerClass, String handlerMethod,
String summary, HandlerFactory handlerFactory) {
return RequestServer.registerEndpoint(apiName, httpMethod, url, handlerClass, handlerMethod, summary, handlerFactory);
}

@Override
public Route registerEndpoint(String methodUri, Class<? extends RestApiHandler> handlerClass) {
return RequestServer.registerEndpoint(methodUri, handlerClass);
}

private Set<Schema> allSchemas = new HashSet<>();

@Override
public void registerSchema(Schema... schemas) {
for (Schema schema : schemas) {
allSchemas.add(schema);
}
}

public Schema[] getAllSchemas() {
return allSchemas.toArray(new Schema[0]);
}
};

}
33 changes: 33 additions & 0 deletions h2o-core/src/main/java/water/api/RestApiContext.java
@@ -0,0 +1,33 @@
package water.api;

import java.util.HashSet;
import java.util.Set;

/**
* REST API registration interfaces.
*
* This is an abstraction layer between a rest server and registration
* modules.
*/
public interface RestApiContext {

Route registerEndpoint(String apiName,
String methodUri,
Class<? extends Handler> handlerClass,
String handlerMethod,
String summary);

Route registerEndpoint(String apiName,
String httpMethod,
String url,
Class<? extends Handler> handlerClass,
String handlerMethod,
String summary,
HandlerFactory handlerFactory);


Route registerEndpoint(String methodUri,
Class<? extends RestApiHandler> handlerClass);

void registerSchema(Schema... schemas);
}
8 changes: 3 additions & 5 deletions h2o-core/src/main/java/water/api/SchemaServer.java
Expand Up @@ -191,15 +191,13 @@ private static void register(Schema schema) {
/**
* Find all schemas using reflection and register them.
*/
synchronized static public void registerAllSchemasIfNecessary() {
synchronized static public void registerAllSchemasIfNecessary(Schema ... schemas) {
if (schemas_registered) return;
long startTime = System.currentTimeMillis();

ServiceLoader<Schema> loader = ServiceLoader.load(Schema.class);
for (Schema schema : loader) {
for (Schema schema : schemas) {
register(schema);
}

Log.info("Registered: " + schemas().size() + " schemas in " + (System.currentTimeMillis() - startTime) + "ms");
schemas_registered = true;
}
Expand Down

0 comments on commit 9489431

Please sign in to comment.