001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.quickstart;
006:
007: import org.mortbay.util.Resource;
008:
009: import java.io.*;
010: import java.util.*;
011: import java.net.URL;
012: import java.net.MalformedURLException;
013:
014: /**
015: * Integration to Jetty.
016: */
017: public class MultiDirResource extends Resource {
018: MultiWebApplicationContext ctx;
019: File[] files;
020: String uri;
021:
022: public MultiDirResource(MultiWebApplicationContext ctx, String uri,
023: List pathPriority, Map paths) {
024: this .ctx = ctx;
025: this .uri = uri;
026: ArrayList files = new ArrayList();
027: for (Iterator iterator = pathPriority.iterator(); iterator
028: .hasNext();) {
029: String path = (String) iterator.next();
030: List dirs = (List) paths.get(path);
031:
032: if (uri.startsWith(path)
033: || (uri.equals("") && path.equals("/"))) {
034: for (Iterator iterator1 = dirs.iterator(); iterator1
035: .hasNext();) {
036: String s = (String) iterator1.next();
037:
038: if (uri.startsWith(path)) {
039: // cut off the path from the start of the URI
040: files.add(new File(s, uri.substring(path
041: .length())));
042: } else {
043: files.add(new File(s, uri));
044: }
045: }
046: }
047: }
048:
049: this .files = (File[]) files.toArray(new File[files.size()]);
050: }
051:
052: public void release() {
053: }
054:
055: public boolean exists() {
056: for (int i = 0; i < files.length; i++) {
057: File file = files[i];
058: if (file.exists()) {
059: return true;
060: }
061: }
062:
063: return false;
064: }
065:
066: public boolean isDirectory() {
067: for (int i = 0; i < files.length; i++) {
068: File file = files[i];
069: if (file.exists()) {
070: return file.isDirectory();
071: }
072: }
073:
074: return false;
075: }
076:
077: public long lastModified() {
078: for (int i = 0; i < files.length; i++) {
079: File file = files[i];
080: if (file.exists()) {
081: return file.lastModified();
082: }
083: }
084:
085: return 0;
086: }
087:
088: public long length() {
089: for (int i = 0; i < files.length; i++) {
090: File file = files[i];
091: if (file.exists()) {
092: return file.length();
093: }
094: }
095:
096: return 0;
097: }
098:
099: public URL getURL() {
100: for (int i = 0; i < files.length; i++) {
101: File file = files[i];
102: if (file.exists()) {
103: try {
104: return file.toURL();
105: } catch (MalformedURLException e) {
106: e.printStackTrace();
107: }
108: }
109: }
110:
111: return null;
112: }
113:
114: public File getFile() throws IOException {
115: for (int i = 0; i < files.length; i++) {
116: File file = files[i];
117: if (file.exists()) {
118: return file;
119: }
120: }
121:
122: return null;
123: }
124:
125: public String getName() {
126: for (int i = 0; i < files.length; i++) {
127: File file = files[i];
128: if (file.exists()) {
129: return file.getName();
130: }
131: }
132:
133: return null;
134: }
135:
136: public InputStream getInputStream() throws IOException {
137: for (int i = 0; i < files.length; i++) {
138: File file = files[i];
139: if (file.exists()) {
140: return new FileInputStream(file);
141: }
142: }
143:
144: return null;
145: }
146:
147: public OutputStream getOutputStream() throws IOException,
148: SecurityException {
149: for (int i = 0; i < files.length; i++) {
150: File file = files[i];
151: if (file.exists()) {
152: return new FileOutputStream(file);
153: }
154: }
155:
156: return null;
157: }
158:
159: public boolean delete() throws SecurityException {
160: for (int i = 0; i < files.length; i++) {
161: File file = files[i];
162: if (file.exists()) {
163: return file.delete();
164: }
165: }
166:
167: return false;
168: }
169:
170: public boolean renameTo(Resource resource) throws SecurityException {
171: return false;
172: }
173:
174: public String[] list() {
175: HashSet set = new HashSet();
176: for (int i = 0; i < files.length; i++) {
177: File file = files[i];
178: if (file.exists()) {
179: String[] files = file.list();
180: for (int j = 0; j < files.length; j++) {
181: String s = files[j];
182: set.add(s);
183: }
184: }
185: }
186:
187: return (String[]) set.toArray(new String[set.size()]);
188: }
189:
190: public Resource addPath(String string) throws IOException,
191: MalformedURLException {
192: return ctx.newResolver(uri + string);
193: }
194: }
|