001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.batchcascadedelete.ejb;
023:
024: public class GrandchildUtil {
025: /**
026: * Cached remote home (EJBHome). Uses lazy loading to obtain its value (loaded by getHome() methods).
027: */
028: private static GrandchildHome cachedRemoteHome = null;
029:
030: /**
031: * Cached local home (EJBLocalHome). Uses lazy loading to obtain its value (loaded by getLocalHome() methods).
032: */
033: private static GrandchildLocalHome cachedLocalHome = null;
034:
035: // Home interface lookup methods
036:
037: /**
038: * Obtain remote home interface from default initial context
039: *
040: * @return Home interface for Grandchild. Lookup using JNDI_NAME
041: */
042: public static GrandchildHome getHome()
043: throws javax.naming.NamingException {
044: if (cachedRemoteHome == null) {
045: // Obtain initial context
046: javax.naming.InitialContext initialContext = new javax.naming.InitialContext();
047: try {
048: java.lang.Object objRef = initialContext
049: .lookup(GrandchildHome.JNDI_NAME);
050: cachedRemoteHome = (GrandchildHome) javax.rmi.PortableRemoteObject
051: .narrow(objRef, GrandchildHome.class);
052: } finally {
053: initialContext.close();
054: }
055: }
056: return cachedRemoteHome;
057: }
058:
059: /**
060: * Obtain remote home interface from parameterised initial context
061: *
062: * @param environment Parameters to use for creating initial context
063: * @return Home interface for Grandchild. Lookup using JNDI_NAME
064: */
065: public static GrandchildHome getHome(java.util.Hashtable environment)
066: throws javax.naming.NamingException {
067: // Obtain initial context
068: javax.naming.InitialContext initialContext = new javax.naming.InitialContext(
069: environment);
070: try {
071: java.lang.Object objRef = initialContext
072: .lookup(GrandchildHome.JNDI_NAME);
073: return (GrandchildHome) javax.rmi.PortableRemoteObject
074: .narrow(objRef, GrandchildHome.class);
075: } finally {
076: initialContext.close();
077: }
078: }
079:
080: /**
081: * Obtain local home interface from default initial context
082: *
083: * @return Local home interface for Grandchild. Lookup using JNDI_NAME
084: */
085: public static GrandchildLocalHome getLocalHome()
086: throws javax.naming.NamingException {
087: // Local homes shouldn't be narrowed, as there is no RMI involved.
088: if (cachedLocalHome == null) {
089: // Obtain initial context
090: javax.naming.InitialContext initialContext = new javax.naming.InitialContext();
091: try {
092: cachedLocalHome = (GrandchildLocalHome) initialContext
093: .lookup(GrandchildLocalHome.JNDI_NAME);
094: } finally {
095: initialContext.close();
096: }
097: }
098: return cachedLocalHome;
099: }
100:
101: /**
102: * Cached per JVM server IP.
103: */
104: private static String hexServerIP = null;
105:
106: // initialise the secure random instance
107: private static final java.security.SecureRandom seeder = new java.security.SecureRandom();
108:
109: /**
110: * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
111: * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
112: *
113: * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
114: */
115: public static final String generateGUID(Object o) {
116: StringBuffer tmpBuffer = new StringBuffer(16);
117: if (hexServerIP == null) {
118: java.net.InetAddress localInetAddress = null;
119: try {
120: // get the inet address
121: localInetAddress = java.net.InetAddress.getLocalHost();
122: } catch (java.net.UnknownHostException uhe) {
123: System.err
124: .println("GrandchildUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
125: // todo: find better way to get around this...
126: uhe.printStackTrace();
127: return null;
128: }
129: byte serverIP[] = localInetAddress.getAddress();
130: hexServerIP = hexFormat(getInt(serverIP), 8);
131: }
132: String hashcode = hexFormat(System.identityHashCode(o), 8);
133: tmpBuffer.append(hexServerIP);
134: tmpBuffer.append(hashcode);
135:
136: long timeNow = System.currentTimeMillis();
137: int timeLow = (int) timeNow & 0xFFFFFFFF;
138: int node = seeder.nextInt();
139:
140: StringBuffer guid = new StringBuffer(32);
141: guid.append(hexFormat(timeLow, 8));
142: guid.append(tmpBuffer.toString());
143: guid.append(hexFormat(node, 8));
144: return guid.toString();
145: }
146:
147: private static int getInt(byte bytes[]) {
148: int i = 0;
149: int j = 24;
150: for (int k = 0; j >= 0; k++) {
151: int l = bytes[k] & 0xff;
152: i += l << j;
153: j -= 8;
154: }
155: return i;
156: }
157:
158: private static String hexFormat(int i, int j) {
159: String s = Integer.toHexString(i);
160: return padHex(s, j) + s;
161: }
162:
163: private static String padHex(String s, int i) {
164: StringBuffer tmpBuffer = new StringBuffer();
165: if (s.length() < i) {
166: for (int j = 0; j < i - s.length(); j++) {
167: tmpBuffer.append('0');
168: }
169: }
170: return tmpBuffer.toString();
171: }
172:
173: }
|