/contrib-other/camelse/camel-jbi-se/src/java/org/openesb/components/camelse/common/deployment/SUDescriptor.java

https://bitbucket.org/ssteinmetz/openesb-components · Java · 361 lines · 225 code · 40 blank · 96 comment · 36 complexity · f16ceb0a6f9350407219b5535cb27588 MD5 · raw file

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