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