Subversion Repositories DevTools

Rev

Rev 4472 | Go to most recent revision | Details | Compare with Previous | 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
 
4341 manwar 6
import javax.naming.Context;
7
import javax.naming.InitialContext;
1815 mhunt 8
import javax.naming.NamingException;
4148 manwar 9
import java.util.Hashtable;
4341 manwar 10
import java.util.Properties;
1815 mhunt 11
<XDtEjbIntf:ifRemoteEjb>
12
import java.util.Date;
4341 manwar 13
import javax.rmi.PortableRemoteObject;
4472 sramanat 14
 
1815 mhunt 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"/>
4148 manwar 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="JNDI_NAME"/>
1815 mhunt 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();
4452 sramanat 83
            	jndiProperties = jp;				
1815 mhunt 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"/>
4341 manwar 107
		 Properties properties = new Properties();
4148 manwar 108
		 properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
109
         InitialContext initialContext = new InitialContext(properties);
1815 mhunt 110
         try {
4148 manwar 111
            java.lang.Object objRef = initialContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.JNDI_NAME);
1815 mhunt 112
            cachedRemoteHome = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
113
         } finally {
114
            initialContext.close();
115
         }
116
 
117
      <XDtClass:ifHasClassTag tagName="ejb:util" paramName="jndi-properties">
118
      }
119
      </XDtClass:ifHasClassTag>
120
 
121
      return cachedRemoteHome;
122
      </XDtConfig:ifConfigParamEquals>
123
      <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
124
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
4341 manwar 125
	  Properties properties = new Properties();
4148 manwar 126
	  properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
127
	  InitialContext initialContext = new InitialContext(properties);
1815 mhunt 128
      try {
4148 manwar 129
         java.lang.Object objRef = initialContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.JNDI_NAME);
1815 mhunt 130
         return (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
131
      } finally {
132
         initialContext.close();
133
      }
134
      </XDtConfig:ifConfigParamNotEquals>
135
   }
4383 manwar 136
 
137
   /**
138
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_home_param_ic"/>
139
    * @param ejbRootNamingContext for lookup
140
    * @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="EJB_JNDI_NAME"/>
141
    */
142
   public static <XDtEjbHome:homeInterface type="remote"/> getHome(Context ejbRootNamingContext) throws NamingException
143
   {
144
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
145
      if ( cachedRemoteHome != null )
146
      {
147
         return cachedRemoteHome;
148
      }
149
      </XDtConfig:ifConfigParamEquals>
150
      java.lang.Object objRef = ejbRootNamingContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.EJB_JNDI_NAME);
151
      cachedRemoteHome = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
152
 
153
      return cachedRemoteHome;    
154
   }
1815 mhunt 155
 
156
   /**
157
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_home_param_ic"/>
158
    * @param environment <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_ic_parameters"/>
4148 manwar 159
    * @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="JNDI_NAME"/>
1815 mhunt 160
    */
161
   public static <XDtEjbHome:homeInterface type="remote"/> getHome( Hashtable environment ) throws NamingException
162
   {
163
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
164
      Date now = new Date();
165
      if ( seekThreshold != null )
166
      {
167
         if ( now.before(seekThreshold) )
168
         {
169
            // Fail-fast if called prior to threshold value
170
            throw new javax.naming.CommunicationException("Communication unavailable until " + seekThreshold);
171
         }
172
         else
173
         {
174
            seekThreshold = null;
175
         }
176
      }
177
 
178
      String value = (String)environment.get("jnp.timeout");
179
      int jnpTimeout = 0;
180
      if ( value != null )
181
      {
182
         jnpTimeout = Integer.parseInt(value);
183
      }
184
 
185
      try
186
      {
187
         Seeker seeker = new Seeker(jnpTimeout,environment);
188
         return seeker.seekHome();
189
      } 
190
      catch ( javax.naming.CommunicationException x )
191
      {
192
         if ( environment.containsKey( "broker.retrytimeout" ) )
193
         {
194
            try
195
            {
196
               int retryTimeoutMillis = Integer.parseInt( environment.get("broker.retrytimeout").toString() );
197
               seekThreshold = new Date( now.getTime() + retryTimeoutMillis );
198
            }
199
            catch ( NumberFormatException nx )
200
            {
201
            }
202
         }
203
         throw x;
204
      }
205
   }
