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 Map
<String
, Resource
> res
= new TreeMap
<String
, Resource
>();
60 String baseResUrl
= getResourceLoaderToUse().getResource(base
)
62 for (String includePattern
: includes
)
63 processInclude(res
, includePattern
, baseResUrl
);
65 } catch (IOException e
) {
66 throw new SlcException("Cannot list resource from " + base
, e
);
70 protected void processInclude(Map
<String
, Resource
> res
, String include
,
71 String baseResUrl
) throws IOException
{
72 String pattern
= base
+ "/" + include
;
73 if (log
.isTraceEnabled())
74 log
.trace("Look for resources with pattern '" + pattern
75 + "' in base url " + baseResUrl
);
76 Resource
[] resources
= resourcePatternResolver
.getResources(pattern
);
77 resources
: for (Resource resource
: resources
) {
78 String url
= resource
.getURL().toString();
79 String relPath
= url
.substring(baseResUrl
.length());
82 if (relPath
.charAt(relPath
.length() - 1) == '/') {
83 if (log
.isTraceEnabled())
84 log
.trace("Skip directory " + relPath
+ "=" + resource
);
88 // make sure there is not starting '/'
89 if (relPath
.charAt(0) == '/')
90 relPath
= relPath
.substring(1);
93 for (String exclude
: excludes
)
94 if (excludePathMatcher
.match(exclude
, relPath
)) {
95 if (log
.isTraceEnabled())
96 log
.trace("Exclude " + relPath
+ "=" + resource
);
100 // check if already exists
101 if (res
.containsKey(relPath
))
102 log
.warn(relPath
+ " already matched by " + res
.get(relPath
)
103 + ", " + resource
+ " will override it.");
105 // store the marched resource
106 res
.put(relPath
, resource
);
107 if (log
.isTraceEnabled())
108 log
.trace(relPath
+ "=" + resource
);
113 public void afterPropertiesSet() throws Exception
{
114 if (resourcePatternResolver
== null)
115 resourcePatternResolver
= new PathMatchingResourcePatternResolver(
116 getResourceLoaderToUse());
118 addCommaSeparatedToList(include
, includes
);
120 addCommaSeparatedToList(exclude
, excludes
);
122 if (includes
.size() == 0)
125 if (useDefaultExcludes
)
126 addCommaSeparatedToList(DEFAULT_EXCLUDES
, excludes
);
129 private void addCommaSeparatedToList(String str
, List
<String
> lst
) {
130 StringTokenizer st
= new StringTokenizer(str
, ",");
131 while (st
.hasMoreTokens()) {
132 String token
= st
.nextToken();
133 if (!lst
.contains(token
))
138 public void setResourceLoader(ResourceLoader resourceLoader
) {
139 this.resourceLoader
= resourceLoader
;
143 * Can be overridden in order to provide the proper resource loader used to
146 public ResourceLoader
getResourceLoaderToUse() {
147 return resourceLoader
;
150 public void setBase(String base
) {
154 public void setInclude(String include
) {
155 this.include
= include
;
158 public void setIncludes(List
<String
> includes
) {
159 this.includes
= includes
;
162 public void setExclude(String exclude
) {
163 this.exclude
= exclude
;
166 public void setExcludes(List
<String
> excludes
) {
167 this.excludes
= excludes
;
170 public void setUseDefaultExcludes(Boolean useDefaultExcludes
) {
171 this.useDefaultExcludes
= useDefaultExcludes
;
174 public void setExcludePathMatcher(PathMatcher excludePathMatcher
) {
175 this.excludePathMatcher
= excludePathMatcher
;
178 public void setResourcePatternResolver(
179 ResourcePatternResolver resourcePatternResolver
) {
180 this.resourcePatternResolver
= resourcePatternResolver
;
183 public ResourcePatternResolver
getResourcePatternResolver() {
184 return resourcePatternResolver
;