001: /**
002: *
003: */package com.bostechcorp.cbesb.common.util.generators;
004:
005: import java.util.Enumeration;
006: import java.util.HashMap;
007: import java.util.Properties;
008:
009: import org.jdom.Element;
010: import org.jdom.Namespace;
011:
012: import com.bostechcorp.cbesb.common.sa.service.EndPoint;
013: import com.bostechcorp.cbesb.common.sa.service.PropertiesKey;
014:
015: /**
016: * @author LPS
017: *
018: */
019: public final class CbrRoutesXMLGenerator extends AbstractDOMGenerator {
020:
021: private static final String ROUTING_RULES_T = "routingRules";
022: private static final String ROUTING_RULE_T = "routingRule";
023: private static final String EXPRESSION_T = "expression";
024: private static final String EXPRESSION_TYPE_A = "type";
025: private static final String TARGET_T = "target";
026: private static final String SERVICE_T = "service";
027:
028: private static final String ROUTING_RULES_NS = "http://cbesb.bostechcorp.com/cbr/routingrules/1.0";
029:
030: private String saName = "";
031: private String endpointName;
032:
033: public CbrRoutesXMLGenerator(String path, String saName) {
034: super (path);
035: this .saName = saName;
036: }
037:
038: /* (non-Javadoc)
039: * @see com.bostechcorp.cbesb.common.util.AbstractDOMGenerator#buildDocumentStructure()
040: */
041: @Override
042: protected void buildDocumentStructure() {
043: rootElement = jdomFactory.element(ROUTING_RULES_T);
044: rootElement.setNamespace(Namespace
045: .getNamespace(ROUTING_RULES_NS));
046: }
047:
048: public void setRoutingRules(EndPoint endPoint) {
049: endpointName = endPoint.getName();
050: HashMap<String, Properties> propertiesHm = new HashMap<String, Properties>();
051: if (endPoint.getSettings().getProperty(
052: PropertiesKey.CBR_WSDL_ROUTNUMBER) != null)
053: for (int i = 1; i <= Integer.parseInt(endPoint
054: .getSettings().getProperty(
055: PropertiesKey.CBR_WSDL_ROUTNUMBER)); i++) {
056: String root = "cbr.wsdl.routingRule";
057: String rootMatch = root + (i - 1) + ".";
058: Enumeration e = endPoint.getSettings().propertyNames();
059: Properties subProperties = new Properties();
060: while (e.hasMoreElements()) {
061: String key = (String) e.nextElement();
062: if (key.indexOf(rootMatch) == 0) {
063: String subKey = key.substring(rootMatch
064: .length());
065: String value = endPoint.getSettings()
066: .getProperty(key);
067: subProperties.setProperty(subKey, value);
068: }
069: }
070: propertiesHm.put("unit" + String.valueOf(i),
071: subProperties);
072: }
073: //setting the namespace
074: for (int k = 1; k <= propertiesHm.size(); k++) {
075: rootElement.addNamespaceDeclaration(Namespace.getNamespace(
076: "unit" + k, "http://bostechcorp.com/SU/"
077: + saName
078: + "_"
079: + ((Properties) propertiesHm
080: .get("unit" + k))
081: .getProperty("name")));
082: }
083: //setting rules
084:
085: for (int j = 1; j <= propertiesHm.size(); j++) {
086: Properties suProperties = (Properties) propertiesHm
087: .get("unit" + String.valueOf(j));
088: String expression = suProperties.getProperty("expression");
089: String type = suProperties.getProperty("type");
090: Element routingRule = jdomFactory.element(ROUTING_RULE_T);
091: rootElement.addContent(routingRule);
092:
093: //adding expresion
094: if (expression != null && !"".equals(expression)) {
095: Element expressionElement = jdomFactory
096: .element(EXPRESSION_T);
097: expressionElement.setAttribute(EXPRESSION_TYPE_A, type);
098: expressionElement.addContent(expression);
099: routingRule.addContent(expressionElement);
100: }
101: suProperties.remove("expression");
102: suProperties.remove("type");
103: //adding target
104: Element targetElement = jdomFactory.element(TARGET_T);
105: routingRule.addContent(targetElement);
106: Enumeration sE = suProperties.propertyNames();
107: while (sE.hasMoreElements()) {
108: String sKey = (String) sE.nextElement();
109: if (!"interfaceName".equals(sKey)
110: && !"name".equals(sKey)) {
111: if ("serviceName".equals(sKey)) {
112: Element service = jdomFactory
113: .element(SERVICE_T);
114: service.addContent("unit" + j + ":" + saName
115: + "_" + suProperties.getProperty(sKey));
116: targetElement.addContent(service);
117: } else if (suProperties.getProperty(sKey) != null) {
118: Element othetTagElement = jdomFactory
119: .element(sKey);
120: othetTagElement.addContent(suProperties
121: .getProperty(sKey));
122: targetElement.addContent(othetTagElement);
123: }
124: }
125: }
126: }
127: }
128:
129: /* (non-Javadoc)
130: * @see com.bostechcorp.cbesb.common.util.AbstractDOMGenerator#getFileOut()
131: */
132: @Override
133: protected String getFileOut() {
134: if (filePath.endsWith("/"))
135: return filePath + saName + "_" + endpointName + ".xml";
136: return filePath + "/" + saName + "_" + endpointName + ".xml";
137: }
138:
139: }
|