01: package org.ztemplates.web.impl;
02:
03: import org.apache.log4j.Logger;
04: import org.ztemplates.web.ZIService;
05: import org.ztemplates.web.ZIServiceFactory;
06: import org.ztemplates.web.ZIServiceRepository;
07:
08: public class ZServiceFactoryImpl implements ZIServiceFactory {
09: static final Logger log = Logger
10: .getLogger(ZServiceFactoryImpl.class);
11:
12: public <T extends ZIService> T createService(
13: ZIServiceRepository serviceRepository, Class<T> ifc,
14: Class<? extends T> defaultImpl) {
15: String paramName = "service:" + ifc.getName();
16: String paramValue = serviceRepository.getServletContext()
17: .getInitParameter(paramName);
18: try {
19: T ret;
20: if (paramValue == null) {
21: if (defaultImpl != null) {
22: ret = defaultImpl.newInstance();
23: } else {
24: throw new RuntimeException(
25: "missing servlet init parameter '"
26: + paramName + "' in web.xml");
27: }
28: } else {
29: ret = (T) Class.forName(paramValue).newInstance();
30: }
31: ret.init(serviceRepository);
32: return ret;
33: } catch (Exception e) {
34: log.error(ifc.getName(), e);
35: throw new RuntimeException(
36: "wrong servlet init parameter '" + paramName
37: + "' = '" + paramValue + "' in web.xml", e);
38: }
39: }
40: }
|