Skip to content

Commit

Permalink
🔧 chore: update codebase #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Aug 17, 2024
1 parent 3eec803 commit a8fa80f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 56 deletions.
4 changes: 2 additions & 2 deletions TOOLS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
You can use the tree command-line utility in Node.js to generate a tree-like structure of your project directory. Here's
how you can use it:

First, make sure you have the `tree-node-cli` package installed globally:
First, make sure you have the `tree-connection-cli` package installed globally:

```bash
npm install -g tree-node-cli
npm install -g tree-connection-cli
```

Then, navigate to your project directory and run the following command:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Rmq4jProperties() {
private boolean enabled = false;
private boolean debugging = false;
private String defaultCluster; // default_cluster
private Map<String, Node> clusters;
private Map<String, Connection> clusters; // clusters

public boolean isEnabled() {
return enabled;
Expand All @@ -42,16 +42,16 @@ public void setDefaultCluster(String defaultCluster) {
this.defaultCluster = defaultCluster;
}

public Map<String, Node> getClusters() {
public Map<String, Connection> getClusters() {
return clusters;
}

public void setClusters(Map<String, Node> clusters) {
public void setClusters(Map<String, Connection> clusters) {
this.clusters = clusters;
}

public static class Node {
public Node() {
public static class Connection {
public Connection() {
super();
}

Expand Down
16 changes: 8 additions & 8 deletions plugin/src/main/groovy/org/rmq4j/service/Rmq4jService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ public interface Rmq4jService {

boolean isDebugging();

Map<String, Rmq4jProperties.Node> getClusters();
Map<String, Rmq4jProperties.Connection> getConnections();

Optional<Rmq4jProperties.Node> getNode(String key);
Optional<Rmq4jProperties.Connection> getConn(String key);

Optional<Rmq4jProperties.Node> getNodeActivated(String key);
Optional<Rmq4jProperties.Connection> getConnActivated(String key);

Optional<ConnectionFactory> createFactory(Rmq4jProperties.Node node);
Optional<ConnectionFactory> createFactory(Rmq4jProperties.Connection connection);

Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties.Node node);
Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties.Connection connection);

Optional<RabbitTemplate> dispatch(Rmq4jProperties.Node node);
Optional<RabbitTemplate> dispatch(Rmq4jProperties.Connection connection);

Optional<RabbitTemplate> dispatch(CachingConnectionFactory factory);

Optional<RabbitAdmin> createAdm(Rmq4jProperties.Node node);
Optional<RabbitAdmin> createAdm(Rmq4jProperties.Connection connection);

String getURLConnSchema(Rmq4jProperties.Node node);
String getURLConnSchema(Rmq4jProperties.Connection connection);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void snapIns() {
if (this.exists()) {
return;
}
for (Map.Entry<String, Rmq4jProperties.Node> entry : rmq4jService.getClusters().entrySet()) {
for (Map.Entry<String, Rmq4jProperties.Connection> entry : rmq4jService.getConnections().entrySet()) {
if (!entry.getValue().isEnabled()) {
continue;
}
Expand Down
80 changes: 40 additions & 40 deletions plugin/src/main/groovy/org/rmq4j/service/impl/Rmq4jServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public boolean isDebugging() {
* @return map of cluster configuration, class {@link Map}
*/
@Override
public Map<String, Rmq4jProperties.Node> getClusters() {
public Map<String, Rmq4jProperties.Connection> getConnections() {
return properties.getClusters();
}

Expand All @@ -67,32 +67,32 @@ public Map<String, Rmq4jProperties.Node> getClusters() {
* If the module is not enabled or the key is empty, it returns an empty `Optional`.
*
* @param key The key (cluster name) used to look up the cluster configuration in the clusters map.
* @return An {@link Optional} containing the {@link Rmq4jProperties.Node} associated with the given key if it exists;
* @return An {@link Optional} containing the {@link Rmq4jProperties.Connection} associated with the given key if it exists;
* otherwise, an empty {@link Optional}.
*/
@Override
public Optional<Rmq4jProperties.Node> getNode(String key) {
public Optional<Rmq4jProperties.Connection> getConn(String key) {
if (!this.isEnabled() || String4j.isEmpty(key)) {
return Optional.empty();
}
return this.getClusters().entrySet().stream().filter(e -> e.getKey().equals(key)).map(Map.Entry::getValue).findFirst();
return this.getConnections().entrySet().stream().filter(e -> e.getKey().equals(key)).map(Map.Entry::getValue).findFirst();
}

/**
* Retrieves a specific cluster configuration based on the provided key, but only if the cluster is enabled.
* <p>
* This method first calls {@link #getNode(String)} to retrieve the cluster configuration for the given key.
* This method first calls {@link #getConn(String)} to retrieve the cluster configuration for the given key.
* If the cluster configuration is present, it further checks if the cluster is enabled.
* If the cluster is enabled, it returns the configuration wrapped in an {@link Optional}.
* If the configuration is not present or the cluster is not enabled, it returns an empty {@link Optional}.
*
* @param key The key (cluster name) used to look up the cluster configuration in the clusters map.
* @return An {@link Optional} containing the {@link Rmq4jProperties.Node} associated with the given key if it exists and is enabled;
* @return An {@link Optional} containing the {@link Rmq4jProperties.Connection} associated with the given key if it exists and is enabled;
* otherwise, an empty {@link Optional}.
*/
@Override
public Optional<Rmq4jProperties.Node> getNodeActivated(String key) {
Optional<Rmq4jProperties.Node> cluster = this.getNode(key);
public Optional<Rmq4jProperties.Connection> getConnActivated(String key) {
Optional<Rmq4jProperties.Connection> cluster = this.getConn(key);
if (!cluster.isPresent()) {
return cluster;
}
Expand All @@ -102,58 +102,58 @@ public Optional<Rmq4jProperties.Node> getNodeActivated(String key) {
/**
* Creates a {@link ConnectionFactory} for RabbitMQ based on the provided cluster configuration.
* <p>
* This method initializes a {@link ConnectionFactory} using the details from the given {@link Rmq4jProperties.Node}.
* This method initializes a {@link ConnectionFactory} using the details from the given {@link Rmq4jProperties.Connection}.
* It sets the host, port, virtual host, username, and password on the factory. If SSL is enabled in the configuration,
* it applies SSL settings to the factory.
* <p>
* If the configuration is null or the cluster is not enabled, it returns an empty {@link Optional}.
* If an exception occurs while applying SSL settings, it logs the error and throws a {@link RuntimeException}.
*
* @param node The cluster configuration used to create the {@link ConnectionFactory}.
* @param connection The cluster configuration used to create the {@link ConnectionFactory}.
* @return An {@link Optional} containing the configured {@link ConnectionFactory} if the configuration is valid and SSL settings are applied successfully;
* otherwise, an empty {@link Optional}.
*/
@Override
public Optional<ConnectionFactory> createFactory(Rmq4jProperties.Node node) {
if (node == null) {
public Optional<ConnectionFactory> createFactory(Rmq4jProperties.Connection connection) {
if (connection == null) {
return Optional.empty();
}
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(node.getHost());
factory.setPort(node.getPort());
factory.setVirtualHost(node.getVirtualHost());
factory.setUsername(node.getUsername());
factory.setPassword(node.getPassword());
factory.setHost(connection.getHost());
factory.setPort(connection.getPort());
factory.setVirtualHost(connection.getVirtualHost());
factory.setUsername(connection.getUsername());
factory.setPassword(connection.getPassword());
try {
if (node.isUseSSL()) {
if (connection.isUseSSL()) {
factory.useSslProtocol();
}
return Optional.of(factory);
} catch (Exception e) {
logger.error("{} Rmq4j, SSL configuration for RabbitMQ could not be applied cause got an exception: {} by URL schema: {}",
IconType.ERROR.getCode(), e.getMessage(), this.getURLConnSchema(node), e);
throw new RuntimeException(String.format("SSL configuration for RabbitMQ could not be applied successfully (%s)", this.getURLConnSchema(node)), e);
IconType.ERROR.getCode(), e.getMessage(), this.getURLConnSchema(connection), e);
throw new RuntimeException(String.format("SSL configuration for RabbitMQ could not be applied successfully (%s)", this.getURLConnSchema(connection)), e);
}
}

/**
* Creates a {@link CachingConnectionFactory} for RabbitMQ based on the provided cluster configuration.
* <p>
* This method first uses {@link #createFactory(Rmq4jProperties.Node)} to create a {@link ConnectionFactory}
* This method first uses {@link #createFactory(Rmq4jProperties.Connection)} to create a {@link ConnectionFactory}
* from the given cluster configuration. If the factory creation is successful, it wraps the factory in a
* {@link CachingConnectionFactory} and returns it as an {@link Optional}.
* <p>
* If the {@link ConnectionFactory} could not be created (e.g., due to invalid configuration), it returns
* an empty {@link Optional}.
*
* @param node The cluster configuration used to create the {@link ConnectionFactory}.
* @param connection The cluster configuration used to create the {@link ConnectionFactory}.
* @return An {@link Optional} containing the {@link CachingConnectionFactory} if the {@link ConnectionFactory}
* was created successfully; otherwise, an empty {@link Optional}.
*/
@SuppressWarnings({"OptionalIsPresent"})
@Override
public Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties.Node node) {
Optional<ConnectionFactory> factory = this.createFactory(node);
public Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties.Connection connection) {
Optional<ConnectionFactory> factory = this.createFactory(connection);
if (!factory.isPresent()) {
return Optional.empty();
}
Expand All @@ -163,22 +163,22 @@ public Optional<CachingConnectionFactory> createCacheConnFactory(Rmq4jProperties
/**
* Creates a {@link RabbitTemplate} for RabbitMQ based on the provided cluster configuration.
* <p>
* This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Node)} to create a
* This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Connection)} to create a
* {@link CachingConnectionFactory} from the given cluster configuration. If the creation of the
* {@link CachingConnectionFactory} is successful, it wraps the factory in a {@link RabbitTemplate}
* and returns it as an {@link Optional}.
* <p>
* If the {@link CachingConnectionFactory} could not be created (e.g., due to invalid configuration),
* it returns an empty {@link Optional}.
*
* @param node The cluster configuration used to create the {@link CachingConnectionFactory}.
* @param connection The cluster configuration used to create the {@link CachingConnectionFactory}.
* @return An {@link Optional} containing the {@link RabbitTemplate} if the {@link CachingConnectionFactory}
* was created successfully; otherwise, an empty {@link Optional}.
*/
@SuppressWarnings({"OptionalIsPresent"})
@Override
public Optional<RabbitTemplate> dispatch(Rmq4jProperties.Node node) {
Optional<CachingConnectionFactory> factory = this.createCacheConnFactory(node);
public Optional<RabbitTemplate> dispatch(Rmq4jProperties.Connection connection) {
Optional<CachingConnectionFactory> factory = this.createCacheConnFactory(connection);
if (!factory.isPresent()) {
return Optional.empty();
}
Expand All @@ -188,7 +188,7 @@ public Optional<RabbitTemplate> dispatch(Rmq4jProperties.Node node) {
/**
* Creates a {@link RabbitTemplate} for RabbitMQ based on the provided cluster configuration.
* <p>
* This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Node)} to create a
* This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Connection)} to create a
* {@link CachingConnectionFactory} from the given cluster configuration. If the creation of the
* {@link CachingConnectionFactory} is successful, it wraps the factory in a {@link RabbitTemplate}
* and returns it as an {@link Optional}.
Expand All @@ -213,22 +213,22 @@ public Optional<RabbitTemplate> dispatch(CachingConnectionFactory factory) {
/**
* Creates a {@link RabbitAdmin} for RabbitMQ based on the provided cluster configuration.
* <p>
* This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Node)} to create a
* This method first uses {@link #createCacheConnFactory(Rmq4jProperties.Connection)} to create a
* {@link CachingConnectionFactory} from the given cluster configuration. If the creation of the
* {@link CachingConnectionFactory} is successful, it wraps the factory in a {@link RabbitAdmin}
* and returns it as an {@link Optional}.
* <p>
* If the {@link CachingConnectionFactory} could not be created (e.g., due to invalid configuration),
* it returns an empty {@link Optional}.
*
* @param node The cluster configuration used to create the {@link CachingConnectionFactory}.
* @param connection The cluster configuration used to create the {@link CachingConnectionFactory}.
* @return An {@link Optional} containing the {@link RabbitAdmin} if the {@link CachingConnectionFactory}
* was created successfully; otherwise, an empty {@link Optional}.
*/
@SuppressWarnings({"OptionalIsPresent"})
@Override
public Optional<RabbitAdmin> createAdm(Rmq4jProperties.Node node) {
Optional<CachingConnectionFactory> factory = this.createCacheConnFactory(node);
public Optional<RabbitAdmin> createAdm(Rmq4jProperties.Connection connection) {
Optional<CachingConnectionFactory> factory = this.createCacheConnFactory(connection);
if (!factory.isPresent()) {
return Optional.empty();
}
Expand All @@ -239,24 +239,24 @@ public Optional<RabbitAdmin> createAdm(Rmq4jProperties.Node node) {
* Generates the connection URL schema for RabbitMQ based on the provided cluster configuration.
* <p>
* This method constructs the connection URL schema using the username, password, host, port, and virtual host from
* the provided {@link Rmq4jProperties.Node}. The URL is formatted as "amqp://username:password@host:port".
* the provided {@link Rmq4jProperties.Connection}. The URL is formatted as "amqp://username:password@host:port".
* If a virtual host is specified, it is appended to the URL.
* <p>
* If the provided configuration is null, it returns an empty string.
*
* @param node The cluster configuration used to build the connection URL.
* @param connection The cluster configuration used to build the connection URL.
* @return A string representing the RabbitMQ connection URL schema based on the provided configuration.
*/
@Override
public String getURLConnSchema(Rmq4jProperties.Node node) {
if (node == null) {
public String getURLConnSchema(Rmq4jProperties.Connection connection) {
if (connection == null) {
return "";
}
String form = String.format("amqp://%s:%s@%s:%d",
node.getUsername(), node.getPassword(), node.getHost(), node.getPort());
if (String4j.isEmpty(node.getVirtualHost())) {
connection.getUsername(), connection.getPassword(), connection.getHost(), connection.getPort());
if (String4j.isEmpty(connection.getVirtualHost())) {
return form;
}
return String.format("%s%s", form, node.getVirtualHost());
return String.format("%s%s", form, connection.getVirtualHost());
}
}
2 changes: 2 additions & 0 deletions plugin/src/main/resources/application.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ spring:
virtual_host: "/vhost"
use_ssl: false
desc: "The default cluster02"
configs:
- key: "guest"

# ################################
# Additional Notes:
Expand Down

0 comments on commit a8fa80f

Please sign in to comment.