Created
February 26, 2015 20:12
-
-
Save anonymous/8cab7e628dd01a8b44da to your computer and use it in GitHub Desktop.
Using remoting endpoint API to connect to Wildfly 8.2.0.Final
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
package sample; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Properties; | |
import java.util.concurrent.TimeUnit; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.security.sasl.SaslException; | |
import org.jboss.ejb.client.EJBClientConfiguration; | |
import org.jboss.ejb.client.EJBClientConfiguration.RemotingConnectionConfiguration; | |
import org.jboss.ejb.client.PropertiesBasedEJBClientConfiguration; | |
import org.jboss.ejb.client.remoting.IoFutureHelper; | |
import org.jboss.remoting3.Connection; | |
import org.jboss.remoting3.Endpoint; | |
import org.jboss.remoting3.Remoting; | |
import org.jboss.remoting3.remote.RemoteConnectionProviderFactory; | |
import org.xnio.IoFuture; | |
import org.xnio.OptionMap; | |
public class HttpRemotingClient { | |
private static final Logger Log = Logger.getLogger(HttpRemotingClient.class.getName()); | |
public static void main(String[] args) throws Exception { | |
Logger.getLogger("org.xnio").setLevel(Level.FINE); | |
Logger.getLogger("org.jboss").setLevel(Level.FINE); | |
Properties properties = createClientContextProperties(); | |
testConnectionToServer(properties, "http-remoting://localhost:8080"); | |
} | |
// properties to initialize ejb client context | |
private static Properties createClientContextProperties() { | |
SampleClientCallbackHandler.bindCredentials("user", "password"); | |
Properties jndiProperties = new Properties(); | |
jndiProperties.put("endpoint.name","client-endpoint-server"); | |
jndiProperties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED","false"); | |
jndiProperties.put("remote.connections","server0"); | |
jndiProperties.put("remote.connection.server0.host","localhost"); | |
jndiProperties.put("remote.connection.server0.port","8080"); | |
jndiProperties.put("remote.connection.server0.callback.handler.class","sample.SampleClientCallbackHandler"); | |
jndiProperties.put("remote.connection.server0.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS","JBOSS-LOCAL-USER"); | |
jndiProperties.put("remote.connection.server0.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT","false"); | |
jndiProperties.put("remote.connection.server0.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS","true"); | |
jndiProperties.put("remote.connection.server0.connect.options.org.xnio.Options.SSL_STARTTLS","false"); | |
return jndiProperties; | |
} | |
private static void testConnectionToServer(final Properties properties, String serverNamingProviderUrls) throws Exception { | |
boolean successfulConnection = false; | |
boolean invalidCredentials = false; | |
List<URI> connectionURIs = new ArrayList<URI>(); | |
String protocol = "http-remoting"; | |
final EJBClientConfiguration ejbClientConfiguration = new PropertiesBasedEJBClientConfiguration( | |
properties); | |
OptionMap remoteConnectionProviderCreationOptions = ejbClientConfiguration.getRemoteConnectionProviderCreationOptions(); | |
Iterator<RemotingConnectionConfiguration> itr = ejbClientConfiguration.getConnectionConfigurations(); | |
while(!successfulConnection && itr.hasNext() && !invalidCredentials) { | |
RemotingConnectionConfiguration remotingConnectionConfiguration = itr.next(); | |
String serverNamingProviderUrl = protocol + "://" + remotingConnectionConfiguration.getHost() + | |
":" + remotingConnectionConfiguration.getPort(); | |
Connection connection = null; | |
Endpoint endpoint = null; | |
try { | |
URI uri = new URI(serverNamingProviderUrl); | |
connectionURIs.add(uri); | |
// remoting endpoint | |
endpoint = Remoting.createEndpoint("endpoint", ejbClientConfiguration.getEndpointCreationOptions()); | |
endpoint.addConnectionProvider(protocol, new RemoteConnectionProviderFactory(), remoteConnectionProviderCreationOptions); | |
OptionMap connectionOptionMap = remotingConnectionConfiguration.getConnectionCreationOptions(); | |
final IoFuture<Connection> futureConnection = endpoint.connect(uri, connectionOptionMap, | |
remotingConnectionConfiguration.getCallbackHandler()); | |
connection = IoFutureHelper.get(futureConnection, 5, TimeUnit.SECONDS); | |
// we don't actually invoke any method. | |
// JBoss 7+, authentication happens during the "connect" | |
successfulConnection = true; | |
} catch (IOException ex) { | |
Log.log(Level.WARNING, "Error connecting to server at " + serverNamingProviderUrl, ex); | |
} catch (URISyntaxException e) { | |
Log.log(Level.WARNING, "Error connecting to server at " + serverNamingProviderUrl, e); | |
} catch (RuntimeException ex) { | |
Log.log(Level.WARNING, "Error connecting to server at " + serverNamingProviderUrl, ex); | |
// check if RuntimeException wraps a SaslException | |
if (ex.getCause() instanceof SaslException) { | |
Log.log(Level.WARNING, "Invalid Credentials at " + serverNamingProviderUrl, ex); | |
invalidCredentials = true; | |
} else { | |
Log.log(Level.WARNING, "Error connecting to server at " + serverNamingProviderUrl, ex); | |
} | |
} finally { | |
try { | |
if (connection != null) connection.close(); | |
} catch (IOException ex) {Log.log(Level.SEVERE, null, ex);} | |
try { | |
if (endpoint != null) endpoint.close(); | |
} catch (IOException ex) {Log.log(Level.SEVERE, null, ex);} | |
} | |
} | |
if(!successfulConnection) { | |
if(invalidCredentials) { | |
throw new Exception("Invalid user/password"); | |
} | |
throw new Exception("Error connecting to server at " + connectionURIs); | |
} | |
} | |
} | |
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
package sample; | |
import java.io.IOException; | |
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 SampleClientCallbackHandler implements CallbackHandler { | |
private static String username; | |
private static String password; | |
@Override | |
public void handle(Callback[] callbacks) throws IOException, | |
UnsupportedCallbackException { | |
for (Callback current : callbacks) { | |
if (current instanceof RealmCallback) { | |
RealmCallback rcb = (RealmCallback) current; | |
rcb.setText(rcb.getDefaultText()); | |
} else 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 { | |
throw new UnsupportedCallbackException(current); | |
} | |
} | |
} | |
public static void bindCredentials(String userName, String passwd) { | |
username = userName; | |
password = passwd; | |
} | |
public static String getCurrentUser() { | |
return username; | |
} | |
} |
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
Error when I run the above client. | |
Feb 26, 2015 12:00:17 PM sample.HttpRemotingClient testConnectionToServer | |
WARNING: Error connecting to server at http-remoting://localhost:8080 | |
java.lang.RuntimeException: Operation failed with status WAITING | |
at org.jboss.ejb.client.remoting.IoFutureHelper.get(IoFutureHelper.java:94) | |
at sample.HttpRemotingClient.testConnectionToServer(HttpRemotingClient.java:92) | |
at sample.HttpRemotingClient.main(HttpRemotingClient.java:37) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:606) | |
at org.eclipse.jdt.internal.launching.LongCommandLineLauncher.main(LongCommandLineLauncher.java:43) | |
Feb 26, 2015 12:00:17 PM sample.HttpRemotingClient testConnectionToServer | |
WARNING: Error connecting to server at http-remoting://localhost:8080 | |
java.lang.RuntimeException: Operation failed with status WAITING | |
at org.jboss.ejb.client.remoting.IoFutureHelper.get(IoFutureHelper.java:94) | |
at sample.HttpRemotingClient.testConnectionToServer(HttpRemotingClient.java:92) | |
at sample.HttpRemotingClient.main(HttpRemotingClient.java:37) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:606) | |
at org.eclipse.jdt.internal.launching.LongCommandLineLauncher.main(LongCommandLineLauncher.java:43) | |
Exception in thread "main" java.lang.reflect.InvocationTargetException | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:606) | |
at org.eclipse.jdt.internal.launching.LongCommandLineLauncher.main(LongCommandLineLauncher.java:43) | |
Caused by: java.lang.Exception: Error connecting to server at [http-remoting://localhost:8080] | |
at sample.HttpRemotingClient.testConnectionToServer(HttpRemotingClient.java:125) | |
at sample.HttpRemotingClient.main(HttpRemotingClient.java:37) | |
... 5 more |
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
SampleClientCallbackHandler.bindCredentials("user", "password"); | |
final Properties jndiProperties = new Properties(); | |
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); | |
jndiProperties.put("org.jboss.ejb.client.scoped.context", "true"); | |
jndiProperties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED","false"); | |
jndiProperties.put("remote.connections","default"); | |
jndiProperties.put("remote.connection.default.host","localhost"); | |
jndiProperties.put("remote.connection.default.port","8080"); | |
jndiProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS","true"); | |
jndiProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT","false"); | |
jndiProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS","JBOSS-LOCAL-USER"); | |
jndiProperties.put("remote.connection.default.callback.handler.class","sample.SampleClientCallbackHandler | |
final Context context = new InitialContext(jndiProperties); | |
// this works!! | |
context.lookup("...........") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment