1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 */
16package org.tp23.antinstaller.runtime.exe;
17
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.InputStream;
21import java.io.InputStreamReader;
22import java.util.ArrayList;
23import java.util.List;
24
25import org.tp23.antinstaller.InstallException;
26import org.tp23.antinstaller.InstallerContext;
27
28
29/**
30 * Loads FilterChains from resource files on the classpath with lists of class
31 * names listed in order.  In the files lines starting with # and blank
32 * lines are ignored
33 * @author Paul Hinds
34 * @version $Id: FilterFactory.java,v 1.4 2007/01/04 22:57:18 teknopaul Exp $
35 */
36public class FilterFactory {
37    
38    public static final String FILTER_RESOURCE = "/antinstall-config.fconfig";
39
40    private FilterFactory() {
41    }
42
43    public static FilterChain factory(String configResource) throws InstallException{
44        try {
45            InputStream is = FilterFactory.class.getResourceAsStream(configResource);
46            BufferedReader br = new BufferedReader(new InputStreamReader(is));
47            String filterClass = null;
48            final List filterChain = new ArrayList();
49            while((filterClass = br.readLine())!=null){
50                if(filterClass.startsWith("#"))continue;
51                filterClass = filterClass.trim();
52                if(filterClass.equals("")){
53                    continue;
54                }
55                filterChain.add( Class.forName(filterClass).newInstance() );
56            }
57            br.close();
58            return new DynamicFilterChain(configResource, filterChain);
59        }
60        catch (IOException e) {
61            e.printStackTrace();
62        }
63        catch (Exception e) {
64            e.printStackTrace();
65        }
66        throw new InstallException("Can not create FilterChain");
67    }
68        
69    static class DynamicFilterChain implements FilterChain{
70        
71        private ExecuteFilter[] filters;
72        private String configResource;
73        
74        private DynamicFilterChain(String configResource, List filterChain){
75            this.configResource = configResource;
76            filters = new ExecuteFilter[filterChain.size()];
77            filterChain.toArray(filters);
78        }
79        public void init(InstallerContext ctx){
80            ctx.setConfigResource(configResource);
81        }
82        public ExecuteFilter[] getFilters(){
83            return filters;
84        }
85    };
86}
87