206
 
207
 
208
   </XDtEjbIntf:ifRemoteEjb>
209
 
210
   <XDtEjbIntf:ifLocalEjb>
211
   /**
212
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_localhome_def_ic"/>
4148 manwar 213
    * @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="COMP_NAME"/>
1815 mhunt 214
    */
215
   public static <XDtEjbHome:homeInterface type="local"/> getLocalHome() throws NamingException
216
   {
217
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_localhome_not_narrowed"/>
218
      <XDtConfig:ifConfigParamEquals paramName="cacheHomes" value="true">
219
      if (cachedLocalHome == null) {
220
         // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
4341 manwar 221
         Properties properties = new Properties();
4148 manwar 222
		 properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
223
         InitialContext initialContext = new InitialContext(properties);
1815 mhunt 224
         try {
4148 manwar 225
            cachedLocalHome = (<XDtEjbHome:homeInterface type="local"/>) initialContext.lookup(<XDtEjbHome:homeInterface type="local"/>.COMP_NAME);
1815 mhunt 226
         } finally {
227
            initialContext.close();
228
         }
229
      }
230
      return cachedLocalHome;
231
      </XDtConfig:ifConfigParamEquals>
232
      <XDtConfig:ifConfigParamNotEquals paramName="cacheHomes" value="true">
233
      // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_obtain_ic"/>
4341 manwar 234
      Properties properties = new Properties();
4148 manwar 235
	  properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
236
	  InitialContext initialContext = new InitialContext(properties);
1815 mhunt 237
      try {
4148 manwar 238
         return (<XDtEjbHome:homeInterface type="local"/>) initialContext.lookup(<XDtEjbHome:homeInterface type="local"/>.COMP_NAME);
1815 mhunt 239
      } finally {
240
         initialContext.close();
241
      }
242
      </XDtConfig:ifConfigParamNotEquals>
243
   }
244
   </XDtEjbIntf:ifLocalEjb>
245
 
246
   <XDtMerge:merge file="util-custom.xdt">
247
   </XDtMerge:merge>
248
 
249
   <XDtConfig:ifConfigParamEquals paramName="includeGUID" value="true">
250
   /** <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_ip"/> */
251
   private static String hexServerIP = null;
252
 
253
   // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_securerandom"/>
254
   private static final SecureRandom seeder = new SecureRandom();
255
 
256
   /**
257
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_1"/>
258
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_2"/>
259
    *
260
    * <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_usage"/>
261
    */
262
   public static final String generateGUID(Object o) {
263
       StringBuffer tmpBuffer = new StringBuffer(16);
264
       if (hexServerIP == null) {
265
           InetAddress localInetAddress = null;
266
           try {
267
               // <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_localinetaddress"/>
268
               localInetAddress = InetAddress.getLocalHost();
269
           }
270
           catch (java.net.UnknownHostException uhe) {
271
               System.err.println("<XDtClass:classOf><XDtEjbUtilObj:utilClass/></XDtClass:classOf>: <XDtI18n:getString bundle="xdoclet.modules.ejb.XDocletModulesEjbMessages" resource="util_guid_unknownhost"/>");
272
               // todo: find better way to get around this...
273
               uhe.printStackTrace();
274
               return null;
275
           }
276
           byte serverIP[] = localInetAddress.getAddress();
277
           hexServerIP = hexFormat(getInt(serverIP), 8);
278
       }
279
       String hashcode = hexFormat(System.identityHashCode(o), 8);
280
       tmpBuffer.append(hexServerIP);
281
       tmpBuffer.append(hashcode);
282
 
283
       long timeNow      = System.currentTimeMillis();
284
       int timeLow       = (int)timeNow & 0xFFFFFFFF;
285
       int node          = seeder.nextInt();
286
 
287
       StringBuffer guid = new StringBuffer(32);
288
       guid.append(hexFormat(timeLow, 8));
289
       guid.append(tmpBuffer.toString());
290
       guid.append(hexFormat(node, 8));
291
       return guid.toString();
292
   }
