001: package com.jat.integration.context;
002:
003: import java.util.Hashtable;
004: import javax.naming.NamingEnumeration;
005: import javax.naming.NamingException;
006: import javax.naming.directory.Attribute;
007: import javax.naming.directory.Attributes;
008: import javax.naming.directory.DirContext;
009: import javax.naming.directory.InitialDirContext;
010: import javax.naming.directory.SearchControls;
011: import javax.naming.directory.SearchResult;
012:
013: import com.jat.business.BusinessObjectProperties;
014: import com.jat.business.BusinessObjectPropertyList;
015: import com.jat.business.MultiBusinessObjectProperties;
016: import com.jat.core.config.Config;
017: import com.jat.core.log.LogManager;
018: import com.jat.integration.GenericDataSource;
019: import com.jat.integration.GenericOperationDefinition;
020: import com.jat.integration.IntegrationException;
021: import java.util.Enumeration;
022:
023: /**
024: * <p>Title: JAT</p>
025: * <p>Description: This class implements a context access (see javax.naming.directory.DirContext).
026: * For example is possible to access to a LDAP context.
027: * </p>
028: * <p><b>Configuration:</b><br/>
029: * In the data source name section put the following parameters:
030: * <ul>
031: * <li>the list of initial (<b>init</b>) properties (such as context factory, URL, etc.):
032: * <ul>
033: * <li><b>name</b> the name of the property</li>
034: * <li><b>value</b> the value of the property</li>
035: * </ul>
036: * </li>
037: * <li><b>base_object_name</b> the base object name used for search objects</li>
038: * <li>list of operations (<b>operation</b>):
039: * <ul>
040: * <li><b>name</b> the name of the operation</li>
041: * <li><b>value</b> the value of the operation
042: * (use symbol '?' for parameters coming from {@link com.jat.business.BusinessObjectProperties} when a query is called (see {@link com.jat.integration.DataSource}))</li>
043: * <li>list of fields (<u>optional</u>) for operation parameters
044: * <br/><i>Note that the number of fields must be exactly as the number of parameters (symbol '?') of the value of operation<i>
045: * </li>
046: </li>
047: * </ul>
048: * </li>
049: * </ul>
050: * <i>Example of LDAP access:</i>
051: * <blockquote>
052: * [<i>ldapDataSourceName</i>]<br/>
053: * init1.name = java.naming.factory.initial<br/>
054: * init1.value = com.sun.jndi.ldap.LdapCtxFactory<br/>
055: * init2.name = java.naming.provider.url<br/>
056: * init2.value = ldap://<i>ldapServer</i>:<i>ldapPort</i>/<i>ldapPath</i><br/>
057: * base_object_name = o=<i>organizationName</i>, c=<i>US</i><br/>
058: * test = true<br/>
059: * operation1.name = list<br/>
060: * operation1.value = sn=List<br/>
061: * operation2.name = listBySn<br/>
062: * operation2.value = sn=?<br/>
063: * operation2.field1 = sn<br/>
064: * operation3.name = listByMail<br/>
065: * operation3.value = mail=?<br/>
066: * operation3.field1 = mail<br/>
067: * </blockquote>
068: * </p>
069: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
070: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
071: * @author stf
072: * @version 1.2
073: * @since 1.2
074: * @see com.jat.integration.DataSource
075: */
076:
077: public class ContextDataSource extends GenericDataSource {
078:
079: public void initDataSource() throws IntegrationException {
080: LogManager.sendDebug(this .getClass().getName()
081: + "::initDataSource: start");
082: this .env = new Hashtable();
083: try {
084: for (int i = 1; i <= Config.getCurrent().getSubKeys(
085: this .getName(), "init").size(); i++) {
086: String contextName = Config.getCurrent().getValue(
087: this .getName(), "init" + i + ".name");
088: String contextValue = Config.getCurrent().getValue(
089: this .getName(), "init" + i + ".value");
090: env.put(contextName, contextValue);
091: LogManager.sendDebug(this .getClass().getName()
092: + "::init " + contextName + " = "
093: + contextValue);
094: }
095: testConnection();
096: try {
097: this .baseObjectName = Config.getCurrent().getValue(
098: this .getName(), "base_object_name");
099: LogManager
100: .sendDebug(this .getClass().getName()
101: + "::init base_object_name = "
102: + baseObjectName);
103: } catch (Exception ex1) {
104: LogManager
105: .sendWarning(this .getClass().getName()
106: + "::init base_object_name not found. using default: "
107: + baseObjectName);
108: }
109: } catch (Exception ex) {
110: throw new IntegrationException(this .getClass().getName()
111: + "::initDataSource: exception: " + ex);
112: }
113: LogManager.sendDebug(this .getClass().getName()
114: + "::initDataSource: start");
115: }
116:
117: protected Hashtable putInitProperties(Hashtable hash)
118: throws Exception {
119: hash.put("base_object_name", this .baseObjectName);
120: hash.put("test", "" + this .test);
121: hash.putAll(this .env);
122: return hash;
123: }
124:
125: protected void testConnection() throws IntegrationException {
126: try {
127: test = Config.getCurrent().getValue(this .getName(), "test")
128: .equalsIgnoreCase("true");
129: } catch (Exception ignored) {
130: }
131: if (!test) {
132: LogManager.sendDebug(this .getClass().getName()
133: + "::testConnection: test skipped");
134: return;
135: }
136: DirContext ctx = null;
137: try {
138: ctx = new InitialDirContext(env);
139: LogManager.sendDebug(this .getClass().getName()
140: + "::testConnection: test successfully done");
141: } catch (NamingException ex) {
142: throw new IntegrationException(this .getClass().getName()
143: + "::testConnection: exception: " + ex);
144: } finally {
145: try {
146: ctx.close();
147: } catch (Exception ignored) {
148: }
149: }
150: }
151:
152: public BusinessObjectPropertyList getData(String operation,
153: BusinessObjectProperties parameters)
154: throws IntegrationException {
155: DirContext ctx = null;
156: BusinessObjectPropertyList ret = new BusinessObjectPropertyList();
157: try {
158: String query = this .getOperation(operation).getValue(
159: parameters);
160: ctx = new InitialDirContext(env);
161: SearchControls sc = new SearchControls();
162: sc.setSearchScope(searchControlScope);
163: NamingEnumeration nn = ctx.search(this .baseObjectName,
164: query, sc);
165: while (nn.hasMore()) {
166: SearchResult sr = (SearchResult) nn.next();
167: BusinessObjectProperties data = new BusinessObjectProperties();
168: Attributes attr = sr.getAttributes();
169: NamingEnumeration ids = attr.getIDs();
170: while (ids.hasMore()) {
171: String id = (String) ids.next();
172: Attribute a = attr.get(id);
173: data.put(id, a.get());
174: }
175: ret.addElement(data);
176: }
177: return ret;
178: } catch (Exception ex) {
179: throw new IntegrationException(this .getClass().getName()
180: + "::getData: exception: " + ex);
181: } finally {
182: try {
183: ctx.close();
184: } catch (Exception ignored) {
185: }
186: }
187: }
188:
189: public BusinessObjectProperties execute(String name,
190: BusinessObjectProperties parameters)
191: throws IntegrationException {
192: throw new IntegrationException(this .getClass().getName()
193: + "::execute: method not implemented");
194: }
195:
196: public BusinessObjectPropertyList execute(
197: MultiBusinessObjectProperties multi)
198: throws IntegrationException {
199: throw new IntegrationException(this .getClass().getName()
200: + "::execute: method not implemented");
201: }
202:
203: public GenericOperationDefinition getEmptyOperationDefinition() {
204: return new OperationDefinition();
205: }
206:
207: public int searchControlScope = SearchControls.SUBTREE_SCOPE;
208: private Hashtable env;
209: private boolean test = false;
210: private String baseObjectName = "";
211: }
|