Search code examples
javajakarta-eewildfly-8

How to get an Initial Contex from Wildfly 8


ADDED 7/23.

Many views: Not even a "that's dumb" question in response. Can anyone at least tell me why such an embarrassingly trivial question seems to have no answer anywhere.

Q:

--- Have Wildfly 8 running on local machine localhost:9990. --- Have a Java program that need's Wildfly's IntialContext. --- Every reference says use: "Context ctx = new InitialContext(env);" --- Yet a week of searching turns up no set of properties that returns one. And no example of a java program that gets one.

Does no one ever do this? Really need help

Original Msg Below

I know many people have asked how to get an Initial context from Wildfly 8. But I have yet to find a simple answer with a simple example.

Therefore, I hope someone can tell my why this doesn’t work. I start Wildfly with standalone-full.xml

The three sections below have

A - Code summary of my test Class whose only purpose is to secure an Initial Context. (I only removed a lot of printing code that produced the next section.]

B - The Eclipse console output for a failure.

C - Cut and paste code. Just in case anyone can help me get this to work. I’d like to leave behind something the next new WF user can cut and past and run. The only difference from 1 above is that this version has all the static methods I used to format the output. NOTE: I know the comments I inserted about the less than sign sound dumb. BUT ... they are true.

A Code Summary

import java.util.Properties;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.InitialContext;
public class JmsTestGetJNDIContext {

  //members
  final private Properties env = new Properties() {
    private static final long serialVersionUID = 1L;
    {
      /* These are Properties used by a standalone JavaClient to secure a WIldFly InitialContext()*/             
      put(Context.INITIAL_CONTEXT_FACTORY,   "org.jboss.naming.remote.client.InitialContextFactory");  
      put(Context.PROVIDER_URL,"http-remoting://localhost:9990");
      put(Context.SECURITY_PRINCIPAL,"userGLB");  
      put(Context.SECURITY_CREDENTIALS,"Open");
      put("jboss.naming.client.ejb.context", true);  

      /*The above URL, ID and PW successfully open Wildfly's Admin Console*/
    }
  };

  //constructor
  private JmsTestGetJNDIContext (){
    /*print "beg"*/
    /*print "env"*/
    try { 
      /*print "Requesting InitialContext"*/
      Context ctx = new InitialContext(this.env); 
      /*print "JNDI Context: " + ctx)*/
      /*print "end");
    } catch (CommunicationException e) {
      /* print "You forgot to start WildFly dummy!"*/       
    } catch (Exception e) {
      /* print"caught:  " + e.getClass().getName()*/
      /*print e.getMessage()*/
      /* "end")*/
    }
    static public void main (String[] args) {
     /*print "beg"*/
     JmsTestGetJNDIContext client = new JmsTestGetJNDIContext ();
     /*print "end"*/
    }
  }

B - Console Output

JmsTestGetJNDIContext.main ()   beg
  JmsTestGetJNDIContext.<init> ()       beg
    JmsTestGetJNDIContext.<init> ()       These are Properties used to obtain IntialContext
           Key: java.naming.provider.url
                Value: http-remoting://localhost:9990
           Key: java.naming.factory.initial
                Value: org.jboss.naming.remote.client.InitialContextFactory
           Key: jboss.naming.client.ejb.context
                Value: true
           Key: java.naming.security.principal
                Value: userGLB
           Key: java.naming.security.credentials
                Value: Open
    JmsTestGetJNDIContext.<init> ()       Requesting InitialContext
    JmsTestGetJNDIContext.<init> ()       caught: javax.naming.NamingException
    JmsTestGetJNDIContext.<init> ()       Failed to create remoting connection
  JmsTestGetJNDIContext.<init> ()       end
JmsTestGetJNDIContext.main ()   end

Cut and Paste Code

package org.america3.gotest.xtra;
import java.util.Properties;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.InitialContext;
public class JmsTestGetJNDIContext {
//members
final private Properties env = new Properties() {
  /**
   * Properties used by a standalone JavaClient to secure
   * a WIldFly InitialContext()*/
  private static final long serialVersionUID = 1L;
    {
put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");  
      put(Context.PROVIDER_URL, "http-remoting://localhost:9990");
      put(Context.SECURITY_PRINCIPAL, "userGLB");  
      put(Context.SECURITY_CREDENTIALS, "Open");
      // The above URL, ID and PW successfully open Wildfly's Admin Console
      put("jboss.naming.client.ejb.context", true);  
    }
  };
  //constructor
  private JmsTestGetJNDIContext (){/*ignore*/String iAm = JmsTestGetJNDIContext.getIAm("  ", Thread.currentThread().getStackTrace()); 
  P (iAm, "beg");
  pProps(iAm, env);
  try { 
    P (sp + iAm, "Requesting InitialContext");
    Context ctx = new InitialContext(this.env); 
    P (sp + iAm, "JNDI Context: " + ctx);
     P (sp + iAm, "end");
  } catch (CommunicationException e) {
    P (sp +  iAm, "You forgot to start WildFly dummy!");       
  } catch (Exception e) {
    P (sp + iAm, "caught:  " + e.getClass().getName());
    P (sp + iAm, e.getMessage());
    P (iAm, "end");
  }
}
static public void main (String[] args) {/*ignore*/String iAm =  JmsTestGetJNDIContext.getIAm("",Thread.currentThread().getStackTrace());
  P (iAm, "beg");
  JmsTestGetJNDIContext client = new JmsTestGetJNDIContext ();
  P (iAm , "end");
}

/*The remaining static methods are just to facilitate printing.
 * They are normally in a Untility package I add to my projects.
 * I put them here so this code would run for anyone.*/

