KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > batchcascadedelete > ejb > ParentUtil


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.batchcascadedelete.ejb;
23
24
25
26 public class ParentUtil
27 {
28    /**
29     * Cached remote home (EJBHome). Uses lazy loading to obtain its value (loaded by getHome() methods).
30     */

31    private static ParentHome cachedRemoteHome = null;
32
33    /**
34     * Cached local home (EJBLocalHome). Uses lazy loading to obtain its value (loaded by getLocalHome() methods).
35     */

36    private static ParentLocalHome cachedLocalHome = null;
37
38    // Home interface lookup methods
39

40    /**
41     * Obtain remote home interface from default initial context
42     *
43     * @return Home interface for Parent. Lookup using JNDI_NAME
44     */

45    public static ParentHome getHome() throws javax.naming.NamingException JavaDoc
46    {
47       if(cachedRemoteHome == null)
48       {
49          // Obtain initial context
50
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc();
51          try
52          {
53             java.lang.Object JavaDoc objRef = initialContext.lookup(ParentHome.JNDI_NAME);
54             cachedRemoteHome = (ParentHome)javax.rmi.PortableRemoteObject.narrow(objRef, ParentHome.class);
55          }
56          finally
57          {
58             initialContext.close();
59          }
60       }
61       return cachedRemoteHome;
62    }
63
64    /**
65     * Obtain remote home interface from parameterised initial context
66     *
67     * @param environment Parameters to use for creating initial context
68     * @return Home interface for Parent. Lookup using JNDI_NAME
69     */

70    public static ParentHome getHome(java.util.Hashtable JavaDoc environment) throws javax.naming.NamingException JavaDoc
71    {
72       // Obtain initial context
73
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc(environment);
74       try
75       {
76          java.lang.Object JavaDoc objRef = initialContext.lookup(ParentHome.JNDI_NAME);
77          return (ParentHome)javax.rmi.PortableRemoteObject.narrow(objRef, ParentHome.class);
78       }
79       finally
80       {
81          initialContext.close();
82       }
83    }
84
85    /**
86     * Obtain local home interface from default initial context
87     *
88     * @return Local home interface for Parent. Lookup using JNDI_NAME
89     */

90    public static ParentLocalHome getLocalHome() throws javax.naming.NamingException JavaDoc
91    {
92       // Local homes shouldn't be narrowed, as there is no RMI involved.
93
if(cachedLocalHome == null)
94       {
95          // Obtain initial context
96
javax.naming.InitialContext JavaDoc initialContext = new javax.naming.InitialContext JavaDoc();
97          try
98          {
99             cachedLocalHome = (ParentLocalHome)initialContext.lookup(ParentLocalHome.JNDI_NAME);
100          }
101          finally
102          {
103             initialContext.close();
104          }
105       }
106       return cachedLocalHome;
107    }
108
109    /**
110     * Cached per JVM server IP.
111     */

112    private static String JavaDoc hexServerIP = null;
113
114    // initialise the secure random instance
115
private static final java.security.SecureRandom JavaDoc seeder = new java.security.SecureRandom JavaDoc();
116
117    /**
118     * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
119     * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
120     *
121     * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
122     */

123    public static final String JavaDoc generateGUID(Object JavaDoc o)
124    {
125       StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
126       if(hexServerIP == null)
127       {
128          java.net.InetAddress JavaDoc localInetAddress = null;
129          try
130          {
131             // get the inet address
132
localInetAddress = java.net.InetAddress.getLocalHost();
133          }
134          catch(java.net.UnknownHostException JavaDoc uhe)
135          {
136             System.err.println("ParentUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
137             // todo: find better way to get around this...
138
uhe.printStackTrace();
139             return null;
140          }
141          byte serverIP[] = localInetAddress.getAddress();
142          hexServerIP = hexFormat(getInt(serverIP), 8);
143       }
144       String JavaDoc hashcode = hexFormat(System.identityHashCode(o), 8);
145       tmpBuffer.append(hexServerIP);
146       tmpBuffer.append(hashcode);
147
148       long timeNow = System.currentTimeMillis();
149       int timeLow = (int)timeNow & 0xFFFFFFFF;
150       int node = seeder.nextInt();
151
152       StringBuffer JavaDoc guid = new StringBuffer JavaDoc(32);
153       guid.append(hexFormat(timeLow, 8));
154       guid.append(tmpBuffer.toString());
155       guid.append(hexFormat(node, 8));
156       return guid.toString();
157    }
158
159    private static int getInt(byte bytes[])
160    {
161       int i = 0;
162       int j = 24;
163       for(int k = 0; j >= 0; k++)
164       {
165          int l = bytes[k] & 0xff;
166          i += l << j;
167          j -= 8;
168       }
169       return i;
170    }
171
172    private static String JavaDoc hexFormat(int i, int j)
173    {
174       String JavaDoc s = Integer.toHexString(i);
175       return padHex(s, j) + s;
176    }
177
178    private static String JavaDoc padHex(String JavaDoc s, int i)
179    {
180       StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
181       if(s.length() < i)
182       {
183          for(int j = 0; j < i - s.length(); j++)
184          {
185             tmpBuffer.append('0');
186          }
187       }
188       return tmpBuffer.toString();
189    }
190
191 }
Popular Tags