]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/deploy/DefaultResourceSet.java
Start working on serialized JMS
[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 String baseResUrl = getResourceLoaderToUse().getResource(base)
58 .getURL().toString();
59 Map<String, Resource> res = new TreeMap<String, Resource>();
60 for (String includePattern : includes)
61 processInclude(res, includePattern, baseResUrl);
62 return res;
63 } catch (IOException e) {
64 throw new SlcException("Cannot list resource from " + base, e);
65 }
66 }
67
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());
78
79 // skip dir
80 if (relPath.charAt(relPath.length() - 1) == '/') {
81 if (log.isTraceEnabled())
82 log.trace("Skip directory " + relPath + "=" + resource);
83 continue resources;
84 }
85
86 // make sure there is not starting '/'
87 if (relPath.charAt(0) == '/')
88 relPath = relPath.substring(1);
89
90 // skip excludes
91 for (String exclude : excludes)
92 if (excludePathMatcher.match(exclude, relPath)) {
93 if (log.isTraceEnabled())
94 log.trace("Exclude " + relPath + "=" + resource);
95 continue resources;
96 }
97
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.");
102
103 // store the marched resource
104 res.put(relPath, resource);
105 if (log.isTraceEnabled())
106 log.trace(relPath + "=" + resource);
107 }
108
109 }
110
111 public void afterPropertiesSet() throws Exception {
112 if (resourcePatternResolver == null)
113 resourcePatternResolver = new PathMatchingResourcePatternResolver(
114 getResourceLoaderToUse());
115 if (include != null)
116 addCommaSeparatedToList(include, includes);
117 if (exclude != null)
118 addCommaSeparatedToList(exclude, excludes);
119
120 if (includes.size() == 0)
121 includes.add("**");
122
123 if (useDefaultExcludes)
124 addCommaSeparatedToList(DEFAULT_EXCLUDES, excludes);
125 }
126
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))
132 lst.add(token);
133 }
134 }
135
136 public void setResourceLoader(ResourceLoader resourceLoader) {
137 this.resourceLoader = resourceLoader;
138 }
139
140 /**
141 * Can be overridden in order to provide the proper resource loader used to
142 * resolve resources.
143 */
144 public ResourceLoader getResourceLoaderToUse() {
145 return resourceLoader;
146 }
147
148 public void setBase(String base) {
149 this.base = base;
150 }
151
152 public void setInclude(String include) {
153 this.include = include;
154 }
155
156 public void setIncludes(List<String> includes) {
157 this.includes = includes;
158 }
159
160 public void setExclude(String exclude) {
161 this.exclude = exclude;
162 }
163
164 public void setExcludes(List<String> excludes) {
165 this.excludes = excludes;
166 }
167
168 public void setUseDefaultExcludes(Boolean useDefaultExcludes) {
169 this.useDefaultExcludes = useDefaultExcludes;
170 }
171
172 public void setExcludePathMatcher(PathMatcher excludePathMatcher) {
173 this.excludePathMatcher = excludePathMatcher;
174 }
175
176 public void setResourcePatternResolver(
177 ResourcePatternResolver resourcePatternResolver) {
178 this.resourcePatternResolver = resourcePatternResolver;
179 }
180
181 public ResourcePatternResolver getResourcePatternResolver() {
182 return resourcePatternResolver;
183 }
184
185 }