1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.trinidad.resource;
20
21 import java.io.IOException;
22 import java.net.URL;
23
24 /**
25 * A Dynamic Resource Loader that is capable of agregation and dynamic content via custom
26 * URLConnection objects.
27 *
28 */
29 public abstract class DynamicResourceLoader extends ResourceLoader
30 {
31 /**
32 * Creates a DynamicResourceLoader for a specified path. The dynamic resource loader will only
33 * respond to the specified path when findResource is executed with the same path. If it is not
34 * the same, findResource will TRY to redirect the request to the parent if one is provided. The
35 * parent ResourceLoader for this constructor is <code>null</code>
36 *
37 * @param path the path that this ResourceLoader will respond to
38 */
39
40 public DynamicResourceLoader(String path)
41 {
42 this(path, null);
43 }
44
45 /**
46 * Creates a CompositeResouceLoader for a specified path. The dynamic resource loader will only
47 * respond to the specified path when findResource is executed with the same path. If it is not
48 * the same, findResource will TRY to redirect the request to the parent if one is provided.
49 *
50 * @param path the path that this ResourceLoader will respond to
51 * @param parent the parent resource loader
52 */
53 public DynamicResourceLoader(String path, ResourceLoader parent)
54 {
55 super(parent);
56
57 if(path==null)
58 {
59 throw new NullPointerException();
60 }
61
62 _path = path;
63 }
64
65 /**
66 * Finds a resource. If the path does not match the path that this object was constructed with
67 * then it will direct the request to the base ResourceLoader. If the path is the same, it calls
68 * the getURL method.
69 *
70 * @param path the path which the ResourceLoader is looking for.
71 * @return a URL for this resource
72 * @throws IOException when something bad happens
73 */
74 @Override
75 protected URL findResource(String path) throws IOException
76 {
77 if (_path.equals(path))
78 {
79 return getURL(path);
80 }
81
82 return super.findResource(path);
83 }
84
85 /**
86 * Returns a URL to this resource. This URL should have a valid connection.'
87 *
88 * @param path the path of the findResourceCall
89 * @return a url for this Resource
90 * @throws IOException when something bad happens
91 */
92 protected abstract URL getURL(String path) throws IOException;
93
94 private String _path;
95
96 }