Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1815 mhunt 1
/*
2
 * <XDtI18n:getString resource="do_not_edit"/>
3
 */
4
package <XDtPackage:packageOf><XDtEjbUtilObj:utilClass/></XDtPackage:packageOf>;
5
 
6
<XDtEjbIntf:ifRemoteEjb>
7
import javax.rmi.PortableRemoteObject;
8
</XDtEjbIntf:ifRemoteEjb>
9
import javax.naming.NamingException;
10
import javax.naming.InitialContext;
11
<XDtEjbIntf:ifRemoteEjb>
12
import java.util.Date;
13
import java.util.Hashtable;
14
import java.util.Properties;
15
</XDtEjbIntf:ifRemoteEjb>
16
 
17
<XDtConfig:ifConfigParamEquals paramName="includeGUID" value="true">
18
import java.net.InetAddress;
19
import java.security.SecureRandom;
20
</XDtConfig:ifConfigParamEquals>
21
 
22
/**
23
 * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="utility_class_for" arguments="<XDtEjb:ejbName/>"/>
24
<XDtClass:classCommentTags indent="0"/> */
25
public class <XDtClass:classOf><XDtEjbUtilObj:utilClass/></XDtClass:classOf>
26
{
27
 <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
28
  <XDtEjbIntf:ifRemoteEjb>
29
   /** Date threshold - when seeking a remote access control server, fail immediately if this value is set and is in the future */
30
   private static Date seekThreshold = null;  
31
   /** <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_cached_remote"/> */
32
   private static <XDtEjbHome:homeInterface type="remote"/> cachedRemoteHome = null;
33
    <XDtClass:ifHasClassTag tagName="ejb:util" paramName="jndi-properties">
34
   /** JNDI properties (null if no configuration resource exists) */
35
   private static Hashtable jndiProperties = null;
36
    </XDtClass:ifHasClassTag>   
37
 
38
    /**
39
     * Clears the cached Remote Home.  This will be needed if a client is
40
     * re-establishing a connection to a Remote EJB after the EJB container
41
     * has restarted.
42
     */
43
    public static void clearCache()
44
    {
45
       cachedRemoteHome=null;
46
    }   
47
  </XDtEjbIntf:ifRemoteEjb>
48
  <XDtEjbIntf:ifLocalEjb>
49
 
50
   /** <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_cached_local"/> */
51
   private static <XDtEjbHome:homeInterface type="local"/> cachedLocalHome = null;
52
  </XDtEjbIntf:ifLocalEjb>
53
 </XDtConfig:ifConfigParamEquals>
54
 
55
   // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_home_lookups"/>
56
 
57
   <XDtEjbIntf:ifRemoteEjb>
58
   /**
59
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_home_def_ic"/>
60
    * @return <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="home_interface_for" arguments="<XDtEjb:ejbName/>"/> <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_lookup_using" arguments="<XDtEjbUtilObj:lookupKind/>"/>
61
    */
62
   public static <XDtEjbHome:homeInterface type="remote"/> getHome() throws NamingException
63
   {
64
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
65
      if ( cachedRemoteHome != null )
66
      {
67
         return cachedRemoteHome;
68
      }
69
      </XDtConfig:ifConfigParamEquals>
70
 
71
      <XDtClass:ifHasClassTag tagName="ejb:util" paramName="jndi-properties">
72
      if ( jndiProperties == null )
73
      {
74
         ClassLoader cl = <XDtClass:classOf><XDtEjbUtilObj:utilClass/></XDtClass:classOf>.class.getClassLoader();
75
         java.io.InputStream jpin = cl.getResourceAsStream("<XDtClass:classTagValue tagName='ejb:util' paramName='jndi-properties'/>");
76
         if ( jpin != null )
77
         {
78
            Properties jp = new Properties();
79
            try
80
            {
81
            	jp.load( jpin );
82
            	jpin.close();
83
            	jndiProperties = jp;
84
            }
85
            catch ( java.io.IOException x )
86
            {
87
            	// fail
88
            }
89
         }
90
      }
91
 
92
      if ( jndiProperties != null )
93
      {
94
         <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
95
         cachedRemoteHome = getHome( jndiProperties );
96
         </XDtConfig:ifConfigParamEquals>
97
         <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
98
         return getHome( jndiProperties );
99
         </XDtConfig:ifConfigParamNotEquals>
100
      }
101
      else
102
      {
103
      </XDtClass:ifHasClassTag>
104
 
105
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
106
         // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
107
         InitialContext initialContext = new InitialContext();
108
         try {
109
            java.lang.Object objRef = initialContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.<XDtEjbUtilObj:lookupKind/>);
110
            cachedRemoteHome = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
111
         } finally {
112
            initialContext.close();
113
         }
114
 
115
      <XDtClass:ifHasClassTag tagName="ejb:util" paramName="jndi-properties">
116
      }
117
      </XDtClass:ifHasClassTag>
118
 
119
      return cachedRemoteHome;
120
      </XDtConfig:ifConfigParamEquals>
121
      <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
122
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
123
      InitialContext initialContext = new InitialContext();
124
      try {
125
         java.lang.Object objRef = initialContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.<XDtEjbUtilObj:lookupKind/>);
126
         return (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
127
      } finally {
128
         initialContext.close();
129
      }
130
      </XDtConfig:ifConfigParamNotEquals>
131
   }
