]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/deploy/DefaultResourceSet.java
Unique launch
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / deploy / DefaultResourceSet.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.core.deploy;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.StringTokenizer;
24 import java.util.TreeMap;
25
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;
37
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/**";
42
43 private String base;
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();
51
52 private ResourceLoader resourceLoader;
53
54 /** List the resources, identified by their relative path. */
55 public Map<String, Resource> listResources() {
56 try {
57 Map<String, Resource> res = new TreeMap<String, Resource>();
58 if (base == null)
59 return res;
60 String baseResUrl = getResourceLoaderToUse().getResource(base)
61 .getURL().toString();
62 for (String includePattern : includes)
63 processInclude(res, includePattern, baseResUrl);
64 return res;
65 } catch (IOException e) {
66 throw new SlcException("Cannot list resource from " + base, e);
67 }
68 }
69
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());
80
81 // skip dir
82 if (relPath.charAt(relPath.length() - 1) == '/') {
83 if (log.isTraceEnabled())
84 log.trace("Skip directory " + relPath + "=" + resource);
85 continue resources;
86 }
87
88 // make sure there is not starting '/'
89 if (relPath.charAt(0) == '/')
90 relPath = relPath.substring(1);
91
92 // skip excludes
93 for (String exclude : excludes)
94 if (excludePathMatcher.match(exclude, relPath)) {
95 if (log.isTraceEnabled())
96 log.trace("Exclude " + relPath + "=" + resource);
97 continue resources;
98 }
99
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.");
104
105 // store the marched resource
106 res.put(relPath, resource);
107 if (log.isTraceEnabled())
108 log.trace(relPath + "=" + resource);
109 }
110
111 }
112
113 public void afterPropertiesSet() throws Exception {
114 if (resourcePatternResolver == null)
115 resourcePatternResolver = new PathMatchingResourcePatternResolver(
116 getResourceLoaderToUse());
117 if (include != null)
118 addCommaSeparatedToList(include, includes);
119 if (exclude != null)
120 addCommaSeparatedToList(exclude, excludes);
121
122 if (includes.size() == 0)
123 includes.add("**");
124
125 if (useDefaultExcludes)
126 addCommaSeparatedToList(DEFAULT_EXCLUDES, excludes);
127 }
128
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))
134 lst.add(token);
135 }
136 }
137
138 public void setResourceLoader(ResourceLoader resourceLoader) {
139 this.resourceLoader = resourceLoader;
140 }
141
142 /**
143 * Can be overridden in order to provide the proper resource loader used to
144 * resolve resources.
145 */
146 public ResourceLoader getResourceLoaderToUse() {
147 return resourceLoader;
148 }
149
150 public void setBase(String base) {
151 this.base = base;
152 }
153
154 public void setInclude(String include) {
155 this.include = include;
156 }
157
158 public void setIncludes(List<String> includes) {
159 this.includes = includes;
160 }
161
162 public void setExclude(String exclude) {
163 this.exclude = exclude;
164 }
165
166 public void setExcludes(List<String> excludes) {
167 this.excludes = excludes;
168 }
169
170 public void setUseDefaultExcludes(Boolean useDefaultExcludes) {
171 this.useDefaultExcludes = useDefaultExcludes;
172 }
173
174 public void setExcludePathMatcher(PathMatcher excludePathMatcher) {
175 this.excludePathMatcher = excludePathMatcher;
176 }
177
178 public void setResourcePatternResolver(
179 ResourcePatternResolver resourcePatternResolver) {
180 this.resourcePatternResolver = resourcePatternResolver;
181 }
182
183 public ResourcePatternResolver getResourcePatternResolver() {
184 return resourcePatternResolver;
185 }
186
187 }