1 20 21 package org.jivesoftware.whack; 22 23 import org.xmpp.component.*; 24 import org.xmpp.packet.JID; 25 import org.xmpp.packet.Packet; 26 import org.jivesoftware.whack.container.ServerContainer; 27 28 import javax.net.SocketFactory; 29 import java.util.Hashtable ; 30 import java.util.Map ; 31 import java.util.prefs.Preferences ; 32 33 39 public class ExternalComponentManager implements ComponentManager { 40 41 45 private String host; 46 49 private int port; 50 55 private String domain; 56 61 private String defaultSecretKey; 62 66 private Map <String , String > secretKeys = new Hashtable <String ,String >(); 67 68 Preferences preferences = Preferences.userRoot(); 69 private String preferencesPrefix; 70 71 74 private Map <String , ExternalComponent> componentsByDomain = new Hashtable <String ,ExternalComponent>(); 75 78 private Map <Component, ExternalComponent> components = new Hashtable <Component,ExternalComponent>(); 79 80 private Log logger; 81 82 88 public ExternalComponentManager(String host) { 89 this(host, 5222); 90 } 91 92 99 public ExternalComponentManager(String host, int port) { 100 this.host = host; 101 this.port = port; 102 this.domain = host; 103 104 createDummyLogger(); 105 106 ComponentManagerFactory.setComponentManager(this); 108 } 109 110 119 public void setSecretKey(String subdomain, String secretKey) { 120 secretKeys.put(subdomain, secretKey); 121 } 122 123 130 public String getSecretKey(String subdomain) { 131 String secretKey = secretKeys.get(subdomain); 133 if (secretKey == null) { 134 secretKey = defaultSecretKey; 135 } 136 return secretKey; 137 } 138 139 148 public void setDefaultSecretKey(String secretKey) { 149 this.defaultSecretKey = secretKey; 150 } 151 152 public void addComponent(String subdomain, Component component) throws ComponentException { 153 if (componentsByDomain.containsKey(subdomain)) { 154 if (componentsByDomain.get(subdomain).getComponent() == component) { 155 return; 157 } 158 else { 159 throw new IllegalArgumentException ("Subdomain already in use by another component"); 160 } 161 } 162 String secretKey = secretKeys.get(subdomain); 164 if (secretKey == null) { 165 secretKey = defaultSecretKey; 166 } 167 ExternalComponent externalComponent = new ExternalComponent(component, this); 169 try { 170 componentsByDomain.put(subdomain, externalComponent); 172 components.put(component, externalComponent); 173 externalComponent.connect(host, port, SocketFactory.getDefault(), subdomain); 175 JID componentJID = new JID(null, externalComponent.getDomain(), null); 177 externalComponent.initialize(componentJID, this); 178 } catch (ComponentException e) { 179 componentsByDomain.remove(subdomain); 181 components.remove(component); 182 throw e; 184 } 185 externalComponent.start(); 187 } 188 189 public void removeComponent(String subdomain) throws ComponentException { 190 ExternalComponent externalComponent = componentsByDomain.remove(subdomain); 191 components.remove(externalComponent.getComponent()); 192 if (externalComponent != null) { 193 externalComponent.shutdown(); 194 } 195 } 196 197 public void sendPacket(Component component, Packet packet) { 198 components.get(component).send(packet); 201 } 202 203 public String getProperty(String name) { 204 return preferences.get(getPreferencesPrefix() + name, null); 205 } 206 207 public void setProperty(String name, String value) { 208 preferences.put(getPreferencesPrefix() + name, value); 209 } 210 211 private String getPreferencesPrefix() { 212 if (preferencesPrefix == null) { 213 preferencesPrefix = "whack." + domain + "."; 214 } 215 return preferencesPrefix; 216 } 217 218 225 public void setServerName(String domain) { 226 this.domain = domain; 227 } 228 229 public String getServerName() { 230 return domain; 231 } 232 233 public boolean isExternalMode() { 234 return true; 235 } 236 237 242 public String getHomeDirectory() { 243 return ServerContainer.getInstance().getHomeDirectory(); 244 } 245 246 public Log getLog() { 247 return logger; 248 } 249 250 private void createDummyLogger() { 251 this.logger = new Log() { 252 public void error(String message) { 253 System.out.println(message); 254 } 255 256 public void error(String message, Throwable throwable) { 257 System.err.println(message); 258 throwable.printStackTrace(); 259 } 260 261 public void error(Throwable throwable) { 262 throwable.printStackTrace(); 263 } 264 265 public void warn(String message) { 266 System.out.println(message); 267 } 268 269 public void warn(String message, Throwable throwable) { 270 System.out.println(message); 271 throwable.printStackTrace(); 272 } 273 274 public void warn(Throwable throwable) { 275 throwable.printStackTrace(); 276 } 277 278 public void info(String message) { 279 System.out.println(message); 280 } 281 282 public void info(String message, Throwable throwable) { 283 System.out.println(message); 284 throwable.printStackTrace(); 285 } 286 287 public void info(Throwable throwable) { 288 throwable.printStackTrace(); 289 } 290 291 public void debug(String message) { 292 System.out.println(message); 293 } 294 295 public void debug(String message, Throwable throwable) { 296 System.out.println(message); 297 throwable.printStackTrace(); 298 } 299 300 public void debug(Throwable throwable) { 301 throwable.printStackTrace(); 302 } 303 }; 304 } 305 } | Popular Tags |