Project

General

Profile

« Previous | Next » 

Revision 4710

Added by daigle over 15 years ago

This service now holds a registry of active services. Refreshing of services must go through this class. Also holds some common values that are discovered via servlet context.

View differences:

src/edu/ucsb/nceas/metacat/service/ServiceService.java
1 1
/**
2 2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements session utility methods 
3
 *    Purpose: A Class that controls other services 
4 4
 *  Copyright: 2008 Regents of the University of California and the
5 5
 *             National Center for Ecological Analysis and Synthesis
6 6
 *    Authors: Michael Daigle
......
27 27
package edu.ucsb.nceas.metacat.service;
28 28

  
29 29
import java.util.Hashtable;
30
import java.util.Vector;
31 30

  
31
import javax.servlet.ServletContext;
32

  
32 33
import org.apache.log4j.Logger;
33 34

  
34
import edu.ucsb.nceas.metacat.util.SessionData;
35

  
36
public class ServiceService extends BaseService {
35
public class ServiceService {
37 36
	
38 37
	private static ServiceService serviceService = null;
39 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
	
40 45
	private static Logger logMetacat = Logger.getLogger(ServiceService.class);
41
	private static Hashtable<String, Class> ServiceList = null;
42

  
46
	private static Hashtable<String, BaseService> serviceList = new Hashtable<String, BaseService>();
47
	
43 48
	/**
44 49
	 * private constructor since this is a singleton
45 50
	 */
46
	private ServiceService() {
47
		
51
	private ServiceService(ServletContext servletContext) {
52
		REAL_CONFIG_DIR = servletContext.getRealPath(CONFIG_DIR);
53
		REAL_SKIN_DIR = servletContext.getRealPath(SKIN_DIR);
48 54
	}
49 55
	
50 56
	/**
......
52 58
	 * 
53 59
	 * @return the single instance of ServiceService
54 60
	 */
55
	public static ServiceService getInstance() {
61
	public static ServiceService getInstance(ServletContext servletContext) {
56 62
		if (serviceService == null) {
57
			serviceService = new ServiceService();
63
			serviceService = new ServiceService(servletContext);
58 64
		}
59 65
		return serviceService;
60 66
	}
61 67
	
62
	public boolean refreshable() {
63
		return false;
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);
64 84
	}
65 85
	
66
	protected void doRefresh() {
67
		return;
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();
68 104
	}
69 105
	
70

  
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
	
71 128
}

Also available in: Unified diff