KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > vcard > VCardManager


1 /**
2  * $RCSfile: VCardManager.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2005/07/20 03:20:39 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.vcard;
13
14 import org.dom4j.Element;
15 import org.jivesoftware.util.*;
16
17 import java.util.StringTokenizer JavaDoc;
18
19 /**
20  * Manages VCard information for users.
21  *
22  * @author Matt Tucker
23  */

24 public class VCardManager {
25
26     private static VCardProvider provider;
27     private static VCardManager instance = new VCardManager();
28
29     private Cache vcardCache;
30
31     static {
32         // Load a VCard provider.
33
String JavaDoc className = JiveGlobals.getXMLProperty("provider.vcard.className",
34                 DefaultVCardProvider.class.getName());
35         try {
36             Class JavaDoc c = ClassUtils.forName(className);
37             provider = (VCardProvider)c.newInstance();
38         }
39         catch (Exception JavaDoc e) {
40             Log.error("Error loading vcard provider: " + className, e);
41             provider = new DefaultVCardProvider();
42         }
43     }
44
45     public static VCardManager getInstance() {
46         return instance;
47     }
48
49     /**
50      * Returns the currently-installed VCardProvider. <b>Warning:</b> in virtually all
51      * cases the vcard provider should not be used directly. Instead, the appropriate
52      * methods in VCardManager should be called. Direct access to the vcard provider is
53      * only provided for special-case logic.
54      *
55      * @return the current VCardProvider.
56      */

57     public static VCardProvider getProvider() {
58         return provider;
59     }
60
61     private VCardManager() {
62         CacheManager.initializeCache("vcardCache", 512 * 1024);
63         vcardCache = CacheManager.getCache("vcardCache");
64     }
65
66     /**
67      * Returns the user's vCard information for a given vcard property name. If the property
68      * has no defined text then an empty string will be returned. However, if the property
69      * does not exist then a <tt>null</tt> value will be answered. Advanced user systems can
70      * use vCard information to link to user directory information or store other relevant
71      * user information.</p>
72      *
73      * Note that many elements in the vCard may have the same path so the returned value in that
74      * case will be the first found element. For instance, "ADR:STREET" may be present in
75      * many addresses of the user. Use {@link #getVCard(String)} to get the whole vCard of
76      * the user.
77      *
78      * @param username The username of the user to return his vCard property.
79      * @param name The name of the vcard property to retrieve encoded with ':' to denote
80      * the path.
81      * @return The vCard value found
82      */

83     public String JavaDoc getVCardProperty(String JavaDoc username, String JavaDoc name) {
84         String JavaDoc answer = null;
85         Element vCardElement = getOrLoadVCard(username);
86         if (vCardElement != null) {
87             // A vCard was found for this user so now look for the correct element
88
Element subElement = null;
89             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(name, ":");
90             while (tokenizer.hasMoreTokens()) {
91                 String JavaDoc tok = tokenizer.nextToken();
92                 if (subElement == null) {
93                     subElement = vCardElement.element(tok);
94                 }
95                 else {
96                     subElement = subElement.element(tok);
97                 }
98                 if (subElement == null) {
99                     break;
100                 }
101             }
102             if (subElement != null) {
103                 answer = subElement.getTextTrim();
104             }
105         }
106         return answer;
107     }
108
109     /**
110      * Sets the user's vCard information. The new vCard information will be persistent. Advanced
111      * user systems can use vCard information to link to user directory information or store
112      * other relevant user information.
113      *
114      * @param username The username of the user to set his new vCard.
115      * @param vCardElement The DOM element sent by the user as his new vcard.
116      * @throws Exception if an error occured while storing the new vCard.
117      */

118     public void setVCard(String JavaDoc username, Element vCardElement) throws Exception JavaDoc {
119         if (provider.isReadOnly()) {
120             throw new UnsupportedOperationException JavaDoc("VCard provider is read-only.");
121         }
122         Element oldVCard = getOrLoadVCard(username);
123         // See if we need to update the vCard or insert a new one.
124
if (oldVCard != null) {
125             // Only update the vCard in the database if the vCard has changed.
126
if (!oldVCard.equals(vCardElement)) {
127                 try {
128                     provider.updateVCard(username, vCardElement);
129                 }
130                 catch (NotFoundException e) {
131                     Log.warn("Tried to update a vCard that does not exist", e);
132                     provider.createVCard(username, vCardElement);
133                 }
134             }
135         }
136         else {
137             try {
138                 provider.createVCard(username, vCardElement);
139             }
140             catch (AlreadyExistsException e) {
141                 Log.warn("Tried to create a vCard when one already exist", e);
142                 provider.updateVCard(username, vCardElement);
143             }
144         }
145         vcardCache.put(username, vCardElement);
146     }
147
148     /**
149      * Deletes the user's vCard from the user account.
150      *
151      * @param username The username of the user to delete his vCard.
152      */

153     public void deleteVCard(String JavaDoc username) {
154         if (provider.isReadOnly()) {
155             throw new UnsupportedOperationException JavaDoc("VCard provider is read-only.");
156         }
157         Element oldVCard = getOrLoadVCard(username);
158         if (oldVCard != null) {
159             vcardCache.remove(username);
160             // Delete the property from the DB if it was present in memory
161
provider.deleteVCard(username);
162         }
163     }
164
165     /**
166      * Returns the vCard of a given user or null if none was defined before. Changes to the
167      * returned vCard will not be stored in the database. Use the returned vCard as a
168      * read-only vCard.
169      *
170      * @return the vCard of a given user.
171      */

172     public Element getVCard(String JavaDoc username) {
173         Element vCardElement = getOrLoadVCard(username);
174         return vCardElement == null ? null : vCardElement.createCopy();
175     }
176
177     private Element getOrLoadVCard(String JavaDoc username) {
178         Element vCardElement = (Element) vcardCache.get(username);
179         if (vCardElement == null) {
180             vCardElement = provider.loadVCard(username);
181             if (vCardElement != null) {
182                 vcardCache.put(username, vCardElement);
183             }
184         }
185         return vCardElement;
186     }
187 }
Popular Tags