Pages

Wednesday, June 21, 2017

Detecting Unsorted Spring Beans

Detecting Unsorted Spring Beans


I like my Spring Beans definition files to be sorted. So I wrote the following code. Eventually I might turn it into a JUnit test for my continuous integration server.
 package play; import java.io.IOException; import java.util.ListIterator; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class UnsortedSpringBeans { public static void checkSortOrder(final String filename) throws JDOMException, IOException { System.out.println(filename); System.out.println("-------------------------------------------------"); String lastBean = null; SAXBuilder parser = new SAXBuilder(); Document doc = parser.build(filename); Element root = doc.getRootElement(); @SuppressWarnings("unchecked") ListIterator children = root.getChildren().listIterator(); while (children.hasNext()) { Element child = children.next(); Attribute id = child.getAttribute("id"); if (id == null) { System.out.println("Skipping : " + child.getName()); continue; } if (lastBean != null) { int cmpValue = id.getValue().compareTo(lastBean); if (cmpValue < 0) { System.out.println("Out Of Order: " + id.getValue()); } else { System.out.println(" " + id.getValue()); } } else { System.out.println(" " + id.getValue()); } lastBean = id.getValue(); } System.out.println(""); } /** * @param args * @throws IOException * @throws JDOMException */ public static void main(String[] args) throws JDOMException, IOException { checkSortOrder("config/bean-definition.xml"); checkSortOrder("config/database-beans.xml"); checkSortOrder("config/hypersonicContext.xml"); checkSortOrder("config/url-processor-beans.xml"); } } 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.