Merge pull request #281 from ankurkaushal/master

Reformat according to google style guide
This commit is contained in:
Ilkka Seppälä
2015-11-02 21:39:17 +02:00
438 changed files with 8257 additions and 8382 deletions

View File

@ -2,31 +2,33 @@ package com.iluwatar.servicelocator;
/**
*
* The Service Locator pattern is a design pattern used in software development
* to encapsulate the processes involved in obtaining a service with a strong
* abstraction layer. This pattern uses a central registry known as the "service
* locator", which on request returns the information necessary to perform a certain task.
* The Service Locator pattern is a design pattern used in software development to encapsulate the
* processes involved in obtaining a service with a strong abstraction layer. This pattern uses a
* central registry known as the "service locator", which on request returns the information
* necessary to perform a certain task.
* <p>
* In this example we use the Service locator pattern to lookup JNDI-services
* and cache them for subsequent requests.
* In this example we use the Service locator pattern to lookup JNDI-services and cache them for
* subsequent requests.
* <p>
*
* @author saifasif
*
*/
public class App {
/**
* Program entry point
* @param args command line args
*/
public static void main(String[] args) {
Service service = ServiceLocator.getService("jndi/serviceA");
service.execute();
service = ServiceLocator.getService("jndi/serviceB");
service.execute();
service = ServiceLocator.getService("jndi/serviceA");
service.execute();
service = ServiceLocator.getService("jndi/serviceA");
service.execute();
}
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
Service service = ServiceLocator.getService("jndi/serviceA");
service.execute();
service = ServiceLocator.getService("jndi/serviceB");
service.execute();
service = ServiceLocator.getService("jndi/serviceA");
service.execute();
service = ServiceLocator.getService("jndi/serviceA");
service.execute();
}
}

View File

