/*
*
*/
package ;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Date;
import javax.rmi.PortableRemoteObject;
import java.net.InetAddress;
import java.security.SecureRandom;
/**
*
*/
public class
{
/** Date threshold - when seeking a remote access control server, fail immediately if this value is set and is in the future */
private static Date seekThreshold = null;
/** */
private static cachedRemoteHome = null;
/** JNDI properties (null if no configuration resource exists) */
private static Hashtable jndiProperties = null;
/**
* Clears the cached Remote Home. This will be needed if a client is
* re-establishing a connection to a Remote EJB after the EJB container
* has restarted.
*/
public static void clearCache()
{
cachedRemoteHome=null;
}
/** */
private static cachedLocalHome = null;
//
/**
*
* @return
*/
public static getHome() throws NamingException
{
if ( cachedRemoteHome != null )
{
return cachedRemoteHome;
}
if ( jndiProperties == null )
{
ClassLoader cl = .class.getClassLoader();
java.io.InputStream jpin = cl.getResourceAsStream("");
if ( jpin != null )
{
Properties jp = new Properties();
try
{
jp.load( jpin );
jpin.close();
jndiProperties = jp;
}
catch ( java.io.IOException x )
{
// fail
}
}
}
if ( jndiProperties != null )
{
cachedRemoteHome = getHome( jndiProperties );
return getHome( jndiProperties );
}
else
{
//
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext initialContext = new InitialContext(properties);
try {
java.lang.Object objRef = initialContext.lookup(.JNDI_NAME);
cachedRemoteHome = () PortableRemoteObject.narrow(objRef, .class);
} finally {
initialContext.close();
}
}
return cachedRemoteHome;
//
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext initialContext = new InitialContext(properties);
try {
java.lang.Object objRef = initialContext.lookup(.JNDI_NAME);
return () PortableRemoteObject.narrow(objRef, .class);
} finally {
initialContext.close();
}
}
/**
*
* @param ejbRootNamingContext for lookup
* @return
*/
public static getHome(Context ejbRootNamingContext) throws NamingException
{
if ( cachedRemoteHome != null )
{
return cachedRemoteHome;
}
java.lang.Object objRef = ejbRootNamingContext.lookup(.EJB_JNDI_NAME);
cachedRemoteHome = () PortableRemoteObject.narrow(objRef, .class);
return cachedRemoteHome;
}
/**
*
* @param environment
* @return
*/
public static getHome( Hashtable environment ) throws NamingException
{
//
Date now = new Date();
if ( seekThreshold != null )
{
if ( now.before(seekThreshold) )
{
// Fail-fast if called prior to threshold value
throw new javax.naming.CommunicationException("Communication unavailable until " + seekThreshold);
}
else
{
seekThreshold = null;
}
}
String value = (String)environment.get("jnp.timeout");
int jnpTimeout = 0;
if ( value != null )
{
jnpTimeout = Integer.parseInt(value);
}
try
{
Seeker seeker = new Seeker(jnpTimeout,environment);
return seeker.seekHome();
}
catch ( javax.naming.CommunicationException x )
{
if ( environment.containsKey( "broker.retrytimeout" ) )
{
try
{
int retryTimeoutMillis = Integer.parseInt( environment.get("broker.retrytimeout").toString() );
seekThreshold = new Date( now.getTime() + retryTimeoutMillis );
}
catch ( NumberFormatException nx )
{
}
}
throw x;
}
}
/**
*
* @return
*/
public static getLocalHome() throws NamingException
{
//
if (cachedLocalHome == null) {
//
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext initialContext = new InitialContext(properties);
try {
cachedLocalHome = () initialContext.lookup(.COMP_NAME);
} finally {
initialContext.close();
}
}
return cachedLocalHome;
//
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
InitialContext initialContext = new InitialContext(properties);
try {
return () initialContext.lookup(.COMP_NAME);
} finally {
initialContext.close();
}
}
/** */
private static String hexServerIP = null;
//
private static final SecureRandom seeder = new SecureRandom();
/**
*
*
*
*
*/
public static final String generateGUID(Object o) {
StringBuffer tmpBuffer = new StringBuffer(16);
if (hexServerIP == null) {
InetAddress localInetAddress = null;
try {
//
localInetAddress = InetAddress.getLocalHost();
}
catch (java.net.UnknownHostException uhe) {
System.err.println(": ");
// todo: find better way to get around this...
uhe.printStackTrace();
return null;
}
byte serverIP[] = localInetAddress.getAddress();
hexServerIP = hexFormat(getInt(serverIP), 8);
}
String hashcode = hexFormat(System.identityHashCode(o), 8);
tmpBuffer.append(hexServerIP);
tmpBuffer.append(hashcode);
long timeNow = System.currentTimeMillis();
int timeLow = (int)timeNow & 0xFFFFFFFF;
int node = seeder.nextInt();
StringBuffer guid = new StringBuffer(32);
guid.append(hexFormat(timeLow, 8));
guid.append(tmpBuffer.toString());
guid.append(hexFormat(node, 8));
return guid.toString();
}
private static int getInt(byte bytes[]) {
int i = 0;
int j = 24;
for (int k = 0; j >= 0; k++) {
int l = bytes[k] & 0xff;
i += l << j;
j -= 8;
}
return i;
}
private static String hexFormat(int i, int j) {
String s = Integer.toHexString(i);
return padHex(s, j) + s;
}
private static String padHex(String s, int i) {
StringBuffer tmpBuffer = new StringBuffer();
if (s.length() < i) {
for (int j = 0; j < i - s.length(); j++) {
tmpBuffer.append('0');
}
}
return tmpBuffer.toString();
}
static class Seeker extends Thread
{
private int timeout = 0;
private Hashtable environment;
private home;
private javax.naming.NamingException ex;
Seeker( int timeout, Hashtable env )
{
super("JNPSeeker");
setDaemon(true);
this.timeout=timeout;
this.environment = env;
}
public void run()
{
InitialContext initialContext = null;
try
{
this.environment.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
initialContext = new InitialContext(environment);
java.lang.Object objRef = initialContext.lookup(.JNDI_NAME);
home = () PortableRemoteObject.narrow(objRef, .class);
synchronized( this )
{
this.notify();
}
}
catch( javax.naming.NamingException x )
{
this.ex = x;
}
finally
{
try
{
initialContext.close();
}
catch ( javax.naming.NamingException x )
{
}
}
}
public seekHome() throws javax.naming.NamingException
{
try
{
synchronized(this)
{
this.start();
this.wait(timeout);
}
}
catch (InterruptedException x)
{
throw new javax.naming.CommunicationException("Connect attempt timed out");
}
// See if the connect thread exited due to an exception
if( ex != null ) throw ex;
if ( home == null ) throw new javax.naming.CommunicationException("Connect attempt timed out");
return home;
}
}
}