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