Advertisement
Guest User

EJB Client without properties file

a guest
Apr 11th, 2012
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package com.ibytecode.client;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.naming.Context;
  6. import javax.naming.NamingException;
  7.  
  8. import org.jboss.ejb.client.ContextSelector;
  9. import org.jboss.ejb.client.EJBClientConfiguration;
  10. import org.jboss.ejb.client.EJBClientContext;
  11. import org.jboss.ejb.client.PropertiesBasedEJBClientConfiguration;
  12. import org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector;
  13.  
  14. import com.ibytecode.business.HelloWorld;
  15. import com.ibytecode.businesslogic.HelloWorldBean;
  16. import com.ibytecode.clientutility.JNDILookupClass;
  17.  
  18. public class EJBAppClientWithoutPropertiesFile {
  19.    
  20.     public static void main(String[] args) {
  21.        
  22.         final EJBClientConfiguration clientConfiguration = new PropertiesBasedEJBClientConfiguration(createClientConfigurationProperties());
  23.         final ContextSelector<EJBClientContext> contextSelector = new ConfigBasedEJBClientContextSelector(clientConfiguration);
  24.  
  25.         // set the selector for use
  26.         EJBClientContext.setSelector(contextSelector);
  27.         HelloWorld bean = doLookup();
  28.         System.out.println(bean.sayHello()); // 4. Call business logic
  29.     }
  30.  
  31.     private static Properties createClientConfigurationProperties() {
  32.           final Properties properties = new Properties();
  33.           properties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
  34.           properties.put("remote.connections", "default");
  35.  
  36.           properties.put("remote.connection.default.host", "localhost");
  37.           properties.put("remote.connection.default.port", "4447");
  38.  
  39.           properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
  40.           // properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS","JBOSS-LOCAL-USER");
  41.           //properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
  42.  
  43.           return properties;
  44.         }
  45.    
  46.     private static HelloWorld doLookup() {
  47.         Context context = null;
  48.         HelloWorld bean = null;
  49.         try {
  50.             // 1. Obtaining Context
  51.             context = JNDILookupClass.getInitialContext();
  52.             // 2. Generate JNDI Lookup name
  53.             String lookupName = getLookupName();
  54.             // 3. Lookup and cast
  55.             bean = (HelloWorld) context.lookup(lookupName);
  56.  
  57.         } catch (NamingException e) {
  58.             e.printStackTrace();
  59.         }
  60.         return bean;
  61.     }
  62.  
  63.     private static String getLookupName() {
  64.         /*
  65.          * The app name is the EAR name of the deployed EJB without .ear suffix.
  66.          * Since we haven't deployed the application as a .ear, the app name for
  67.          * us will be an empty string
  68.          */
  69.         String appName = "";
  70.  
  71.         // The module name is the JAR name of the deployed EJB without the .jar
  72.         // suffix.
  73.         String moduleName = "HelloWorldSessionBean";
  74.  
  75.         /*
  76.          * AS7 allows each deployment to have an (optional) distinct name. This
  77.          * can be an empty string if distinct name is not specified.
  78.          */
  79.         String distinctName = "";
  80.  
  81.         // The EJB bean implementation class name
  82.         String beanName = HelloWorldBean.class.getSimpleName();
  83.  
  84.         // Fully qualified remote interface name
  85.         final String interfaceName = HelloWorld.class.getName();
  86.  
  87.         // Create a look up string name
  88.         String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
  89.                 + "/" + beanName + "!" + interfaceName;
  90.  
  91.         return name;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement