Commit caac858f by Dan Markwat

Sweeping changes to the Service implementations.

ServiceInitializer and Services collection no longer exist and have been replaced by Guice dependency injection. Added comments and improved testability. Maven verify now runs - almost ready for CI (issues with keytool maven plugin)
parent a56bf342
...@@ -27,13 +27,6 @@ import java.io.IOException; ...@@ -27,13 +27,6 @@ import java.io.IOException;
public interface Service extends Closeable { public interface Service extends Closeable {
/** /**
* Name of the service.
*
* @return name of the service
*/
String getName();
/**
* Starts the service. This method blocks until the service has completely started. * Starts the service. This method blocks until the service has completely started.
* *
* @throws Exception * @throws Exception
......
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.metadata.service;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.util.ReflectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Initializer that uses at startup to bring up all the Metadata startup
* services.
*/
public class ServiceInitializer {
private static final Logger LOG = LoggerFactory
.getLogger(ServiceInitializer.class);
private final Services services = Services.get();
// default property file name/path
private static final String DEFAULT_CONFIG_PATH = "application.properties";
// system property referenced by this class to extract user-overriden
// properties file
public static final String PROPERTIES_SYS_PROP = "metadata.properties";
// Path to the properties file (must be on the classpath for
// PropertiesConfiguration to work)
private final String propertyPath;
/**
* Default constructor. Use the metadata.properties System property to
* determine the property file name.
*/
public ServiceInitializer() {
propertyPath = System.getProperty(PROPERTIES_SYS_PROP,
DEFAULT_CONFIG_PATH);
}
/**
* Create a ServiceInitializer, specifying the properties file filename
* explicitly
*
* @param propPath
* the filename of the properties file with the service
* intializer information
*/
public ServiceInitializer(String propPath) {
propertyPath = propPath;
}
/**
* Get the configuration properties for the ServiceInitializer
*
* @return
* @throws ConfigurationException
*/
public PropertiesConfiguration getConfiguration()
throws ConfigurationException {
return new PropertiesConfiguration(propertyPath);
}
/**
* Initialize the services specified by the application.services property
*
* @throws MetadataException
*/
public void initialize() throws MetadataException {
/*
* TODO - determine whether this service model is the right model;
* Inter-service dependencies can wreak havoc using the current model
*/
String[] serviceClassNames;
LOG.info("Loading services using properties file: {}", propertyPath);
try {
PropertiesConfiguration configuration = getConfiguration();
serviceClassNames = configuration
.getStringArray("application.services");
} catch (ConfigurationException e) {
throw new RuntimeException("unable to get server properties");
}
for (String serviceClassName : serviceClassNames) {
serviceClassName = serviceClassName.trim();
if (serviceClassName.isEmpty()) {
continue;
}
Service service = ReflectionUtils
.getInstanceByClassName(serviceClassName);
services.register(service);
LOG.info("Initializing service: {}", serviceClassName);
try {
service.start();
} catch (Throwable t) {
LOG.error("Failed to initialize service {}", serviceClassName,
t);
throw new MetadataException(t);
}
LOG.info("Service initialized: {}", serviceClassName);
}
}
public void destroy() throws MetadataException {
for (Service service : services) {
LOG.info("Destroying service: {}", service.getClass().getName());
try {
service.stop();
} catch (Throwable t) {
LOG.error("Failed to destroy service {}", service.getClass()
.getName(), t);
throw new MetadataException(t);
}
LOG.info("Service destroyed: {}", service.getClass().getName());
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.metadata.service;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.util.ReflectionUtils;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* Repository of services initialized at startup.
*/
public final class Services implements Iterable<Service> {
private static final Services INSTANCE = new Services();
private Services() {
}
public static Services get() {
return INSTANCE;
}
private final Map<String, Service> services =
new LinkedHashMap<String, Service>();
public synchronized void register(Service service) throws MetadataException {
if (services.containsKey(service.getName())) {
throw new MetadataException("Service " + service.getName() + " already registered");
} else {
services.put(service.getName(), service);
}
}
@SuppressWarnings("unchecked")
public <T extends Service> T getService(String serviceName) {
if (services.containsKey(serviceName)) {
return (T) services.get(serviceName);
} else {
throw new NoSuchElementException(
"Service " + serviceName + " not registered with registry");
}
}
public boolean isRegistered(String serviceName) {
return services.containsKey(serviceName);
}
@Override
public Iterator<Service> iterator() {
return services.values().iterator();
}
public Service init(String serviceName) throws MetadataException {
if (isRegistered(serviceName)) {
throw new MetadataException("Service is already initialized " + serviceName);
}
String serviceClassName;
try {
PropertiesConfiguration configuration =
new PropertiesConfiguration("application.properties");
serviceClassName = configuration.getString(serviceName + ".impl");
} catch (ConfigurationException e) {
throw new MetadataException("unable to get server properties");
}
Service service = ReflectionUtils.getInstanceByClassName(serviceClassName);
register(service);
return service;
}
public void reset() {
services.clear();
}
}
package org.apache.hadoop.metadata.service;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Unit test for the Service Initializer.
*
* Test functionality to allow loading of different property files.
*/
public class ServiceInitializerTest {
private final String propertiesFileName = "test.application.properties";
private ServiceInitializer sinit;
@BeforeClass
public void setUp() throws Exception {
// setup for the test properties file
System.setProperty(ServiceInitializer.PROPERTIES_SYS_PROP,
propertiesFileName);
sinit = new ServiceInitializer();
}
@AfterClass
public void tearDown() throws Exception {
// test destruction of the Services - no exceptions is assumed a success
sinit.destroy();
}
@Test
public void testPropsAreSet() throws Exception {
Assert.assertEquals(
sinit.getConfiguration().getString(
"application.services"),
TestService.NAME);
}
@Test
public void testInitialize() throws Exception {
// test the initialization of the initializer
// no exceptions is assumed a success
sinit.initialize();
}
}
package org.apache.hadoop.metadata.service;
import java.io.IOException;
public class TestService implements Service {
public static final String NAME = TestService.class.getName();
@Override
public String getName() {
return NAME;
}
@Override
public void start() throws Exception {
}
@Override
public void stop() {
}
@Override
public void close() throws IOException {
}
}
application.services=org.apache.hadoop.metadata.service.TestService
\ No newline at end of file
...@@ -24,13 +24,14 @@ ...@@ -24,13 +24,14 @@
*/ */
package org.apache.hadoop.metadata; package org.apache.hadoop.metadata;
import org.apache.hadoop.metadata.services.GraphBackedMetadataRepositoryService; import org.apache.hadoop.metadata.services.GraphBackedMetadataRepository;
import org.apache.hadoop.metadata.services.GraphProvider; import org.apache.hadoop.metadata.services.GraphProvider;
import org.apache.hadoop.metadata.services.GraphService; import org.apache.hadoop.metadata.services.GraphService;
import org.apache.hadoop.metadata.services.GraphServiceConfigurator; import org.apache.hadoop.metadata.services.GraphServiceConfigurator;
import org.apache.hadoop.metadata.services.MetadataRepositoryService; import org.apache.hadoop.metadata.services.MetadataRepository;
import org.apache.hadoop.metadata.services.TitanGraphProvider; import org.apache.hadoop.metadata.services.TitanGraphProvider;
import com.google.inject.Scopes;
import com.google.inject.throwingproviders.ThrowingProviderBinder; import com.google.inject.throwingproviders.ThrowingProviderBinder;
import com.thinkaurelius.titan.core.TitanGraph; import com.thinkaurelius.titan.core.TitanGraph;
...@@ -42,26 +43,27 @@ public class RepositoryMetadataModule extends com.google.inject.AbstractModule { ...@@ -42,26 +43,27 @@ public class RepositoryMetadataModule extends com.google.inject.AbstractModule {
// Graph Service implementation class // Graph Service implementation class
private Class<? extends GraphService> graphServiceClass; private Class<? extends GraphService> graphServiceClass;
// MetadataRepositoryService implementation class // MetadataRepositoryService implementation class
private Class<? extends MetadataRepositoryService> metadataRepoClass; private Class<? extends MetadataRepository> metadataRepoClass;
public RepositoryMetadataModule() { public RepositoryMetadataModule() {
GraphServiceConfigurator gsp = new GraphServiceConfigurator(); GraphServiceConfigurator gsp = new GraphServiceConfigurator();
// get the impl classes for the repo and the graph service
this.graphServiceClass = gsp.getImplClass(); this.graphServiceClass = gsp.getImplClass();
this.metadataRepoClass = GraphBackedMetadataRepositoryService.class; this.metadataRepoClass = GraphBackedMetadataRepository.class;
} }
protected void configure() { protected void configure() {
// special wiring for Titan Graph // special wiring for Titan Graph
ThrowingProviderBinder.create(binder()) ThrowingProviderBinder.create(binder())
.bind(GraphProvider.class, TitanGraph.class) .bind(GraphProvider.class, TitanGraph.class)
.to(TitanGraphProvider.class); .to(TitanGraphProvider.class)
.in(Scopes.SINGLETON);
// allow for dynamic binding of the metadata repo service & graph // allow for dynamic binding of the metadata repo & graph service
// service
// bind the MetadataRepositoryService interface to an implementation // bind the MetadataRepositoryService interface to an implementation
bind(MetadataRepositoryService.class).to(metadataRepoClass); bind(MetadataRepository.class).to(metadataRepoClass);
// bind the GraphService interface to an implementation // bind the GraphService interface to an implementation
bind(GraphService.class).to(graphServiceClass); bind(GraphService.class).to(graphServiceClass);
} }
......
...@@ -18,18 +18,6 @@ ...@@ -18,18 +18,6 @@
package org.apache.hadoop.metadata.services; package org.apache.hadoop.metadata.services;
import com.google.common.base.Preconditions;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.TransactionalGraph;
import com.tinkerpop.blueprints.Vertex;
import org.apache.hadoop.metadata.service.Services;
import org.apache.hadoop.metadata.util.GraphUtils;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -37,71 +25,31 @@ import java.util.UUID; ...@@ -37,71 +25,31 @@ import java.util.UUID;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.hadoop.metadata.util.GraphUtils;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.tinkerpop.blueprints.TransactionalGraph;
import com.tinkerpop.blueprints.Vertex;
/** /**
* An implementation backed by Titan Graph DB. * An implementation backed by a Graph database provided
* as a Graph Service.
*/ */
public class GraphBackedMetadataRepositoryService implements MetadataRepositoryService { public class GraphBackedMetadataRepository implements MetadataRepository {
private static final Logger LOG = private static final Logger LOG =
LoggerFactory.getLogger(GraphBackedMetadataRepositoryService.class); LoggerFactory.getLogger(GraphBackedMetadataRepository.class);
public static final String NAME = GraphBackedMetadataRepositoryService.class.getSimpleName();
private GraphService graphService; private GraphService graphService;
@Inject @Inject
GraphBackedMetadataRepositoryService(GraphService service) { GraphBackedMetadataRepository(GraphService service) {
this.graphService = service; this.graphService = service;
} }
/**
* Name of the service.
*
* @return name of the service
*/
@Override
public String getName() {
return NAME;
}
/**
* Starts the service. This method blocks until the service has completely started.
*
* @throws Exception
*/
@Override
public void start() throws Exception {
}
/**
* Stops the service. This method blocks until the service has completely shut down.
*/
@Override
public void stop() {
// do nothing
graphService = null;
}
/**
* A version of stop() that is designed to be usable in Java7 closure
* clauses.
* Implementation classes MUST relay this directly to {@link #stop()}
*
* @throws java.io.IOException never
* @throws RuntimeException on any failure during the stop operation
*/
@Override
public void close() throws IOException {
stop();
}
private Graph getBlueprintsGraph() {
return graphService.getBlueprintsGraph();
}
private TransactionalGraph getTransactionalGraph() {
return graphService.getTransactionalGraph();
}
@Override @Override
public String submitEntity(String entity, String entityType) { public String submitEntity(String entity, String entityType) {
LOG.info("adding entity={} type={}", entity, entityType); LOG.info("adding entity={} type={}", entity, entityType);
...@@ -114,7 +62,7 @@ public class GraphBackedMetadataRepositoryService implements MetadataRepositoryS ...@@ -114,7 +62,7 @@ public class GraphBackedMetadataRepositoryService implements MetadataRepositoryS
// todo check if this is a duplicate // todo check if this is a duplicate
final String guid = UUID.randomUUID().toString(); final String guid = UUID.randomUUID().toString();
final TransactionalGraph transactionalGraph = getTransactionalGraph(); final TransactionalGraph transactionalGraph = graphService.getTransactionalGraph();
try { try {
transactionalGraph.rollback(); transactionalGraph.rollback();
...@@ -137,7 +85,7 @@ public class GraphBackedMetadataRepositoryService implements MetadataRepositoryS ...@@ -137,7 +85,7 @@ public class GraphBackedMetadataRepositoryService implements MetadataRepositoryS
@Override @Override
public String getEntityDefinition(String entityName, String entityType) { public String getEntityDefinition(String entityName, String entityType) {
LOG.info("Retrieving entity name={} type={}", entityName, entityType); LOG.info("Retrieving entity name={} type={}", entityName, entityType);
Vertex entityVertex = GraphUtils.findVertex(getBlueprintsGraph(), entityName, entityType); Vertex entityVertex = GraphUtils.findVertex(graphService.getBlueprintsGraph(), entityName, entityType);
if (entityVertex == null) { if (entityVertex == null) {
return null; return null;
} }
......
...@@ -18,12 +18,13 @@ ...@@ -18,12 +18,13 @@
package org.apache.hadoop.metadata.services; package org.apache.hadoop.metadata.services;
import java.util.Set;
import org.apache.hadoop.metadata.service.Service;
import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.KeyIndexableGraph; import com.tinkerpop.blueprints.KeyIndexableGraph;
import com.tinkerpop.blueprints.TransactionalGraph; import com.tinkerpop.blueprints.TransactionalGraph;
import org.apache.hadoop.metadata.service.Service;
import java.util.Set;
/** /**
* A blueprints based graph service. * A blueprints based graph service.
......
...@@ -25,7 +25,7 @@ import java.util.List; ...@@ -25,7 +25,7 @@ import java.util.List;
/** /**
* An interface for persisting metadata into a blueprints enabled graph db. * An interface for persisting metadata into a blueprints enabled graph db.
*/ */
public interface MetadataRepositoryService extends Service { public interface MetadataRepository {
String submitEntity(String entity, String entityType); String submitEntity(String entity, String entityType);
......
...@@ -18,22 +18,6 @@ ...@@ -18,22 +18,6 @@
package org.apache.hadoop.metadata.services; package org.apache.hadoop.metadata.services;
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.KeyIndexableGraph;
import com.tinkerpop.blueprints.TransactionalGraph;
import com.tinkerpop.blueprints.Vertex;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
...@@ -42,14 +26,30 @@ import java.util.List; ...@@ -42,14 +26,30 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.schema.TitanGraphIndex;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.KeyIndexableGraph;
import com.tinkerpop.blueprints.TransactionalGraph;
import com.tinkerpop.blueprints.Vertex;
/** /**
* Default implementation for Graph service backed by Titan. * Default implementation for Graph service backed by Titan.
*/ */
@Singleton
public class TitanGraphService implements GraphService { public class TitanGraphService implements GraphService {
private static final Logger LOG = LoggerFactory.getLogger(TitanGraphService.class); private static final Logger LOG = LoggerFactory.getLogger(TitanGraphService.class);
public static final String NAME = TitanGraphService.class.getSimpleName();
/** /**
* Constant for the configuration property that indicates the prefix. * Constant for the configuration property that indicates the prefix.
...@@ -59,32 +59,25 @@ public class TitanGraphService implements GraphService { ...@@ -59,32 +59,25 @@ public class TitanGraphService implements GraphService {
private final TitanGraph titanGraph; private final TitanGraph titanGraph;
@Inject
TitanGraphService(GraphProvider<TitanGraph> graph) throws ConfigurationException {
this.titanGraph = graph.get();
// TODO decouple from Service class and run start() here
// can use a shutdown hook to run the stop() method
// this.start();
}
/** /**
* Name of the service. * Initialize this service through injection with a custom Provider.
* *
* @return name of the service * @param graph
* @throws ConfigurationException
*/ */
@Override @Inject
public String getName() { TitanGraphService(GraphProvider<TitanGraph> graph) throws ConfigurationException {
return NAME; // TODO reimplement to save the Provider and initialize the graph inside the start() method
this.titanGraph = graph.get();
//start();
} }
/** /**
* Starts the service. This method blocks until the service has completely * Initializes this Service. The starting of Titan is handled by the Provider
* started. * @throws ConfigurationException
*
* @throws Exception
*/ */
@Override @Override
public void start() throws Exception { public void start() throws ConfigurationException {
createIndicesForVertexKeys(); createIndicesForVertexKeys();
// todo - create Edge Cardinality Constraints // todo - create Edge Cardinality Constraints
LOG.info("Initialized titanGraph db: {}", titanGraph); LOG.info("Initialized titanGraph db: {}", titanGraph);
...@@ -116,13 +109,12 @@ public class TitanGraphService implements GraphService { ...@@ -116,13 +109,12 @@ public class TitanGraphService implements GraphService {
return graphConfig; return graphConfig;
} }
protected TitanGraph initializeGraphDB(Configuration graphConfig) { /**
LOG.info("Initializing titanGraph db"); * Initializes the indices for the graph.
return TitanFactory.open(graphConfig); * @throws ConfigurationException
} */
// TODO move this functionality to the MetadataRepository?
protected void createIndicesForVertexKeys() throws ConfigurationException { protected void createIndicesForVertexKeys() throws ConfigurationException {
if (!titanGraph.getIndexedKeys(Vertex.class).isEmpty()) { if (!titanGraph.getIndexedKeys(Vertex.class).isEmpty()) {
LOG.info("Indexes already exist for titanGraph"); LOG.info("Indexes already exist for titanGraph");
return; return;
......
...@@ -19,7 +19,7 @@ public class GraphBackedMetadataRepositoryServiceTest extends RepositoryModuleBa ...@@ -19,7 +19,7 @@ public class GraphBackedMetadataRepositoryServiceTest extends RepositoryModuleBa
private static final String TABLE_NAME = "clicks-table"; private static final String TABLE_NAME = "clicks-table";
private TitanGraphService titanGraphService; private TitanGraphService titanGraphService;
private GraphBackedMetadataRepositoryService repositoryService; private GraphBackedMetadataRepository repositoryService;
@BeforeClass @BeforeClass
public void setUp() throws Exception { public void setUp() throws Exception {
...@@ -27,8 +27,7 @@ public class GraphBackedMetadataRepositoryServiceTest extends RepositoryModuleBa ...@@ -27,8 +27,7 @@ public class GraphBackedMetadataRepositoryServiceTest extends RepositoryModuleBa
titanGraphService.start(); titanGraphService.start();
//Services.get().register(titanGraphService); //Services.get().register(titanGraphService);
repositoryService = super.injector.getInstance(GraphBackedMetadataRepositoryService.class); repositoryService = super.injector.getInstance(GraphBackedMetadataRepository.class);
repositoryService.start();
//Services.get().register(repositoryService); //Services.get().register(repositoryService);
} }
...@@ -41,14 +40,6 @@ public class GraphBackedMetadataRepositoryServiceTest extends RepositoryModuleBa ...@@ -41,14 +40,6 @@ public class GraphBackedMetadataRepositoryServiceTest extends RepositoryModuleBa
} }
@Test @Test
public void testGetName() throws Exception {
Assert.assertEquals(GraphBackedMetadataRepositoryService.NAME,
GraphBackedMetadataRepositoryService.class.getSimpleName());
Assert.assertEquals(repositoryService.getName(),
GraphBackedMetadataRepositoryService.NAME);
}
@Test
public void testSubmitEntity() throws Exception { public void testSubmitEntity() throws Exception {
String entityStream = getTestEntityJSON(); String entityStream = getTestEntityJSON();
String guid = repositoryService.submitEntity(entityStream, ENTITY_TYPE); String guid = repositoryService.submitEntity(entityStream, ENTITY_TYPE);
......
...@@ -26,12 +26,6 @@ public class TitanGraphServiceTest extends RepositoryModuleBaseTest { ...@@ -26,12 +26,6 @@ public class TitanGraphServiceTest extends RepositoryModuleBaseTest {
} }
@Test @Test
public void testGetName() throws Exception {
Assert.assertEquals(TitanGraphService.NAME, TitanGraphService.class.getSimpleName());
Assert.assertEquals(titanGraphService.getName(), TitanGraphService.NAME);
}
@Test
public void testStart() throws Exception { public void testStart() throws Exception {
Assert.assertNotNull(titanGraphService.getBlueprintsGraph()); Assert.assertNotNull(titanGraphService.getBlueprintsGraph());
} }
......
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.metadata.web.listeners;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.service.ServiceInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* Listener for bootstrapping Services and configuration properties.
*/
public class ApplicationStartupListener implements ServletContextListener {
private static final Logger LOG = LoggerFactory.getLogger(ApplicationStartupListener.class);
private final ServiceInitializer startupServices = new ServiceInitializer();
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
startupServices.initialize();
showStartupInfo();
} catch (MetadataException e) {
throw new RuntimeException("Error starting services", e);
}
}
private void showStartupInfo() {
StringBuilder buffer = new StringBuilder();
buffer.append("\n############################################");
buffer.append("\n Metadata Server (STARTED) ");
buffer.append("\n############################################");
try {
PropertiesConfiguration configuration = new PropertiesConfiguration("application.properties");
buffer.append(configuration.toString());
} catch (ConfigurationException e) {
buffer.append("*** Unable to get build info ***").append(e.getMessage());
}
LOG.info(buffer.toString());
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
try {
startupServices.destroy();
} catch (MetadataException e) {
LOG.warn("Error destroying services", e);
}
StringBuilder buffer = new StringBuilder();
buffer.append("\n############################################");
buffer.append("\n Metadata Server (SHUTDOWN) ");
buffer.append("\n############################################");
LOG.info(buffer.toString());
}
}
\ No newline at end of file
...@@ -4,6 +4,9 @@ import java.util.HashMap; ...@@ -4,6 +4,9 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.hadoop.metadata.RepositoryMetadataModule; import org.apache.hadoop.metadata.RepositoryMetadataModule;
import org.apache.hadoop.metadata.services.GraphService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
...@@ -14,15 +17,18 @@ import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; ...@@ -14,15 +17,18 @@ import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
public class GuiceServletConfig extends GuiceServletContextListener { public class GuiceServletConfig extends GuiceServletContextListener {
private static final Logger LOG = LoggerFactory
.getLogger(GuiceServletConfig.class);
private static final String GUICE_CTX_PARAM = "guice.packages"; private static final String GUICE_CTX_PARAM = "guice.packages";
@Override @Override
protected Injector getInjector() { protected Injector getInjector() {
LOG.info("Loading Guice modules");
/* /*
* More information on this can be found here: * More information on this can be found here:
* https://jersey.java.net/nonav/apidocs/1.11/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html * https://jersey.java.net/nonav/apidocs/1.11/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html
*/ */
return Guice.createInjector( Injector injector = Guice.createInjector(
new RepositoryMetadataModule(), new RepositoryMetadataModule(),
new JerseyServletModule() { new JerseyServletModule() {
...@@ -30,10 +36,30 @@ public class GuiceServletConfig extends GuiceServletContextListener { ...@@ -30,10 +36,30 @@ public class GuiceServletConfig extends GuiceServletContextListener {
protected void configureServlets() { protected void configureServlets() {
String packages = getServletContext().getInitParameter(GUICE_CTX_PARAM); String packages = getServletContext().getInitParameter(GUICE_CTX_PARAM);
LOG.info("Jersey loading from packages: " + packages);
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, packages); params.put(PackagesResourceConfig.PROPERTY_PACKAGES, packages);
serve("/*").with(GuiceContainer.class, params); serve("/api/metadata/*").with(GuiceContainer.class, params);
} }
}); });
LOG.info("Guice modules loaded");
LOG.info("Bootstrapping services");
// get the Graph Service
GraphService graphService = injector.getInstance(GraphService.class);
try {
// start/init the service
graphService.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
LOG.info(String.format("Loaded Service: %s", graphService.getClass().getName()));
LOG.info("Services bootstrapped successfully");
return injector;
} }
} }
...@@ -39,7 +39,7 @@ import javax.ws.rs.core.MediaType; ...@@ -39,7 +39,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.hadoop.metadata.services.MetadataRepositoryService; import org.apache.hadoop.metadata.services.MetadataRepository;
import org.apache.hadoop.metadata.web.util.Servlets; import org.apache.hadoop.metadata.web.util.Servlets;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
...@@ -57,11 +57,17 @@ import com.google.common.base.Preconditions; ...@@ -57,11 +57,17 @@ import com.google.common.base.Preconditions;
@Singleton @Singleton
public class EntityResource { public class EntityResource {
private final MetadataRepositoryService repositoryService; private final MetadataRepository repository;
/**
* Created by the Guice ServletModule and injected with the
* configured MetadataRepository.
*
* @param repository
*/
@Inject @Inject
public EntityResource(MetadataRepositoryService repositoryService) { public EntityResource(MetadataRepository repository) {
this.repositoryService = repositoryService; this.repository = repository;
} }
@POST @POST
...@@ -75,7 +81,7 @@ public class EntityResource { ...@@ -75,7 +81,7 @@ public class EntityResource {
System.out.println("entity = " + entity); System.out.println("entity = " + entity);
validateEntity(entity, entityType); validateEntity(entity, entityType);
final String guid = repositoryService.submitEntity(entity, entityType); final String guid = repository.submitEntity(entity, entityType);
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put("GUID", guid); response.put("GUID", guid);
...@@ -112,7 +118,7 @@ public class EntityResource { ...@@ -112,7 +118,7 @@ public class EntityResource {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getEntityDefinition(@PathParam("entityType") String entityType, public Response getEntityDefinition(@PathParam("entityType") String entityType,
@PathParam("entityName") String entityName) { @PathParam("entityName") String entityName) {
final String entityDefinition = repositoryService.getEntityDefinition(entityName, entityType); final String entityDefinition = repository.getEntityDefinition(entityName, entityType);
return (entityDefinition == null) return (entityDefinition == null)
? Response.status(Response.Status.NOT_FOUND).build() ? Response.status(Response.Status.NOT_FOUND).build()
: Response.ok(entityDefinition).build(); : Response.ok(entityDefinition).build();
......
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
application.services=org.apache.hadoop.metadata.services.TitanGraphService,\
org.apache.hadoop.metadata.services.GraphBackedMetadataRepositoryService
######### Implementation classes #########
## DO NOT MODIFY UNLESS SURE ABOUT CHANGE ##
metadata.GraphService.impl=org.apache.hadoop.metadata.services.TitanGraphService
metadata.MetadataRepositoryService.impl=org.apache.hadoop.metadata.services.GraphBackedMetadataRepositoryService
######### Implementation classes #########
######### Graph Database Configs #########
# Graph implementation
#metadata.graph.blueprints.graph=com.thinkaurelius.titan.core.TitanFactory
# Graph Storage
metadata.graph.storage.backend=berkeleyje
metadata.graph.storage.directory=target/data/berkeley
# Graph Search Index
metadata.graph.index.search.backend=elasticsearch
metadata.graph.index.search.directory=target/data/es
metadata.graph.index.search.elasticsearch.client-only=false
metadata.graph.index.search.elasticsearch.local-mode=true
######### Graph Database Configs #########
######### Security Properties #########
# SSL config
metadata.enableTLS=false
######### Security Properties #########
...@@ -40,53 +40,9 @@ ...@@ -40,53 +40,9 @@
</filter-mapping> </filter-mapping>
<listener> <listener>
<listener-class>org.apache.hadoop.metadata.web.listeners.GuiceServletConfig</listener-class> <listener-class>org.apache.hadoop.metadata.web.listeners.ApplicationStartupListener</listener-class>
</listener> </listener>
<!--
<filter>
<filter-name>audit</filter-name>
<filter-class>org.apache.hadoop.metadata.web.filters.AuditFilter</filter-class>
</filter>
<filter>
<filter-name>authentication</filter-name>
<filter-class>org.apache.hadoop.metadata.web.filters.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>audit</filter-name>
<servlet-name>MetadataRESTApi</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>authentication</filter-name>
<servlet-name>MetadataRESTApi</servlet-name>
</filter-mapping>
<listener> <listener>
<listener-class>org.apache.hadoop.metadata.web.listeners.ApplicationStartupListener</listener-class> <listener-class>org.apache.hadoop.metadata.web.listeners.GuiceServletConfig</listener-class>
</listener> </listener>
<servlet>
<servlet-name>MetadataRESTApi</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
org.apache.hadoop.metadata.web.resources,org.apache.hadoop.metadata.web.params
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MetadataRESTApi</servlet-name>
<url-pattern>/api/metadata/*</url-pattern>
</servlet-mapping>
-->
</web-app> </web-app>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment