1 11 12 package org.jivesoftware.messenger.vcard; 13 14 import org.dom4j.Element; 15 import org.jivesoftware.util.*; 16 17 import java.util.StringTokenizer ; 18 19 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 String className = JiveGlobals.getXMLProperty("provider.vcard.className", 34 DefaultVCardProvider.class.getName()); 35 try { 36 Class c = ClassUtils.forName(className); 37 provider = (VCardProvider)c.newInstance(); 38 } 39 catch (Exception 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 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 83 public String getVCardProperty(String username, String name) { 84 String answer = null; 85 Element vCardElement = getOrLoadVCard(username); 86 if (vCardElement != null) { 87 Element subElement = null; 89 StringTokenizer tokenizer = new StringTokenizer (name, ":"); 90 while (tokenizer.hasMoreTokens()) { 91 String 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 118 public void setVCard(String username, Element vCardElement) throws Exception { 119 if (provider.isReadOnly()) { 120 throw new UnsupportedOperationException ("VCard provider is read-only."); 121 } 122 Element oldVCard = getOrLoadVCard(username); 123 if (oldVCard != null) { 125 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 153 public void deleteVCard(String username) { 154 if (provider.isReadOnly()) { 155 throw new UnsupportedOperationException ("VCard provider is read-only."); 156 } 157 Element oldVCard = getOrLoadVCard(username); 158 if (oldVCard != null) { 159 vcardCache.remove(username); 160 provider.deleteVCard(username); 162 } 163 } 164 165 172 public Element getVCard(String username) { 173 Element vCardElement = getOrLoadVCard(username); 174 return vCardElement == null ? null : vCardElement.createCopy(); 175 } 176 177 private Element getOrLoadVCard(String 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 |