Subversion Repositories DevTools

Rev

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