293
 
294
   private static int getInt(byte bytes[]) {
295
       int i = 0;
296
       int j = 24;
297
       for (int k = 0; j >= 0; k++) {
298
           int l = bytes[k] & 0xff;
299
           i += l << j;
300
           j -= 8;
301
       }
302
       return i;
303
   }
304
 
305
   private static String hexFormat(int i, int j) {
306
       String s = Integer.toHexString(i);
307
       return padHex(s, j) + s;
308
   }
309
 
310
   private static String padHex(String s, int i) {
311
       StringBuffer tmpBuffer = new StringBuffer();
312
       if (s.length() < i) {
313
           for (int j = 0; j < i - s.length(); j++) {
314
               tmpBuffer.append('0');
315
           }
316
       }
317
       return tmpBuffer.toString();
318
   }
319
   </XDtConfig:ifConfigParamEquals>
320
 
321
   <XDtMerge:merge file="lookup-custom.xdt">
322
   </XDtMerge:merge>
323
 
324
 
325
   <XDtEjbIntf:ifRemoteEjb>
326
   static class Seeker extends Thread
327
   {
328
      private int timeout = 0;
329
      private Hashtable environment;
330
      private <XDtEjbHome:homeInterface type="remote"/> home;
331
      private javax.naming.NamingException ex;
332
 
333
      Seeker( int timeout, Hashtable env )
334
      {
335
         super("JNPSeeker");
336
         setDaemon(true);
337
         this.timeout=timeout;
338
         this.environment = env;
339
      }
340
 
341
      public void run()
6298 dborse 342
	      {
343
         InitialContext initialContext =null;
1815 mhunt 344
         try
345
         {
6298 dborse 346
			Properties properties = new Properties();
347
	  		properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
348
	   		initialContext = new InitialContext(properties);
4148 manwar 349
            java.lang.Object objRef = initialContext.lookup(<XDtEjbHome:homeInterface type="remote"/>.JNDI_NAME);
1815 mhunt 350
            home = (<XDtEjbHome:homeInterface type="remote"/>) PortableRemoteObject.narrow(objRef, <XDtEjbHome:homeInterface type="remote"/>.class);
351
 
352
            synchronized( this )
353
            {
354
               this.notify();
355
            }
356
         }
357
         catch( javax.naming.NamingException x )
358
         {
359
            this.ex = x;
360
         }
361
         finally 
362
         {
363
         	try
364
         	{
365
               initialContext.close();
366
            }
367
            catch ( javax.naming.NamingException x )
368
            {
369
            }
370
         }
371
 
372
      }
373
 
374
      public <XDtEjbHome:homeInterface type="remote"/> seekHome() throws javax.naming.NamingException
375
      {
376
         try
377
         {
378
            synchronized(this)
379
            {
380
               this.start();
381
               this.wait(timeout);
382
            }
383
         }
384
         catch (InterruptedException x)
385
         {
386
            throw new javax.naming.CommunicationException("Connect attempt timed out");
387
         }
388
 
389
         // See if the connect thread exited due to an exception
390
         if( ex != null ) throw ex;
391
 
392
         if ( home == null ) throw new javax.naming.CommunicationException("Connect attempt timed out");
393
 
394
         return home;
395
      }
396
   }
397
   </XDtEjbIntf:ifRemoteEjb>
398
}