Commit 1143f69d by Dan Markwat

Updated ServiceInitializer to add ability to load different properties

files (e.g. for testing). Also added a unit test to ensure this functionality is working.
parent f8356cb5
...@@ -26,19 +26,74 @@ import org.slf4j.Logger; ...@@ -26,19 +26,74 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* Initializer that uses at startup to bring up all the Metadata startup services. * Initializer that uses at startup to bring up all the Metadata startup
* services.
*/ */
public class ServiceInitializer { public class ServiceInitializer {
private static final Logger LOG = LoggerFactory.getLogger(ServiceInitializer.class); private static final Logger LOG = LoggerFactory
.getLogger(ServiceInitializer.class);
private final Services services = Services.get(); 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 { 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; String[] serviceClassNames;
LOG.info("Loading services using properties file: {}", propertyPath);
try { try {
PropertiesConfiguration configuration = PropertiesConfiguration configuration = getConfiguration();
new PropertiesConfiguration("application.properties"); serviceClassNames = configuration
serviceClassNames = configuration.getStringArray("application.services"); .getStringArray("application.services");
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
throw new RuntimeException("unable to get server properties"); throw new RuntimeException("unable to get server properties");
} }
...@@ -48,13 +103,15 @@ public class ServiceInitializer { ...@@ -48,13 +103,15 @@ public class ServiceInitializer {
if (serviceClassName.isEmpty()) { if (serviceClassName.isEmpty()) {
continue; continue;
} }
Service service = ReflectionUtils.getInstanceByClassName(serviceClassName); Service service = ReflectionUtils
.getInstanceByClassName(serviceClassName);
services.register(service); services.register(service);
LOG.info("Initializing service: {}", serviceClassName); LOG.info("Initializing service: {}", serviceClassName);
try { try {
service.start(); service.start();
} catch (Throwable t) { } catch (Throwable t) {
LOG.error("Failed to initialize service {}", serviceClassName, t); LOG.error("Failed to initialize service {}", serviceClassName,
t);
throw new MetadataException(t); throw new MetadataException(t);
} }
LOG.info("Service initialized: {}", serviceClassName); LOG.info("Service initialized: {}", serviceClassName);
...@@ -67,7 +124,8 @@ public class ServiceInitializer { ...@@ -67,7 +124,8 @@ public class ServiceInitializer {
try { try {
service.stop(); service.stop();
} catch (Throwable t) { } catch (Throwable t) {
LOG.error("Failed to destroy service {}", service.getClass().getName(), t); LOG.error("Failed to destroy service {}", service.getClass()
.getName(), t);
throw new MetadataException(t); throw new MetadataException(t);
} }
LOG.info("Service destroyed: {}", service.getClass().getName()); LOG.info("Service destroyed: {}", service.getClass().getName());
......
application.services=org.apache.hadoop.metadata.service.TestService
\ No newline at end of file
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 {
}
}
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