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