To separate the Interface(or Super Class) and It's Implementation Class(or Sub-Class), It use a xml file "services.xml". In runtime It will be roaded from classpath.
'services.xml' is very simple.
It has N-'service' elements. Each 'service' element means one Service class.
One 'sevice' element have following properties.
'id' is just name(key) of service. so It is used -> ServiceManager.lookupService(id);
'class' is actual class full name. This class must have the default constructor.
This project is inspired by Avalon, HiveMind and etc... Some code was borrowed from Avalon.
In developping the application. This is designed to solve the following problems.
It located on \serviceManager\src\example
Configuration 'services.xml'
<?xml version="1.0" encoding="UTF-8"?>
<services>
<service id="hasha" class="java.util.Hashtable" /> <service id="hashb" class="java.util.Hashtable" /> </service>
java source
package example; import java.util.Map; import sunheart.service.ServiceManager; public class SimpleTest { public static void main(String[] args) { System.out.println("test start"); // when you need a singleton hashtable, // simple solution is that just make a singleton object // which contain a hashTable(or subclass of hashtable) // but using ServiceManager, // it can be replaced with a xml' configuration // //// see services.xml (new SimpleTest()).execute(); System.out.println("test end"); } public void execute() { final String sameKey = "driver"; final String serviceA = "hasha"; final String serviceB = "hashb"; Map a = getMapService(serviceA); a.put(sameKey, "oracle"); Map b = getMapService(serviceB); b.put(sameKey, "mysql"); if (a.get(sameKey).equals(b.get(sameKey))) System.err.println("It doesn't work "); else System.out.println("it works"); if (a.equals(getMapService(serviceA)) && b.equals(getMapService(serviceB))) System.out.println("it works"); else System.err.println("It doesn't work "); } public Map getMapService(String id) { Object service = ServiceManager.lookupService(id); Map map = null; if (service != null) { map = (Map) service; } return map; } }
the result
test start *INFO * [main] ServiceManager: ############################## *INFO * [main] ServiceManager: # ServiceManger is creating........... *INFO * [main] ServiceManager: use DefaultRepository class *INFO * [main] ServiceManager: Daemon set=false *INFO * [main] ServiceManager: Regist services is started *INFO * [main] ServiceManager: SUCCESS TO CREATE SERVICE:[hasha]=java.util.Hashtable *INFO * [main] ServiceManager: SUCCESS TO CREATE SERVICE:[hashb]=java.util.Hashtable *INFO * [main] ServiceManager: Regist services is ended. Success:2, Failure:0 *INFO * [main] ServiceManager: Daemon set=false *INFO * [main] ServiceManager: # ServiceManger is created. Duration(ms)= 1202 *INFO * [main] ServiceManager: ############################## it works it works test end #################### # JVM is killed... # Shutdown is started Shutdown service ....hashb Shutdown service ....hasha # Shutdown is ended ####################
It located on \serviceManager\src\example
Configuration 'services.xml'
<?xml version="1.0" encoding="UTF-8"?>
<services>
<service id="simpleService" class="example.FooSimpleService"> <FooConfigFile>fooConfig.xml</FooConfigFile>
<max>100</max> <min>5</min> </service> </service>
java source
package example; import sunheart.service.ServiceManager; /** * */ public class SimpleServiceTest { public static void main(String[] args) { SimpleService service = (SimpleService) ServiceManager .lookupService("simpleService"); System.out.println("max=" + service.getMax()); System.out.println("min=" + service.getMin()); } }
the result
*INFO * [main] ServiceManager: ############################## *INFO * [main] ServiceManager: # ServiceManger is creating........... *INFO * [main] ServiceManager: use DefaultRepository class *INFO * [main] ServiceManager: Daemon set=false *INFO * [main] ServiceManager: Regist services is started configured. It simply set max, min values from 'services.xml' Of course It can use external config, for example config file=fooConfig.xml intialized.... it's started.... *INFO * [main] ServiceManager: SUCCESS TO CREATE SERVICE:[simpleService]=example.FooSimpleService *INFO * [main] ServiceManager: Regist services is ended. Success:1, Failure:0 *INFO * [main] ServiceManager: Daemon set=false *INFO * [main] ServiceManager: # ServiceManger is created. Duration(ms)= 1152 *INFO * [main] ServiceManager: ############################## max=100 min=5 #################### # JVM is killed... # Shutdown is started Shutdown service ....simpleService stopped.... # Shutdown is ended ####################