czwartek, września 20, 2012

JBoss 7 UserTransaction via JNDI

public class JBossUserTransactionContextFactory implements InitialContextFactory, Context {
private Properties p = new Properties();
private String host = "localhost";

public Object lookup(Name name) throws NamingException {
return lookup(name!=null ? name.toString() : null);
}


public Object lookup(String name) throws NamingException {
System.setProperty("jboss.node.name", host);
if (name!=null && name.toString().contains("UserTransaction"))
return EJBClient.getUserTransaction(host);
else
throw new NamingException("not found: "+name);
}

private static String getVal(Hashtable h, String key) {
return h.get(key)!=null ? h.get(key).toString() : null;
}
private boolean isNumber(String s) {
try {
Integer.valueOf(s);
return true;
}
catch (Exception e) {}
return false;
}

public Context getInitialContext(Hashtable h)
throws NamingException {
p.put("endpoint.name", "client-endpoint");
p.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
p.put("remote.connections", "default");
p.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
String url = getVal(h, PROVIDER_URL);
String user = getVal(h, SECURITY_PRINCIPAL);
String pass = getVal(h, SECURITY_CREDENTIALS);
if (url==null)
url = "remote://localhost:4447";
String[] tokens = url.split(":");
String port = "4447";
if (tokens.length==3) {
host = tokens[1];
port = tokens[2];
}
else if (tokens.length==2) {
if (isNumber(tokens[1])) {
host = tokens[0];
port = tokens[1];
}
else {
host = tokens[1];
}
}
else if (tokens.length==1)
host = tokens[0];
while (host.charAt(0) == '/')
host = host.substring(1);

p.put("remote.connection.default.port", port);
p.put("remote.connection.default.host", host);
p.put("remote.connection.default.username", user);
p.put("remote.connection.default.password", pass);

EJBClientConfiguration cc = new PropertiesBasedEJBClientConfiguration(p);
ContextSelector selector = new ConfigBasedEJBClientContextSelector(cc);
EJBClientContext.setSelector(selector);
return this;
} // ....
}

0 komentarze: