How to see JNDIView using Java based DMR APIs in EAP 6 ?
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6.x
Issue
- How to see JNDIView using Java based DMR APIs in EAP 6 ?
Resolution
- The DMR code implements following CLI command :
/subsystem=naming:jndi-view
To run code JndiInfo.java follow below steps :
-
Create the management relem user and password admin/admin12# with the help of ./add-user.sh or add-user.bat
-
Set
JBOSS_HOMEas per JBoss installation and then setCLASSPATHwith following jars :
export CLASSPATH=$JBOSS_HOME/modules/system/layers/base/org/jboss/as/cli/main/jboss-as-cli-7.3.0.Final-redhat-14.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/as/controller-client/main/jboss-as-controller-client-7.3.0.Final-redhat-14.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/as/protocol/main/jboss-as-protocol-7.3.0.Final-redhat-14.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/dmr/main/jboss-dmr-1.2.0.Final-redhat-1.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/logging/main/jboss-logging-3.1.2.GA-redhat-1.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/remoting3/main/jboss-remoting-3.2.18.GA-redhat-1.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/threads/main/jboss-threads-2.1.1.Final-redhat-1.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/staxmapper/main/staxmapper-1.1.0.Final-redhat-2.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/xnio/main/xnio-api-3.0.7.GA-redhat-1.jar:$JBOSS_HOME/modules/system/layers/base/org/jboss/xnio/nio/main/xnio-nio-3.0.7.GA-redhat-1.jar:.:
- Replace localhost and management port 9999 as per environment. Compile code and run it.
JndiInfo.java
import java.io.IOException;
import java.util.ArrayList;
import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandContextFactory;
import org.jboss.as.cli.CommandLineException;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.dmr.ModelNode;
import java.net.*;
import java.util.*;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.sasl.RealmCallback;
public class JndiInfo {
public static void main(String[] args) throws CommandLineException, IOException
{
String host = "localhost"; //replace it with ip address
int port = 9999; //Make sure that port offset is added in managemt port (if any)
String user = "admin"; //replace it with management username created with the help of add-user.sh
String password = "admin12#"; //respective password
ModelControllerClient client = createClient(InetAddress.getByName(host), port, user, password);
// /subsystem=naming:jndi-view()
ModelNode readJndiInfo = new ModelNode();
readJndiInfo.get("operation").set("jndi-view");
ModelNode addressJndiInfo = readJndiInfo.get("address");
addressJndiInfo.add("subsystem", "naming");
ModelNode returnJndiInfo = client.execute(readJndiInfo);
System.out.println(returnJndiInfo.get("result").toString());
client.close();
}
private static final ModelControllerClient createClient(final InetAddress host, final int port, final String username, final String password) {
final CallbackHandler callackHandler = new CallbackHandler() {
public void handle(Callback[] callback) throws IOException, UnsupportedCallbackException {
for (Callback current : callback) {
if (current instanceof NameCallback) {
NameCallback ncb = (NameCallback) current;
ncb.setName(username);
}
else if (current instanceof PasswordCallback) {
PasswordCallback pcb = (PasswordCallback) current;
pcb.setPassword(password.toCharArray());
}
else if (current instanceof RealmCallback) {
RealmCallback rcb = (RealmCallback) current;
rcb.setText(rcb.getDefaultText());
}
else {
throw new UnsupportedCallbackException(current);
}
}
}
};
return ModelControllerClient.Factory.create(host, port, callackHandler);
}
}
SBR
Components
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.