Repository: knox
Updated Branches:
  refs/heads/master f76eec991 -> 4fe30705b


KNOX-764 initial service definition registry service


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

Branch: refs/heads/master
Commit: 4fe30705b16ca54dc6a58a88fdd135a4dc59bc75
Parents: f76eec9
Author: Sumit Gupta <su...@apache.org>
Authored: Thu Oct 27 14:27:47 2016 -0400
Committer: Sumit Gupta <su...@apache.org>
Committed: Thu Oct 27 14:28:40 2016 -0400

----------------------------------------------------------------------
 .../apache/hadoop/gateway/GatewayMessages.java  |  3 +
 .../services/DefaultGatewayServices.java        |  5 ++
 .../registry/impl/DefaultServiceDefEntry.java   | 52 +++++++++++
 .../impl/DefaultServiceDefinitionRegistry.java  | 95 ++++++++++++++++++++
 .../DefaultServiceDefinitionRegistryTest.java   | 46 ++++++++++
 .../resources/services/foo/1.0.0/service.xml    |  3 +
 .../gateway/services/GatewayServices.java       |  1 +
 .../services/registry/ServiceDefEntry.java      | 27 ++++++
 .../registry/ServiceDefinitionRegistry.java     | 25 ++++++
 9 files changed, 257 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayMessages.java
