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 */ 017 package org.apache.camel.component.file; 018 019 import java.lang.reflect.Method; 020 021 import org.apache.camel.util.ObjectHelper; 022 import static org.apache.camel.util.CollectionHelper.collectionAsCommaDelimitedString; 023 024 /** 025 * File filter using Spring's AntPathMatcher. 026 * <p/> 027 * Exclude take precedence over includes. If a file match both exclude and include it will be regarded as excluded. 028 */ 029 public class AntPathMatcherGenericFileFilter implements GenericFileFilter { 030 private static final String ANTPATHMATCHER_CLASSNAME = "org.apache.camel.spring.util.SpringAntPathMatcherFileFilter"; 031 032 private String[] excludes; 033 private String[] includes; 034 035 private Object filter; 036 private Method includesMethod; 037 private Method excludesMethod; 038 private Method acceptsMethod; 039 040 public boolean accept(GenericFile file) { 041 try { 042 synchronized (this) { 043 if (filter == null) { 044 init(); 045 } 046 } 047 048 // invoke setIncludes(String), must using string type as invoking with string[] does not work 049 ObjectHelper.invokeMethod(includesMethod, filter, collectionAsCommaDelimitedString(includes)); 050 051 // invoke setExcludes(String), must using string type as invoking with string[] does not work 052 ObjectHelper.invokeMethod(excludesMethod, filter, collectionAsCommaDelimitedString(excludes)); 053 054 // invoke acceptPathName(String) 055 String path = file.getRelativeFilePath(); 056 return (Boolean) ObjectHelper.invokeMethod(acceptsMethod, filter, path); 057 058 } catch (NoSuchMethodException e) { 059 throw new TypeNotPresentException(ANTPATHMATCHER_CLASSNAME, e); 060 } 061 } 062 063 @SuppressWarnings("unchecked") 064 private void init() throws NoSuchMethodException { 065 // we must use reflection to invoke the AntPathMatcherFileFilter that reside in camel-spring.jar 066 // and we don't want camel-ftp to have runtime dependency on camel-spring.jar 067 Class clazz = ObjectHelper.loadClass(ANTPATHMATCHER_CLASSNAME); 068 ObjectHelper.notNull(clazz, ANTPATHMATCHER_CLASSNAME + " not found in classpath. camel-spring.jar is required in the classpath."); 069 070 filter = ObjectHelper.newInstance(clazz); 071 072 includesMethod = filter.getClass().getMethod("setIncludes", String.class); 073 excludesMethod = filter.getClass().getMethod("setExcludes", String.class); 074 acceptsMethod = filter.getClass().getMethod("acceptPathName", String.class); 075 } 076 077 public String[] getExcludes() { 078 return excludes; 079 } 080 081 public void setExcludes(String[] excludes) { 082 this.excludes = excludes; 083 } 084 085 public String[] getIncludes() { 086 return includes; 087 } 088 089 public void setIncludes(String[] includes) { 090 this.includes = includes; 091 } 092 093 /** 094 * Sets excludes using a single string where each element can be separated with comma 095 */ 096 public void setExcludes(String excludes) { 097 setExcludes(excludes.split(",")); 098 } 099 100 /** 101 * Sets includes using a single string where each element can be separated with comma 102 */ 103 public void setIncludes(String includes) { 104 setIncludes(includes.split(",")); 105 } 106 107 }