View Javadoc

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.creadur.whisker.app.load;
20  
21  import java.io.File;
22  
23  import org.apache.creadur.whisker.app.StreamableResource;
24  
25  /**
26   * Conveniently builds {@link StreamableResource} implementations.
27   */
28  public final class StreamableResourceFactory {
29  
30      /**
31       * Builds instance that streams, on demand,
32       * from resource on the class path.
33       * @param name full name, including path, of the resource not null
34       * @return an instance that streams, on demand, the given resource,
35       * not null
36       */
37      public StreamableResource streamFromClassPathResource(final String name) {
38          return new StreamableClassPathResource(name);
39      }
40  
41      /**
42       * Builds instance that streams, on demand,
43       * from resource stored in the file system.
44       * @param fileName full name, including path, of the resource not null
45       * @return an instance that streams, on demand, the given resource,
46       * not null
47       */
48      public StreamableResource streamFromFileResource(final String fileName) {
49          return new StreamableFileNameResource(fileName);
50      }
51  
52      /**
53       * Builds instance that streams, on demand,
54       * from resource stored in the file system.
55       * @param file a file storing the resource not null
56       * @return an instance that streams, on demand, the given resource,
57       * not null
58       */
59      public StreamableResource streamFromFileResource(final File file) {
60          return new StreamableFileResource(file);
61      }
62  
63      /**
64       * When the resource is found on the classpath,
65       * builds an instance that streams, on demand,
66       * from the classpath. Otherwise, builds an
67       * instances that streams from the file system.
68       * @param resourceName
69       * @return not null
70       */
71      public StreamableResource streamFromResource(final String resourceName) {
72          final StreamableClassPathResource streamFromClasspath =
73                  new StreamableClassPathResource(resourceName);
74          if (streamFromClasspath.exists()) {
75              return streamFromClasspath;
76          }
77          return streamFromFileResource(resourceName);
78      }
79  }