aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java
blob: f176c331b3ff0f3ae6ce548cbc7ddd2d7b69e85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.filters.util;

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.filters.BaseFilterReader;
import org.apache.tools.ant.filters.ChainableReader;
import org.apache.tools.ant.types.AntFilterReader;
import org.apache.tools.ant.types.FilterChain;
import org.apache.tools.ant.types.Parameter;
import org.apache.tools.ant.types.Parameterizable;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileUtils;

/**
 * Process a FilterReader chain.
 *
 */
public final class ChainReaderHelper {

    // default buffer size
    private static final int DEFAULT_BUFFER_SIZE = 8192;
    // CheckStyle:VisibilityModifier OFF - bc
    /**
     * The primary reader to which the reader chain is to be attached.
     */
    public Reader primaryReader;

    /**
     * The size of the buffer to be used.
     */
    public int bufferSize = DEFAULT_BUFFER_SIZE;

    /**
     * Chain of filters
     */
    public Vector<FilterChain> filterChains = new Vector<FilterChain>();

    /** The Ant project */
    private Project project = null;

    // CheckStyle:VisibilityModifier ON

    /**
     * Sets the primary reader
     * @param rdr the reader object
     */
    public void setPrimaryReader(Reader rdr) {
        primaryReader = rdr;
    }

    /**
     * Set the project to work with
     * @param project the current project
     */
    public void setProject(final Project project) {
        this.project = project;
    }

    /**
     * Get the project
     *
     * @return the current project
     */
    public Project getProject() {
        return project;
    }

    /**
     * Sets the buffer size to be used.  Defaults to 8192,
     * if this method is not invoked.
     * @param size the buffer size to use
     */
    public void setBufferSize(int size) {
        bufferSize = size;
    }

    /**
     * Sets the collection of filter reader sets
     *
     * @param fchain the filter chains collection
     */
    public void setFilterChains(Vector<FilterChain> fchain) {
        filterChains = fchain;
    }

    /**
     * Assemble the reader
     * @return the assembled reader
     * @exception BuildException if an error occurs
     */
    public Reader getAssembledReader() throws BuildException {
        if (primaryReader == null) {
            throw new BuildException("primaryReader must not be null.");
        }

        Reader instream = primaryReader;
        final int filterReadersCount = filterChains.size();
        final Vector<Object> finalFilters = new Vector<Object>();
        final ArrayList<AntClassLoader> classLoadersToCleanUp =
            new ArrayList<AntClassLoader>();

        for (int i = 0; i < filterReadersCount; i++) {
            final FilterChain filterchain =
                filterChains.elementAt(i);
            final Vector<Object> filterReaders = filterchain.getFilterReaders();
            final int readerCount = filterReaders.size();
            for (int j = 0; j < readerCount; j++) {
                finalFilters.addElement(filterReaders.elementAt(j));
            }
        }

        final int filtersCount = finalFilters.size();

        if (filtersCount > 0) {
            boolean success = false;
            try {
                for (int i = 0; i < filtersCount; i++) {
                    Object o = finalFilters.elementAt(i);

                    if (o instanceof AntFilterReader) {
                        instream =
                            expandReader((AntFilterReader) finalFilters.elementAt(i),
                                         instream, classLoadersToCleanUp);
                    } else if (o instanceof ChainableReader) {
                        setProjectOnObject(o);
                        instream = ((ChainableReader) o).chain(instream);
                        setProjectOnObject(instream);
                    }
                }
                success = true;
            } finally {
                if (!success && classLoadersToCleanUp.size() > 0) {
                    cleanUpClassLoaders(classLoadersToCleanUp);
                }
            }
        }
        final Reader finalReader = instream;
        return classLoadersToCleanUp.size() == 0 ? finalReader
            : new FilterReader(finalReader) {
                    public void close() throws IOException {
                        FileUtils.close(in);
                        cleanUpClassLoaders(classLoadersToCleanUp);
                    }
                    protected void finalize() throws Throwable {
                        try {
                            close();
                        } finally {
                            super.finalize();
                        }
                    }
                };
    }

    /**
     * helper method to set the project on an object.
     * the reflection setProject does not work for anonymous/protected/private
     * classes, even if they have public methods.
     */
    private void setProjectOnObject(Object obj) {
        if (project == null) {
            return;
        }
        if (obj instanceof BaseFilterReader) {
            ((BaseFilterReader) obj).setProject(project);
            return;
        }
        project.setProjectReference(obj);
    }

    /**
     * Deregisters Classloaders from the project so GC can remove them later.
     */
    private static void cleanUpClassLoaders(List<AntClassLoader> loaders) {
        for (Iterator<AntClassLoader> it = loaders.iterator(); it.hasNext();) {
            it.next().cleanup();
        }
    }

    /**
     * Read data from the reader and return the
     * contents as a string.
     * @param rdr the reader object
     * @return the contents of the file as a string
     * @exception IOException if an error occurs
     */
    public String readFully(Reader rdr)
        throws IOException {
        return FileUtils.readFully(rdr, bufferSize);
    }

    /**
     * Creates and parameterizes a new FilterReader from a
     * &lt;filterreader&gt; element.
     *
     * @since Ant 1.8.0
     */
    private Reader expandReader(final AntFilterReader filter,
                                final Reader ancestor,
                                final List<AntClassLoader> classLoadersToCleanUp) {
        final String className = filter.getClassName();
        final Path classpath = filter.getClasspath();
        final Project pro = filter.getProject();
        if (className != null) {
            try {
                Class<?> clazz = null;
                if (classpath == null) {
                    clazz = Class.forName(className);
                } else {
                    AntClassLoader al = pro.createClassLoader(classpath);
                    classLoadersToCleanUp.add(al);
                    clazz = Class.forName(className, true, al);
                }
                if (clazz != null) {
                    if (!FilterReader.class.isAssignableFrom(clazz)) {
                        throw new BuildException(className + " does not extend"
                                                 + " java.io.FilterReader");
                    }
                    final Constructor<?>[] constructors = clazz.getConstructors();
                    int j = 0;
                    boolean consPresent = false;
                    for (; j < constructors.length; j++) {
                        Class<?>[] types = constructors[j].getParameterTypes();
                        if (types.length == 1
                            && types[0].isAssignableFrom(Reader.class)) {
                            consPresent = true;
                            break;
                        }
                    }
                    if (!consPresent) {
                        throw new BuildException(className + " does not define"
                                                 + " a public constructor"
                                                 + " that takes in a Reader"
                                                 + " as its single argument.");
                    }
                    final Reader[] rdr = {ancestor};
                    Reader instream =
                        (Reader) constructors[j].newInstance((Object[]) rdr);
                    setProjectOnObject(instream);
                    if (Parameterizable.class.isAssignableFrom(clazz)) {
                        final Parameter[] params = filter.getParams();
                        ((Parameterizable) instream).setParameters(params);
                    }
                    return instream;
                }
            } catch (final ClassNotFoundException cnfe) {
                throw new BuildException(cnfe);
            } catch (final InstantiationException ie) {
                throw new BuildException(ie);
            } catch (final IllegalAccessException iae) {
                throw new BuildException(iae);
            } catch (final InvocationTargetException ite) {
                throw new BuildException(ite);
            }
        }
        // Ant 1.7.1 and earlier ignore <filterreader> without a
        // classname attribute, not sure this is a good idea -
        // backwards compatibility makes it hard to change, though.
        return ancestor;
    }
}