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
import javax.servlet.ServletContext;
31
import org.apache.log4j.Logger;
32

    
33
import edu.ucsb.nceas.metacat.util.SystemUtil;
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 String REAL_APPLICATION_CONTEXT = null;
46
	
47
	private static Logger logMetacat = Logger.getLogger(ServiceService.class);
48
	private static Hashtable<String, BaseService> serviceList = new Hashtable<String, BaseService>();
49
	
50
	/**
51
	 * private constructor since this is a singleton
52
	 */
53
	private ServiceService(ServletContext servletContext) {
54
		REAL_CONFIG_DIR = servletContext.getRealPath(CONFIG_DIR);
55
		REAL_SKIN_DIR = servletContext.getRealPath(SKIN_DIR);
56
		
57
		REAL_APPLICATION_CONTEXT = SystemUtil.discoverApplicationContext(servletContext);
58
	}
59
	
60
	/**
61
	 * Get the single instance of ServiceService.
62
	 * 
63
	 * @return the single instance of ServiceService
64
	 */
65
	public static ServiceService getInstance(ServletContext servletContext) {
66
		if (serviceService == null) {
67
			serviceService = new ServiceService(servletContext);
68
		}
69
		return serviceService;
70
	}
71
	
72
	/**
73
	 * Register a service with the system.
74
	 * 
75
	 * @param serviceName
76
	 *            the name of the service
77
	 * @param service
78
	 *            the singleton instance of the service
79
	 */
80
	public static void registerService(String serviceName, BaseService service)
81
			throws ServiceException {
82
		if (serviceList.containsKey(serviceName)) {
83
			throw new ServiceException("Service: " + serviceName + " is already registered."
84
					+ " Use ServiceService.reregister() to replace the service.");
85
		}
86
		logMetacat.info("Registering Service: " + serviceName);
87
		serviceList.put(serviceName, service);
88
	}
89
	
90
	/**
91
	 * Refresh a service.
92
	 * 
93
	 * @param serviceName
94
	 *            the name of the service to refresh
95
	 */
96
	public static void refreshService(String serviceName)
97
			throws ServiceException {
98
		if (!serviceList.containsKey(serviceName)) {
99
			throw new ServiceException("Service: " + serviceName + " is not registered.");
100
		}
101
		
102
		BaseService baseService = serviceList.get(serviceName);
103
		if (!baseService.refreshable()) {
104
			throw new ServiceException("Service: " + serviceName + " is not refreshable.");
105
		}
106
		logMetacat.info("Refreshing Service: " + serviceName);
107
		baseService.refresh();
108
	}
109
	
110
	/**
111
	 * Convert the relative config directory to a full path
112
	 * @return the full config path
113
	 */
114
	public static String getRealConfigDir() throws ServiceException {
115
		if (serviceService == null) {
116
			throw new ServiceException("Cannot access config dir before Service has been initialized");
117
		}
118
		return REAL_CONFIG_DIR;
119
	}
120
	
121
	/**
122
	 * Convert the relative skins directory to a full path
123
	 * @return the full skins directory path
124
	 */
125
	public static String getRealSkinDir() throws ServiceException {
126
		if (serviceService == null) {
127
			throw new ServiceException("Cannot access skin dir before Service has been initialized");
128
		}
129
		return REAL_SKIN_DIR;
130
	}
131
	
132
	/**
133
	 * Get the servlet context name
134
	 * @return a string holding the context name
135
	 */
136
	public static String getRealApplicationContext() throws ServiceException {
137
		if (REAL_APPLICATION_CONTEXT == null) {
138
			throw new ServiceException("Application context name is null");
139
		}
140
		return REAL_APPLICATION_CONTEXT;
141
	}
142
	
143
}
(5-5/9)