132
 
133
   /**
134
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_home_param_ic"/>
135
    * @param environment <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_ic_parameters"/>
136
    * @return <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="home_interface_for" arguments="<XDtEjb:ejbName/>"/> <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_lookup_using" arguments="<XDtEjbUtilObj:lookupKind/>"/>
137
    */
138
   public static <XDtEjbHome:homeInterface type="remote"/> getHome( Hashtable environment ) throws NamingException
139
   {
140
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
141
      Date now = new Date();
142
      if ( seekThreshold != null )
143
      {
144
         if ( now.before(seekThreshold) )
145
         {
146
            // Fail-fast if called prior to threshold value
147
            throw new javax.naming.CommunicationException("Communication unavailable until " + seekThreshold);
148
         }
149
         else
150
         {
151
            seekThreshold = null;
152
         }
153
      }
154
 
155
      String value = (String)environment.get("jnp.timeout");
156
      int jnpTimeout = 0;
157
      if ( value != null )
158
      {
159
         jnpTimeout = Integer.parseInt(value);
160
      }
161
 
162
      try
163
      {
164
         Seeker seeker = new Seeker(jnpTimeout,environment);
165
         return seeker.seekHome();
166
      } 
167
      catch ( javax.naming.CommunicationException x )
168
      {
169
         if ( environment.containsKey( "broker.retrytimeout" ) )
170
         {
171
            try
172
            {
173
               int retryTimeoutMillis = Integer.parseInt( environment.get("broker.retrytimeout").toString() );
174
               seekThreshold = new Date( now.getTime() + retryTimeoutMillis );
175
            }
176
            catch ( NumberFormatException nx )
177
            {
178
            }
179
         }
180
         throw x;
181
      }
182
   }
183
 
184
 
185
   </XDtEjbIntf:ifRemoteEjb>
186
 
187
   <XDtEjbIntf:ifLocalEjb>
188
   /**
189
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_localhome_def_ic"/>
190
    * @return <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="local_home_interface_for" arguments="<XDtEjb:ejbName/>"/> <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_lookup_using" arguments="<XDtEjbUtilObj:lookupKind/>"/>
191
    */
192
   public static <XDtEjbHome:homeInterface type="local"/> getLocalHome() throws NamingException
193
   {
194
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_localhome_not_narrowed"/>
195
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
196
      if (cachedLocalHome == null) {
197
         // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
198
         InitialContext initialContext = new InitialContext();
199
         try {
200
            cachedLocalHome = (<XDtEjbHome:homeInterface type="local"/>) initialContext.lookup(<XDtEjbHome:homeInterface type="local"/>.<XDtEjbUtilObj:lookupKind/>);
201
         } finally {
202
            initialContext.close();
203
         }
204
      }
205
      return cachedLocalHome;
206
      </XDtConfig:ifConfigParamEquals>
207
      <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
208
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
209
      InitialContext initialContext = new InitialContext();
210
      try {
211
         return (<XDtEjbHome:homeInterface type="local"/>) initialContext.lookup(<XDtEjbHome:homeInterface type="local"/>.<XDtEjbUtilObj:lookupKind/>);
212
      } finally {
213
         initialContext.close();
214
      }
215
      </XDtConfig:ifConfigParamNotEquals>
216
   }
217
   </XDtEjbIntf:ifLocalEjb>
218
 
219
   <XDtMerge:merge file="util-custom.xdt">
220
   </XDtMerge:merge>
221
 
222
   <XDtConfig:ifConfigParamEquals paramName="includeGUID" value="true">
223
   /** <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_ip"/> */
224
   private static String hexServerIP = null;
225
 
226
   // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_securerandom"/>
227
   private static final SecureRandom seeder = new SecureRandom();
