KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > sun > sunone > ejb > SunONETagsHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.sun.sunone.ejb;
6
7 import java.net.InetAddress JavaDoc;
8 import java.security.SecureRandom JavaDoc;
9 import java.util.*;
10
11 import xjavadoc.*;
12
13 import xdoclet.XDocletException;
14 import xdoclet.XDocletMessages;
15 import xdoclet.tagshandler.AbstractProgramElementTagsHandler;
16 import xdoclet.tagshandler.MethodTagsHandler;
17 import xdoclet.util.Translator;
18
19 /**
20  * @author <a HREF="mailto:stevensa@users.sourceforge.net">Andrew Stevens</a>
21  * @created 23 February, 2003
22  * @xdoclet.taghandler namespace="SunONE"
23  * @version $Revision: 1.1 $
24  */

25 public class SunONETagsHandler extends AbstractProgramElementTagsHandler
26 {
27
28     // initialise the secure random instance
29
private final static SecureRandom JavaDoc seeder = new SecureRandom JavaDoc();
30
31     /**
32      * Cached per JVM server IP.
33      */

34     private static String JavaDoc hexServerIP = null;
35     /**
36      * For use in extracting method names.
37      */

38     protected MethodTagsHandler handler = new MethodTagsHandler();
39
40     /**
41      * Collection of attributes. XXX: Does this need to be synchronized?
42      */

43     protected Map attributes = Collections.synchronizedMap(new HashMap());
44
45     /**
46      * For looping through indexed tags.
47      */

48     protected int index = 0;
49
50     /**
51      * A 32 byte GUID generator (Globally Unique ID). I couldn't find any documentation for how Sun's own utilities
52      * generate them, so I've copied the method used in the generated Util classes. Hopefully that's good enough.
53      *
54      * @return The name of current EJB bean.
55      * @exception XDocletException
56      * @doc.tag type="content"
57      */

58     public static String JavaDoc generateGUID() throws XDocletException
59     {
60         return generateGUID(getCurrentClass());
61     }
62
63     /**
64      * A 32 byte GUID generator (Globally Unique ID). I couldn't find any documentation for how Sun's own utilities
65      * generate them, so I've copied the method used in the generated Util classes. Hopefully that's good enough.
66      *
67      * @param o
68      * @return The name of current EJB bean.
69      * @exception XDocletException
70      */

71     public static String JavaDoc generateGUID(Object JavaDoc o) throws XDocletException
72     {
73         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc(16);
74
75         if (hexServerIP == null) {
76             InetAddress JavaDoc localInetAddress = null;
77
78             try {
79                 // get the inet address
80
localInetAddress = InetAddress.getLocalHost();
81             }
82             catch (java.net.UnknownHostException JavaDoc uhe) {
83                 System.err.println("Could not get the local IP address using InetAddress.getLocalHost()!");
84                 // todo: find better way to get around this...
85
uhe.printStackTrace();
86                 return null;
87             }
88
89             byte serverIP[] = localInetAddress.getAddress();
90
91             hexServerIP = hexFormat(getInt(serverIP), 8);
92         }
93
94         String JavaDoc hashcode = hexFormat(System.identityHashCode(o), 8);
95
96         tmpBuffer.append(hexServerIP);
97         tmpBuffer.append(hashcode);
98
99         long timeNow = System.currentTimeMillis();
100         int timeLow = (int) timeNow & 0xFFFFFFFF;
101         int node = seeder.nextInt();
102
103         StringBuffer JavaDoc guid = new StringBuffer JavaDoc(32);
104
105         guid.append(hexFormat(timeLow, 8));
106         guid.append(tmpBuffer.toString());
107         guid.append(hexFormat(node, 8));
108         return "{" + guid.substring(0, 8) + "-" + guid.substring(8, 12) + "-"
109             + guid.substring(12, 16) + "-" + guid.substring(16, 20) + "-"
110             + guid.substring(20, 32) + "}";
111     }
112
113     private static int getInt(byte bytes[])
114     {
115         int i = 0;
116         int j = 24;
117
118         for (int k = 0; j >= 0; k++) {
119             int l = bytes[k] & 0xff;
120
121             i += l << j;
122             j -= 8;
123         }
124         return i;
125     }
126
127     private static String JavaDoc hexFormat(int i, int j)
128     {
129         String JavaDoc s = Integer.toHexString(i);
130
131         return padHex(s, j) + s;
132     }
133
134     private static String JavaDoc padHex(String JavaDoc s, int i)
135     {
136         StringBuffer JavaDoc tmpBuffer = new StringBuffer JavaDoc();
137
138         if (s.length() < i) {
139             for (int j = 0; j < i - s.length(); j++) {
140                 tmpBuffer.append('0');
141             }
142         }
143         return tmpBuffer.toString();
144     }
145
146 }
147
Popular Tags