Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
34 changed files
with
1,558 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core-api/src/main/java/org/openhubframework/openhub/api/entity/AbstractNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.openhubframework.openhub.api.entity; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
||
/** | ||
* Abstract node with base methods for get {@link NodeState} information. | ||
* | ||
* @author Roman Havlicek | ||
* @see Node | ||
* @see MutableNode | ||
* @since 2.0 | ||
*/ | ||
public abstract class AbstractNode implements Node { | ||
|
||
@Override | ||
public boolean isStopped() { | ||
return getState().equals(NodeState.STOPPED); | ||
} | ||
|
||
@Override | ||
public boolean isAbleToHandleNewMessages() { | ||
return getState().isAbleToHandleNewMessages(); | ||
} | ||
|
||
@Override | ||
public boolean isAbleToHandleExistingMessages() { | ||
return getState().isAbleToHandleExistingMessages(); | ||
} | ||
|
||
//--------------------------------------------- TOSTRING / HASH / EQUALS ------------------------------------------- | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
|
||
if (!(o instanceof Node)) return false; | ||
|
||
Node node = (Node) o; | ||
|
||
return new EqualsBuilder() | ||
.append(getNodeId(), node.getNodeId()) | ||
.isEquals(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder(17, 37) | ||
.append(getNodeId()) | ||
.toHashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.append("nodeId", getNodeId()) | ||
.append("code", getCode()) | ||
.append("name", getName()) | ||
.append("description", StringUtils.substring(getDescription(), 100)) | ||
.append("state", getState()) | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public String toHumanString() { | ||
return "(code = " + getCode() + ")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
core-api/src/main/java/org/openhubframework/openhub/api/entity/MutableNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package org.openhubframework.openhub.api.entity; | ||
|
||
import javax.annotation.Nullable; | ||
import javax.persistence.*; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Contains information about one node in cluster. | ||
* Attributes in this entity can be changed. | ||
* | ||
* @author Roman Havlicek | ||
* @see Node | ||
* @since 2.0 | ||
*/ | ||
@Entity | ||
@Table(name = "node", | ||
uniqueConstraints = { | ||
@UniqueConstraint(name = "uq_node_code", columnNames = {"code"}), | ||
@UniqueConstraint(name = "uq_node_name", columnNames = {"name"})}) | ||
public class MutableNode extends AbstractNode { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(Node.class); | ||
|
||
/** | ||
* Identifier. | ||
*/ | ||
@Id | ||
@Column(name = "node_id") | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long nodeId; | ||
|
||
/** | ||
* Unique node code. | ||
*/ | ||
@Column(name = "code", length = 64, nullable = false, unique = true) | ||
private String code; | ||
|
||
/** | ||
* Unique node name. | ||
*/ | ||
@Column(name = "name", length = 256, nullable = false, unique = true) | ||
private String name; | ||
|
||
/** | ||
* Description. | ||
*/ | ||
@Column(name = "description", length = 2056, nullable = true) | ||
private String description; | ||
|
||
/** | ||
* Node state. | ||
*/ | ||
@Enumerated(EnumType.STRING) | ||
@Column(name = "state", length = 64, nullable = false) | ||
private NodeState state; | ||
|
||
/** | ||
* New instance only for JPA. | ||
*/ | ||
protected MutableNode() { | ||
} | ||
|
||
/** | ||
* New instance with {@link NodeState#RUN} state. | ||
* | ||
* @param code code | ||
* @param name name | ||
*/ | ||
public MutableNode(String code, String name) { | ||
this(code, name, NodeState.RUN); | ||
} | ||
|
||
/** | ||
* New instance. | ||
* | ||
* @param code code | ||
* @param name name | ||
* @param state state | ||
*/ | ||
public MutableNode(String code, String name, NodeState state) { | ||
Assert.hasText(code, "code must not be empty"); | ||
Assert.hasText(name, "name must not be empty"); | ||
Assert.notNull(state, "state must not be null"); | ||
|
||
this.code = code; | ||
this.name = name; | ||
this.state = state; | ||
} | ||
|
||
//-------------------------------------------------- STATE --------------------------------------------------------- | ||
|
||
/** | ||
* Node handles all messages (existing and new) {@link NodeState#RUN}. | ||
*/ | ||
public void setRunState() { | ||
setState(NodeState.RUN); | ||
} | ||
|
||
/** | ||
* Node handles only existing messages (new message will be rejected) | ||
* {@link NodeState#HANDLES_EXISTING_MESSAGES}. | ||
*/ | ||
public void setHandleOnlyExistingMessageState() { | ||
setState(NodeState.HANDLES_EXISTING_MESSAGES); | ||
} | ||
|
||
/** | ||
* Node is stopped (new message will be rejected and existing is not processing) | ||
* {@link NodeState#STOPPED}. | ||
*/ | ||
public void setStoppedState() { | ||
setState(NodeState.STOPPED); | ||
} | ||
|
||
//--------------------------------------------------- SET / GET ---------------------------------------------------- | ||
|
||
@Override | ||
@Nullable | ||
public Long getNodeId() { | ||
return nodeId; | ||
} | ||
|
||
@Override | ||
public String getCode() { | ||
return code; | ||
} | ||
|
||
/** | ||
* Sets code of this node. | ||
* | ||
* @param code code | ||
*/ | ||
public void setCode(String code) { | ||
Assert.hasText(code, "code must not be empty"); | ||
|
||
this.code = code; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Sets name ot this node. | ||
* | ||
* @param name name | ||
*/ | ||
public void setName(String name) { | ||
Assert.hasText(name, "name must not be empty"); | ||
|
||
this.name = name; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
/** | ||
* Sets description | ||
* | ||
* @param description description, {@code NULL} - node has no description | ||
*/ | ||
public void setDescription(@Nullable String description) { | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public NodeState getState() { | ||
return state; | ||
} | ||
|
||
/** | ||
* Sets state of this node. | ||
* | ||
* @param state state | ||
*/ | ||
private void setState(NodeState state) { | ||
Assert.notNull(state, "state must not be null"); | ||
|
||
this.state = state; | ||
|
||
LOG.info("State of node {} was changed to {}", toHumanString(), getState()); | ||
} | ||
} |
Oops, something went wrong.