View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.groupware;
24  
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.NoSuchElementException;
28  
29  import de.bea.domingo.DDatabase;
30  import de.bea.domingo.DNotesException;
31  import de.bea.domingo.DNotesFactory;
32  import de.bea.domingo.DNotesMonitor;
33  import de.bea.domingo.DNotesRuntimeException;
34  import de.bea.domingo.DSession;
35  import de.bea.domingo.groupware.map.MailDatabase;
36  import de.bea.domingo.groupware.map.NamesDatabase;
37  import de.bea.domingo.map.NotesLocation;
38  
39  /***
40   * Main entry point to the groupware functionality of domingo.
41   *
42   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
43   */
44  public final class Groupware {
45  
46      /*** Reference to the associated mail database. */
47      private MailDatabase fMailDatabase;
48  
49      /*** references to the associated address books */
50      private List addressBooks;
51  
52      private DNotesFactory fFactory;
53  
54      private DSession fSession;
55  
56      private final NotesLocation fLocation;
57  
58      /***
59       * Constructor.
60       *
61       * The location is only used to create a domingo session; the mail-server
62       * and the path to the mail database is examined from the current user.
63       *
64       * <p>If the location is a IIOP location and the mail-server of the current
65       * user is not the server as specified in the location the connection will
66       * fail.</p>
67       *
68       * @param locationUri location URI of notes database
69       * @throws GroupwareException if the groupware cannot be initialized
70       */
71      public Groupware(final String locationUri) throws GroupwareException {
72          this(new NotesLocation(locationUri), null);
73      }
74  
75      /***
76       * Constructor.
77       *
78       * The location is only used to create a domingo session; the mail-server
79       * and the path to the mail database is examined from the current user.
80       *
81       * <p>If the location is a IIOP location and the mail-server of the current
82       * user is not the server as specified in the location the connection will
83       * fail.</p>
84       *
85       * @param locationUri location URI of notes database
86       * @param monitor a monitor
87       * @throws GroupwareException if the groupware cannot be initialized
88       */
89      public Groupware(final String locationUri, final DNotesMonitor monitor) throws GroupwareException {
90          this(new NotesLocation(locationUri), monitor);
91      }
92  
93      /***
94       * Constructor.
95       *
96       * The location is only used to create a domingo session; the mail-server
97       * and the path to the mail database is examined from the current user.
98       *
99       * <p>If the location is a IIOP location and the mail-server of the current
100      * user is not the server as specified in the location the connection will
101      * fail.</p>
102      *
103      * @param location location of notes database
104      * @throws GroupwareException if the groupware cannot be initialized
105      */
106     public Groupware(final NotesLocation location) throws GroupwareException {
107         this(location, null);
108     }
109 
110     /***
111      * Constructor.
112      *
113      * The location is only used to create a domingo session; the mail-server
114      * and the path to the mail database is examined from the current user.
115      *
116      * <p>If the location is a IIOP location and the mail-server of the current
117      * user is not the server as specified in the location the connection will
118      * fail.</p>
119      *
120      * @param location location of notes database
121      * @param monitor a monitor
122      * @throws GroupwareException if the groupware cannot be initialized
123      */
124     public Groupware(final NotesLocation location, final DNotesMonitor monitor) throws GroupwareException {
125         fLocation = location;
126         try {
127             fSession = getSession(location, monitor);
128         } catch (DNotesException e) {
129             throw new GroupwareException("Cannot initialize Groupware", e);
130         }
131     }
132 
133     /***
134      * Returns an implementation of the mailbox interface for the current user.
135      *
136      * @return mailbox interface
137      * @throws GroupwareException if the mailbox cannot be opened
138      */
139     public Mailbox getMailbox() throws GroupwareException {
140         if (fMailDatabase == null) {
141             String username = fSession.getCommonUserName();
142             String server = fSession.getMailServer();
143             String path = fSession.getMailDatabaseName();
144             NotesLocation mailLocation = NotesLocation.getInstance(fLocation, server, path);
145             try {
146                 fMailDatabase = new MailDatabase(fSession, mailLocation, username);
147             } catch (DNotesException e) {
148                 throw new GroupwareException("Cannot open mail database " + server + "!!" + path, e);
149             }
150         }
151         return fMailDatabase;
152     }
153 
154     /***
155      * Returns an implementation of the mailbox interface for a given user.
156      *
157      * @param username username
158      * @return mailbox interface
159      * @throws GroupwareException if the mailbox cannot be opened
160      */
161     public Mailbox getMailbox(final String username) throws GroupwareException {
162         if (fMailDatabase == null) {
163             try {
164                 DDatabase database = fSession.getMailDatabase(username);
165                 fMailDatabase = new MailDatabase(database, username);
166             } catch (DNotesException e) {
167                 throw new GroupwareException("Cannot open mail database for user " + username, e);
168             }
169         }
170         return fMailDatabase;
171     }
172 
173     /***
174      * Returns an implementation of the calendar interface.
175      *
176      * @return calendar interface
177      * @throws GroupwareException if the calendar database cannot be opened.
178      */
179     public CalendarInterface getCalendar() throws GroupwareException {
180         return (CalendarInterface) getMailbox();
181     }
182 
183     /***
184      * Returns an iterator over all available public and private addressbooks.
185      * @return Iterator
186      * @see de.bea.domingo.groupware.AddressBook
187      */
188     public Iterator getAddressBooks() {
189         if (addressBooks == null) {
190             addressBooks = fSession.getAddressBooks();
191         }
192         return new AddressBookIterator(addressBooks.iterator());
193     }
194 
195     /***
196      * Closes the groupware instance, including all internal resources of the
197      * Notes connection.
198      *
199      * @throws DNotesRuntimeException if an error occurs during disposal or
200      *              if not all objects can be disposed
201      */
202     public void close() throws DNotesRuntimeException {
203         if (fLocation.isHttp() || fLocation.isIIOP()) {
204             fFactory.disposeInstance(true);
205         }
206     }
207 
208     /***
209      * Iterates thru a collection of address books
210      */
211     private static class AddressBookIterator implements Iterator {
212 
213        private final Iterator fAllEntries;
214 
215        /***
216         * Constructor.
217         *
218         * @param allEntries collection of view-entries
219         */
220        public AddressBookIterator(final Iterator allEntries) {
221            fAllEntries = allEntries;
222        }
223 
224        /***
225         * @see java.util.Iterator#hasNext()
226         */
227        public boolean hasNext() {
228            return fAllEntries.hasNext();
229        }
230 
231        /***
232         * Returns instances of class {@link NamesDatabase}.
233         * @see java.util.Iterator#next()
234         */
235        public Object next() throws NoSuchElementException {
236             DDatabase entry = (DDatabase) fAllEntries.next();
237             NotesLocation loc = new NotesLocation(entry.getServer(), entry.getFilePath());
238             try {
239                 return new NamesDatabase(entry.getSession(), loc);
240             } catch (DNotesException e) {
241                 throw new NoSuchElementException("Error getting addresss book " + entry.getServer() + " " + entry.getFilePath());
242             }
243         }
244 
245        /***
246         * @see java.util.Iterator#remove()
247         */
248        public void remove() {
249            throw new UnsupportedOperationException();
250        }
251    }
252 
253     /***
254      * Creates and returns a new domingo session for a given location.
255      *
256      * @param location location of database.
257      * @return domingo session
258      * @throws DNotesException if the uri is invalid or the database cannot be
259      *             opened
260      */
261     private DSession getSession(final NotesLocation location, final DNotesMonitor monitor) throws DNotesException {
262         if (location.isLocal()) {
263             fFactory = DNotesFactory.getInstance(monitor);
264             return fFactory.getSession();
265         }
266         final String host = location.getHost();
267         final String user = location.getUsername();
268         final String passwd = location.getPassword();
269         if (location.isHttp()) {
270             final DNotesFactory factory = DNotesFactory.newInstance("de.bea.domingo.http.NotesHttpFactory", monitor);
271             return factory.getSession(host, user, passwd);
272         } else if (location.isIIOP()) {
273             final DNotesFactory factory = DNotesFactory.newInstance(monitor);
274             return factory.getSession(host, user, passwd);
275         }
276         throw new DNotesException("Invalid notes uri: " + location);
277     }
278 }