aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java
blob: 23db3743217179add839d8b2d424a6c64186d497 (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
package org.apache.velocity.runtime.resource.loader;

/*
 * 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.
 */

import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.util.ExtProperties;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * <p>
 * ResourceLoader to load templates from multiple Jar files.
 * </p>
 * <p>
 * The configuration of the JarResourceLoader is straightforward -
 * You simply add the JarResourceLoader to the configuration via
 * </p>
 * <pre><code>
 *    resource.loaders = jar
 *    resource.loader.jar.class = org.apache.velocity.runtime.resource.loader.JarResourceLoader
 *    resource.loader.jar.path = list of JAR &lt;URL&gt;s
 * </code></pre>
 *
 * <p> So for example, if you had a jar file on your local filesystem, you could simply do</p>
 *    <pre><code>
 *    resource.loader.jar.path = jar:file:/opt/myfiles/jar1.jar
 *    </code></pre>
 * <p> Note that jar specification for the <code>.path</code> configuration property
 * conforms to the same rules for the java.net.JarUrlConnection class.
 * </p>
 *
 * <p> For a working example, see the unit test case,
 *  org.apache.velocity.test.MultiLoaderTestCase class
 * </p>
 *
 * @author <a href="mailto:mailmur@yahoo.com">Aki Nieminen</a>
 * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
 * @version $Id$
 */
public class JarResourceLoader extends ResourceLoader
{
    /**
     * Maps entries to the parent JAR File
     * Key = the entry *excluding* plain directories
     * Value = the JAR URL
     */
    private Map<String, String> entryDirectory = new HashMap<>(559);

    /**
     * Maps JAR URLs to the actual JAR
     * Key = the JAR URL
     * Value = the JAR
     */
    private Map<String, JarHolder> jarfiles = new HashMap<>(89);

    /**
     * Called by Velocity to initialize the loader
     * @param configuration
     */
    @Override
    public void init(ExtProperties configuration)
    {
        log.trace("JarResourceLoader: initialization starting.");

        List<String> paths = configuration.getList(RuntimeConstants.RESOURCE_LOADER_PATHS);

        if (paths != null)
        {
            log.debug("JarResourceLoader # of paths: {}", paths.size() );

            for (ListIterator<String> it = paths.listIterator(); it.hasNext(); )
            {
                String jar = StringUtils.trim(it.next());
                it.set(jar);
                loadJar(jar);
            }
        }

        log.trace("JarResourceLoader: initialization complete.");
    }

    private void loadJar( String path )
    {
        log.debug("JarResourceLoader: trying to load \"{}\"", path);

        // Check path information
        if ( path == null )
        {
            String msg = "JarResourceLoader: can not load JAR - JAR path is null";
            log.error(msg);
            throw new RuntimeException(msg);
        }
        if ( !path.startsWith("jar:") )
        {
            String msg = "JarResourceLoader: JAR path must start with jar: -> see java.net.JarURLConnection for information";
            log.error(msg);
            throw new RuntimeException(msg);
        }
        if (!path.contains("!/"))
        {
            path += "!/";
        }

        // Close the jar if it's already open
        // this is useful for a reload
        closeJar( path );

        // Create a new JarHolder
        JarHolder temp = new JarHolder( rsvc,  path, log );
        // Add it's entries to the entryCollection
        addEntries(temp.getEntries());
        // Add it to the Jar table
        jarfiles.put(temp.getUrlPath(), temp);
    }

    /**
     * Closes a Jar file and set its URLConnection
     * to null.
     */
    private void closeJar( String path )
    {
        if ( jarfiles.containsKey(path) )
        {
            JarHolder theJar = jarfiles.get(path);
            theJar.close();
        }
    }

    /**
     * Copy all the entries into the entryDirectory
     * It will overwrite any duplicate keys.
     */
    private void addEntries( Map<String, String> entries )
    {
        entryDirectory.putAll( entries );
    }

    /**
     * Get a Reader so that the Runtime can build a
     * template with it.
     *
     * @param source name of template to get
     * @param encoding asked encoding
     * @return InputStream containing the template
     * @throws ResourceNotFoundException if template not found
     *         in the file template path.
     * @since 2.0
    */
    @Override
    public Reader getResourceReader(String source, String encoding )
            throws ResourceNotFoundException
    {
        Reader result = null;

        if (org.apache.commons.lang3.StringUtils.isEmpty(source))
        {
            throw new ResourceNotFoundException("Need to have a resource!");
        }

        String normalizedPath = FilenameUtils.normalize( source, true );

        if ( normalizedPath == null || normalizedPath.length() == 0 )
        {
            String msg = "JAR resource error: argument " + normalizedPath +
                    " contains .. and may be trying to access " +
                    "content outside of template root.  Rejected.";

            log.error( "JarResourceLoader: {}", msg );

            throw new ResourceNotFoundException ( msg );
        }

        /*
         *  if a / leads off, then just nip that :)
         */
        if ( normalizedPath.startsWith("/") )
        {
            normalizedPath = normalizedPath.substring(1);
        }

        if ( entryDirectory.containsKey( normalizedPath ) )
        {
            String jarurl  = entryDirectory.get( normalizedPath );

            if ( jarfiles.containsKey( jarurl ) )
            {
                JarHolder holder = (JarHolder)jarfiles.get( jarurl );
                InputStream rawStream = holder.getResource( normalizedPath );
                try
                {
                    return buildReader(rawStream, encoding);
                }
                catch (Exception e)
                {
                    if (rawStream != null)
                    {
                        try
                        {
                            rawStream.close();
                        }
                        catch (IOException ioe) {}
                    }
                    String msg = "JAR resource error: Exception while loading " + source;
                    log.error(msg, e);
                    throw new VelocityException(msg, e, rsvc.getLogContext().getStackTrace());
                }
            }
        }

        throw new ResourceNotFoundException( "JarResourceLoader Error: cannot find resource " +
                source );

    }

    // TODO: SHOULD BE DELEGATED TO THE JARHOLDER

    /**
     * @see ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
     */
    @Override
    public boolean isSourceModified(Resource resource)
    {
        return true;
    }

    /**
     * @see ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
     */
    @Override
    public long getLastModified(Resource resource)
    {
        return 0;
    }
}