/core/src/main/java/org/mule/config/dsl/Mule.java

https://github.com/porcelli/mule-dsl · Java · 172 lines · 96 code · 18 blank · 58 comment · 17 complexity · 3dd2438b4ce73fc3d33ef459b78979b0 MD5 · raw file

  1. /*
  2. * ---------------------------------------------------------------------------
  3. * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
  4. *
  5. * The software in this package is published under the terms of the CPAL v1.0
  6. * license, a copy of which has been included with this distribution in the
  7. * LICENSE.txt file.
  8. */
  9. package org.mule.config.dsl;
  10. import com.google.inject.Guice;
  11. import com.google.inject.Injector;
  12. import org.mule.api.MuleContext;
  13. import org.mule.api.MuleException;
  14. import org.mule.api.construct.FlowConstruct;
  15. import org.mule.config.dsl.internal.DefaultCatalogImpl;
  16. import org.mule.config.dsl.internal.FlowInterfaceHandler;
  17. import org.mule.config.dsl.internal.MuleAdvancedConfig;
  18. import org.mule.config.dsl.internal.MuleContextBuilder;
  19. import org.mule.construct.Flow;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import static org.mule.config.dsl.util.Preconditions.checkContentsNotNull;
  25. import static org.mule.config.dsl.util.Preconditions.checkNotEmpty;
  26. import static org.mule.config.dsl.util.Preconditions.checkNotNull;
  27. /**
  28. * {@code Mule} is the entry point to Mule DSL. Creates {@link MuleContext} from
  29. * {@link Module}s and also allows user's start or stop a global defined context.
  30. *
  31. * @author porcelli
  32. */
  33. public final class Mule {
  34. private final MuleContext muleContext;
  35. private final MuleAdvancedConfig advancedConfig;
  36. private final Map<String, FlowInterfaceHandler> flowCache;
  37. /**
  38. * Creates a new {@link Mule} for the given set of modules.
  39. *
  40. * @param modules array of non-null {@link Module}s
  41. * @return a new instance properly configured, but not started
  42. * @throws NullPointerException if any of given {@code modules} is null
  43. * @throws IllegalArgumentException if {@code modules} is empty
  44. * @throws ConfigurationException if something unexpected happens during configuration
  45. */
  46. public static Mule newInstance(final Module... modules) throws NullPointerException, IllegalArgumentException, ConfigurationException {
  47. checkContentsNotNull(modules, "modules");
  48. if (modules.length < 1) {
  49. throw new IllegalArgumentException("At least one module should be provided.");
  50. }
  51. final DefaultCatalogImpl myCatalog = new DefaultCatalogImpl();
  52. final List<com.google.inject.Module> guiceModules = new ArrayList<com.google.inject.Module>();
  53. for (final Module module : modules) {
  54. module.configure(myCatalog);
  55. if (module instanceof com.google.inject.Module) {
  56. guiceModules.add((com.google.inject.Module) module);
  57. }
  58. }
  59. final MuleContextBuilder muleContextBuilder;
  60. if (guiceModules.size() > 0) {
  61. final Injector injector = Guice.createInjector(guiceModules);
  62. muleContextBuilder = new MuleContextBuilder(myCatalog, injector);
  63. } else {
  64. muleContextBuilder = new MuleContextBuilder(myCatalog, null);
  65. }
  66. return new Mule(muleContextBuilder.build());
  67. }
  68. /**
  69. * @param muleContext an already configured mule context
  70. * @throws NullPointerException if any of given {@code modules} is null
  71. * @throws IllegalArgumentException if {@code modules} is empty
  72. */
  73. private Mule(final MuleContext muleContext) {
  74. this.muleContext = checkNotNull(muleContext, "muleContext");
  75. this.advancedConfig = new MuleAdvancedConfig(this.muleContext);
  76. this.flowCache = new HashMap<String, FlowInterfaceHandler>();
  77. }
  78. /**
  79. * Starts mule.
  80. *
  81. * @throws NullPointerException if any of given {@code modules} is null
  82. * @throws IllegalArgumentException if {@code modules} is empty
  83. * @throws FailedToStartException if can't start mule context
  84. */
  85. public synchronized Mule start() throws FailedToStartException {
  86. if (muleContext != null && muleContext.isStarted()) {
  87. return this;
  88. }
  89. try {
  90. muleContext.start();
  91. } catch (final MuleException e) {
  92. throw new FailedToStartException("Can't start mule context.", e);
  93. }
  94. return this;
  95. }
  96. /**
  97. * Stops mule context.
  98. *
  99. * @throws FailedToStopException if can't stop mule context
  100. */
  101. public synchronized Mule stop() throws FailedToStopException {
  102. if (isStarted()) {
  103. try {
  104. muleContext.stop();
  105. } catch (final MuleException e) {
  106. throw new FailedToStopException("Can't stop mule context.", e);
  107. }
  108. }
  109. return this;
  110. }
  111. /**
  112. * Checks if mule is started.
  113. *
  114. * @return true if started, otherwise false
  115. */
  116. public synchronized boolean isStarted() {
  117. if (muleContext == null) {
  118. return false;
  119. }
  120. return muleContext.isStarted();
  121. }
  122. /**
  123. * Returns an interface that exposes some advanced data related to {@link Mule}
  124. *
  125. * @return interface that exposes some advanced data
  126. */
  127. public MuleAdvancedConfig advanced() {
  128. return advancedConfig;
  129. }
  130. /**
  131. * Returns an interface that exposes flow related functions related to given flow name.
  132. *
  133. * @param name the flow name
  134. * @return interface that exposes flow related functions related to given flow name
  135. * @throws IllegalArgumentException if {@code name} is null or empty
  136. * @throws FlowNotFoundException if flow not found
  137. */
  138. public FlowInterfaceHandler flow(String name) throws IllegalArgumentException, FlowNotFoundException {
  139. checkNotEmpty(name, "name");
  140. if (!flowCache.containsKey(name)) {
  141. final FlowConstruct flow = muleContext.getRegistry().lookupFlowConstruct(name);
  142. if (flow == null) {
  143. flowCache.put(name, null);
  144. } else {
  145. flowCache.put(name, new FlowInterfaceHandler((Flow) flow, muleContext));
  146. }
  147. }
  148. final FlowInterfaceHandler flowProcess = flowCache.get(name);
  149. if (flowProcess == null) {
  150. throw new FlowNotFoundException("Flow not found");
  151. }
  152. return flowProcess;
  153. }
  154. }