228
 
229
   /**
230
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_1"/>
231
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_2"/>
232
    *
233
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_usage"/>
234
    */
235
   public static final String generateGUID(Object o) {
236
       StringBuffer tmpBuffer = new StringBuffer(16);
237
       if (hexServerIP == null) {
238
           InetAddress localInetAddress = null;
239
           try {
240
               // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_localinetaddress"/>
241
               localInetAddress = InetAddress.getLocalHost();
242
           }
243
           catch (java.net.UnknownHostException uhe) {
244
               System.err.println("<XDtClass:classOf><XDtEjbUtilObj:utilClass/></XDtClass:classOf>: <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_unknownhost"/>");
245
               // todo: find better way to get around this...
246
               uhe.printStackTrace();
247
               return null;
248
           }
249
           byte serverIP[] = localInetAddress.getAddress();
250
           hexServerIP = hexFormat(getInt(serverIP), 8);
251
       }
252
       String hashcode = hexFormat(System.identityHashCode(o), 8);
253
       tmpBuffer.append(hexServerIP);
254
       tmpBuffer.append(hashcode);
255
 
256
       long timeNow      = System.currentTimeMillis();
257
       int timeLow       = (int)timeNow & 0xFFFFFFFF;
258
       int node          = seeder.nextInt();
259
 
260
       StringBuffer guid = new StringBuffer(32);
261
       guid.append(hexFormat(timeLow, 8));
262
       guid.append(tmpBuffer.toString());
263
       guid.append(hexFormat(node, 8));
264
       return guid.toString();
265
   }
266
 
267
   private static int getInt(byte bytes[]) {
268
       int i = 0;
269
       int j = 24;
270
       for (int k = 0; j >= 0; k++) {
271
           int l = bytes[k] & 0xff;
272
           i += l << j;
273
           j -= 8;
274
       }
275
       return i;
276
   }
277
 
278
   private static String hexFormat(int i, int j) {
279
       String s = Integer.toHexString(i);
280
       return padHex(s, j) + s;
281
   }
282
 
283
   private static String padHex(String s, int i) {
284
       StringBuffer tmpBuffer = new StringBuffer();
285
       if (s.length() < i) {
286
           for (int j = 0; j < i - s.length(); j++) {
287
               tmpBuffer.append('0');
288
           }
289
       }
290
       return tmpBuffer.toString();
291
   }
292
   </XDtConfig:ifConfigParamEquals>
293
 
294
   <XDtMerge:merge file="lookup-custom.xdt">
295
   </XDtMerge:merge>
296
 
297
 
298
   <XDtEjbIntf:ifRemoteEjb>
299
   static class Seeker extends Thread
300
   {
301
      private int timeout = 0;
302
      private Hashtable environment;
303
      private <XDtEjbHome:homeInterface type="remote"/> home;
304
      private javax.naming.NamingException ex;
305
 
306
      Seeker( int timeout, Hashtable env )
307
      {
308
         super("JNPSeeker");
309
         setDaemon(true);
310
         this.timeout=timeout;
311
         this.environment = env;
312
      }
313
 
314
      public void run()
315
      {
316
         InitialContext initialContext = null;
317
         try
318
         {
319
            initialContext = new InitialContext(environment);
320
            java.lang.Object objRef = initialContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.<XDtEjbUtilObj:lookupKind/>);
321
            home = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
322
 
323
            synchronized( this )
324
            {
325
               this.notify();
326
            }
327
         }
328
         catch( javax.naming.NamingException x )
329
         {
330
            this.ex = x;
331
         }
332
         finally 
333
         {
334
         	try
335
         	{
336
               initialContext.close();
337
            }
338
            catch ( javax.naming.NamingException x )
339
            {
340
            }
341
         }
342
 
343
      }
344
 
345
      public <XDtEjbHome:homeInterface type="remote"/> seekHome() throws javax.naming.NamingException
346
      {
347
         try
348
         {
349
            synchronized(this)
350
            {
351
               this.start();
352
               this.wait(timeout);
353
            }
354
         }
355
         catch (InterruptedException x)
356
         {
357
            throw new javax.naming.CommunicationException("Connect attempt timed out");
358
         }
359
 
360
         // See if the connect thread exited due to an exception
361
         if( ex != null ) throw ex;
362
 
363
         if ( home == null ) throw new javax.naming.CommunicationException("Connect attempt timed out");
364
 
365
         return home;
366
      }
367
   }
368
   </XDtEjbIntf:ifRemoteEjb>
369
}