001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.file; 018 019import java.io.IOException; 020 021import org.apache.wicket.util.resource.FileResourceStream; 022import org.apache.wicket.util.resource.IResourceStream; 023 024 025/** 026 * An {@link IResourceFinder} that looks for its resources in a filesystem path. 027 * 028 * @author Jonathan Locke 029 * @author Carl-Eric Menzel 030 */ 031public class Path implements IResourceFinder 032{ 033 private final Folder folder; 034 035 /** 036 * Constructor 037 * 038 * @param folder 039 * The folder to look in 040 */ 041 public Path(final String folder) 042 { 043 this(new Folder(folder)); 044 } 045 046 /** 047 * Constructor 048 * 049 * @param folder 050 * The folder to look in 051 */ 052 public Path(final Folder folder) 053 { 054 if (!folder.exists()) 055 { 056 throw new IllegalArgumentException("Folder " + folder + " does not exist"); 057 } 058 this.folder = folder; 059 } 060 061 /** 062 * Looks for {@code pathname} in the provided {@code folder}. 063 * 064 * @param clazz 065 * The class requesting the resource stream 066 * @param pathname 067 * the path to the needed resource. Must be relative to {@code folder} 068 * @see org.apache.wicket.util.file.IResourceFinder#find(Class, String) 069 */ 070 @Override 071 public IResourceStream find(final Class<?> clazz, final String pathname) 072 { 073 final File file = new File(folder, pathname); 074 075 if (file.exists()) 076 { 077 return new FileResourceStream(file); 078 } 079 else 080 { 081 return null; 082 } 083 } 084 085 /** 086 * @see java.lang.Object#toString() 087 */ 088 @Override 089 public String toString() 090 { 091 try 092 { 093 return "[Path: folder = " + folder.getCanonicalPath() + "]"; 094 } 095 catch (IOException e) 096 { 097 return "[Path: exception while inspecting folder]"; 098 } 099 } 100}