View Javadoc
1   package ejava.examples.jndidemo.ejb;
2   
3   import java.io.PrintWriter;
4   import java.io.StringWriter;
5   
6   import javax.ejb.SessionContext;
7   import javax.naming.InitialContext;
8   import javax.naming.NamingException;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import ejava.examples.jndidemo.Scheduler;
14  import ejava.util.jndi.JNDIUtil;
15  
16  public abstract class SchedulerBase implements Scheduler {
17      protected Logger log = LoggerFactory.getLogger(getClass());
18      protected SessionContext ctx;
19      
20      protected abstract void setSessionContext(SessionContext ctx);
21      
22      public String getJndiProperty(String name) {
23          Object object=null;
24          try {
25              InitialContext jndi = new InitialContext();
26              object = jndi.lookup(name);
27          }
28          catch (NamingException ex) {
29              object = ex;
30          }        
31          log.debug("jndi: " + name + "=" + object);
32  
33          return (object != null) ? object.toString() : null;            
34      }
35  
36      public String getCtxProperty(String name) {
37          Object object=null;
38          try {
39              object = ctx.lookup(name);
40          }
41          catch (RuntimeException ex) {
42              object = ex;
43          }        
44          log.debug("enc: " + name + "=" + object);
45          return (object != null) ? object.toString() : null;            
46      }
47      
48      @Override
49      public String getEnv() {
50      	try {
51  			String result=new JNDIUtil().dump(new InitialContext(), "java:comp/env");
52  			log.debug("java:comp/env=" + result);
53  			return result;
54  		} catch (NamingException ex) {
55  			StringWriter sw = new StringWriter(); 
56  			PrintWriter pw = new PrintWriter(sw);
57  			ex.printStackTrace(pw);
58  			log.error(pw.toString());
59  			return pw.toString();
60  		}
61      }
62  }