----------------------------------------------------------------------
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayMessages.java 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayMessages.java
index c256a65..93dc68f 100644
--- 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayMessages.java
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/GatewayMessages.java
@@ -421,4 +421,7 @@ public interface GatewayMessages {
 
   @Message( level = MessageLevel.WARN, text = "Only retrieved first {0} groups 
due to SizeLimitExceededException." )
   void sizeLimitExceededOnlyRetrieved(int numResults);
+
+  @Message( level = MessageLevel.DEBUG, text = "Failed to parse path into 
Template: {0} : {1}" )
+  void failedToParsePath( String path, @StackTrace( level = MessageLevel.DEBUG 
) Exception e );
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
----------------------------------------------------------------------
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
index 4455078..1024d7f 100644
--- 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
@@ -23,6 +23,7 @@ import org.apache.hadoop.gateway.deploy.DeploymentContext;
 import org.apache.hadoop.gateway.descriptor.FilterParamDescriptor;
 import org.apache.hadoop.gateway.descriptor.ResourceDescriptor;
 import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
+import 
org.apache.hadoop.gateway.services.registry.impl.DefaultServiceDefinitionRegistry;
 import org.apache.hadoop.gateway.services.topology.impl.DefaultTopologyService;
 import 
org.apache.hadoop.gateway.services.hostmap.impl.DefaultHostMapperService;
 import 
org.apache.hadoop.gateway.services.registry.impl.DefaultServiceRegistryService;
@@ -105,6 +106,10 @@ public class DefaultGatewayServices implements 
GatewayServices {
     DefaultTopologyService tops = new DefaultTopologyService();
     tops.init(  config, options  );
     services.put(  TOPOLOGY_SERVICE, tops  );
+
+    DefaultServiceDefinitionRegistry sdr = new 
DefaultServiceDefinitionRegistry();
+    sdr.init( config, options );
+    services.put( SERVICE_DEFINITION_REGISTRY, sdr );
   }
   
   public void start() throws ServiceLifecycleException {

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefEntry.java
----------------------------------------------------------------------
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefEntry.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefEntry.java
new file mode 100644
index 0000000..ba385db
--- /dev/null
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefEntry.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway.services.registry.impl;
+
+import org.apache.hadoop.gateway.services.registry.ServiceDefEntry;
+
+public class DefaultServiceDefEntry implements ServiceDefEntry {
+
+  private String role;
+
+  private String name;
+
+  private String route;
+
+  public DefaultServiceDefEntry(String role, String name, String route) {
+    this.role = role;
+    this.name = name;
+    this.route = route;
+  }
+
+  @Override
+  public String getRole() {
+    return role;
+  }
+
+  @Override
+  public String getName() {
+    return name;
+  }
+
+  @Override
+  public String getRoute() {
+    return route;
+  }
+
+}
+

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefinitionRegistry.java
----------------------------------------------------------------------
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefinitionRegistry.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefinitionRegistry.java
new file mode 100644
index 0000000..8a5b113
--- /dev/null
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceDefinitionRegistry.java
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway.services.registry.impl;
+
+import org.apache.hadoop.gateway.GatewayMessages;
+import org.apache.hadoop.gateway.config.GatewayConfig;
+import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
+import org.apache.hadoop.gateway.service.definition.Route;
+import org.apache.hadoop.gateway.service.definition.ServiceDefinition;
+import org.apache.hadoop.gateway.services.Service;
+import org.apache.hadoop.gateway.services.ServiceLifecycleException;
+import org.apache.hadoop.gateway.services.registry.ServiceDefEntry;
+import org.apache.hadoop.gateway.services.registry.ServiceDefinitionRegistry;
+import org.apache.hadoop.gateway.util.ServiceDefinitionsLoader;
+import org.apache.hadoop.gateway.util.urltemplate.Matcher;
+import org.apache.hadoop.gateway.util.urltemplate.Parser;
+import org.apache.hadoop.gateway.util.urltemplate.Template;
+
+import java.io.File;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class DefaultServiceDefinitionRegistry implements 
ServiceDefinitionRegistry, Service {
+
+  private static GatewayMessages LOG = MessagesFactory.get( 
GatewayMessages.class );
+
+  private Matcher<ServiceDefEntry> entries = new Matcher<>();
+
+  @Override
+  public void init(GatewayConfig config, Map<String, String> options) throws 
ServiceLifecycleException {
+    String stacks = config.getGatewayServicesDir();
+    File stacksDir = new File(stacks);
+    Set<ServiceDefinition> serviceDefinitions = 
ServiceDefinitionsLoader.getServiceDefinitions(stacksDir);
+
+    for (ServiceDefinition serviceDefinition : serviceDefinitions) {
+      List<Route> routes = serviceDefinition.getRoutes();
+      for (Route route : routes) {
+        try {
+          Template template = Parser.parseTemplate(route.getPath());
+          addServiceDefEntry(template, serviceDefinition);
+        } catch ( URISyntaxException e ) {
+          LOG.failedToParsePath(route.getPath(), e);
+        }
+      }
+    }
+  }
+
+  private void addServiceDefEntry(Template template, ServiceDefinition 
serviceDefinition) {
+    ServiceDefEntry entry = entries.get(template);
+    if (entry == null) {
+      entries.add(template, new 
DefaultServiceDefEntry(serviceDefinition.getRole(), 
serviceDefinition.getName(), template.getPattern()));
+    }
+  }
+
+  @Override
+  public void start() throws ServiceLifecycleException {
+
+  }
+
+  @Override
+  public void stop() throws ServiceLifecycleException {
+
+  }
+
+  @Override
+  public ServiceDefEntry getMatchingService(String urlPattern) {
+    Matcher<ServiceDefEntry>.Match match = null;
+    try {
+      match =  entries.match(Parser.parseLiteral(urlPattern));
+    } catch ( URISyntaxException e ) {
+      LOG.failedToParsePath(urlPattern, e);
+    }
+    if (match != null) {
+      return match.getValue();
+    }
+    return null;
+  }
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-server/src/test/java/org/apache/hadoop/gateway/services/registry/DefaultServiceDefinitionRegistryTest.java
----------------------------------------------------------------------
diff --git 
a/gateway-server/src/test/java/org/apache/hadoop/gateway/services/registry/DefaultServiceDefinitionRegistryTest.java
 
b/gateway-server/src/test/java/org/apache/hadoop/gateway/services/registry/DefaultServiceDefinitionRegistryTest.java
new file mode 100644
index 0000000..4556a0e
--- /dev/null
+++ 
b/gateway-server/src/test/java/org/apache/hadoop/gateway/services/registry/DefaultServiceDefinitionRegistryTest.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway.services.registry;
+
+import org.apache.hadoop.gateway.config.GatewayConfig;
+import 
org.apache.hadoop.gateway.services.registry.impl.DefaultServiceDefinitionRegistry;
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+import java.io.File;
+import java.net.URL;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+public class DefaultServiceDefinitionRegistryTest {
+
+  @Test
+  public void matchSimplePattern() throws Exception {
+    DefaultServiceDefinitionRegistry registry = new 
DefaultServiceDefinitionRegistry();
+    GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
+    URL url = ClassLoader.getSystemResource("services");
+    EasyMock.expect(config.getGatewayServicesDir()).andReturn(new 
File(url.getFile()).getAbsolutePath()).anyTimes();
+    EasyMock.replay(config);
+    registry.init(config, null);
+    ServiceDefEntry entry = registry.getMatchingService("/foo/somepath");
+    assertThat( entry.getRole(), is("FOO"));
+    entry = registry.getMatchingService("/bar/?somepath");
+    assertThat( entry.getRole(), is("BAR"));
+  }
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-server/src/test/resources/services/foo/1.0.0/service.xml
----------------------------------------------------------------------
diff --git a/gateway-server/src/test/resources/services/foo/1.0.0/service.xml 
b/gateway-server/src/test/resources/services/foo/1.0.0/service.xml
index f739b0b..04e6009 100644
--- a/gateway-server/src/test/resources/services/foo/1.0.0/service.xml
+++ b/gateway-server/src/test/resources/services/foo/1.0.0/service.xml
@@ -19,5 +19,8 @@
         <route path="/foo/?**">
             <rewrite apply="FOO/foo/inbound" to="request.url"/>
         </route>
+        <route path="/foo/**">
+            <rewrite apply="FOO/foo/inbound" to="request.url"/>
+        </route>
     </routes>
 </service>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/GatewayServices.java
----------------------------------------------------------------------
diff --git 
a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/GatewayServices.java
 
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/GatewayServices.java
index 33c844d..cc4f312 100644
--- 
a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/GatewayServices.java
+++ 
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/GatewayServices.java
@@ -36,6 +36,7 @@ public interface GatewayServices extends Service, 
ProviderDeploymentContributor
   public static final String HOST_MAPPING_SERVICE = "HostMappingService";
   public static final String SERVER_INFO_SERVICE = "ServerInfoService";
   public static final String TOPOLOGY_SERVICE = "TopologyService";
+  public static final String SERVICE_DEFINITION_REGISTRY = 
"ServiceDefinitionRegistry";
 
   public abstract Collection<String> getServiceNames();
 

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefEntry.java
----------------------------------------------------------------------
diff --git 
a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefEntry.java
 
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefEntry.java
new file mode 100644
index 0000000..d4f131a
--- /dev/null
+++ 
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefEntry.java
@@ -0,0 +1,27 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway.services.registry;
+
+public interface ServiceDefEntry {
+
+  String getRole();
+
+  String getName();
+
+  String getRoute();
+}

http://git-wip-us.apache.org/repos/asf/knox/blob/4fe30705/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefinitionRegistry.java
----------------------------------------------------------------------
diff --git 
a/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefinitionRegistry.java
 
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefinitionRegistry.java
new file mode 100644
index 0000000..3a557b2
--- /dev/null
+++ 
b/gateway-spi/src/main/java/org/apache/hadoop/gateway/services/registry/ServiceDefinitionRegistry.java
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.gateway.services.registry;
+
+public interface ServiceDefinitionRegistry {
+
+  ServiceDefEntry getMatchingService(String urlPattern);
+
+
+}

Reply via email to