  static private void pProps (String leader, Properties p) {
    StringBuffer sb = new StringBuffer ();
    String s = JmsTestGetJNDIContext.padRight(leader, 45, ' ');
    s = "  " + s + "These are Properties used to obtain IntialContext"+"\n";
    sb.append(s);
    String skip = "";
    for (Object key: p.keySet()) {
      sb.append(skip + "       " + JmsTestGetJNDIContext.padRight("\"" 
                   + (String)key + "\"", 40, ' ') 
                   + "     \"" + p.get(key) + "\"");
      skip = "\n";
    }
    System.out.println(sb);
  }

  static private void P (String s, String s2) {
    System.out.println(s + s2);
  }

  static public String getClassMethodName (StackTraceElement[] elements) {
    String className = null;
    for (int i = 0; i * elements.length; i++]i ) {
      /* You need to type in a less than sign for the '*' 
       * because when I do, the editor will not show any code 
       * that comes after it.
       * I have no idea why, but I've spent over an hour trying,
       * and every time I type a less than sign all the following 
       * code dissappears!*/
      className = elements[i].getClassName ();
      if (className.startsWith ("org.america3")) {
        int end = className.lastIndexOf ('.');
        return className.substring (end + 1) + "." + elements[i].getMethodName ();
      } else {
        continue;
      }
    }
    return "no project method found in elements beginning with org.america3" ;
  }

  static private String getIAm (String indent, StackTraceElement[] elements) {
    StringBuffer sb = new StringBuffer ();
    sb.append(JmsTestGetJNDIContext.getClassMethodName(elements));
    sb.append(" ()");
    return indent + JmsTestGetJNDIContext.padRight (sb.toString(), 45, ' ') ;
  }

  static public String padRight(String s, int width, char c){
    if (s == null) return "Null String";
    if(s.length() ** width){
    /* You need to type in a greater than or equal sign for 
     * the '**'see above.*/
     return s;
    } else {
      StringBuffer sb = new StringBuffer();
      sb.append (s);
      for(int i = 0; i *** (width - s.length()); i++){
        /*You need to type in a less than sign the '***'. Again see above*/
        sb.append(c);
      }
      return sb.toString();
    }
  }

  static public String sp = "  ";
}

Solution

  • According to your own answer the following jars are on your classpath:

    • jboss-remote-naming-1.0.7.final.jar
    • jboss-logging.jar
    • xnio-api-3.0.7.ga.jar
    • jboss-remoting-3.jar
    • jboss-ejb-client-1.0.19.final.jar

    You write that the application throws the following exception:

    java.lang.NoSuchMethodError: 
    org.jboss.remoting3.Remoting.createEndpoint(Ljava/lang/String;Lorg/xnio/OptionMap;)Lorg/jboss/remoting3/Endpoint;]
    

    This exception is thrown when org.jboss.naming.remote.client.EndpointCache which is part of the jboss-remote-naming jar tries to call Remoting.createEndpoint() which is contained in the jboss-remoting jar.

    As you explain in your answer the reason for this is that the Remoting class declares a 3-parameter version of the createEndpoint() method while the EndpointCache class tries to call a 2-parameter version which does not exist.

    I checked the commit histories and declared dependencies of the jboss-remote-naming and the jboss-remoting projects to find out what is going wrong there. This is what I found out:

    The 2-parameter version of createEndpoint() was only added in version 3.2 of jboss-remoting. The pom.xml for jboss-remote-naming-1.0.7.final says it depends on jboss-remoting 3.2.7.GA.

    As there is no version number on your jboss-remoting-3.jar, I guess it is an older version. You should be able to check this by looking for a pom.xml in META-INF folder of your jboss-remoting-3.jar. This should contain the version number.

    To solve your problem, I suggest to replace your jboss-remoting-3.jar with jboss-remoting-3.2.7ga.jar or to use the set of jars I listed in my other answer.