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