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