Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

Commit

Permalink
refactoring on Mule class - better api now
Browse files Browse the repository at this point in the history
  • Loading branch information
porcelli committed Aug 8, 2011
1 parent 91aadab commit 5be7700
Show file tree
Hide file tree
Showing 57 changed files with 725 additions and 753 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/mule/config/dsl/AbstractModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public FilterBuilder filter(final String name) throws IllegalArgumentException {
* @see org.mule.api.construct.FlowConstruct
*/
public FlowBuilder flow() {
return flow(newName("Flow"));
return flow(newName("Flows"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.mule.config.i18n.Message;

/**
* Thrown from {@link Mule#startMuleContext(Module...)} when can't
* Thrown from {@link Mule#start(Module...)} when can't
* start the mule context.
*
* @author porcelli
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.mule.config.i18n.Message;

/**
* Thrown from {@link org.mule.config.dsl.Mule#stopMuleContext()} when can't
* Thrown from {@link org.mule.config.dsl.Mule#stop()} when can't
* stop the mule context.
*
* @author porcelli
Expand Down
243 changes: 39 additions & 204 deletions core/src/main/java/org/mule/config/dsl/Mule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@

import com.google.inject.Guice;
import com.google.inject.Injector;
import org.mule.DefaultMuleEvent;
import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.construct.FlowConstruct;
import org.mule.api.endpoint.InboundEndpoint;
import org.mule.api.lifecycle.CreateException;
import org.mule.api.transport.MuleMessageFactory;
import org.mule.api.transport.PropertyScope;
import org.mule.config.dsl.internal.DefaultCatalogImpl;
import org.mule.config.dsl.internal.FlowInterfaceHandler;
import org.mule.config.dsl.internal.MuleAdvancedConfig;
import org.mule.config.dsl.internal.MuleContextBuilder;
import org.mule.config.dsl.internal.MuleFlowProcessReturnImpl;
import org.mule.construct.AbstractFlowConstruct;
import org.mule.session.DefaultMuleSession;
import org.mule.transport.DefaultMuleMessageFactory;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -37,28 +29,22 @@

/**
* {@code Mule} is the entry point to Mule DSL. Creates {@link MuleContext} from
* {@link Module}s and also allows user's directly invoke defined flows.
* {@link Module}s and also allows user's start or stop a global defined context.
*
* @author porcelli
*/
public final class Mule {

private Mule() {
}

private static MuleContext globalMuleContext = null;
private static MuleFlowProcessReturnImpl nullFlowReturn = new MuleFlowProcessReturnImpl(null);
private final MuleContext muleContext;
private final MuleAdvancedConfig advancedConfig;
private final Map<String, FlowInterfaceHandler> flowCache;

/**
* Creates a {@link MuleContext} for the given set of modules.
*
* @param modules array of non-null {@link Module}s
* @return the mule context properly configured, but not started
* @throws NullPointerException if any of given {@code modules} is null
* @throws IllegalArgumentException if {@code modules} is empty
*/
public static synchronized MuleContext newMuleContext(final Module... modules) throws NullPointerException, IllegalArgumentException {

public Mule(final Module... modules) throws NullPointerException, IllegalArgumentException {
checkContentsNotNull(modules, "modules");

if (modules.length < 1) {
Expand All @@ -83,227 +69,76 @@ public static synchronized MuleContext newMuleContext(final Module... modules) t
muleContextBuilder = new MuleContextBuilder(myCatalog, null);
}

return muleContextBuilder.build();
this.muleContext = muleContextBuilder.build();
this.advancedConfig = new MuleAdvancedConfig(muleContext);
this.flowCache = new HashMap<String, FlowInterfaceHandler>();
}

/**
* Starts mule.
*
* Creates and starts {@link MuleContext} for the given set of modules.
* <p/>
* Using this method user's don't need interact directly with {@link MuleContext}, it's up to
* {@link Mule} class handle it.
*
* @param modules array of non-null {@link Module}s
* @throws NullPointerException if any of given {@code modules} is null
* @throws IllegalArgumentException if {@code modules} is empty
* @throws FailedToStartException if can't start mule context
*/
public static synchronized void startMuleContext(final Module... modules) throws FailedToStartException {
if (Mule.globalMuleContext != null && Mule.globalMuleContext.isStarted()) {
return;
public synchronized Mule start() throws FailedToStartException {
if (muleContext != null && muleContext.isStarted()) {
return this;
}

try {
Mule.globalMuleContext = newMuleContext(modules);
globalMuleContext.start();
muleContext.start();
} catch (final MuleException e) {
throw new FailedToStartException("Can't start mule context.", e);
}
return this;
}

/**
* Stops mule context.
*
* @throws FailedToStopException if can't stop mule context
*/
public static synchronized void stopMuleContext() throws FailedToStopException {
stopMuleContext(globalMuleContext);
}

/**
* Checks if mule is started.
*
* @return true if started, otherwise false
*/
public static synchronized boolean isStarted() {
if (globalMuleContext == null) {
return false;
}
return globalMuleContext.isStarted();
}


/**
* Stops the given mule context.
*
* @param muleContext the mule context
* @throws FailedToStopException if can't stop mule context
*/
public static synchronized void stopMuleContext(final MuleContext muleContext) throws FailedToStopException {
public synchronized Mule stop() throws FailedToStopException {
if (muleContext != null && muleContext.isStarted()) {
try {
muleContext.stop();
} catch (final MuleException e) {
throw new FailedToStopException("Can't stop mule context.", e);
}
}
return this;
}

/**
* Process a flow based on given parameters.
*
* @param flowName the name of the flow to be executed
* @return an instance of {@link org.mule.api.MuleEvent} wrapped by
* @throws IllegalArgumentException if {@code flowName} is empty or null
* @throws FlowNotFoundException if {@code flowName} is not registered or found on mule context
* @throws ConfigurationException if can't configure properly and cereate a {@link MuleMessage}
* @throws FlowProcessException if some problem occurs on flow execution
*/
public static synchronized MuleFlowProcessReturn process(final String flowName)
throws IllegalArgumentException, FlowNotFoundException, ConfigurationException, FlowProcessException {
return process(globalMuleContext, flowName);
}

/**
* Process a flow based on given parameters.
*
* @param flowName the name of the flow to be executed
* @param input input data to be used as payload
* @return an instance of {@link org.mule.api.MuleEvent} wrapped by
* @throws IllegalArgumentException if {@code flowName} is empty or null
* @throws FlowNotFoundException if {@code flowName} is not registered or found on mule context
* @throws ConfigurationException if can't configure properly and cereate a {@link MuleMessage}
* @throws FlowProcessException if some problem occurs on flow execution
*/
public static synchronized MuleFlowProcessReturn process(final String flowName, final Object input)
throws IllegalArgumentException, FlowNotFoundException, ConfigurationException, FlowProcessException {
return process(globalMuleContext, flowName, input);
}

/**
* Process a flow based on given parameters.
*
* @param flowName the name of the flow to be executed
* @param input input data to be used as payload
* @param properties properties to be uset on message payload {@link MuleFlowProcessReturn}
* @return an instance of {@link org.mule.api.MuleEvent} wrapped by
* @throws IllegalArgumentException if {@code flowName} is empty or null
* @throws FlowNotFoundException if {@code flowName} is not registered or found on mule context
* @throws ConfigurationException if can't configure properly and cereate a {@link MuleMessage}
* @throws FlowProcessException if some problem occurs on flow execution
*/
public static synchronized MuleFlowProcessReturn process(final String flowName, final Object input, Map<String, Object> properties)
throws IllegalArgumentException, FlowNotFoundException, ConfigurationException, FlowProcessException {
return process(globalMuleContext, flowName, input, properties);
}

/**
* Process a flow based on given parameters.
* Checks if mule is started.
*
* @param muleContext the mule context
* @param flowName the name of the flow to be executed
* @return an instance of {@link org.mule.api.MuleEvent} wrapped by
* @throws IllegalArgumentException if {@code flowName} is empty or null
* @throws FlowNotFoundException if {@code flowName} is not registered or found on mule context
* @throws ConfigurationException if can't configure properly and cereate a {@link MuleMessage}
* @throws FlowProcessException if some problem occurs on flow execution
* @return true if started, otherwise false
*/
public static synchronized MuleFlowProcessReturn process(final MuleContext muleContext, final String flowName)
throws IllegalArgumentException, FlowNotFoundException, ConfigurationException, FlowProcessException {
return process(muleContext, flowName, null, null);
public synchronized boolean isStarted() {
if (muleContext == null) {
return false;
}
return muleContext.isStarted();
}

/**
* Process a flow based on given parameters.
*
* @param muleContext the mule context
* @param flowName the name of the flow to be executed
* @param input input data to be used as payload
* @return an instance of {@link org.mule.api.MuleEvent} wrapped by
* @throws IllegalArgumentException if {@code flowName} is empty or null
* @throws FlowNotFoundException if {@code flowName} is not registered or found on mule context
* @throws ConfigurationException if can't configure properly and cereate a {@link MuleMessage}
* @throws FlowProcessException if some problem occurs on flow execution
*/
public static synchronized MuleFlowProcessReturn process(final MuleContext muleContext, final String flowName, final Object input)
throws IllegalArgumentException, FlowNotFoundException, ConfigurationException, FlowProcessException {
return process(muleContext, flowName, input, null);
public MuleAdvancedConfig advanced() {
return advancedConfig;
}

/**
* Process a flow based on given parameters.
*
* @param muleContext the mule context
* @param flowName the name of the flow to be executed
* @param input input data to be used as payload
* @param properties properties to be uset on message payload {@link MuleFlowProcessReturn}
* @return an instance of {@link org.mule.api.MuleEvent} wrapped by
* @throws IllegalArgumentException if {@code flowName} is empty or null
* @throws FlowNotFoundException if {@code flowName} is not registered or found on mule context
* @throws ConfigurationException if can't configure properly and cereate a {@link MuleMessage}
* @throws FlowProcessException if some problem occurs on flow execution
*/
public static synchronized MuleFlowProcessReturn process(final MuleContext muleContext, final String flowName, final Object input, Map<String, Object> properties)
throws IllegalArgumentException, FlowNotFoundException, ConfigurationException, FlowProcessException {
checkNotEmpty(flowName, "flowName");

if (muleContext != null && muleContext.isStarted()) {
final FlowConstruct flow = muleContext.getRegistry().lookupFlowConstruct(flowName);
public FlowInterfaceHandler flow(String name) {
checkNotEmpty(name, "name");
if (!flowCache.containsKey(name)) {
final FlowConstruct flow = muleContext.getRegistry().lookupFlowConstruct(name);
if (flow == null) {
throw new FlowNotFoundException("Flow not found");
}
if (flow.getMessageProcessorChain().getMessageProcessors().size() == 0) {
return nullFlowReturn;
}
InboundEndpoint source = null;
MuleMessageFactory messageFactory = null;
if (flow instanceof AbstractFlowConstruct && ((AbstractFlowConstruct) flow).getMessageSource() != null && ((AbstractFlowConstruct) flow).getMessageSource() instanceof InboundEndpoint) {
source = (InboundEndpoint) ((AbstractFlowConstruct) flow).getMessageSource();
try {
messageFactory = source.getConnector().createMuleMessageFactory();
} catch (CreateException e) {
}
flowCache.put(name, null);
} else {
flow.getStatistics().setEnabled(false);
}

MuleMessage message = null;
boolean isDefaultMessageFactory = false;

if (messageFactory == null) {
isDefaultMessageFactory = true;
messageFactory = new DefaultMuleMessageFactory(muleContext);
}

try {
message = messageFactory.create(input, null);
} catch (Exception e) {
if (!isDefaultMessageFactory) {
try {
message = new DefaultMuleMessageFactory(muleContext).create(input, null);
} catch (Exception e1) {
throw new ConfigurationException("Can't create message.", e);
}
}
if (message == null) {
throw new ConfigurationException("Can't create message.", e);
}
}

if (properties != null) {
message.addProperties(new HashMap<String, Object>(properties), PropertyScope.OUTBOUND);
}

try {
DefaultMuleEvent muleEvent = new DefaultMuleEvent(message, source, new DefaultMuleSession(flow, muleContext));
if (source == null) {
muleEvent.setTimeout(0);
}
return new MuleFlowProcessReturnImpl(flow.getMessageProcessorChain().process(muleEvent));
} catch (MuleException e) {
throw new FlowProcessException("Error during flow process.", e);
flowCache.put(name, new FlowInterfaceHandler(flow, muleContext));
}
}
return nullFlowReturn;
final FlowInterfaceHandler flowProcess = flowCache.get(name);
if (flowProcess == null) {
throw new FlowNotFoundException("Flow not found");
}
return flowProcess;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface MuleFlowProcessReturn {
* @return the payload typed
* @throws ClassCastException if can't cast payload to expected type
*/
<T> T getPayload(Class<T> type) throws ClassCastException;
<T> T getPayloadAs(Class<T> type) throws ClassCastException;

/**
* Returns the payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public MuleEvent process(MuleEvent event) throws MuleException, FlowConstructInv

FlowConstruct flow = event.getMuleContext().getRegistry().lookupFlowConstruct(flowName);
if (flow == null) {
throw new FlowConstructInvalidException(MessageFactory.createStaticMessage("Flow not found."));
throw new FlowConstructInvalidException(MessageFactory.createStaticMessage("Flows not found."));
}

return ((MessageProcessor) flow).process(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void newRegistry(String name, Object obj) throws IllegalArgumentException
public FlowBuilder newFlow(final String flowName) throws IllegalArgumentException {
checkNotEmpty(flowName, "flowName");
if (flows.containsKey(flowName)) {
throw new IllegalArgumentException("Flow name already registered.");
throw new IllegalArgumentException("Flows name already registered.");
}

final FlowBuilderImpl fb = new FlowBuilderImpl(flowName);
Expand Down
Loading

0 comments on commit 5be7700

Please sign in to comment.