Source code: org/ahlner/Log4J2DB/beans/LogEventUtil.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 LogEvent.
16 */
17 public class LogEventUtil
18 {
19
20 // Home interface lookup methods
21
22 /**
23 * Obtain remote home interface from default initial context
24 * @return Home interface for LogEvent. Lookup using JNDI_NAME
25 */
26 public static org.ahlner.Log4J2DB.beans.LogEventHome 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.LogEventHome.JNDI_NAME);
32 return (org.ahlner.Log4J2DB.beans.LogEventHome) PortableRemoteObject.narrow(objRef, org.ahlner.Log4J2DB.beans.LogEventHome.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 LogEvent. Lookup using JNDI_NAME
42 */
43 public static org.ahlner.Log4J2DB.beans.LogEventHome 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.LogEventHome.JNDI_NAME);
49 return (org.ahlner.Log4J2DB.beans.LogEventHome) PortableRemoteObject.narrow(objRef, org.ahlner.Log4J2DB.beans.LogEventHome.class);
50 } finally {
51 initialContext.close();
52 }
53 }
54
55 /**
56 * Obtain local home interface from default initial context
57 * @return Local home interface for LogEvent. Lookup using JNDI_NAME
58 */
59 public static org.ahlner.Log4J2DB.beans.LogEventLocalHome getLocalHome() throws NamingException
60 {
61 // Local homes shouldn't be narrowed, as there is no RMI involved.
62 // Obtain initial context
63 InitialContext initialContext = new InitialContext();
64 try {
65 return (org.ahlner.Log4J2DB.beans.LogEventLocalHome) initialContext.lookup(org.ahlner.Log4J2DB.beans.LogEventLocalHome.JNDI_NAME);
66 } finally {
67 initialContext.close();
68 }
69 }
70
71 /** Cached per JVM server IP. */
72 private static String hexServerIP = null;
73
74 // initialise the secure random instance
75 private static final SecureRandom seeder = new SecureRandom();
76
77 /**
78 * A 32 byte GUID generator (Globally Unique ID). These artificial keys SHOULD <strong>NOT </strong> be seen by the user,
79 * not even touched by the DBA but with very rare exceptions, just manipulated by the database and the programs.
80 *
81 * Usage: Add an id field (type java.lang.String) to your EJB, and add setId(XXXUtil.generateGUID(this)); to the ejbCreate method.
82 */
83 public static final String generateGUID(Object o) {
84 StringBuffer tmpBuffer = new StringBuffer(16);
85 if (hexServerIP == null) {
86 InetAddress localInetAddress = null;
87 try {
88 // get the inet address
89 localInetAddress = InetAddress.getLocalHost();
90 }
91 catch (java.net.UnknownHostException uhe) {
92 System.err.println("LogEventUtil: Could not get the local IP address using InetAddress.getLocalHost()!");
93 // todo: find better way to get around this...
94 uhe.printStackTrace();
95 return null;
96 }
97 byte serverIP[] = localInetAddress.getAddress();
98 hexServerIP = hexFormat(getInt(serverIP), 8);
99 }
100 String hashcode = hexFormat(System.identityHashCode(o), 8);
101 tmpBuffer.append(hexServerIP);
102 tmpBuffer.append(hashcode);
103
104 long timeNow = System.currentTimeMillis();
105 int timeLow = (int)timeNow & 0xFFFFFFFF;
106 int node = seeder.nextInt();
107
108 StringBuffer guid = new StringBuffer(32);
109 guid.append(hexFormat(timeLow, 8));
110 guid.append(tmpBuffer.toString());
111 guid.append(hexFormat(node, 8));
112 return guid.toString();
113 }
114
115 private static int getInt(byte bytes[]) {
116 int i = 0;
117 int j = 24;
118 for (int k = 0; j >= 0; k++) {
119 int l = bytes[k] & 0xff;
120 i += l << j;
121 j -= 8;
122 }
123 return i;
124 }
125
126 private static String hexFormat(int i, int j) {
127 String s = Integer.toHexString(i);
128 return padHex(s, j) + s;
129 }
130
131 private static String padHex(String s, int i) {
132 StringBuffer tmpBuffer = new StringBuffer();
133 if (s.length() < i) {
134 for (int j = 0; j < i - s.length(); j++) {
135 tmpBuffer.append('0');
136 }
137 }
138 return tmpBuffer.toString();
139 }
140
141 }