1 package org
.argeo
.slc
.core
.deploy
;
3 import java
.io
.IOException
;
4 import java
.util
.ArrayList
;
7 import java
.util
.StringTokenizer
;
8 import java
.util
.TreeMap
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.springframework
.beans
.factory
.InitializingBean
;
14 import org
.springframework
.context
.ResourceLoaderAware
;
15 import org
.springframework
.core
.io
.Resource
;
16 import org
.springframework
.core
.io
.ResourceLoader
;
17 import org
.springframework
.core
.io
.support
.PathMatchingResourcePatternResolver
;
18 import org
.springframework
.core
.io
.support
.ResourcePatternResolver
;
19 import org
.springframework
.util
.AntPathMatcher
;
20 import org
.springframework
.util
.PathMatcher
;
22 public class DefaultResourceSet
implements ResourceLoaderAware
,
23 InitializingBean
, ResourceSet
{
24 private final static Log log
= LogFactory
.getLog(DefaultResourceSet
.class);
25 public final static String DEFAULT_EXCLUDES
= "**/.svn/**";
28 private String include
;
29 private List
<String
> includes
= new ArrayList
<String
>();
30 private String exclude
;
31 private List
<String
> excludes
= new ArrayList
<String
>();
32 private Boolean useDefaultExcludes
= true;
33 private ResourcePatternResolver resourcePatternResolver
;
34 private PathMatcher excludePathMatcher
= new AntPathMatcher();
36 private ResourceLoader resourceLoader
;
38 /** List the resources, identified by their relative path. */
39 public Map
<String
, Resource
> listResources() {
41 String baseResUrl
= getResourceLoaderToUse().getResource(base
)
43 Map
<String
, Resource
> res
= new TreeMap
<String
, Resource
>();
44 for (String includePattern
: includes
)
45 processInclude(res
, includePattern
, baseResUrl
);
47 } catch (IOException e
) {
48 throw new SlcException("Cannot list resource from " + base
, e
);
52 protected void processInclude(Map
<String
, Resource
> res
, String include
,
53 String baseResUrl
) throws IOException
{
54 String pattern
= base
+ "/" + include
;
55 if (log
.isDebugEnabled())
56 log
.debug("Look for resources with pattern '" + pattern
57 + "' in base url " + baseResUrl
);
58 Resource
[] resources
= resourcePatternResolver
.getResources(pattern
);
59 resources
: for (Resource resource
: resources
) {
60 String url
= resource
.getURL().toString();
61 String relPath
= url
.substring(baseResUrl
.length());
64 if (relPath
.charAt(relPath
.length() - 1) == '/') {
65 if (log
.isTraceEnabled())
66 log
.debug("Skip directory " + relPath
+ "=" + resource
);
70 // make sure there is not starting '/'
71 if (relPath
.charAt(0) == '/')
72 relPath
= relPath
.substring(1);
75 for (String exclude
: excludes
)
76 if (excludePathMatcher
.match(exclude
, relPath
)) {
77 if (log
.isTraceEnabled())
78 log
.debug("Exclude " + relPath
+ "=" + resource
);
82 // check if already exists
83 if (res
.containsKey(relPath
))
84 log
.warn(relPath
+ " already matched by " + res
.get(relPath
)
85 + ", " + resource
+ " will override it.");
87 // store the marched resource
88 res
.put(relPath
, resource
);
89 if (log
.isDebugEnabled())
90 log
.debug(relPath
+ "=" + resource
);
95 public void afterPropertiesSet() throws Exception
{
96 if (resourcePatternResolver
== null)
97 resourcePatternResolver
= new PathMatchingResourcePatternResolver(
98 getResourceLoaderToUse());
100 addCommaSeparatedToList(include
, includes
);
102 addCommaSeparatedToList(exclude
, excludes
);
104 if (includes
.size() == 0)
107 if (useDefaultExcludes
)
108 addCommaSeparatedToList(DEFAULT_EXCLUDES
, excludes
);
111 private void addCommaSeparatedToList(String str
, List
<String
> lst
) {
112 StringTokenizer st
= new StringTokenizer(str
, ",");
113 while (st
.hasMoreTokens()) {
114 String token
= st
.nextToken();
115 if (!lst
.contains(token
))
120 public void setResourceLoader(ResourceLoader resourceLoader
) {
121 this.resourceLoader
= resourceLoader
;
125 * Can be overridden in order to provide the proper resource loader used to
128 public ResourceLoader
getResourceLoaderToUse() {
129 return resourceLoader
;
132 public void setBase(String base
) {
136 public void setInclude(String include
) {
137 this.include
= include
;
140 public void setIncludes(List
<String
> includes
) {
141 this.includes
= includes
;
144 public void setExclude(String exclude
) {
145 this.exclude
= exclude
;
148 public void setExcludes(List
<String
> excludes
) {
149 this.excludes
= excludes
;
152 public void setUseDefaultExcludes(Boolean useDefaultExcludes
) {
153 this.useDefaultExcludes
= useDefaultExcludes
;
156 public void setExcludePathMatcher(PathMatcher excludePathMatcher
) {
157 this.excludePathMatcher
= excludePathMatcher
;
160 public void setResourcePatternResolver(
161 ResourcePatternResolver resourcePatternResolver
) {
162 this.resourcePatternResolver
= resourcePatternResolver
;
165 public ResourcePatternResolver
getResourcePatternResolver() {
166 return resourcePatternResolver
;