01: /*
02: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * 20.11.2007
13: * @author www.gerdziegler.de
14: */
15: package org.ztemplates.web.impl;
16:
17: import javax.servlet.ServletContext;
18:
19: import org.ztemplates.classpath.ZClassPathItem;
20: import org.ztemplates.classpath.ZClassPathItemWebapp;
21: import org.ztemplates.classpath.ZClassPathRoots;
22: import org.ztemplates.classpath.ZClassRepository;
23: import org.ztemplates.classpath.ZIClassRepository;
24: import org.ztemplates.web.ZIClasspathService;
25: import org.ztemplates.web.ZIServiceRepository;
26:
27: public class ZClasspathServiceImpl implements ZIClasspathService {
28: private ZIClassRepository classRepository;
29:
30: private static final String key = ZIClassRepository.class.getName();
31:
32: public void init(ZIServiceRepository serviceRepository)
33: throws Exception {
34: final ServletContext servletContext = serviceRepository
35: .getServletContext();
36: classRepository = preloadClassRepository(servletContext);
37: }
38:
39: public ZIClassRepository getClassRepository() {
40: return classRepository;
41: }
42:
43: public static ZIClassRepository preloadClassRepository(
44: final ServletContext servletContext) {
45: ZIClassRepository classRepository = (ZIClassRepository) servletContext
46: .getAttribute(key);
47: if (classRepository == null) {
48: ZClassPathRoots roots = new ZClassPathRoots() {
49: public ZClassPathItem[] getRoots() throws Exception {
50: return new ZClassPathItem[] {
51: new ZClassPathItemWebapp(servletContext,
52: "/WEB-INF/classes/"),
53: new ZClassPathItemWebapp(servletContext,
54: "/WEB-INF/lib/"), };
55: }
56: };
57: final String pojoPackage = servletContext
58: .getInitParameter("pojoPackage");
59: classRepository = new ZClassRepository(roots, pojoPackage);
60: servletContext.setAttribute(key, classRepository);
61: }
62: return classRepository;
63: }
64: }
|