1 4 package org.oddjob.framework; 5 6 import java.lang.reflect.Method ; 7 8 public class Service { 9 10 private final Method startMethod; 11 private final Method stopMethod; 12 private final Object component; 13 14 private Service(Object component, Method startMethod, Method stopMethod) { 15 this.component = component; 16 this.startMethod = startMethod; 17 this.stopMethod = stopMethod; 18 } 19 20 public void start() throws Exception { 21 startMethod.invoke(component, new Object [0]); 22 } 23 24 public void stop() throws Exception { 25 stopMethod.invoke(component, new Object [0]); 26 } 27 28 public Object getComponent() { 29 return component; 30 } 31 32 public static Service serviceFor(Object component) { 33 Class cl = component.getClass(); 34 try { 35 Method startMethod = cl.getDeclaredMethod("start", new Class [0]); 36 if (startMethod.getReturnType() != Void.TYPE) { 37 return null; 38 } 39 Method stopMethod = cl.getDeclaredMethod("stop", new Class [0]); 40 if (startMethod.getReturnType() != Void.TYPE) { 41 return null; 42 } 43 return new Service(component, startMethod, stopMethod); 44 } catch (Exception e) { 45 return null; 46 } 47 } 48 } 49 | Popular Tags |