@ -1,29 +1,29 @@
package com.iluwatar.servicelocator;
/**
* For JNDI lookup of services from the web.xml. Will match name of the service name that
* is being requested and return a newly created service object with the name
* For JNDI lookup of services from the web.xml. Will match name of the service name that is being
* requested and return a newly created service object with the name
*
* @author saifasif
*/
public class InitContext {
/**
* Perform the lookup based on the service name. The returned object will need to be
* casted into a {@link Service}
*
* @param serviceName a string
* @return an {@link Object}
*/
public Object lookup(String serviceName) {
if (serviceName.equals("jndi/serviceA")) {
System.out.println("Looking up service A and creating new service for A");
return new ServiceImpl("jndi/serviceA");
} else if (serviceName.equals("jndi/serviceB")) {
System.out.println("Looking up service B and creating new service for B");
return new ServiceImpl("jndi/serviceB");
} else {
return null;
}
/**
* Perform the lookup based on the service name. The returned object will need to be casted into a
* {@link Service}
*
* @param serviceName a string
* @return an {@link Object}
*/
public Object lookup(String serviceName) {
if (serviceName.equals("jndi/serviceA")) {
System.out.println("Looking up service A and creating new service for A");
return new ServiceImpl("jndi/serviceA");
} else if (serviceName.equals("jndi/serviceB")) {
System.out.println("Looking up service B and creating new service for B");
return new ServiceImpl("jndi/serviceB");
} else {
return null;
}
}
}

View File

@ -1,29 +1,26 @@
package com.iluwatar.servicelocator;
/**
* This is going to be the parent service interface which we will
* use to create our services. All services will have a
* <li>service name</li>
* <li>unique id</li>
* <li>execution work flow</li>
* This is going to be the parent service interface which we will use to create our services. All
* services will have a <li>service name</li> <li>unique id</li> <li>execution work flow</li>
*
* @author saifasif
*
*/
public interface Service {
/*
* The human readable name of the service
*/
String getName();
/*
* Unique ID of the particular service
*/
int getId();
/*
* The workflow method that defines what this service does
*/
void execute();
/*
* The human readable name of the service
*/
String getName();
/*
* Unique ID of the particular service
*/
int getId();
/*
* The workflow method that defines what this service does
*/
void execute();
}

View File

@ -4,44 +4,45 @@ import java.util.HashMap;
import java.util.Map;
/**
* The service cache implementation which will cache services that are being created.
* On first hit, the cache will be empty and thus any service that is being requested, will be
* created fresh and then placed into the cache map. On next hit, if same service name will
* be requested, it will be returned from the cache
* The service cache implementation which will cache services that are being created. On first hit,
* the cache will be empty and thus any service that is being requested, will be created fresh and
* then placed into the cache map. On next hit, if same service name will be requested, it will be
* returned from the cache
*
* @author saifasif
*/
public class ServiceCache {
private final Map<String, Service> serviceCache;
private final Map<String, Service> serviceCache;
public ServiceCache() {
serviceCache = new HashMap<String, Service>();
}
public ServiceCache() {
serviceCache = new HashMap<String, Service>();
}
/**
* Get the service from the cache. null if no service is found matching the name
*
* @param serviceName a string
* @return {@link Service}
*/
public Service getService(String serviceName) {
Service cachedService = null;
for (String serviceJndiName : serviceCache.keySet()) {
if (serviceJndiName.equals(serviceName)) {
cachedService = serviceCache.get(serviceJndiName);
System.out.println("(cache call) Fetched service " + cachedService.getName() + "(" + cachedService.getId() + ") from cache... !");
}
}
return cachedService;
/**
* Get the service from the cache. null if no service is found matching the name
*
* @param serviceName a string
* @return {@link Service}
*/
public Service getService(String serviceName) {
Service cachedService = null;
for (String serviceJndiName : serviceCache.keySet()) {
if (serviceJndiName.equals(serviceName)) {
cachedService = serviceCache.get(serviceJndiName);
System.out.println("(cache call) Fetched service " + cachedService.getName() + "("
+ cachedService.getId() + ") from cache... !");
}
}
return cachedService;
}
/**
* Adds the service into the cache map
*
* @param newService a {@link Service}
*/
public void addService(Service newService) {
serviceCache.put(newService.getName(), newService);
}
/**
* Adds the service into the cache map
*
* @param newService a {@link Service}
*/
public void addService(Service newService) {
serviceCache.put(newService.getName(), newService);
}
}

View File

@ -1,37 +1,37 @@
package com.iluwatar.servicelocator;
/**
* This is a single service implementation of a sample service. This is the actual
* service that will process the request. The reference for this service is to
* be looked upon in the JNDI server that can be set in the web.xml deployment descriptor
* This is a single service implementation of a sample service. This is the actual service that will
* process the request. The reference for this service is to be looked upon in the JNDI server that
* can be set in the web.xml deployment descriptor
*
* @author saifasif
*/
public class ServiceImpl implements Service {
private final String serviceName;
private final int id;
private final String serviceName;
private final int id;
public ServiceImpl(String serviceName) {
// set the service name
this.serviceName = serviceName;
public ServiceImpl(String serviceName) {
// set the service name
this.serviceName = serviceName;
// Generate a random id to this service object
this.id = (int) Math.floor(Math.random() * 1000) + 1;
}
// Generate a random id to this service object
this.id = (int) Math.floor(Math.random() * 1000) + 1;
}
@Override
public String getName() {
return serviceName;
}
@Override
public String getName() {
return serviceName;
}
@Override
public int getId() {
return id;
}
@Override
public int getId() {
return id;
}
@Override
public void execute() {
System.out.println("Service " + getName() + " is now executing with id " + getId());
}
@Override
public void execute() {
System.out.println("Service " + getName() + " is now executing with id " + getId());
}
}

View File

@ -1,36 +1,36 @@
package com.iluwatar.servicelocator;
/**
* The service locator module.
* Will fetch service from cache, otherwise creates a fresh service and update cache
* The service locator module. Will fetch service from cache, otherwise creates a fresh service and
* update cache
*
* @author saifasif
*/
public class ServiceLocator {
private static ServiceCache serviceCache = new ServiceCache();
private static ServiceCache serviceCache = new ServiceCache();
/**
* Fetch the service with the name param from the cache first,
* if no service is found, lookup the service from the {@link InitContext} and
* then add the newly created service into the cache map for future requests.
*
* @param serviceJndiName a string
* @return {@link Service}
*/
public static Service getService(String serviceJndiName) {
Service serviceObj = serviceCache.getService(serviceJndiName);
if (serviceObj != null) {
return serviceObj;
} else {
/*
* If we are unable to retrive anything from cache, then
* lookup the service and add it in the cache map
*/
InitContext ctx = new InitContext();
serviceObj = (Service) ctx.lookup(serviceJndiName);
serviceCache.addService(serviceObj);
return serviceObj;
}
/**
* Fetch the service with the name param from the cache first, if no service is found, lookup the
* service from the {@link InitContext} and then add the newly created service into the cache map
* for future requests.
*
* @param serviceJndiName a string
* @return {@link Service}
*/
public static Service getService(String serviceJndiName) {
Service serviceObj = serviceCache.getService(serviceJndiName);
if (serviceObj != null) {
return serviceObj;
} else {
/*
* If we are unable to retrive anything from cache, then lookup the service and add it in the
* cache map
*/
InitContext ctx = new InitContext();
serviceObj = (Service) ctx.lookup(serviceJndiName);
serviceCache.addService(serviceObj);
return serviceObj;
}
}
}

View File

@ -11,9 +11,9 @@ import com.iluwatar.servicelocator.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}