2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.core
.deploy
;
19 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import java
.util
.StringTokenizer
;
24 import java
.util
.TreeMap
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.springframework
.beans
.factory
.InitializingBean
;
30 import org
.springframework
.context
.ResourceLoaderAware
;
31 import org
.springframework
.core
.io
.Resource
;
32 import org
.springframework
.core
.io
.ResourceLoader
;
33 import org
.springframework
.core
.io
.support
.PathMatchingResourcePatternResolver
;
34 import org
.springframework
.core
.io
.support
.ResourcePatternResolver
;
35 import org
.springframework
.util
.AntPathMatcher
;
36 import org
.springframework
.util
.PathMatcher
;
38 public class DefaultResourceSet
implements ResourceLoaderAware
,
39 InitializingBean
, ResourceSet
{
40 private final static Log log
= LogFactory
.getLog(DefaultResourceSet
.class);
41 public final static String DEFAULT_EXCLUDES
= "**/.svn/**";
44 private String include
;
45 private List
<String
> includes
= new ArrayList
<String
>();
46 private String exclude
;
47 private List
<String
> excludes
= new ArrayList
<String
>();
48 private Boolean useDefaultExcludes
= true;
49 private ResourcePatternResolver resourcePatternResolver
;
50 private PathMatcher excludePathMatcher
= new AntPathMatcher();
52 private ResourceLoader resourceLoader
;
54 /** List the resources, identified by their relative path. */
55 public Map
<String
, Resource
> listResources() {
57 String baseResUrl
= getResourceLoaderToUse().getResource(base
)
59 Map
<String
, Resource
> res
= new TreeMap
<String
, Resource
>();
60 for (String includePattern
: includes
)
61 processInclude(res
, includePattern
, baseResUrl
);
63 } catch (IOException e
) {
64 throw new SlcException("Cannot list resource from " + base
, e
);
68 protected void processInclude(Map
<String
, Resource
> res
, String include
,
69 String baseResUrl
) throws IOException
{
70 String pattern
= base
+ "/" + include
;
71 if (log
.isTraceEnabled())
72 log
.trace("Look for resources with pattern '" + pattern
73 + "' in base url " + baseResUrl
);
74 Resource
[] resources
= resourcePatternResolver
.getResources(pattern
);
75 resources
: for (Resource resource
: resources
) {
76 String url
= resource
.getURL().toString();
77 String relPath
= url
.substring(baseResUrl
.length());
80 if (relPath
.charAt(relPath
.length() - 1) == '/') {
81 if (log
.isTraceEnabled())
82 log
.trace("Skip directory " + relPath
+ "=" + resource
);
86 // make sure there is not starting '/'
87 if (relPath
.charAt(0) == '/')
88 relPath
= relPath
.substring(1);
91 for (String exclude
: excludes
)
92 if (excludePathMatcher
.match(exclude
, relPath
)) {
93 if (log
.isTraceEnabled())
94 log
.trace("Exclude " + relPath
+ "=" + resource
);
98 // check if already exists
99 if (res
.containsKey(relPath
))
100 log
.warn(relPath
+ " already matched by " + res
.get(relPath
)
101 + ", " + resource
+ " will override it.");
103 // store the marched resource
104 res
.put(relPath
, resource
);
105 if (log
.isTraceEnabled())
106 log
.trace(relPath
+ "=" + resource
);
111 public void afterPropertiesSet() throws Exception
{
112 if (resourcePatternResolver
== null)
113 resourcePatternResolver
= new PathMatchingResourcePatternResolver(
114 getResourceLoaderToUse());
116 addCommaSeparatedToList(include
, includes
);
118 addCommaSeparatedToList(exclude
, excludes
);
120 if (includes
.size() == 0)
123 if (useDefaultExcludes
)
124 addCommaSeparatedToList(DEFAULT_EXCLUDES
, excludes
);
127 private void addCommaSeparatedToList(String str
, List
<String
> lst
) {
128 StringTokenizer st
= new StringTokenizer(str
, ",");
129 while (st
.hasMoreTokens()) {
130 String token
= st
.nextToken();
131 if (!lst
.contains(token
))
136 public void setResourceLoader(ResourceLoader resourceLoader
) {
137 this.resourceLoader
= resourceLoader
;
141 * Can be overridden in order to provide the proper resource loader used to
144 public ResourceLoader
getResourceLoaderToUse() {
145 return resourceLoader
;
148 public void setBase(String base
) {
152 public void setInclude(String include
) {
153 this.include
= include
;
156 public void setIncludes(List
<String
> includes
) {
157 this.includes
= includes
;
160 public void setExclude(String exclude
) {
161 this.exclude
= exclude
;
164 public void setExcludes(List
<String
> excludes
) {
165 this.excludes
= excludes
;
168 public void setUseDefaultExcludes(Boolean useDefaultExcludes
) {
169 this.useDefaultExcludes
= useDefaultExcludes
;
172 public void setExcludePathMatcher(PathMatcher excludePathMatcher
) {
173 this.excludePathMatcher
= excludePathMatcher
;
176 public void setResourcePatternResolver(
177 ResourcePatternResolver resourcePatternResolver
) {
178 this.resourcePatternResolver
= resourcePatternResolver
;
181 public ResourcePatternResolver
getResourcePatternResolver() {
182 return resourcePatternResolver
;