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    /** default value not used usually */
39    public static final String FILTER_RESOURCE = "/antinstall-config.fconfig";
40
41    private FilterFactory() {
42    }
43
44    public static FilterChain factory(String configResource) throws InstallException{
45        try {
46            InputStream is = FilterFactory.class.getResourceAsStream(configResource);
47            BufferedReader br = new BufferedReader(new InputStreamReader(is));
48            String filterClass = null;
49            final List filterChain = new ArrayList();
50            while((filterClass = br.readLine())!=null){
51                if(filterClass.startsWith("#"))continue;
52                filterClass = filterClass.trim();
53                if(filterClass.equals("")){
54                    continue;
55                }
56                filterChain.add( Class.forName(filterClass).newInstance() );
57            }
58            br.close();
59            return new DynamicFilterChain(configResource, filterChain);
60        }
61        catch (IOException e) {
62            e.printStackTrace();
63        }
64        catch (Exception e) {
65            e.printStackTrace();
66        }
67        throw new InstallException("Can not create FilterChain");
68    }
69        
70    static class DynamicFilterChain implements FilterChain{
71        
72        private ExecuteFilter[] filters;
73        private String configResource;
74        
75        private DynamicFilterChain(String configResource, List filterChain){
76            this.configResource = configResource;
77            filters = new ExecuteFilter[filterChain.size()];
78            filterChain.toArray(filters);
79        }
80        public void init(InstallerContext ctx){
81            ctx.setConfigResource(configResource);
82        }
83        public ExecuteFilter[] getFilters(){
84            return filters;
85        }
86    };
87}
88