ItemUtil.java
上传用户:hgs128
上传日期:2007-02-03
资源大小:166k
文件大小:5k
源码类别:

百货/超市行业

开发平台:

WINDOWS

  1. /*
  2.  * Generated file - Do not edit!
  3.  */
  4. package cmp;
  5. /**
  6.  * Utility class for Item.
  7.  * @lomboz generated
  8.  */
  9. public class ItemUtil
  10. {
  11.    /** Cached remote home (EJBHome). Uses lazy loading to obtain its value (loaded by getHome() methods). */
  12.    private static cmp.ItemHome cachedRemoteHome = null;
  13.    /** Cached local home (EJBLocalHome). Uses lazy loading to obtain its value (loaded by getLocalHome() methods). */
  14.    private static cmp.ItemLocalHome cachedLocalHome = null;
  15.    private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException {
  16.       // Obtain initial context
  17.       javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
  18.       try {
  19.          Object objRef = initialContext.lookup(jndiName);
  20.          // only narrow if necessary
  21.          if (narrowTo.isInstance(java.rmi.Remote.class))
  22.             return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);
  23.          else
  24.             return objRef;
  25.       } finally {
  26.          initialContext.close();
  27.       }
  28.    }
  29.    // Home interface lookup methods
  30.    /**
  31.     * Obtain remote home interface from default initial context
  32.     * @return Home interface for Item. Lookup using JNDI_NAME
  33.     */
  34.    public static cmp.ItemHome getHome() throws javax.naming.NamingException
  35.    {
  36.       if (cachedRemoteHome == null) {
  37.             cachedRemoteHome = (cmp.ItemHome) lookupHome(null, cmp.ItemHome.JNDI_NAME, cmp.ItemHome.class);
  38.       }
  39.       return cachedRemoteHome;
  40.    }
  41.    /**
  42.     * Obtain remote home interface from parameterised initial context
  43.     * @param environment Parameters to use for creating initial context
  44.     * @return Home interface for Item. Lookup using JNDI_NAME
  45.     */
  46.    public static cmp.ItemHome getHome( java.util.Hashtable environment ) throws javax.naming.NamingException
  47.    {
  48.        return (cmp.ItemHome) lookupHome(environment, cmp.ItemHome.JNDI_NAME, cmp.ItemHome.class);
  49.    }
  50.    /**
  51.     * Obtain local home interface from default initial context
  52.     * @return Local home interface for Item. Lookup using JNDI_NAME
  53.     */
  54.    public static cmp.ItemLocalHome getLocalHome() throws javax.naming.NamingException
  55.    {
  56.       if (cachedLocalHome == null) {
  57.             cachedLocalHome = (cmp.ItemLocalHome) lookupHome(null, cmp.ItemLocalHome.JNDI_NAME, cmp.ItemLocalHome.class);
  58.       }
  59.       return cachedLocalHome;
  60.    }
  61.    /** Cached per JVM server IP. */
  62.    private static String hexServerIP = null;
  63.    // initialise the secure random instance
  64.    private static final java.security.SecureRandom seeder = new java.security.SecureRandom();
  65.    /**
  66.     * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
  67.     * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
  68.     *
  69.     * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
  70.     */
  71.    public static final String generateGUID(Object o) {
  72.        StringBuffer tmpBuffer = new StringBuffer(16);
  73.        if (hexServerIP == null) {
  74.            java.net.InetAddress localInetAddress = null;
  75.            try {
  76.                // get the inet address
  77.                localInetAddress = java.net.InetAddress.getLocalHost();
  78.            }
  79.            catch (java.net.UnknownHostException uhe) {
  80.                System.err.println("ItemUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
  81.                // todo: find better way to get around this...
  82.                uhe.printStackTrace();
  83.                return null;
  84.            }
  85.            byte serverIP[] = localInetAddress.getAddress();
  86.            hexServerIP = hexFormat(getInt(serverIP), 8);
  87.        }
  88.        String hashcode = hexFormat(System.identityHashCode(o), 8);
  89.        tmpBuffer.append(hexServerIP);
  90.        tmpBuffer.append(hashcode);
  91.        long timeNow      = System.currentTimeMillis();
  92.        int timeLow       = (int)timeNow & 0xFFFFFFFF;
  93.        int node          = seeder.nextInt();
  94.        StringBuffer guid = new StringBuffer(32);
  95.        guid.append(hexFormat(timeLow, 8));
  96.        guid.append(tmpBuffer.toString());
  97.        guid.append(hexFormat(node, 8));
  98.        return guid.toString();
  99.    }
  100.    private static int getInt(byte bytes[]) {
  101.        int i = 0;
  102.        int j = 24;
  103.        for (int k = 0; j >= 0; k++) {
  104.            int l = bytes[k] & 0xff;
  105.            i += l << j;
  106.            j -= 8;
  107.        }
  108.        return i;
  109.    }
  110.    private static String hexFormat(int i, int j) {
  111.        String s = Integer.toHexString(i);
  112.        return padHex(s, j) + s;
  113.    }
  114.    private static String padHex(String s, int i) {
  115.        StringBuffer tmpBuffer = new StringBuffer();
  116.        if (s.length() < i) {
  117.            for (int j = 0; j < i - s.length(); j++) {
  118.                tmpBuffer.append('0');
  119.            }
  120.        }
  121.        return tmpBuffer.toString();
  122.    }
  123. }