View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
16   */
17  
18  package org.apache.jdo.impl.enhancer.util;
19  
20  import java.util.Set;
21  import java.util.Map;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Iterator;
25  
26  import java.io.IOException;
27  import java.io.File;
28  import java.io.FileNotFoundException;
29  import java.io.PrintWriter;
30  import java.io.InputStream;
31  import java.io.FileInputStream;
32  
33  import java.net.URL;
34  
35  
36  /***
37   * Searches resources among a set of files.
38   */
39  public class ListResourceLocator
40      extends ResourceLocatorBase
41      implements ResourceLocator
42  {
43      /***
44       * The map of jdo files.
45       */
46      final Map files = new HashMap();
47  
48      /***
49       * Creates an intsance.
50       */
51      public ListResourceLocator(PrintWriter out,
52                                 boolean verbose,
53                                 List fileNames)
54          throws IOException
55      {
56          super(out, verbose);
57          affirm(fileNames != null);
58          
59          // hash the file objects by their canonical name
60          for (Iterator i = fileNames.iterator(); i.hasNext();) {
61              final String s = (String)i.next();
62  
63              // canonicalize file name
64              final File file = new File(s).getCanonicalFile();
65              final URL url = file.toURL();
66              final String canonicalName = url.toString();
67              affirm(canonicalName != null);
68  
69              // ensure file is readable
70              if (!file.canRead()) {
71                  final String msg
72                      = getI18N("enhancer.cannot_read_resource",
73                                file.toString());
74                  throw new IOException(msg);
75              }
76  
77              // hash file by its canonicalized resource name
78              files.put(canonicalName, file);
79              printMessage(getI18N("enhancer.using_file",
80                                   canonicalName));
81          }
82      }
83      
84      /***
85       * Finds a resource with a given name.
86       */
87      public InputStream getInputStreamForResource(String resourceName)
88      {
89          //printMessage("ListResourceLocator.getInputStreamForResource() : resourceName = " + resourceName);
90  
91          affirm(resourceName != null);
92          
93          final Set entries = files.entrySet();
94          for (Iterator i = entries.iterator(); i.hasNext();) {
95              final Map.Entry entry = (Map.Entry)i.next();
96              final String fileName = (String)entry.getKey();
97              if (!fileName.endsWith(resourceName)) {
98                  continue;
99              }
100             final File file = (File)entry.getValue();
101             
102             final InputStream stream;
103             try {
104                 stream = new FileInputStream(file);
105             } catch (FileNotFoundException ex) {
106                 // would be better to throw an IOException but currently
107                 // not supported by the JDOModel's JavaModel interface
108                 final String msg
109                     = getI18N("enhancer.io_error_while_reading_resource",
110                               file.toString(), ex.getMessage());
111                 throw new RuntimeException(msg);
112             }
113             affirm(stream != null);
114             printMessage(getI18N("enhancer.found_resource", resourceName));
115             return stream;
116         }
117         printMessage(getI18N("enhancer.not_found_resource", resourceName));
118         return null;
119     }
120 }