001: package com.sun.portal.config;
002:
003: import java.util.Properties;
004: import com.sun.addressbook.*;
005:
006: /*
007: * address book required inputs:
008: *
009: * host
010: * port
011: * user
012: * aid
013: * aidPassword
014: * dirSearchBase
015: * pabSearchBase
016: * mail default domain
017: *
018: */
019: public class PopulateAddressBook {
020:
021: protected static String serviceClass = "com.sun.addressbook.ldap.LdapABStore";
022: protected String host = null;
023: protected String port = null;
024: protected String user = null;
025: protected String aid = null;
026: protected String aidPassword = null;
027: protected String dirSearchBase = null;
028: protected String pabSearchBase = null;
029: protected String cn = null;
030: protected String fn = null;
031: protected String ln = null;
032: protected String mailDomain = null;
033:
034: // known users
035: protected final static String CHRIS = "chris";
036: protected final static String CHRIS_FN = "Chris";
037: protected final static String CHRIS_LN = "Content-Editor";
038: protected final static String CHRIS_CN = CHRIS_FN + " " + CHRIS_LN;
039:
040: protected final static String ED = "ed";
041: protected final static String ED_FN = "Ed";
042: protected final static String ED_LN = "Employee";
043: protected final static String ED_CN = ED_FN + " " + ED_LN;
044:
045: protected final static String MARY = "mary";
046: protected final static String MARY_FN = "Mary";
047: protected final static String MARY_LN = "Manager";
048: protected final static String MARY_CN = MARY_FN + " " + MARY_LN;
049:
050: /*
051: * Constructor
052: */
053: public PopulateAddressBook(String args[]) {
054: host = args[0];
055: port = args[1];
056: user = args[2];
057: aid = args[3];
058: aidPassword = args[4];
059: dirSearchBase = args[5];
060: pabSearchBase = args[6];
061: mailDomain = args[7];
062: }
063:
064: /**
065: * Returns a connected Ldap ABStore object.
066: */
067: public ABStore getABStore() throws Exception {
068:
069: ABSession abSession = getABSession();
070: ABStore abStore = abSession.getABStore(serviceClass);
071: abStore.connect();
072:
073: return abStore;
074: }
075:
076: /**
077: * Returns a JABAPI ABSession object.
078: */
079: public ABSession getABSession() throws Exception {
080:
081: ABSession abSession = null;
082: Properties props = new Properties();
083:
084: props.put("ab.host", host);
085: props.put("ab.port", port);
086: props.put("ab.userName", user);
087: props.put("ab.ldap.authId", aid);
088: props.put("ab.ldap.authPw", aidPassword);
089: props.put("ab.ldap.dirSearchBase", dirSearchBase);
090: props.put("ab.ldap.pabSearchBase", pabSearchBase);
091: props.put("ab.ldap.lang", "en");
092: props.put("ab.ldap.connPoolMin", "5");
093: props.put("ab.ldap.connPoolMax", "20");
094: props.put("ab.ldap.timeout", "10000");
095:
096: abSession = abSession.getInstance(props);
097:
098: return abSession;
099: }
100:
101: /*
102: *
103: *
104: */
105: public void generate() {
106: ABStore store = null;
107: AddressBook ab = null;
108:
109: try {
110: store = getABStore();
111: ab = store.openAddressBook();
112: } catch (NullPointerException npe) {
113: // the jabapi complains that the paburi attribute is not
114: // found and throws a NPE then creates it. just ignore.
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118:
119: try {
120:
121: if (user.equals(CHRIS)) {
122: setPabForChris(ab);
123: } else if (user.equals(ED)) {
124: setPabForEd(ab);
125: } else if (user.equals(MARY)) {
126: setPabForMary(ab);
127: }
128:
129: } catch (Exception e) {
130: e.printStackTrace();
131: System.exit(-1);
132: }
133:
134: try {
135: store.disconnect();
136: } catch (Exception e) {
137: e.printStackTrace();
138: System.exit(-1);
139: }
140: }
141:
142: /*
143: * Chris' Address Book
144: *
145: */
146: public void setPabForChris(AddressBook ab) throws Exception {
147:
148: Entry entry = new Entry();
149: entry.setCn(MARY_CN);
150: entry.setFn(MARY_FN);
151: entry.setLn(MARY_LN);
152: entry.setEm(MARY + "@" + mailDomain);
153: ab.add((Element) entry);
154:
155: entry = new Entry();
156: entry.setCn(ED_CN);
157: entry.setFn(ED_FN);
158: entry.setLn(ED_LN);
159: entry.setEm(ED + "@" + mailDomain);
160: ab.add((Element) entry);
161: }
162:
163: /*
164: * Ed's Address Book
165: *
166: */
167: public void setPabForEd(AddressBook ab) throws Exception {
168:
169: Entry entry = new Entry();
170: entry.setCn(CHRIS_CN);
171: entry.setFn(CHRIS_FN);
172: entry.setLn(CHRIS_LN);
173: entry.setEm(CHRIS + "@" + mailDomain);
174: ab.add((Element) entry);
175:
176: entry = new Entry();
177: entry.setCn(MARY_CN);
178: entry.setFn(MARY_FN);
179: entry.setLn(MARY_LN);
180: entry.setEm(MARY + "@" + mailDomain);
181: ab.add((Element) entry);
182:
183: }
184:
185: /*
186: * Mary's Address Book
187: *
188: */
189: public void setPabForMary(AddressBook ab) throws Exception {
190:
191: Entry entry = new Entry();
192: entry.setCn(CHRIS_CN);
193: entry.setFn(CHRIS_FN);
194: entry.setLn(CHRIS_LN);
195: entry.setEm(CHRIS + "@" + mailDomain);
196: ab.add((Element) entry);
197:
198: entry = new Entry();
199: entry.setCn(ED_CN);
200: entry.setFn(ED_FN);
201: entry.setLn(ED_LN);
202: entry.setEm(ED + "@" + mailDomain);
203: ab.add((Element) entry);
204: }
205:
206: /*
207: *
208: *
209: */
210: private static void usage() {
211: System.out
212: .println("usage: java PopulateAddressBook <host> <port> <user> <aid> "
213: + "<aid password> <directory search base> <pab search base>"
214: + "<mail domain>");
215: }
216:
217: /*
218: *
219: *
220: */
221: public static void main(String args[]) {
222:
223: if (args.length < 8) {
224: usage();
225: System.exit(-1);
226: }
227:
228: PopulateAddressBook seed = new PopulateAddressBook(args);
229: seed.generate();
230: System.exit(1);
231: }
232:
233: }
|