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