001: /* JndiVariableResolver.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Dec 1 2007, Created by jeffliu
010: }}IS_NOTE
011:
012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: }}IS_RIGHT
016: */
017: package org.zkoss.zkplus.jndi;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025:
026: import org.zkoss.util.Maps;
027: import org.zkoss.util.logging.Log;
028: import org.zkoss.xel.VariableResolver;
029: import org.zkoss.xel.XelException;
030:
031: /**
032: * JndiVariableResolver, a jndi variableResolver
033: * @author Jeff
034: *
035: */
036: public class JndiVariableResolver implements VariableResolver {
037:
038: private static final Log log = Log
039: .lookup(JndiVariableResolver.class);
040:
041: private String _jndiPrepend = null;
042:
043: private Map _jndiMapping = new HashMap();
044:
045: /**
046: * This constructor take agruments to initialize JNDI names.
047: * <ul>
048: * <li>prepend - The prepended part of JNDI name</li>
049: * <li>mapping - The key-value pairs for JNDI name and its corresponding variable name</li>
050: * </ul>
051: *
052: * The variable will be resolved in following priority
053: * <ol>
054: * <li>java:comp/env</li>
055: * <li>java:comp</li>
056: * <li>java:</li>
057: * <li>The variable will be look up as a sessionBean with prepend.</li>
058: * <li>The key-value pairs which is defined by mapping</li>
059: * </ol>
060: * </p>
061: *
062: * <p>
063: * For example:
064: * </p>
065: * <p>
066: * By default, session beans will bind to JNDI in the form ejbName/remote for remote interfaces and ejbName/local in the case of local interfaces. When the EJBs are deployed in an .ear file, the default jndi binding will be prepended by the name of the .ear file.
067: * As a result, if the ear file name is foo.ear, prepend is: foo
068: * </p>
069: * </br>
070: * <p>
071: * If you define your own jdni binding, the string should be in key-value pairs format as</br>
072: * "a=custom/MySession,b=custom/MySession2,emf=java:/EntityManagerFactory"
073: * </p>
074: *
075: * @param prepend prepended part of JNDI name
076: * @param mapping key-value pairs for JNDI name and its corresponding variable name
077: */
078: public JndiVariableResolver(String prepend, String mapping) {
079: _jndiMapping = new HashMap();
080: Maps.parse(_jndiMapping, (String) mapping, ',', '=');
081: _jndiPrepend = prepend;
082: }
083:
084: public JndiVariableResolver() {
085: //Do Nothing
086: }
087:
088: /**
089: * Get object from JNDI binding
090: * @param var JNDI binding name
091: * @return bean of context
092: */
093: public Object resolveVariable(String var) throws XelException {
094: Object variable = null;
095: /*
096: * First, find the variable, var in JNDI key-value map. If
097: * not found, look for the variable as a sessionBean
098: */
099:
100: variable = jndiLookup("java:comp/env/" + var);
101:
102: if (variable == null) {
103: variable = jndiLookup("java:comp/" + var);
104: }
105: if (variable == null) {
106: variable = jndiLookup("java:/" + var);
107: }
108: if (variable == null) {
109: variable = defaultBean(var);
110: }
111: if (!_jndiMapping.isEmpty() && variable == null) {
112: Object jndiPattern = _jndiMapping.get(var);
113: if (jndiPattern != null) {
114: variable = jndiLookup(jndiPattern.toString());
115: }
116: }
117: return variable;
118: }
119:
120: private Object defaultBean(String name) {
121: Object variable = null;
122:
123: variable = jndiLookup(_jndiPrepend + "/" + name + "/local");
124: //If not found in local, lookup remote
125: if (variable == null) {
126: variable = jndiLookup(_jndiPrepend + "/" + name + "/remote");
127: }
128: return variable;
129: }
130:
131: private Object jndiLookup(String jndiPattern) {
132: Object obj = null;
133: try {
134: Context ctx = new InitialContext();
135: obj = ctx.lookup(jndiPattern);
136: } catch (NamingException ex) {
137: //Not found, logging
138: if (log.debugable()) {
139: log.debug("JNDI binding not found: " + ex);
140: }
141: }
142: return obj;
143: }
144:
145: }
|