View Javadoc
1   /*
2    * Copyright 2010 Capgemini and Contributors
3    *
4    * Licensed under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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   * This is the entry point for services monitor.
34   *
35   * <p>
36   * Must be initialized once before calling other methods.
37   * <p>
38   * AppStatusServices services = new AppStatusServices(); <br/>
39   * services.init();
40   * </p>
41   *
42   * @author Nicolas Richeton
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  	 * Status Service creator.
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  	 * Try to instantiate a class.
71  	 *
72  	 * @param className
73  	 * @return an object instance of "className" class or null if instantiation
74  	 *         is not possible
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 			// Load plugins
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 		// Inject servlet context if possible
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 				// serviceManager
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 	 * Load a properties file from a given URL.
171 	 *
172 	 * @param url
173 	 *            an url
174 	 * @return a {@link Properties} object
175 	 * @throws IOException
176 	 *             in an error occurs
177 	 */
178 	private Properties loadProperties(URL url) throws IOException {
179 		// Load plugin configuration
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 }