I am writing my first Mulesoft Connector. I am trying to implement @ConnectionManagementStrategy. I have referenced:
https://developer.mulesoft.com/docs/display/current/Connection+Management
I am trying to implement a connection to a custom web service. For my client, I need to specify username, password, and endpoint. I am able to get the connection to work if I hardcode the endpoint. However, we may be re-using this connector for other services and I want to be able to configure the endpoint. Per the documentation, the devkit calls/creates the class instance automatically. I want to to be able to pass a an argument to the constructor based on a configurable property in the @Connector or pass an additional argument to the @Connect method.
I am not finding in the documentation how to customize these methods.
My @Connector:
@Connector(name="testContactConnector", friendlyName = "Test Contact Connector")
public class Contact {
@Configurable
@Default("https://my.cool.service")
public String webServiceEndpoint;
@ConnectionStrategy
private Connection connection;
public void setConnection(Connection connection) {
this.connection = connection;
}
public Connection getConnection() {
return this.connection;
}
@Processor
public String getSessionID() throws Exception {
return this.connection.connectionID();
}
}
My @ConnectionManagementStrategy:
@ConnectionManagement(friendlyName = "Contact Service Connection")
public class Connection {
private String serviceEndpoint, username, password, sessionID;
private Service service;
public Connection() {
}
@Connect
@TestConnectivity
public void connect(@ConnectionKey String username, @Password String password)
throws ConnectionException {
this.username = username;
this.password = password;
try {
this.service = Service.Logon(this.username, this.password, this.serviceEndpoint);
this.sessionID = this.service.getSession();
} catch (Exception error) {
throw new ConnectionException(ConnectionExceptionCode.INCORRECT_CREDENTIALS, null, error.getMessage(), error);
}
}
@Disconnect
public void disconnect() {
if(this.service != null) {
try {
this.service.killSession();
} catch (Exception error) {
error.printStackTrace();
}
finally {
this.service = null;
}
}
}
@ValidateConnection
public boolean isConnected() {
try {
return (this.service != null && this.service.getSession() != null);
} catch (Exception error) {
error.printStackTrace();
return false;
}
}
@ConnectionIdentifier
public String connectionID() {
try {
return this.service.getSession();
} catch (Exception error) {
error.printStackTrace();
return null;
}
}
}
EDIT Fat fingered a character in the example code
THE SOLUTION I CAME UP WITH PER THE DOCUMENTATION PROVIDED IN THE ANSWER
In the @ConnectionManagement class I added the following property:
@Configurable
@Default("https://your.web.service")
public String webServiceEndpoint;
In the @Connect method I referenced that property:
@Connect
@TestConnectivity
public void connect(@ConnectionKey String username, @Password String password) throws ConnectionException {
this.username = username;
this.password = password;
try {
this.service = Service.Logon(this.username, this.password, this.webServiceEndpoint);
this.sessionID = this.service.getSessionID();
} catch (Exception error) {
throw new ConnectionException(ConnectionExceptionCode.INCORRECT_CREDENTIALS, null, error.getMessage(), error);
}
}
In my config file I have the following setting the values:
<myconnector:config name="config" webServiceEndpoint="http://my.custom.service" username="foo" password="foo"/>
Here you can find an easy example how to implement connection management. Furthermore this documentation provides an easy step by step guide on how to build a connector from start to finish. HTH.