Created
June 21, 2012 07:44
-
-
Save flopezluis/2964429 to your computer and use it in GitHub Desktop.
Check whether you have repeated channels in the spring integration context.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.xml.sax.SAXException; | |
@Component("duplicatedChannels") | |
public class DuplicatedChannels { | |
private final Log logger = LogFactory.getLog(this.getClass()); | |
@Autowired | |
private ApplicationContext appContext; | |
private final String channelMsgTemplate = "The channel %s is repeated in the next files:%s"; | |
private final Pattern pattern = Pattern.compile("<int:channel.*id=\"(.*)\""); | |
@PostConstruct | |
public void check() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { | |
Resource[] res = appContext.getResources("classpath:META-INF/**/*.xml"); | |
HashMap<String, List<String>> channels = new HashMap<String, List<String>>(); | |
int len = res.length; | |
for (int i = 0; i < len; i++) { | |
InputStream inputStream = res[i].getInputStream(); | |
String xml = IOUtils.toString(inputStream, "UTF-8"); | |
Matcher m = pattern.matcher(xml); | |
boolean result = m.find(); | |
while(result) { | |
String name = m.group(1); | |
List<String> files = channels.get(name); | |
if (files == null) { | |
files = new ArrayList<String>(); | |
} | |
files.add(res[i].getFilename()); | |
channels.put(name, files); | |
result = m.find(); | |
} | |
} | |
Iterator<Entry<String, List<String>>> it = channels.entrySet().iterator(); | |
StringBuffer msgFiles = new StringBuffer(); | |
while (it.hasNext()) { | |
Entry<String, List<String>> channel = it.next(); | |
List<String> files = channel.getValue(); | |
if (files.size() > 1) { | |
for (String file: files) { | |
msgFiles.append("\n" + file); | |
} | |
logger.warn(String.format(channelMsgTemplate, channel.getKey(), msgFiles)); | |
msgFiles.delete(0, msgFiles.length()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment