View Javadoc

1   package net.sf.appstatus.services;
2   
3   import java.util.ArrayList;
4   import java.util.Hashtable;
5   import java.util.List;
6   import java.util.Properties;
7   
8   import net.sf.appstatus.core.services.IService;
9   import net.sf.appstatus.core.services.IServiceManager;
10  import net.sf.appstatus.core.services.IServiceMonitor;
11  
12  /**
13   * A service manager which stores service statistics in an ArrayList. There is
14   * no history of calls so this implementation is safe event on production
15   * systems (no memory increase over time).
16   * <p>
17   * Data are lost when restarting the application.
18   * <p>
19   * Reads the following configuration :
20   * <ul>
21   * <li>services.log.format</li>
22   * <li>services.log</li>
23   * </ul>
24   * 
25   * @author Nicolas Richeton
26   * 
27   */
28  public class InProcessServiceManager implements IServiceManager {
29  
30  	Properties configuration = null;
31  	boolean log = true;
32  	String format = null;
33  	boolean useThreadLocal = false;
34  
35  	Hashtable<String, IService> services = new Hashtable<String, IService>();
36  	private int minMaxDelay = 0;
37  
38  	/**
39  	 * {@inheritDoc}
40  	 */
41  	public IServiceMonitor getMonitor(IService service) {
42  		ServiceCall call = new ServiceCall((Service) service, log, useThreadLocal);
43  
44  		if (format != null){
45  			call.setLogFormat(format);
46  		}
47  		return call;
48  	}
49  
50  	/**
51  	 * {@inheritDoc}
52  	 */
53  	public List<IService> getServices() {
54  		return new ArrayList<IService>(services.values());
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	public IService getService(String name, String group) {
61  		IService result = services.get(group + "/" + name);
62  		if (result == null) {
63  			synchronized (this) {
64  				result = services.get(group + "/" + name);
65  				if (result == null) {
66  					Service newService = new Service(minMaxDelay);
67  					newService.setName(name);
68  					newService.setGroup(group);
69  					services.put(group + "/" + name, newService);
70  					result = newService;
71  				}
72  			}
73  		}
74  
75  		return result;
76  	}
77  
78  	/**
79  	 * {@inheritDoc}
80  	 * 
81  	 * Init using "services.log.format", "services.log",
82  	 * "services.useThreadLocal" properties.
83  	 * 
84  	 */
85  	public void setConfiguration(Properties configuration) {
86  		this.configuration = configuration;
87  
88  		if (configuration != null) {
89  			String confLogFormat = configuration.getProperty("services.log.format");
90  
91  			if (confLogFormat != null)
92  				format = confLogFormat;
93  
94  			String logEnabled = configuration.getProperty("services.log");
95  			if (logEnabled != null) {
96  				log = Boolean.valueOf(logEnabled);
97  			}
98  			
99  			String confMinMaxDelay = configuration.getProperty("services.minMaxDelay");
100 			if (confMinMaxDelay != null) {
101 				minMaxDelay  = Integer.valueOf(confMinMaxDelay);
102 			}
103 
104 			useThreadLocal = Boolean.valueOf(configuration.getProperty("services.useThreadLocal"));
105 		}
106 	}
107 	
108 	public Properties getConfiguration() {
109 		return configuration;
110 	}
111 
112 }