View Javadoc

1   /*
2    * $Id: MultiDirResource.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.quickstart;
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  import java.util.ArrayList;
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.mortbay.util.Resource;
35  
36  /***
37   */
38  public class MultiDirResource extends Resource {
39  	
40  	private static final long serialVersionUID = -7571068340424106599L;
41  	
42  	MultiWebApplicationContext ctx;
43      File[] files;
44      String uri;
45  
46      public MultiDirResource(MultiWebApplicationContext ctx, String uri, List pathPriority, Map paths) {
47          this.ctx = ctx;
48          this.uri = uri;
49          ArrayList files = new ArrayList();
50          for (Iterator iterator = pathPriority.iterator(); iterator.hasNext();) {
51              String path = (String) iterator.next();
52              List dirs = (List) paths.get(path);
53  
54              if (uri.startsWith(path) || (uri.equals("") && path.equals("/"))) {
55                  for (Iterator iterator1 = dirs.iterator(); iterator1.hasNext();) {
56                      String s = (String) iterator1.next();
57  
58                      if (uri.startsWith(path)) {
59                          // cut off the path from the start of the URI
60                          files.add(new File(s, uri.substring(path.length())));
61                      } else {
62                          files.add(new File(s, uri));
63                      }
64                  }
65              }
66          }
67  
68          this.files = (File[]) files.toArray(new File[files.size()]);
69      }
70  
71      public void release() {
72      }
73  
74      public boolean exists() {
75          for (int i = 0; i < files.length; i++) {
76              File file = files[i];
77              if (file.exists()) {
78                  return true;
79              }
80          }
81  
82          return false;
83      }
84  
85      public boolean isDirectory() {
86          for (int i = 0; i < files.length; i++) {
87              File file = files[i];
88              if (file.exists()) {
89                  return file.isDirectory();
90              }
91          }
92  
93          return false;
94      }
95  
96      public long lastModified() {
97          for (int i = 0; i < files.length; i++) {
98              File file = files[i];
99              if (file.exists()) {
100                 return file.lastModified();
101             }
102         }
103 
104         return 0;
105     }
106 
107     public long length() {
108         for (int i = 0; i < files.length; i++) {
109             File file = files[i];
110             if (file.exists()) {
111                 return file.length();
112             }
113         }
114 
115         return 0;
116     }
117 
118     public URL getURL() {
119         for (int i = 0; i < files.length; i++) {
120             File file = files[i];
121             if (file.exists()) {
122                 try {
123                     return file.toURL();
124                 } catch (MalformedURLException e) {
125                     e.printStackTrace();
126                 }
127             }
128         }
129 
130         return null;
131     }
132 
133     public File getFile() throws IOException {
134         for (int i = 0; i < files.length; i++) {
135             File file = files[i];
136             if (file.exists()) {
137                 return file;
138             }
139         }
140 
141         return null;
142     }
143 
144     public String getName() {
145         for (int i = 0; i < files.length; i++) {
146             File file = files[i];
147             if (file.exists()) {
148                 return file.getName();
149             }
150         }
151 
152         return null;
153     }
154 
155     public InputStream getInputStream() throws IOException {
156         for (int i = 0; i < files.length; i++) {
157             File file = files[i];
158             if (file.exists()) {
159                 return new FileInputStream(file);
160             }
161         }
162 
163         return null;
164     }
165 
166     public OutputStream getOutputStream() throws IOException, SecurityException {
167         for (int i = 0; i < files.length; i++) {
168             File file = files[i];
169             if (file.exists()) {
170                 return new FileOutputStream(file);
171             }
172         }
173 
174         return null;
175     }
176 
177     public boolean delete() throws SecurityException {
178         for (int i = 0; i < files.length; i++) {
179             File file = files[i];
180             if (file.exists()) {
181                 return file.delete();
182             }
183         }
184 
185         return false;
186     }
187 
188     public boolean renameTo(Resource resource) throws SecurityException {
189         return false;
190     }
191 
192     public String[] list() {
193         HashSet set = new HashSet();
194         for (int i = 0; i < files.length; i++) {
195             File file = files[i];
196             if (file.exists()) {
197                 String[] files = file.list();
198                 for (int j = 0; j < files.length; j++) {
199                     String s = files[j];
200                     set.add(s);
201                 }
202             }
203         }
204 
205         return (String[]) set.toArray(new String[set.size()]);
206     }
207 
208     public Resource addPath(String string) throws IOException, MalformedURLException {
209         return ctx.newResolver(uri + string);
210     }
211 }