View Javadoc

1   /*
2    * $Id: CheckDatabaseOrUrl.java 22126 2011-06-06 11:12:05Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jdbc.config;
12  
13  import org.mule.config.spring.parsers.PreProcessor;
14  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
15  import org.mule.config.spring.util.SpringXMLUtils;
16  import org.mule.util.StringUtils;
17  
18  import org.w3c.dom.Attr;
19  import org.w3c.dom.Element;
20  import org.w3c.dom.NamedNodeMap;
21  
22  public class CheckDatabaseOrUrl implements PreProcessor
23  {
24      @Override
25      public void preProcess(PropertyConfiguration config, Element element)
26      {
27          boolean urlAttributePresent = false;
28          boolean databaseAttributePresent = false;
29  
30          NamedNodeMap attributes = element.getAttributes();
31          for (int i = 0; i < attributes.getLength(); i++)
32          {
33              String attributeName = SpringXMLUtils.attributeName((Attr) attributes.item(i));
34              if (StringUtils.equalsIgnoreCase(attributeName, "url"))
35              {
36                  urlAttributePresent = true;
37              }
38              else if (StringUtils.equalsIgnoreCase(attributeName, "database"))
39              {
40                  databaseAttributePresent = true;
41              }
42          }
43  
44          if (urlAttributePresent && databaseAttributePresent)
45          {
46              throw new IllegalStateException("Either \"url\" or \"database\" can be configured, not both");
47          }
48      }
49  }