001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.directory;
009:
010: import java.util.*;
011: import java.io.*;
012: import javax.naming.*;
013: import javax.naming.directory.*;
014:
015: import dtw.webmail.util.*;
016:
017: public class DirectoryManager {
018:
019: //class attributes
020: private static DirectoryManager c_Self; //singleton reference
021:
022: //instance attributes
023: private Properties m_Properties;
024: private HashMap m_LookupContextPools;
025: private HashMap m_ValidationContextPools;
026: private List m_Directories;
027: private DirectoryUtil m_DirUtil;
028:
029: private DirectoryManager(Properties properties) {
030: m_Properties = properties;
031: m_LookupContextPools = new HashMap(10);
032: m_ValidationContextPools = new HashMap(10);
033: m_Directories = new ArrayList(10);
034: m_DirUtil = new DirectoryUtil(this );
035: c_Self = this ;
036: }//constructor
037:
038: private void prepare() {
039: //1. get the directory names
040: String directories = m_Properties.getProperty("directories");
041:
042: //m_Properties.list(System.out);
043: //System.out.println(directories);
044:
045: String[] dirlist = new String[0];
046: if (directories == null || directories.length() < 0) {
047: //handle
048: return;
049: } else {
050: dirlist = StringUtil.split(directories, ",");
051: }
052: //get classes for them all
053: for (int i = 0; i < dirlist.length; i++) {
054: //add dir
055: m_Directories.add(dirlist[i]);
056: //System.out.println(dirlist[i]);
057: //load url
058: String url = m_Properties.getProperty(dirlist[i] + ".url");
059: //load poolsize
060: int poolsize = Integer.parseInt(m_Properties
061: .getProperty(dirlist[i] + ".poolsize"));
062: //load username and passwd if exists
063: String user = m_Properties
064: .getProperty(dirlist[i] + ".user");
065: String passwd = m_Properties.getProperty(dirlist[i]
066: + ".password");
067: //create pool
068: ContextPool newpool = null;
069: if (user == null || passwd == null || user.length() == 0
070: || passwd.length() == 0) {
071: newpool = new ContextPool(poolsize, url);
072: } else {
073: newpool = new ContextPool(poolsize, url, user, passwd);
074: }
075:
076: //load searchname & base filter
077: newpool.setSearchName(m_Properties.getProperty(dirlist[i]
078: + ".searchname"));
079: newpool.setBaseFilter(m_Properties.getProperty(dirlist[i]
080: + ".basefilter"));
081:
082: //load type
083: String type = m_Properties
084: .getProperty(dirlist[i] + ".type");
085:
086: if (type.equals("validation")) {
087: String domain = m_Properties.getProperty(dirlist[i]
088: + ".domain");
089: //System.out.println(domain);
090: m_ValidationContextPools.put(domain, newpool);
091: } else if (type.equals("lookup")) {
092: newpool.setResultAttributes(StringUtil.split(
093: m_Properties.getProperty(dirlist[i]
094: + ".resultattributes"), ","));
095: String name = m_Properties.getProperty(dirlist[i]
096: + ".name");
097: m_LookupContextPools.put(name, newpool);
098: }
099: }//for loop end
100:
101: }//prepare
102:
103: public DirectoryUtil getDirectoryUtil() {
104: return m_DirUtil;
105: }//getDirectoryUtil
106:
107: public ContextPool getLookupContextPool(String key) {
108: return (ContextPool) m_LookupContextPools.get(key);
109: }//getLookupPool
110:
111: public ContextPool getValidationContextPool(String key) {
112: return (ContextPool) m_ValidationContextPools.get(key);
113: }//getValidationLookupPool
114:
115: /**
116: * Returns the reference to the singleton instance.
117: */
118: public static DirectoryManager getReference() {
119: return c_Self;
120: }//getReference
121:
122: /**
123: * Factory method for singleton with parameters
124: */
125: public static DirectoryManager createDirectoryManager(
126: Properties props) {
127: DirectoryManager dirmgr = new DirectoryManager(props);
128: dirmgr.prepare();
129: return dirmgr;
130: }//createDirectoryManager
131:
132: public static void main(String[] args) {
133: try {
134: FileInputStream in = new FileInputStream(args[0]);
135: Properties props = new Properties();
136: props.load(in);
137: DirectoryManager dmgr = DirectoryManager
138: .createDirectoryManager(props);
139: DirectoryUtil dirutil = dmgr.getDirectoryUtil();
140: List l = dirutil.lookupEntry("Users", args[1]);
141:
142: } catch (Exception ex) {
143: ex.printStackTrace();
144: }
145: }//main
146:
147: }//DirectoryManager
|