/contrib-stort/Asterisk/src/java/com/sun/jbi/sample/component/common/deployment/SUDescriptor.java

https://bitbucket.org/ssteinmetz/openesb-components · Java · 354 lines · 225 code · 40 blank · 89 comment · 36 complexity · 18fd785e36c09eb721eff47751ee62c7 MD5 · raw file

  1. /*
  2. * Asterisk JBI Binding Component.
  3. * Copyright (C) 2007 Stort Systems.
  4. * www.stortsystems.com
  5. *
  6. * This library is distributed under the CDDL license.
  7. *
  8. * $Id: SUDescriptor.java,v 1.2 2008/01/27 20:59:41 tiago_cury Exp $
  9. */
  10. package com.sun.jbi.sample.component.common.deployment;
  11. import com.sun.jbi.sample.component.common.RuntimeHelper;
  12. import java.io.FileReader;
  13. import java.io.IOException;
  14. import java.io.Reader;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import javax.jbi.management.DeploymentException;
  20. import javax.xml.namespace.QName;
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Element;
  23. import org.w3c.dom.NodeList;
  24. public interface SUDescriptor {
  25. Collection<Provides> getProvidedServices();
  26. Collection<Consumes> getConsumedServices();
  27. boolean isForBindingComponent();
  28. /**
  29. * base interface that models the service information described in the service unit descriptor for
  30. * consumed services and provided services.
  31. */
  32. public interface Service {
  33. /**
  34. * Getter for property interfaceQName.
  35. * @return Value of property interfaceQName.
  36. */
  37. QName getInterface();
  38. /**
  39. * Getter for property serviceName.
  40. * @return Value of property serviceName.
  41. */
  42. QName getServiceName();
  43. /**
  44. * Getter for property endpointName.
  45. * @return Value of property endpointName.
  46. */
  47. String getEndpointName();
  48. }
  49. /**
  50. * marker interface that represents the provided services in the service unit
  51. */
  52. public interface Provides extends Service {
  53. }
  54. /**
  55. * this interface represents the consumed service information in the su descriptor.
  56. */
  57. public interface Consumes extends Service {
  58. public final static String STANDARD_LINK = "standard";
  59. public final static String SOFT_LINK = "soft";
  60. public final static String HARD_LINK = "hard";
  61. /**
  62. * Getter for property linkType.
  63. * @return Value of property linkType.
  64. */
  65. String getLinkType();
  66. }
  67. /**
  68. * This is a factory class that can build the Service Unit Descriptor model from the jbi.xml
  69. */
  70. public static class SUDescriptorFactory {
  71. protected final static String JBI_TAG_NAME = "services";
  72. protected final static String SERVICES_TAG_NAME = "services";
  73. protected final static String BC_TAG_NAME = "binding-component";
  74. protected final static String PROVIDES_TAG_NAME = "provides";
  75. protected final static String CONSUMES_TAG_NAME = "consumes";
  76. protected final static String INTERFACE_TAG_NAME = "interface-name";
  77. protected final static String SERVICE_TAG_NAME = "service-name";
  78. protected final static String ENDPOINT_TAG_NAME = "endpoint-name";
  79. protected final static String LINK_TYPE_TAG_NAME = "link-type";
  80. /**
  81. * method that builds the Service unit descriptor model from the jbi.xml
  82. */
  83. public static SUDescriptor getSUDescriptor(String jbiXmlPath) throws Exception {
  84. FileReader reader = null;
  85. try {
  86. reader = new FileReader(jbiXmlPath);
  87. SUDescriptor suDesc = getSUDescriptor(reader);
  88. return suDesc;
  89. } finally {
  90. if ( reader != null ) {
  91. try {
  92. reader.close();
  93. } catch (IOException ex) {
  94. // ignore
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * method that builds the Service unit descriptor model from the jbi.xml
  101. */
  102. public static SUDescriptor getSUDescriptor(Reader reader) throws Exception {
  103. SUDescriptor suDescriptor = null;
  104. Document suDescDoc = RuntimeHelper.buildDOMDocument(reader);
  105. Element jbiEl = suDescDoc.getDocumentElement();
  106. if (JBI_TAG_NAME.equals(jbiEl.getTagName())) {
  107. throw new DeploymentException("Invalid service unit descriptor : no jbi root element");
  108. }
  109. NodeList servicesNL = jbiEl.getElementsByTagName(SERVICES_TAG_NAME);
  110. if (servicesNL != null && servicesNL.getLength() == 1) {
  111. Element servicesEl = (Element) servicesNL.item(0);
  112. suDescriptor = SUDescriptorImpl.createSUDescriptor(servicesEl);
  113. } else {
  114. throw new DeploymentException("Invalid service unit descriptor : invalid services element");
  115. }
  116. return suDescriptor;
  117. }
  118. }
  119. /**
  120. * This class implements SUDescriptor
  121. */
  122. public static class SUDescriptorImpl implements SUDescriptor {
  123. private List<Consumes> mConsumedList;
  124. private List<Provides> mProvidedList;
  125. private boolean mIsForBindingComponent;
  126. protected SUDescriptorImpl(boolean isForBindingComponent) {
  127. this.mIsForBindingComponent = isForBindingComponent;
  128. this.mConsumedList = new ArrayList<Consumes>();
  129. this.mProvidedList = new ArrayList<Provides>();
  130. }
  131. protected void addProvidedService(Provides provides) {
  132. this.mProvidedList.add(provides);
  133. }
  134. protected void addConsumedService(Consumes consumes) {
  135. this.mConsumedList.add(consumes);
  136. }
  137. public Collection<Provides> getProvidedServices() {
  138. // return unmodifiable collection
  139. return Collections.unmodifiableCollection(this.mProvidedList);
  140. }
  141. public Collection<Consumes> getConsumedServices() {
  142. // return unmodifiable collection
  143. return Collections.unmodifiableCollection(this.mConsumedList);
  144. }
  145. public boolean isForBindingComponent() {
  146. return this.mIsForBindingComponent;
  147. }
  148. protected static SUDescriptor createSUDescriptor(Element servicesEl) throws Exception {
  149. boolean isForBC = false;
  150. String bcTagString = servicesEl.getAttribute(SUDescriptorFactory.BC_TAG_NAME);
  151. isForBC = Boolean.valueOf(bcTagString).booleanValue();
  152. SUDescriptorImpl suDesc = new SUDescriptorImpl(isForBC);
  153. // add consumes
  154. NodeList consumesNL = servicesEl.getElementsByTagName(SUDescriptorFactory.CONSUMES_TAG_NAME);
  155. for ( int i=0; i < consumesNL.getLength(); ++i) {
  156. Element consumesEl = (Element) consumesNL.item(i);
  157. Consumes consumes = ConsumedService.createConsumedService(consumesEl);
  158. suDesc.addConsumedService(consumes);
  159. }
  160. // add provides
  161. NodeList providesNL = servicesEl.getElementsByTagName(SUDescriptorFactory.PROVIDES_TAG_NAME);
  162. for ( int i=0; i < providesNL.getLength(); ++i) {
  163. Element providesEl = (Element) providesNL.item(i);
  164. Provides provides = ProvidedService.createProvidedService(providesEl);
  165. suDesc.addProvidedService(provides);
  166. }
  167. return suDesc;
  168. }
  169. }
  170. /**
  171. * Base class that implements the Service interface
  172. */
  173. public static abstract class AbstractService implements Service {
  174. private QName mInterface;
  175. private QName mServiceName;
  176. private String mEndpointName;
  177. private AbstractService() {
  178. }
  179. /**
  180. * Getter for property interfaceQName.
  181. * @return Value of property interfaceQName.
  182. */
  183. public QName getInterface() {
  184. return this.mInterface;
  185. }
  186. /**
  187. * Setter for property interfaceQName.
  188. * @param interfaceQName New value of property interfaceQName.
  189. */
  190. protected void setInterface(QName interfaceQName) {
  191. this.mInterface = interfaceQName;
  192. }
  193. /**
  194. * Getter for property serviceName.
  195. * @return Value of property serviceName.
  196. */
  197. public QName getServiceName() {
  198. return this.mServiceName;
  199. }
  200. /**
  201. * Setter for property serviceName.
  202. * @param serviceName New value of property serviceName.
  203. */
  204. protected void setServiceName(QName serviceName) {
  205. this.mServiceName = serviceName;
  206. }
  207. /**
  208. * Getter for property endpointName.
  209. * @return Value of property endpointName.
  210. */
  211. public String getEndpointName() {
  212. return this.mEndpointName;
  213. }
  214. /**
  215. * Setter for property endpointName.
  216. * @param endpointName New value of property endpointName.
  217. */
  218. protected void setEndpointName(String endpointName) {
  219. this.mEndpointName = endpointName;
  220. }
  221. }
  222. /**
  223. * This class implements the Provides interface
  224. */
  225. public static class ProvidedService
  226. extends AbstractService
  227. implements Provides {
  228. protected ProvidedService(QName interfaceQName, QName serviceName, String endpointName) {
  229. this.setInterface(interfaceQName);
  230. this.setServiceName(serviceName);
  231. this.setEndpointName(endpointName);
  232. }
  233. @Override
  234. public String toString() {
  235. return "Provides :" +
  236. "\n\t interface-name= " + getInterface() +
  237. "\n\t service-name= " + getServiceName() +
  238. "\n\t endpont-name= " + getEndpointName();
  239. }
  240. protected static Provides createProvidedService(Element providesEl) throws Exception {
  241. String ifName = providesEl.getAttribute(SUDescriptorFactory.INTERFACE_TAG_NAME);
  242. String serviceName = providesEl.getAttribute(SUDescriptorFactory.SERVICE_TAG_NAME);
  243. String endpointName = providesEl.getAttribute(SUDescriptorFactory.ENDPOINT_TAG_NAME);
  244. if ( ifName == null || serviceName == null || endpointName == null ) {
  245. throw new Exception("Invalid provides element: missing " + SUDescriptorFactory.INTERFACE_TAG_NAME +
  246. " or " + SUDescriptorFactory.SERVICE_TAG_NAME + " or " + SUDescriptorFactory.ENDPOINT_TAG_NAME );
  247. }
  248. QName ifQName = RuntimeHelper.resolveAttrQName(ifName, providesEl);
  249. QName serviceQName = RuntimeHelper.resolveAttrQName(serviceName, providesEl);
  250. return new ProvidedService(ifQName, serviceQName, endpointName);
  251. }
  252. }
  253. /**
  254. * This class implements the Consumes interface.
  255. */
  256. public static class ConsumedService
  257. extends AbstractService
  258. implements Consumes {
  259. private String mLinkType;
  260. protected ConsumedService(QName interfaceQName,
  261. QName serviceName, String endpointName, String linkType) {
  262. this.setInterface(interfaceQName);
  263. this.setServiceName(serviceName);
  264. this.setEndpointName(endpointName);
  265. this.mLinkType = linkType;
  266. }
  267. /**
  268. * Getter for property linkType.
  269. * @return Value of property linkType.
  270. */
  271. public String getLinkType() {
  272. return this.mLinkType;
  273. }
  274. @Override
  275. public String toString() {
  276. return "Cosumes :" +
  277. "\n\t interface-name= " + getInterface() +
  278. "\n\t service-name= " + getServiceName() +
  279. "\n\t endpont-name= " + getEndpointName() +
  280. "\n\t link-type= " + getLinkType();
  281. }
  282. protected static Consumes createConsumedService(Element consumesEl) throws Exception {
  283. String ifName = consumesEl.getAttribute(SUDescriptorFactory.INTERFACE_TAG_NAME);
  284. String serviceName = consumesEl.getAttribute(SUDescriptorFactory.SERVICE_TAG_NAME);
  285. String endpointName = consumesEl.getAttribute(SUDescriptorFactory.ENDPOINT_TAG_NAME);
  286. String linkType = consumesEl.getAttribute(SUDescriptorFactory.LINK_TYPE_TAG_NAME);
  287. if ( linkType == null || linkType.trim().length() == 0 ) {
  288. linkType = STANDARD_LINK;
  289. }
  290. if ( ifName == null ) {
  291. throw new Exception("Invalid consumes element: missing " +
  292. SUDescriptorFactory.INTERFACE_TAG_NAME );
  293. }
  294. if ( serviceName == null || endpointName == null ) {
  295. throw new Exception("Invalid consumes element: missing " +
  296. SUDescriptorFactory.SERVICE_TAG_NAME + " or "
  297. + SUDescriptorFactory.ENDPOINT_TAG_NAME );
  298. }
  299. QName ifQName = RuntimeHelper.resolveAttrQName(ifName, consumesEl);
  300. QName serviceQName = null;
  301. if ( serviceName != null ) {
  302. serviceQName = RuntimeHelper.resolveAttrQName(serviceName, consumesEl);
  303. }
  304. if ( serviceQName != null && endpointName != null && linkType != null ) {
  305. if (!(STANDARD_LINK.equals(linkType) ||
  306. SOFT_LINK.equals(linkType) || HARD_LINK.equals(linkType)) ) {
  307. throw new Exception("Invalid consumes attribute value" +
  308. SUDescriptorFactory.LINK_TYPE_TAG_NAME + "=" + linkType);
  309. }
  310. }
  311. return new ConsumedService(ifQName, serviceQName, endpointName, linkType);
  312. }
  313. }
  314. }