001: package org.ztemplates.web.impl;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005:
006: import javax.servlet.ServletContext;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import org.apache.log4j.Logger;
011: import org.ztemplates.web.ZIActionService;
012: import org.ztemplates.web.ZIClasspathService;
013: import org.ztemplates.web.ZIEncryptionService;
014: import org.ztemplates.web.ZIExceptionService;
015: import org.ztemplates.web.ZIRenderService;
016: import org.ztemplates.web.ZISecurityService;
017: import org.ztemplates.web.ZIService;
018: import org.ztemplates.web.ZIServiceFactory;
019: import org.ztemplates.web.ZIServiceRepository;
020: import org.ztemplates.web.ZIServletService;
021:
022: public class ZServiceRepositoryImpl implements ZIServiceRepository {
023: static final Logger log = Logger
024: .getLogger(ZServiceRepositoryImpl.class);
025:
026: private HttpServletRequest request;
027:
028: private HttpServletResponse response;
029:
030: private ServletContext servletContext;
031:
032: private ZIServiceFactory serviceFactory;
033:
034: private ZIServletService servletService;
035:
036: private ZIRenderService renderService;
037:
038: private ZIActionService actionService;
039:
040: private ZIClasspathService classpathService;
041:
042: private ZIEncryptionService encryptionService;
043:
044: private ZISecurityService securityService;
045:
046: private ZIExceptionService exceptionService;
047:
048: private final Map<Class, Object> services = new HashMap<Class, Object>();
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.ztemplates.web.impl.ZIServiceRepository#init(javax.servlet.http.HttpServletRequest,
054: * javax.servlet.http.HttpServletResponse)
055: */
056: public void init(HttpServletRequest req, HttpServletResponse resp)
057: throws Exception {
058: // use default implementation
059: this .request = req;
060: this .response = resp;
061: this .servletContext = req.getSession().getServletContext();
062: serviceFactory = new ZServiceFactoryImpl();
063: }
064:
065: public ZIServiceFactory getServiceFactory() {
066: return serviceFactory;
067: }
068:
069: public void setServiceFactory(ZIServiceFactory serviceFactory) {
070: this .serviceFactory = serviceFactory;
071: // clear cached services
072: servletService = null;
073: renderService = null;
074: classpathService = null;
075: encryptionService = null;
076: securityService = null;
077: exceptionService = null;
078: actionService = null;
079: services.clear();
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.ztemplates.web.impl.ZIServiceRepository#getService(java.lang.Class)
086: */
087: public <T extends ZIService> T getService(Class<T> clazz) {
088: T ret = (T) services.get(clazz);
089: if (ret == null) {
090: ret = serviceFactory.createService(this , clazz, null);
091: services.put(clazz, ret);
092: }
093: return ret;
094: }
095:
096: /*
097: * (non-Javadoc)
098: *
099: * @see org.ztemplates.web.impl.ZIServiceRepository#getServletService()
100: */
101: public ZIServletService getServletService() {
102: if (servletService == null) {
103: servletService = serviceFactory.createService(this ,
104: ZIServletService.class, ZServletServiceImpl.class);
105: }
106: return servletService;
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see org.ztemplates.web.impl.ZIServiceRepository#getRenderService()
113: */
114: public ZIRenderService getRenderService() {
115: if (renderService == null) {
116: renderService = serviceFactory.createService(this ,
117: ZIRenderService.class, ZRenderServiceImpl.class);
118: }
119: return renderService;
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see org.ztemplates.web.impl.ZIServiceRepository#getClasspathService()
126: */
127: public ZIClasspathService getClasspathService() {
128: if (classpathService == null) {
129: classpathService = serviceFactory.createService(this ,
130: ZIClasspathService.class,
131: ZClasspathServiceImpl.class);
132: }
133: return classpathService;
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see org.ztemplates.web.impl.ZIServiceRepository#getEncryptionService()
140: */
141: public ZIEncryptionService getEncryptionService() {
142: if (encryptionService == null) {
143: encryptionService = serviceFactory.createService(this ,
144: ZIEncryptionService.class,
145: ZEncryptionServiceImpl.class);
146: }
147: return encryptionService;
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see org.ztemplates.web.impl.ZIServiceRepository#getSecurityService()
154: */
155: public ZISecurityService getSecurityService() {
156: if (securityService == null) {
157: securityService = serviceFactory
158: .createService(this , ZISecurityService.class,
159: ZSecurityServiceImpl.class);
160: }
161: return securityService;
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see org.ztemplates.web.impl.ZIServiceRepository#getExceptionService()
168: */
169: public ZIExceptionService getExceptionService() {
170: if (exceptionService == null) {
171: exceptionService = serviceFactory.createService(this ,
172: ZIExceptionService.class,
173: ZExceptionServiceImpl.class);
174: }
175: return exceptionService;
176: }
177:
178: public ZIActionService getActionService() {
179: if (actionService == null) {
180: actionService = serviceFactory.createService(this ,
181: ZIActionService.class, ZActionServiceImpl.class);
182: }
183: return actionService;
184: }
185:
186: public HttpServletRequest getRequest() {
187: return request;
188: }
189:
190: public HttpServletResponse getResponse() {
191: return response;
192: }
193:
194: public ServletContext getServletContext() {
195: return servletContext;
196: }
197: }
|