Subversion Repositories DevTools

Rev

Rev 4452 | Rev 6298 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * <XDtI18n:getString resource="do_not_edit"/>
 */
package <XDtPackage:packageOf><XDtEjbUtilObj:utilClass/></XDtPackage:packageOf>;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
import java.util.Properties;
<XDtEjbIntf:ifRemoteEjb>
import java.util.Date;
import javax.rmi.PortableRemoteObject;

</XDtEjbIntf:ifRemoteEjb>

<XDtConfig:ifConfigParamEquals paramName="includeGUID" value="true">
import java.net.InetAddress;
import java.security.SecureRandom;
</XDtConfig:ifConfigParamEquals>

/**
 * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="utility_class_for" arguments="<XDtEjb:ejbName/>"/>
<XDtClass:classCommentTags indent="0"/> */
public class <XDtClass:classOf><XDtEjbUtilObj:utilClass/></XDtClass:classOf>
{
 <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
  <XDtEjbIntf:ifRemoteEjb>
   /** 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;  
   /** <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_cached_remote"/> */
   private static <XDtEjbHome:homeInterface type="remote"/> cachedRemoteHome = null;
    <XDtClass:ifHasClassTag tagName="ejb:util" paramName="jndi-properties">
   /** JNDI properties (null if no configuration resource exists) */
   private static Hashtable jndiProperties = null;
    </XDtClass:ifHasClassTag>   
   
    /**
     * 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;
    }   
  </XDtEjbIntf:ifRemoteEjb>
  <XDtEjbIntf:ifLocalEjb>

   /** <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_cached_local"/> */
   private static <XDtEjbHome:homeInterface type="local"/> cachedLocalHome = null;
  </XDtEjbIntf:ifLocalEjb>
 </XDtConfig:ifConfigParamEquals>

   // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_home_lookups"/>

   <XDtEjbIntf:ifRemoteEjb>
   /**
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_home_def_ic"/>
    * @return <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="home_interface_for" arguments="<XDtEjb:ejbName/>"/> <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_lookup_using" arguments="JNDI_NAME"/>
    */
   public static <XDtEjbHome:homeInterface type="remote"/> getHome() throws NamingException
   {
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
      if ( cachedRemoteHome != null )
      {
         return cachedRemoteHome;
      }
      </XDtConfig:ifConfigParamEquals>
   
      <XDtClass:ifHasClassTag tagName="ejb:util" paramName="jndi-properties">
      if ( jndiProperties == null )
      {
         ClassLoader cl = <XDtClass:classOf><XDtEjbUtilObj:utilClass/></XDtClass:classOf>.class.getClassLoader();
         java.io.InputStream jpin = cl.getResourceAsStream("<XDtClass:classTagValue tagName='ejb:util' paramName='jndi-properties'/>");
         if ( jpin != null )
         {
            Properties jp = new Properties();
            try
            {
                jp.load( jpin );
                jpin.close();
                jndiProperties = jp;                            
            }
            catch ( java.io.IOException x )
            {
                // fail
            }
         }
      }
      
      if ( jndiProperties != null )
      {
         <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
         cachedRemoteHome = getHome( jndiProperties );
         </XDtConfig:ifConfigParamEquals>
         <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
         return getHome( jndiProperties );
         </XDtConfig:ifConfigParamNotEquals>
      }
      else
      {
      </XDtClass:ifHasClassTag>
   
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
         // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
                 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(<XDtEjbHome:homeInterface type="remote"/>.JNDI_NAME);
            cachedRemoteHome = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
         } finally {
            initialContext.close();
         }

      <XDtClass:ifHasClassTag tagName="ejb:util" paramName="jndi-properties">
      }
      </XDtClass:ifHasClassTag>

