1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.appstatus.core;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21 import java.util.Enumeration;
22 import java.util.List;
23 import java.util.Properties;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import net.sf.appstatus.core.services.IService;
29 import net.sf.appstatus.core.services.IServiceManager;
30 import net.sf.appstatus.core.services.IServiceMonitor;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @Deprecated
46 public class AppStatusServices {
47
48 private static Logger logger = LoggerFactory.getLogger(AppStatusServices.class);
49
50 private boolean initDone = false;
51 private IObjectInstantiationListener objectInstanciationListener = null;
52 private IServiceManager serviceManager = null;
53 private IServletContextProvider servletContextProvider = null;
54
55
56
57
58 public AppStatusServices() {
59 }
60
61 private void checkInit() {
62 if (!initDone) {
63 logger.warn("Not initialized. Starting init");
64 init();
65
66 }
67 }
68
69
70
71
72
73
74
75
76 private Object getClassInstance(String className) {
77 Object obj = null;
78
79 if (objectInstanciationListener != null) {
80 obj = objectInstanciationListener.getInstance(className);
81 }
82
83 if (obj == null) {
84 try {
85 obj = Class.forName(className).newInstance();
86 } catch (ClassNotFoundException e) {
87 logger.warn("Class {} not found ", className, e);
88 } catch (InstantiationException e) {
89 logger.warn("Cannot instanciate {} ", className, e);
90 } catch (IllegalAccessException e) {
91 logger.warn("Cannot access class {} for instantiation ", className, e);
92 }
93 }
94
95 if (obj != null) {
96 injectServletContext(obj);
97 }
98
99 return obj;
100 }
101
102 public IServiceMonitor getServiceMonitor(String name, String group) {
103 checkInit();
104
105 IService batch = null;
106 if (serviceManager != null) {
107 batch = serviceManager.getService(name, group);
108 return serviceManager.getMonitor(batch);
109 }
110
111 return null;
112 }
113
114 public List<IService> getServices() {
115 return serviceManager.getServices();
116 }
117
118 public IServletContextProvider getServletContext() {
119 return servletContextProvider;
120 }
121
122 public synchronized void init() {
123
124 if (initDone) {
125 logger.warn("Already initialized");
126 return;
127 }
128
129 try {
130
131
132 loadPlugins();
133 } catch (Exception e) {
134 logger.error("Initialization error", e);
135 }
136
137 initDone = true;
138
139 }
140
141 private void injectServletContext(Object instance) {
142
143 if (instance instanceof IServletContextAware && servletContextProvider != null) {
144 ((IServletContextAware) instance).setServletContext(servletContextProvider.getServletContext());
145 }
146 }
147
148 private void loadPlugins() {
149 try {
150 Enumeration<URL> plugins = AppStatusServices.class.getClassLoader()
151 .getResources("net/sf/appstatus/plugin.properties");
152 while (plugins.hasMoreElements()) {
153 URL url = plugins.nextElement();
154 logger.info(url.toString());
155 Properties p = loadProperties(url);
156
157
158 String serviceManagerClass = p.getProperty("serviceManager");
159 if (serviceManagerClass != null) {
160 serviceManager = (IServiceManager) getClassInstance(serviceManagerClass);
161 }
162
163 }
164 } catch (IOException e) {
165 logger.warn("Error loading plugins", e);
166 }
167 }
168
169
170
171
172
173
174
175
176
177
178 private Properties loadProperties(URL url) throws IOException {
179
180 Properties p = new Properties();
181 InputStream is = url.openStream();
182 p.load(is);
183 is.close();
184 return p;
185 }
186
187 public void setObjectInstanciationListener(IObjectInstantiationListener objectInstanciationListener) {
188 this.objectInstanciationListener = objectInstanciationListener;
189 }
190
191 public void setServletContextProvider(IServletContextProvider servletContext) {
192 this.servletContextProvider = servletContext;
193 }
194 }