01: /*
02: * Copyright 2005 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.tp23.antinstaller.runtime.exe;
17:
18: import java.io.BufferedReader;
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.InputStreamReader;
22: import java.util.ArrayList;
23: import java.util.List;
24:
25: import org.tp23.antinstaller.InstallException;
26: import org.tp23.antinstaller.InstallerContext;
27:
28: /**
29: * Loads FilterChains from resource files on the classpath with lists of class
30: * names listed in order. In the files lines starting with # and blank
31: * lines are ignored
32: * @author Paul Hinds
33: * @version $Id: FilterFactory.java,v 1.4 2007/01/04 22:57:18 teknopaul Exp $
34: */
35: public class FilterFactory {
36:
37: /** default value not used usually */
38: public static final String FILTER_RESOURCE = "/antinstall-config.fconfig";
39:
40: private FilterFactory() {
41: }
42:
43: public static FilterChain factory(String configResource)
44: throws InstallException {
45: try {
46: InputStream is = FilterFactory.class
47: .getResourceAsStream(configResource);
48: BufferedReader br = new BufferedReader(
49: new InputStreamReader(is));
50: String filterClass = null;
51: final List filterChain = new ArrayList();
52: while ((filterClass = br.readLine()) != null) {
53: if (filterClass.startsWith("#"))
54: continue;
55: filterClass = filterClass.trim();
56: if (filterClass.equals("")) {
57: continue;
58: }
59: filterChain.add(Class.forName(filterClass)
60: .newInstance());
61: }
62: br.close();
63: return new DynamicFilterChain(configResource, filterChain);
64: } catch (IOException e) {
65: e.printStackTrace();
66: } catch (Exception e) {
67: e.printStackTrace();
68: }
69: throw new InstallException("Can not create FilterChain");
70: }
71:
72: static class DynamicFilterChain implements FilterChain {
73:
74: private ExecuteFilter[] filters;
75: private String configResource;
76:
77: private DynamicFilterChain(String configResource,
78: List filterChain) {
79: this .configResource = configResource;
80: filters = new ExecuteFilter[filterChain.size()];
81: filterChain.toArray(filters);
82: }
83:
84: public void init(InstallerContext ctx) {
85: ctx.setConfigResource(configResource);
86: }
87:
88: public ExecuteFilter[] getFilters() {
89: return filters;
90: }
91: };
92: }
|