001: package com.sun.addressbook.wabp;
002:
003: import com.sun.addressbook.ABStoreException;
004: import com.sun.addressbook.ABSession;
005: import com.sun.addressbook.ABStore;
006: import com.sun.addressbook.AddressBook;
007: import com.sun.addressbook.MissingPropertiesException;
008: import com.sun.addressbook.OperationNotSupportedException;
009: import com.sun.addressbook.ABLogger;
010:
011: import com.iplanet.iabs.wabpc.WABPClientPStore;
012: import com.iplanet.iabs.iabsapi.Book;
013: import com.iplanet.iabs.iabsapi.PStoreException;
014:
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017:
018: public class WabpABStore extends ABStore {
019:
020: // wabp address book server store
021: private WABPClientPStore wabpStore = null;
022:
023: // wabp address book connection settings
024: private String host = null;
025: private String port = null;
026: private String protocol = null;
027: private String user = null;
028: private String password = null;
029: private String contextURI = null;
030: private String serverURL = null;
031: private String enableProxyAuth = null;
032: private String proxyAdminUid = null;
033: private String proxyAdminPassword = null;
034:
035: // used for debugging
036: private static final String CLASSNAME = "WabpABStore";
037:
038: // Create a Logger for this class
039: private static Logger debugLogger = ABLogger
040: .getLogger("com.sun.portal.addressbook.wabp");
041:
042: /*
043: * Initializes and assigns ABStore properties. In addition,
044: * checks for missing properties, verifies property types,
045: * and constructs the <code>serverURL</code>
046: *
047: * @throws MissingPropertiesException, ABStoreException
048: */
049: public void init(ABSession session)
050: throws MissingPropertiesException, ABStoreException {
051:
052: // store session
053: this .session = session;
054:
055: // check for missing properties in the session
056: host = session.getProperty("ab.host");
057: checkForMissingProperty(session, "ab.host");
058:
059: port = session.getProperty("ab.port");
060: checkForMissingProperty(session, "ab.port");
061: checkForIntegerProperty(port);
062:
063: protocol = session.getProperty("ab.protocol");
064: checkForMissingProperty(session, "ab.protocol");
065:
066: user = session.getProperty("ab.userName");
067: checkForMissingProperty(session, "ab.userName");
068:
069: contextURI = session.getProperty("ab.contextURI");
070: checkForMissingProperty(session, "ab.contextURI");
071:
072: enableProxyAuth = session.getProperty("ab.enableProxyAuth");
073:
074: //serverURL = protocol + "://" + host + ":" + port + "/" + contextURI;
075: if (enableProxyAuth != null
076: && enableProxyAuth.equalsIgnoreCase("true")) {
077: proxyAdminUid = session.getProperty("ab.proxyAdminUid");
078: checkForMissingProperty(session, "ab.proxyAdminUid");
079: proxyAdminPassword = session
080: .getProperty("ab.proxyAdminPassword");
081: checkForMissingProperty(session, "ab.proxyAdminPassword");
082: } else {
083: password = session.getProperty("ab.userPassword");
084: checkForMissingProperty(session, "ab.userPassword");
085: }
086: serverURL = protocol + "://" + host + ":" + port + "/"
087: + contextURI;
088: debugLogger.log(Level.FINER, "PSJA_CSAW0003", user);
089:
090: }
091:
092: /*
093: * Establishes a connection to the Address Book server.
094: *
095: * @throws ABStoreException
096: */
097: public void connect() throws ABStoreException {
098:
099: try {
100: debugLogger.log(Level.FINER, "PSJA_CSAW0004", new String[] {
101: user, serverURL });
102: if (enableProxyAuth != null
103: && enableProxyAuth.equalsIgnoreCase("true")) {
104: wabpStore = new WABPClientPStore(serverURL,
105: proxyAdminUid, proxyAdminPassword, user);
106: } else {
107: wabpStore = new WABPClientPStore(serverURL, user,
108: password);
109: }
110: } catch (PStoreException pe) {
111: throw new ABStoreException(CLASSNAME
112: + " connection failed for " + user + " at "
113: + serverURL + " : " + pe);
114: }
115: debugLogger.log(Level.FINER, "PSJA_CSAW0005", user);
116: }
117:
118: /*
119: * Terminates the Address Book Server connection.
120: *
121: * @throws ABStoreException
122: */
123: public void disconnect() throws ABStoreException {
124:
125: try {
126: debugLogger.log(Level.FINER, "PSJA_CSAW0006", user);
127: wabpStore.disconnect();
128: wabpStore = null;
129: debugLogger.log(Level.FINER, "PSJA_CSAW0007", user);
130: } catch (PStoreException pe) {
131: throw new ABStoreException(CLASSNAME
132: + " connection termination " + "failed : " + pe);
133: }
134:
135: }
136:
137: /*
138: * Checks if the connection to the store object is still valid.
139: *
140: * @throws ABStoreException
141: */
142: public boolean isConnected() throws ABStoreException {
143: // TODO. no equivalent in Address Book Server Client API
144: debugLogger.log(Level.FINER, "PSJA_CSAW0008", user);
145: if (wabpStore == null) {
146: return false;
147: }
148: return true;
149: }
150:
151: /*
152: * Retrieve a list of all the user's address book id's.
153: *
154: * @return array of user's address book ids
155: */
156: public String[] getAddressBooks() throws ABStoreException,
157: OperationNotSupportedException {
158:
159: String[] list = null;
160:
161: try {
162: Book[] books = wabpStore.listBooks(Book.BOOKTYPE_ABOOK);
163:
164: int count = books.length;
165: list = new String[count];
166:
167: for (int i = 0; i < count; i++) {
168: String id = books[i].getEntryID();
169:
170: if (id != null) {
171: list[i] = id;
172: debugLogger.log(Level.FINER, "PSJA_CSAW0009",
173: new String[] { user, id });
174: }
175: }
176:
177: } catch (PStoreException pe) {
178: throw new ABStoreException(CLASSNAME
179: + " getAddressBooks() failed: " + pe);
180: }
181:
182: return list;
183: }
184:
185: /*
186: * Retrieve the user's default address book id.
187: *
188: * @return the user's default address book id.
189: */
190: protected String getDefaultAbID() throws ABStoreException {
191: String id = null;
192:
193: try {
194: Book book = wabpStore.getDefaultBook(Book.BOOKTYPE_ABOOK);
195: id = book.getEntryID();
196: } catch (PStoreException pe) {
197: throw new ABStoreException(CLASSNAME
198: + " getDefaultAbID() failed: " + pe);
199: }
200:
201: debugLogger.log(Level.FINER, "PSJA_CSAW0010", new String[] {
202: user, id });
203: return id;
204: }
205:
206: /*
207: * Retrieve an address book
208: *
209: * @return the address book specified by abID
210: */
211: public AddressBook openAddressBook(String abID)
212: throws ABStoreException {
213: debugLogger.log(Level.FINER, "PSJA_CSAW0011", new String[] {
214: user, abID });
215: return new WabpAddressBook(this , abID);
216:
217: }
218:
219: // TODO: NEED TO IMPLEMENT
220: public void closeAddressBook(AddressBook ab)
221: throws ABStoreException {
222: debugLogger.log(Level.FINER, "PSJA_CSAW0009", new String[] {
223: user, ab.getAbID() });
224: }
225:
226: /*
227: * This method checks to see if the property is null, if it is
228: * then it throws a MissingPropertiesException
229: *
230: * @throws MissingPropertiesException
231: */
232: protected void checkForMissingProperty(ABSession session,
233: String propertyName) throws MissingPropertiesException {
234:
235: String property = session.getProperty(propertyName);
236:
237: if (property == null) {
238: String msg = CLASSNAME
239: + " setup failed, missing property: "
240: + propertyName;
241: throw new MissingPropertiesException(msg);
242: }
243: }
244:
245: /*
246: * This method checks to see if the property is an Integer, if it
247: * is not then it throws a MissingPropertiesException
248: *
249: * @throws MissingPropertiesException
250: */
251: protected void checkForIntegerProperty(String property)
252: throws MissingPropertiesException {
253:
254: try {
255: Integer.parseInt(property);
256:
257: } catch (Exception e) {
258: String msg = CLASSNAME
259: + " setup failed, property is not an integer: "
260: + property;
261: throw new MissingPropertiesException(msg);
262: }
263: }
264:
265: /*
266: * Get the WABPPClientPStore object
267: */
268: protected WABPClientPStore getWABPClientPStore() {
269: return wabpStore;
270: }
271:
272: /*
273: * Get the WabpABStore user
274: */
275: protected String getStoreUser() {
276: return user;
277: }
278:
279: /*
280: * Test method for junit
281: * @return userid string for comparison to verify login
282: */
283: String getUserString() throws PStoreException {
284: return getWABPClientPStore().getUserPrefs()
285: .getElementsByTagName("userid").item(0).getFirstChild()
286: .getNodeValue();
287: }
288: }
|