      return cachedRemoteHome;
      </XDtConfig:ifConfigParamEquals>
      <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
          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(<XDtEjbHome:homeInterface type="remote"/>.JNDI_NAME);
         return (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
      } finally {
         initialContext.close();
      }
      </XDtConfig:ifConfigParamNotEquals>
   }
   
   /**
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_home_param_ic"/>
    * @param ejbRootNamingContext for lookup
    * @return <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="home_interface_for" arguments="<XDtEjb:ejbName/>"/> <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_lookup_using" arguments="EJB_JNDI_NAME"/>
    */
   public static <XDtEjbHome:homeInterface type="remote"/> getHome(Context ejbRootNamingContext) throws NamingException
   {
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
      if ( cachedRemoteHome != null )
      {
         return cachedRemoteHome;
      }
      </XDtConfig:ifConfigParamEquals>
      java.lang.Object objRef = ejbRootNamingContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.EJB_JNDI_NAME);
      cachedRemoteHome = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
    
      return cachedRemoteHome;    
   }

   /**
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_home_param_ic"/>
    * @param environment <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_ic_parameters"/>
    * @return <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="home_interface_for" arguments="<XDtEjb:ejbName/>"/> <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_lookup_using" arguments="JNDI_NAME"/>
    */
   public static <XDtEjbHome:homeInterface type="remote"/> getHome( Hashtable environment ) throws NamingException
   {
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
      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;
      }
   }
   
   
   </XDtEjbIntf:ifRemoteEjb>

   <XDtEjbIntf:ifLocalEjb>
   /**
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_localhome_def_ic"/>
    * @return <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="local_home_interface_for" arguments="<XDtEjb:ejbName/>"/> <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_lookup_using" arguments="COMP_NAME"/>
    */
   public static <XDtEjbHome:homeInterface type="local"/> getLocalHome() throws NamingException
   {
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_localhome_not_narrowed"/>
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
      if (cachedLocalHome == null) {
         // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
         Properties properties = new Properties();
                 properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
         InitialContext initialContext = new InitialContext(properties);
         try {
            cachedLocalHome = (<XDtEjbHome:homeInterface type="local"/>) initialContext.lookup(<XDtEjbHome:homeInterface type="local"/>.COMP_NAME);
         } finally {
            initialContext.close();
         }
      }
      return cachedLocalHome;
      </XDtConfig:ifConfigParamEquals>
      <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
      Properties properties = new Properties();
          properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
          InitialContext initialContext = new InitialContext(properties);
      try {
         return (<XDtEjbHome:homeInterface type="local"/>) initialContext.lookup(<XDtEjbHome:homeInterface type="local"/>.COMP_NAME);
      } finally {
         initialContext.close();
      }
      </XDtConfig:ifConfigParamNotEquals>
   }
   </XDtEjbIntf:ifLocalEjb>

   <XDtMerge:merge file="util-custom.xdt">
   </XDtMerge:merge>

   <XDtConfig:ifConfigParamEquals paramName="includeGUID" value="true">
   /** <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_ip"/> */
   private static String hexServerIP = null;

   // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_securerandom"/>
   private static final SecureRandom seeder = new SecureRandom();

   /**
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_1"/>
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_2"/>
    *
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_usage"/>
    */
   public static final String generateGUID(Object o) {
       StringBuffer tmpBuffer = new StringBuffer(16);
       if (hexServerIP == null) {
           InetAddress localInetAddress = null;
           try {
               // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_localinetaddress"/>
               localInetAddress = InetAddress.getLocalHost();
           }
           catch (java.net.UnknownHostException uhe) {
               System.err.println("<XDtClass:classOf><XDtEjbUtilObj:utilClass/></XDtClass:classOf>: <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_unknownhost"/>");
               // 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();
   }
   </XDtConfig:ifConfigParamEquals>

   <XDtMerge:merge file="lookup-custom.xdt">
   </XDtMerge:merge>
 
 
   <XDtEjbIntf:ifRemoteEjb>
   static class Seeker extends Thread
   {
      private int timeout = 0;
      private Hashtable environment;
      private <XDtEjbHome:homeInterface type="remote"/> 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(<XDtEjbHome:homeInterface type="remote"/>.JNDI_NAME);
            home = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
         
            synchronized( this )
            {
               this.notify();
            }
         }
         catch( javax.naming.NamingException x )
         {
            this.ex = x;
         }
         finally 
         {
                try
                {
               initialContext.close();
            }
            catch ( javax.naming.NamingException x )
            {
            }
         }
         
      }
      
      public <XDtEjbHome:homeInterface type="remote"/> 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;
      }
   }
   </XDtEjbIntf:ifRemoteEjb>
}