Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that controls other services 
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-10-09 09:53:18 -0700 (Thu, 09 Oct 2008) $'
10
 * '$Revision: 4429 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.service;
28

    
29
import java.util.Hashtable;
30

    
31
import javax.servlet.ServletContext;
32

    
33
import org.apache.log4j.Logger;
34

    
35
public class ServiceService {
36
	
37
	private static ServiceService serviceService = null;
38
	
39
    private static final String CONFIG_DIR = "WEB-INF";
40
	private static String REAL_CONFIG_DIR = "";
41
    
42
	private static final String SKIN_DIR = "/style/skins";
43
	private static String REAL_SKIN_DIR = "";
44
	
45
	private static Logger logMetacat = Logger.getLogger(ServiceService.class);
46
	private static Hashtable<String, BaseService> serviceList = new Hashtable<String, BaseService>();
47
	
48
	/**
49
	 * private constructor since this is a singleton
50
	 */
51
	private ServiceService(ServletContext servletContext) {
52
		REAL_CONFIG_DIR = servletContext.getRealPath(CONFIG_DIR);
53
		REAL_SKIN_DIR = servletContext.getRealPath(SKIN_DIR);
54
	}
55
	
56
	/**
57
	 * Get the single instance of ServiceService.
58
	 * 
59
	 * @return the single instance of ServiceService
60
	 */
61
	public static ServiceService getInstance(ServletContext servletContext) {
62
		if (serviceService == null) {
63
			serviceService = new ServiceService(servletContext);
64
		}
65
		return serviceService;
66
	}
67
	
68
	/**
69
	 * Register a service with the system.
70
	 * 
71
	 * @param serviceName
72
	 *            the name of the service
73
	 * @param service
74
	 *            the singleton instance of the service
75
	 */
76
	public static void registerService(String serviceName, BaseService service)
77
			throws ServiceException {
78
		if (serviceList.containsKey(serviceName)) {
79
			throw new ServiceException("Service: " + serviceName + " is already registered."
80
					+ " Use ServiceService.reregister() to replace the service.");
81
		}
82
		logMetacat.info("Registering Service: " + serviceName);
83
		serviceList.put(serviceName, service);
84
	}
85
	
86
	/**
87
	 * Refresh a service.
88
	 * 
89
	 * @param serviceName
90
	 *            the name of the service to refresh
91
	 */
92
	public static void refreshService(String serviceName)
93
			throws ServiceException {
94
		if (!serviceList.containsKey(serviceName)) {
95
			throw new ServiceException("Service: " + serviceName + " is not registered.");
96
		}
97
		
98
		BaseService baseService = serviceList.get(serviceName);
99
		if (!baseService.refreshable()) {
100
			throw new ServiceException("Service: " + serviceName + " is not refreshable.");
101
		}
102
		logMetacat.info("Refreshing Service: " + serviceName);
103
		baseService.refresh();
104
	}
105
	
106
	/**
107
	 * Convert the relative config directory to a full path
108
	 * @return the full config path
109
	 */
110
	public static String getRealConfigDir() throws ServiceException {
111
		if (serviceService == null) {
112
			throw new ServiceException("Cannot access config dir before Service has been initialized");
113
		}
114
		return REAL_CONFIG_DIR;
115
	}
116
	
117
	/**
118
	 * Convert the relative skins directory to a full path
119
	 * @return the full skins directory path
120
	 */
121
	public static String getRealSkinDir() throws ServiceException {
122
		if (serviceService == null) {
123
			throw new ServiceException("Cannot access skin dir before Service has been initialized");
124
		}
125
		return REAL_SKIN_DIR;
126
	}
127
	
128
}
(5-5/9)