Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands for resource adapter manipulation #146

Merged
merged 1 commit into from Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package org.wildfly.extras.creaper.commands.ra;

import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Batch;
import org.wildfly.extras.creaper.core.online.operations.Operations;
import org.wildfly.extras.creaper.core.online.operations.Values;

import java.util.HashMap;
import java.util.Map;

/**
* Adds admin object to existing resource adapter
*/
public final class AddAdminObjectToRA implements OnlineCommand {
private final String poolName;
private final String localJndiName;
private final String className;
private final String resourceAdapterId;
private final boolean useJavaContext;
private final Map<String, String> properties;

private AddAdminObjectToRA(Builder builder) {
this.poolName = builder.poolName;
this.localJndiName = builder.localJndiName;
this.className = builder.className;
this.resourceAdapterId = builder.resourceAdapterId;
this.useJavaContext = builder.useJavaContext;
this.properties = builder.properties;
}

@Override
public void apply(OnlineCommandContext ctx) throws Exception {
Batch batch = new Batch();
Address address = Address.subsystem("resource-adapters").and("resource-adapter", resourceAdapterId)
.and("admin-objects", poolName);
batch.add(address, Values.of("class-name", className)
.and("jndi-name", localJndiName).and("use-java-context", useJavaContext));
for (Map.Entry<String, String> pair : properties.entrySet()) {
Address configPropsAddress = Address.subsystem("resource-adapters")
.and("resource-adapter", resourceAdapterId)
.and("admin-objects", poolName).and("config-properties", pair.getKey());
batch.add(configPropsAddress, Values.of("value", pair.getValue()));
}
new Operations(ctx.client).batch(batch);
}

public static final class Builder {
private final String poolName;
private final String localJndiName;
private final String resourceAdapterId;
private final Map<String, String> properties = new HashMap<String, String>();
private String className;
private boolean useJavaContext;

public Builder(String poolName, String localJndiName, String resourceAdapterId) {
if (poolName == null) {
throw new IllegalArgumentException("poolName must be specified");
}
if (localJndiName == null) {
throw new IllegalArgumentException("localJndiName must be specified");
}

if (resourceAdapterId == null) {
throw new IllegalArgumentException("resourceAdapterId must be specified");
}
this.poolName = poolName;
this.localJndiName = localJndiName;
this.resourceAdapterId = resourceAdapterId;
}

/**
* Other properties depending on particular resource adapter requirements
*
* @param name name of property
* @param value value
*/
public Builder addProperty(String name, String value) {
properties.put(name, value);
return this;
}

public Builder setUseJavaContext(boolean useJavaContext) {
this.useJavaContext = useJavaContext;
return this;
}

public Builder setClassName(String className) {
this.className = className;
return this;
}

public AddAdminObjectToRA build() {
if (className == null) {
throw new IllegalArgumentException("className must be specified");
}
return new AddAdminObjectToRA(this);
}
}

@Override
public String toString() {
return "AddAdminObjectToRA " + poolName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.wildfly.extras.creaper.commands.ra;

import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Batch;
import org.wildfly.extras.creaper.core.online.operations.Operations;
import org.wildfly.extras.creaper.core.online.operations.Values;

import java.util.HashMap;
import java.util.Map;


/**
* Adds new connection factory to existing resource adapter
*/
public final class AddConnectionFactoryToRA implements OnlineCommand {
private final String poolName;
private final String localJndiName;
private final String resourceAdapterId;
private final String userName;
private final String password;
private final Map<String, String> properties;
private final boolean security;
private final boolean tracking;

private AddConnectionFactoryToRA(Builder builder) {
this.poolName = builder.poolName;
this.localJndiName = builder.localJndiName;
this.resourceAdapterId = builder.resourceAdapterId;
this.userName = builder.userName;
this.password = builder.password;
this.security = builder.security;
this.properties = builder.properties;
this.tracking = builder.tracking;
}

@Override
public void apply(OnlineCommandContext ctx) throws Exception {
Batch batch = new Batch();
Address address = Address.subsystem("resource-adapters").and("resource-adapter", resourceAdapterId)
.and("connection-definitions", poolName);
batch.add(address, Values.of("class-name", "org.jboss.resource.adapter.jms.JmsManagedConnectionFactory")
.and("jndi-name", localJndiName));
if (security) {
batch.writeAttribute(address, "security-application", true);
}
if (userName != null) {
batch.writeAttribute(address, "recovery-username", userName);
}
if (password != null) {
batch.writeAttribute(address, "recovery-password", password);
}
batch.writeAttribute(address, "tracking", tracking);
for (Map.Entry<String, String> pair : properties.entrySet()) {
Address configPropsAddress = Address.subsystem("resource-adapters")
.and("resource-adapter", resourceAdapterId)
.and("connection-definitions", poolName).and("config-properties", pair.getKey());
batch.add(configPropsAddress, Values.of("value", pair.getValue()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider creating a Batch and then performing all ops in a single "transaction"? It's not strictly required, but very nice to have :-)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I didn't.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't force you to do it, but it would be nice :-)

new Operations(ctx.client).batch(batch);
}

public static final class Builder {
private final String poolName;
private final String localJndiName;
private final String resourceAdapterId;
private String userName;
private String password;
private boolean security = false;
private boolean tracking = true;
private Map<String, String> properties = new HashMap<String, String>();

/**
* @param poolName unique id for connection factory (not used for jndi lookup)
* @param localJndiName jndi name for connection factory used for jndi lookup from apps
* @param resourceAdapterId id of the resource adapter where this connection factory will be added
*/
public Builder(String poolName, String localJndiName, String resourceAdapterId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 constructors parameters of the same type are the absolute maximum. I'm fine with it, but please consider if breaking these down into separate builder methods wouldn't be better.

if (poolName == null) {
throw new IllegalArgumentException("poolName must be specified");
}
if (localJndiName == null) {
throw new IllegalArgumentException("localJndiName must be specified");
}
if (resourceAdapterId == null) {
throw new IllegalArgumentException("resourceAdapterId must be specified");
}
this.poolName = poolName;
this.localJndiName = localJndiName;
this.resourceAdapterId = resourceAdapterId;
}

/**
* XA recovery credentials
*
* @param userName user name
* @param password password
*/
public Builder addXARecovery(String userName, String password) {
this.userName = userName;
this.password = password;
return this;
}

/**
* @param applicationSecurity sets security element if true
*/
public Builder setApplicationSecurity(boolean applicationSecurity) {
this.security = applicationSecurity;
return this;
}

/**
* Other properties depending on particular resource adapter requirements
*
* @param name name of property
* @param value value
*/
public Builder addProperty(String name, String value) {
properties.put(name, value);
return this;
}

/**
* Defines whether IronJacamar should track connection handles across transaction boundaries
*
* @param tracking default is true
*/
public Builder setTracking(boolean tracking) {
this.tracking = tracking;
return this;
}

public AddConnectionFactoryToRA build() {
return new AddConnectionFactoryToRA(this);
}
}

@Override
public String toString() {
return "AddConnectionFactoryToRA " + poolName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.wildfly.extras.creaper.commands.ra;

import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Operations;
import org.wildfly.extras.creaper.core.online.operations.Values;

/**
* Adds new resource adapter
*/
public final class AddResourceAdapter implements OnlineCommand {
private final String id;
private final String module;
private final String transactions;

private AddResourceAdapter(Builder builder) {
this.id = builder.resourceAdapterId;
this.module = builder.module;
switch (builder.transactions) {
case NONE:
transactions = TransactionType.NONE.getValue();
break;
case XA:
transactions = TransactionType.XA.getValue();
break;
default:
transactions = TransactionType.NONE.getValue();
break;
}
}

@Override
public void apply(OnlineCommandContext ctx) throws Exception {
Operations ops = new Operations(ctx.client);
Address address = Address.subsystem("resource-adapters").and("resource-adapter", id);
ops.add(address, Values.of("transaction-support", transactions).and("module", module).and("slot", "main"));
}

public static final class Builder {
private final String resourceAdapterId;
private final String module;
private final TransactionType transactions;

/**
* @param resourceAdapterId name of resource adapter
* @param resourceAdapterModule module with resource adapter
* @param transactionSupport type of transaction
*/
public Builder(String resourceAdapterId, String resourceAdapterModule, TransactionType transactionSupport) {
if (resourceAdapterId == null) {
throw new IllegalArgumentException("resourceAdapterId must be specified");
}
if (resourceAdapterModule == null) {
throw new IllegalArgumentException("resourceAdapterModule must be specified");
}
if (transactionSupport == null) {
throw new IllegalArgumentException("transactionSupport must be specified");
}

this.resourceAdapterId = resourceAdapterId;
this.module = resourceAdapterModule;
this.transactions = transactionSupport;
}

public AddResourceAdapter build() {
return new AddResourceAdapter(this);
}
}

@Override
public String toString() {
return "AddResourceAdapter " + id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.wildfly.extras.creaper.commands.ra;

import org.wildfly.extras.creaper.core.online.OnlineCommand;
import org.wildfly.extras.creaper.core.online.OnlineCommandContext;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.Operations;

/**
* Removes existing admin object from resource adapter
*/
public final class RemoveAdminObjectFromRA implements OnlineCommand {
private final String poolName;
private final String resourceAdapterId;

private RemoveAdminObjectFromRA(Builder builder) {
this.poolName = builder.poolName;
this.resourceAdapterId = builder.resourceAdapterId;
}

@Override
public void apply(OnlineCommandContext ctx) throws Exception {
Operations ops = new Operations(ctx.client);
Address address = Address.subsystem("resource-adapters").and("resource-adapter", resourceAdapterId)
.and("admin-objects", poolName);
ops.remove(address);
}

public static final class Builder {
private final String poolName;
private final String resourceAdapterId;

/**
* @param poolName unique id of connection factory
* @param resourceAdapterId name of resource adapter
*/
public Builder(String poolName, String resourceAdapterId) {
this.poolName = poolName;
this.resourceAdapterId = resourceAdapterId;
}

public RemoveAdminObjectFromRA build() {
return new RemoveAdminObjectFromRA(this);
}
}

@Override
public String toString() {
return "RemoveAdminObjectFromRA " + poolName;
}
}
Loading