Projects tigase _server tigase-pubsub Commits 592c1cc8
ctrl k
  • src/main/java/tigase/pubsub/AbstractNodeConfig.java
    ■ ■ ■ ■ ■
    skipped 43 lines
    44 44   
    45 45   protected final Form form = new Form("form", null, null);
    46 46   
    47  - public AbstractNodeConfig() {
     47 + private final String nodeName;
     48 + 
     49 + public AbstractNodeConfig(final String nodeName) {
     50 + this.nodeName = nodeName;
    48 51   init();
    49 52   }
    50 53   
    51  - public AbstractNodeConfig(final AbstractNodeConfig config) {
     54 + public AbstractNodeConfig(final String nodeName, final AbstractNodeConfig config) {
     55 + this.nodeName = nodeName;
    52 56   init();
    53 57   copyFrom(config);
    54 58   }
    skipped 56 lines
    111 115   }
    112 116   }
    113 117   
     118 + public String getNodeName() {
     119 + return nodeName;
     120 + }
     121 + 
    114 122   public NodeType getNodeType() {
    115 123   String tmp = form.getAsString("pubsub#node_type");
    116 124   if (tmp == null) {
    skipped 12 lines
    129 137   }
    130 138   
    131 139   protected void init() {
    132  - // blacklist.add("pubsub#children");
    133 140   blacklist.add("pubsub#node_type");
    134 141   
    135 142   form.addField(Field.fieldListSingle(PUBSUB + "node_type", null, null, null, new String[] { NodeType.leaf.name(),
    skipped 127 lines
  • src/main/java/tigase/pubsub/CollectionNodeConfig.java
    ■ ■ ■ ■ ■ ■
    skipped 20 lines
    21 21   */
    22 22  package tigase.pubsub;
    23 23   
     24 +import java.util.Arrays;
     25 +import java.util.HashSet;
     26 +import java.util.Set;
     27 + 
     28 +import tigase.form.Field;
     29 + 
    24 30  public class CollectionNodeConfig extends AbstractNodeConfig {
     31 + 
     32 + public CollectionNodeConfig(String nodeName) {
     33 + super(nodeName);
     34 + }
     35 + 
     36 + public void addChildren(String... children) {
     37 + Set<String> list = new HashSet<String>();
     38 + String[] cur = getChildren();
     39 + if (cur != null)
     40 + list.addAll(Arrays.asList(cur));
     41 + for (String kid : children) {
     42 + list.add(kid);
     43 + }
     44 + setChildren(list.toArray(new String[] {}));
     45 + }
     46 + 
     47 + @Override
     48 + protected void init() {
     49 + super.init();
     50 + Field f = Field.fieldTextMulti("pubsub#children", "", null);
     51 + add(f);
     52 + }
     53 + 
     54 + public void removeChildren(String nodeName) {
     55 + Set<String> list = new HashSet<String>();
     56 + String[] cur = getChildren();
     57 + if (cur != null)
     58 + list.addAll(Arrays.asList(cur));
     59 + list.remove(nodeName);
     60 + setChildren(list.toArray(new String[] {}));
     61 + }
     62 + 
     63 + public void setChildren(String[] children) {
     64 + setValue("pubsub#children", children);
     65 + }
    25 66   
    26 67  }
    27 68   
  • src/main/java/tigase/pubsub/LeafNodeConfig.java
    ■ ■ ■ ■ ■ ■
    skipped 20 lines
    21 21   */
    22 22  package tigase.pubsub;
    23 23   
    24  - 
    25 24  public class LeafNodeConfig extends AbstractNodeConfig {
    26 25   
    27  - public LeafNodeConfig() {
    28  - super();
     26 + public LeafNodeConfig(final String nodeName) {
     27 + super(nodeName);
    29 28   }
    30 29   
    31  - public LeafNodeConfig(AbstractNodeConfig config) {
    32  - super(config);
     30 + public LeafNodeConfig(final String nodeName, AbstractNodeConfig config) {
     31 + super(nodeName, config);
    33 32   }
    34 33   
    35 34   public Integer getMaxItems() {
    skipped 16 lines
  • src/main/java/tigase/pubsub/PubSubComponent.java
    ■ ■ ■ ■ ■ ■
    skipped 55 lines
    56 56  import tigase.pubsub.modules.UnsubscribeNodeModule;
    57 57  import tigase.pubsub.modules.XmppPingModule;
    58 58  import tigase.pubsub.modules.XsltTool;
     59 +import tigase.pubsub.modules.commands.DatabaseCleanCommand;
    59 60  import tigase.pubsub.modules.commands.DefaultConfigCommand;
     61 +import tigase.pubsub.modules.commands.RebuildDatabaseCommand;
    60 62  import tigase.pubsub.repository.IPubSubRepository;
    61 63  import tigase.pubsub.repository.PubSubDAO;
    62  -import tigase.pubsub.repository.inmemory.InMemoryPubSubRepository;
    63 64  import tigase.pubsub.repository.stateless.StatelessPubSubRepository;
    64 65  import tigase.server.AbstractMessageReceiver;
    65 66  import tigase.server.DisableDisco;
    skipped 21 lines
    87 88   protected DefaultConfigModule defaultConfigModule;
    88 89  
    89 90   protected LeafNodeConfig defaultNodeConfig;
     91 +
     92 + private PubSubDAO directPubSubRepository;
    90 93  
    91 94   public String[] HOSTNAMES_PROP_VAL = { "localhost", "hostname" };
    92 95  
    skipped 177 lines
    270 273   } catch (Exception e) {
    271 274   log.log(Level.SEVERE, "Reading default config error", e);
    272 275   }
    273  - }
     276 + };
    274 277  
    275 278   @Override
    276 279   public void processPacket(final Packet packet) {
    skipped 18 lines
    295 298   log.throwing("PubSub Service", "processPacket (sending internal-server-error)", e);
    296 299   }
    297 300   }
    298  - };
     301 + }
    299 302  
    300 303   public <T extends Module> T registerModule(final T module) {
    301 304   log.config("Register PubSub plugin: " + module.getClass().getCanonicalName());
    skipped 40 lines
    342 345  
    343 346   serviceEntity.addFeatures("http://jabber.org/protocol/pubsub");
    344 347  
    345  -
    346 348   this.config.setAdmins((String[]) props.get(ADMINS_KEY));
    347 349  
    348 350   this.config.setServiceName("tigase-pubsub");
    skipped 1 lines
    350 352   try {
    351 353   String cls_name = (String) props.get(PUBSUB_REPO_CLASS_PROP_KEY);
    352 354   String res_uri = (String) props.get(PUBSUB_REPO_URL_PROP_KEY);
    353  -// if (!res_uri.contains("autoCreateUser=true")) {
    354  -// res_uri += "&autoCreateUser=true";
    355  -// }
     355 + // if (!res_uri.contains("autoCreateUser=true")) {
     356 + // res_uri += "&autoCreateUser=true";
     357 + // }
    356 358  
    357 359   this.userRepository = RepositoryFactory.getUserRepository("pubsub", cls_name, res_uri, null);
    358 360   userRepository.initRepository(res_uri, null);
    359 361  
    360  - PubSubDAO directPubSubRepository = new PubSubDAO(userRepository, this.config);
     362 + directPubSubRepository = new PubSubDAO(userRepository, this.config);
    361 363  
    362 364   this.pubsubRepository = createPubSubRepository(directPubSubRepository);
    363  - this.defaultNodeConfig = new LeafNodeConfig();
     365 + this.defaultNodeConfig = new LeafNodeConfig("default");
    364 366   this.defaultNodeConfig.read(userRepository, config, PubSubComponent.DEFAULT_LEAF_NODE_CONFIG_KEY);
    365 367   this.defaultNodeConfig.write(userRepository, config, PubSubComponent.DEFAULT_LEAF_NODE_CONFIG_KEY);
    366 368  
    skipped 7 lines
    374 376   init();
    375 377  
    376 378   final DefaultConfigCommand configCommand = new DefaultConfigCommand(this.config, this.userRepository);
     379 + final DatabaseCleanCommand databaseCleanCommand = new DatabaseCleanCommand(this.config, this.directPubSubRepository);
    377 380   configCommand.addListener(this);
     381 +
     382 + this.adHocCommandsModule.register(new RebuildDatabaseCommand(this.config, this.directPubSubRepository));
    378 383   this.adHocCommandsModule.register(configCommand);
     384 + this.adHocCommandsModule.register(databaseCleanCommand);
    379 385  
    380 386   StringBuilder sb = new StringBuilder();
    381 387  
    skipped 9 lines
    391 397   }
    392 398   log.config("Supported features: " + sb.toString());
    393 399   }
    394  -
    395 400  }
    396 401   
  • src/main/java/tigase/pubsub/modules/DiscoverItemsModule.java
    ■ ■ ■ ■ ■ ■
    skipped 67 lines
    68 68   Element resultQuery = new Element("query", new String[] { "xmlns" },
    69 69   new String[] { "http://jabber.org/protocol/disco#items" });
    70 70   resultIq.addChild(resultQuery);
    71  - log.finest("Asking about Items of node " + nodeName);
    72  - AbstractNodeConfig nodeConfig = nodeName == null ? null : repository.getNodeConfig(nodeName);
    73  - if (nodeName == null || (nodeConfig != null && nodeConfig.getNodeType() == NodeType.collection)) {
    74  - String parentName;
    75  - if (nodeName == null) {
    76  - parentName = "";
    77  - } else {
    78  - parentName = nodeName;
    79  - }
    80  - String[] nodes = this.repository.getNodesList();
    81  - if (nodes != null) {
    82  - for (String node : nodes) {
    83  - AbstractNodeConfig childNodeConfig = this.repository.getNodeConfig(node);
    84  - if (childNodeConfig != null) {
    85  - boolean allowed = (senderJid == null || childNodeConfig == null) ? true : Utils.isAllowedDomain(
    86  - senderJid, childNodeConfig.getDomains());
    87  - String collection = childNodeConfig.getCollection();
    88  - if (allowed) {
    89  - String name = childNodeConfig.getTitle();
    90  - name = name == null || name.length() == 0 ? node : name;
    91  - Element item = new Element("item", new String[] { "jid", "node", "name" }, new String[] {
    92  - element.getAttribute("to"), node, name });
    93 71   
    94  - if (parentName.equals(collection))
    95  - resultQuery.addChild(item);
    96  - } else {
    97  - log.fine("User " + senderJid + " not allowed to see node '" + node + "'");
    98  - }
    99  - }
    100  - }
    101  - }
    102  - } else if ("http://jabber.org/protocol/commands".equals(nodeName)) {
     72 + if ("http://jabber.org/protocol/commands".equals(nodeName)) {
    103 73   List<Element> commandList = this.adHocCommandsModule.getCommandListItems(senderJid, element.getAttribute("to"));
    104 74   if (commandList != null)
    105 75   for (Element item : commandList) {
    106 76   resultQuery.addChild(item);
    107 77   }
    108 78   } else {
    109  - boolean allowed = (senderJid == null || nodeConfig == null) ? true : Utils.isAllowedDomain(senderJid,
    110  - nodeConfig.getDomains());
    111  - if (!allowed)
    112  - throw new PubSubException(Authorization.FORBIDDEN);
    113  - resultQuery.addAttribute("node", nodeName);
    114  - String[] itemsId = repository.getItemsIds(nodeName);
    115  - if (itemsId != null) {
    116  - for (String itemId : itemsId) {
    117  - resultQuery.addChild(new Element("item", new String[] { "jid", "name" }, new String[] {
    118  - element.getAttribute("to"), itemId }));
     79 + 
     80 + log.finest("Asking about Items of node " + nodeName);
     81 + AbstractNodeConfig nodeConfig = nodeName == null ? null : repository.getNodeConfig(nodeName);
     82 + String[] nodes;
     83 + if (nodeName == null || (nodeConfig != null && nodeConfig.getNodeType() == NodeType.collection)) {
     84 + String parentName;
     85 + if (nodeName == null) {
     86 + parentName = "";
     87 + nodes = repository.getRootCollection();
     88 + } else {
     89 + parentName = nodeName;
     90 + nodes = nodeConfig.getChildren();
     91 + }
     92 + // = this.repository.getNodesList();
     93 + if (nodes != null) {
     94 + for (String node : nodes) {
     95 + AbstractNodeConfig childNodeConfig = this.repository.getNodeConfig(node);
     96 + if (childNodeConfig != null) {
     97 + boolean allowed = (senderJid == null || childNodeConfig == null) ? true : Utils.isAllowedDomain(
     98 + senderJid, childNodeConfig.getDomains());
     99 + String collection = childNodeConfig.getCollection();
     100 + if (allowed) {
     101 + String name = childNodeConfig.getTitle();
     102 + name = name == null || name.length() == 0 ? node : name;
     103 + Element item = new Element("item", new String[] { "jid", "node", "name" }, new String[] {
     104 + element.getAttribute("to"), node, name });
    119 105   
     106 + if (parentName.equals(collection))
     107 + resultQuery.addChild(item);
     108 + } else {
     109 + log.fine("User " + senderJid + " not allowed to see node '" + node + "'");
     110 + }
     111 + }
     112 + }
     113 + }
     114 + } else {
     115 + boolean allowed = (senderJid == null || nodeConfig == null) ? true : Utils.isAllowedDomain(senderJid,
     116 + nodeConfig.getDomains());
     117 + if (!allowed)
     118 + throw new PubSubException(Authorization.FORBIDDEN);
     119 + resultQuery.addAttribute("node", nodeName);
     120 + String[] itemsId = repository.getItemsIds(nodeName);
     121 + if (itemsId != null) {
     122 + for (String itemId : itemsId) {
     123 + resultQuery.addChild(new Element("item", new String[] { "jid", "name" }, new String[] {
     124 + element.getAttribute("to"), itemId }));
     125 + 
     126 + }
    120 127   }
    121 128   }
    122 129   }
    skipped 10 lines
  • src/main/java/tigase/pubsub/modules/NodeConfigModule.java
    ■ ■ ■ ■ ■ ■
    skipped 35 lines
    36 36  import tigase.pubsub.exceptions.PubSubErrorCondition;
    37 37  import tigase.pubsub.exceptions.PubSubException;
    38 38  import tigase.pubsub.repository.IPubSubRepository;
     39 +import tigase.pubsub.repository.RepositoryException;
    39 40  import tigase.pubsub.repository.inmemory.NodeAffiliation;
    40 41  import tigase.xml.Element;
    41 42  import tigase.xmpp.Authorization;
    skipped 134 lines
    176 177   } else if (colNodeType == NodeType.leaf) {
    177 178   throw new PubSubException(element, Authorization.NOT_ALLOWED);
    178 179   }
    179  - repository.setNewNodeCollection(nodeName, collectionName);
     180 + switchCollection(nodeName, collectionName);
    180 181   if (!"".equals(collectionName)) {
    181 182   Element colE = new Element("collection", new String[] { "node" }, new String[] { collectionName });
    182 183   colE.addChild(new Element("associate", new String[] { "node" }, new String[] { nodeName }));
    skipped 21 lines
    204 205   if (nc == null) {
    205 206   throw new PubSubException(element, Authorization.ITEM_NOT_FOUND);
    206 207   }
    207  - repository.setNewNodeCollection(node, nodeName);
     208 + switchCollection(node, nodeName);
    208 209   
    209 210   Element colE = new Element("collection", new String[] { "node" }, new String[] { nodeName });
    210 211   colE.addChild(new Element("associate", new String[] { "node" }, new String[] { node }));
    skipped 5 lines
    216 217   if (nc == null) {
    217 218   throw new PubSubException(element, Authorization.ITEM_NOT_FOUND);
    218 219   }
    219  - repository.setNewNodeCollection(node, "");
     220 + switchCollection(node, "");
    220 221   Element colE = new Element("collection", new String[] { "node" }, new String[] { nodeName });
    221 222   colE.addChild(new Element("disassociate", new String[] { "node" }, new String[] { node }));
    222 223   resultArray.addAll(publishModule.prepareNotification(colE, element.getAttribute("to"), nodeName));
    skipped 27 lines
    250 251   
    251 252   public void removeNodeConfigListener(NodeConfigListener listener) {
    252 253   this.nodeConfigListeners.remove(listener);
     254 + }
     255 + 
     256 + private void switchCollection(String nodeName, String collectionNodeName) throws RepositoryException {
     257 + repository.setNewNodeCollection(nodeName, collectionNodeName);
    253 258   }
    254 259  }
    255 260   
  • src/main/java/tigase/pubsub/modules/NodeCreateModule.java
    ■ ■ ■ ■ ■
    skipped 27 lines
    28 28  import tigase.criteria.Criteria;
    29 29  import tigase.criteria.ElementCriteria;
    30 30  import tigase.pubsub.AbstractNodeConfig;
     31 +import tigase.pubsub.CollectionNodeConfig;
    31 32  import tigase.pubsub.LeafNodeConfig;
    32 33  import tigase.pubsub.NodeType;
    33 34  import tigase.pubsub.PubSubConfig;
    skipped 70 lines
    104 105   
    105 106   NodeType nodeType = NodeType.leaf;
    106 107   String collection = null;
    107  - LeafNodeConfig nodeConfig = new LeafNodeConfig(defaultNodeConfig);
     108 + LeafNodeConfig nodeConfig = new LeafNodeConfig(nodeName, defaultNodeConfig);
    108 109   if (configure != null) {
    109 110   Element x = configure.getChild("x", "jabber:x:data");
    110 111   if (x != null && "submit".equals(x.getAttribute("type"))) {
    skipped 16 lines
    127 128   }
    128 129   }
    129 130   
     131 + CollectionNodeConfig colNodeConfig = null;
    130 132   if (collection != null) {
    131  - AbstractNodeConfig colNodeConfig = repository.getNodeConfig(collection);
    132  - if (colNodeConfig == null) {
     133 + AbstractNodeConfig absNodeConfig = repository.getNodeConfig(collection);
     134 + if (absNodeConfig == null) {
    133 135   throw new PubSubException(element, Authorization.ITEM_NOT_FOUND);
    134  - } else if (colNodeConfig.getNodeType() == NodeType.leaf) {
     136 + } else if (absNodeConfig.getNodeType() == NodeType.leaf) {
    135 137   throw new PubSubException(element, Authorization.NOT_ALLOWED);
    136 138   }
     139 + colNodeConfig = (CollectionNodeConfig) absNodeConfig;
    137 140   }
    138 141   
    139 142   if (nodeType != NodeType.leaf && nodeType != NodeType.collection)
    skipped 1 lines
    141 144   
    142 145   repository.createNode(nodeName, JIDUtils.getNodeID(element.getAttribute("from")), nodeConfig, nodeType,
    143 146   collection == null ? "" : collection);
     147 + 
     148 + if (colNodeConfig == null) {
     149 + repository.addToRootCollection(nodeName);
     150 + } else {
     151 + colNodeConfig.addChildren(nodeName);
     152 + repository.update(collection, colNodeConfig);
     153 + }
     154 + 
    144 155   fireOnNodeCreatedConfigChange(nodeName);
    145 156   
    146 157   ArrayList<Element> notifications = new ArrayList<Element>();
    skipped 32 lines
  • src/main/java/tigase/pubsub/modules/NodeDeleteModule.java
    ■ ■ ■ ■ ■ ■
    skipped 27 lines
    28 28  import tigase.criteria.ElementCriteria;
    29 29  import tigase.pubsub.AbstractModule;
    30 30  import tigase.pubsub.AbstractNodeConfig;
     31 +import tigase.pubsub.CollectionNodeConfig;
    31 32  import tigase.pubsub.PubSubConfig;
    32 33  import tigase.pubsub.exceptions.PubSubException;
    33 34  import tigase.pubsub.repository.IPubSubRepository;
    skipped 64 lines
    98 99   String pssJid = element.getAttribute("to");
    99 100   Element del = new Element("delete", new String[] { "node" }, new String[] { nodeName });
    100 101   resultArray.addAll(this.publishModule.prepareNotification(del, pssJid, nodeName));
     102 + }
     103 + 
     104 + final String parentNodeName = nodeConfig.getCollection();
     105 + CollectionNodeConfig parentCollectionConfig = null;
     106 + if (parentNodeName != null && !parentNodeName.equals("")) {
     107 + parentCollectionConfig = (CollectionNodeConfig) repository.getNodeConfig(parentNodeName);
     108 + parentCollectionConfig.removeChildren(nodeName);
     109 + } else {
     110 + repository.removeFromRootCollection(nodeName);
     111 + }
     112 + 
     113 + if (nodeConfig instanceof CollectionNodeConfig) {
     114 + CollectionNodeConfig cnc = (CollectionNodeConfig) nodeConfig;
     115 + final String[] childrenNodes = cnc.getChildren();
     116 + if (childrenNodes != null && childrenNodes.length > 0) {
     117 + for (String childNodeName : childrenNodes) {
     118 + AbstractNodeConfig childNodeConfig = repository.getNodeConfig(childNodeName);
     119 + childNodeConfig.setCollection(parentNodeName);
     120 + repository.update(childNodeName, childNodeConfig);
     121 + if (parentNodeName != null && !parentNodeName.equals("")) {
     122 + parentCollectionConfig.addChildren(childNodeName);
     123 + } else {
     124 + repository.addToRootCollection(childNodeName);
     125 + }
     126 + }
     127 + }
     128 + }
     129 + if (parentCollectionConfig != null) {
     130 + repository.update(parentNodeName, parentCollectionConfig);
    101 131   }
    102 132   
    103 133   log.fine("Delete node [" + nodeName + "]");
    skipped 18 lines
  • src/main/java/tigase/pubsub/modules/commands/DefaultConfigCommand.java
    ■ ■ ■ ■ ■ ■
    skipped 69 lines
    70 70   
    71 71   response.cancelSession();
    72 72   } else if (data == null) {
    73  - LeafNodeConfig defaultNodeConfig = new LeafNodeConfig();
     73 + LeafNodeConfig defaultNodeConfig = new LeafNodeConfig("default");
    74 74   defaultNodeConfig.read(userRepository, config, PubSubComponent.DEFAULT_LEAF_NODE_CONFIG_KEY);
    75 75   response.getElements().add(defaultNodeConfig.getFormElement());
    76 76   response.startSession();
    77 77   } else {
    78 78   Form form = new Form(data);
    79 79   if ("submit".equals(form.getType())) {
    80  - LeafNodeConfig nodeConfig = new LeafNodeConfig();
     80 + LeafNodeConfig nodeConfig = new LeafNodeConfig("default");
    81 81   nodeConfig.read(userRepository, config, PubSubComponent.DEFAULT_LEAF_NODE_CONFIG_KEY);
    82 82   
    83 83   NodeConfigModule.parseConf(nodeConfig, request.getCommand());
    skipped 43 lines
  • src/main/java/tigase/pubsub/modules/commands/RebuildDatabaseCommand.java
    ■ ■ ■ ■ ■ ■
     1 +package tigase.pubsub.modules.commands;
     2 + 
     3 +import java.util.HashMap;
     4 +import java.util.HashSet;
     5 +import java.util.Map;
     6 +import java.util.Set;
     7 +import java.util.Map.Entry;
     8 + 
     9 +import tigase.adhoc.AdHocCommand;
     10 +import tigase.adhoc.AdHocCommandException;
     11 +import tigase.adhoc.AdHocResponse;
     12 +import tigase.adhoc.AdhHocRequest;
     13 +import tigase.form.Field;
     14 +import tigase.form.Form;
     15 +import tigase.pubsub.AbstractNodeConfig;
     16 +import tigase.pubsub.CollectionNodeConfig;
     17 +import tigase.pubsub.PubSubConfig;
     18 +import tigase.pubsub.repository.PubSubDAO;
     19 +import tigase.pubsub.repository.RepositoryException;
     20 +import tigase.util.JIDUtils;
     21 +import tigase.xml.Element;
     22 +import tigase.xmpp.Authorization;
     23 + 
     24 +public class RebuildDatabaseCommand implements AdHocCommand {
     25 + 
     26 + private final PubSubConfig config;
     27 + private final PubSubDAO dao;
     28 + 
     29 + public RebuildDatabaseCommand(PubSubConfig config, PubSubDAO directPubSubRepository) {
     30 + this.dao = directPubSubRepository;
     31 + this.config = config;
     32 + }
     33 + 
     34 + @Override
     35 + public void execute(AdhHocRequest request, AdHocResponse response) throws AdHocCommandException {
     36 + try {
     37 + if (!config.isAdmin(JIDUtils.getNodeID(request.getSender()))) {
     38 + throw new AdHocCommandException(Authorization.FORBIDDEN);
     39 + }
     40 + 
     41 + final Element data = request.getCommand().getChild("x", "jabber:x:data");
     42 + if (request.getAction() != null && "cancel".equals(request.getAction())) {
     43 + response.cancelSession();
     44 + } else if (data == null) {
     45 + Form form = new Form("result", "Rebuild nodes tree", "To rebuild tree of nodes please check checkbox.");
     46 + 
     47 + form.addField(Field.fieldBoolean("tigase-pubsub#rebuild", Boolean.FALSE, "Rebuild nodes tree?"));
     48 + 
     49 + response.getElements().add(form.getElement());
     50 + response.startSession();
     51 + 
     52 + } else {
     53 + Form form = new Form(data);
     54 + if ("submit".equals(form.getType())) {
     55 + final Boolean rebuild = form.getAsBoolean("tigase-pubsub#rebuild");
     56 + 
     57 + if (rebuild != null && rebuild.booleanValue() == true) {
     58 + startRebuild();
     59 + Form f = new Form(null, "Info", "Nodes tree has been rebuild");
     60 + response.getElements().add(f.getElement());
     61 + } else {
     62 + Form f = new Form(null, "Info", "Rebuild cancelled.");
     63 + response.getElements().add(f.getElement());
     64 + }
     65 + }
     66 + response.completeSession();
     67 + }
     68 + 
     69 + } catch (AdHocCommandException e) {
     70 + throw e;
     71 + } catch (Exception e) {
     72 + e.printStackTrace();
     73 + throw new AdHocCommandException(Authorization.INTERNAL_SERVER_ERROR, e.getMessage());
     74 + }
     75 + }
     76 + 
     77 + @Override
     78 + public String getName() {
     79 + return "Rebuild database";
     80 + }
     81 + 
     82 + @Override
     83 + public String getNode() {
     84 + return "rebuild-db";
     85 + }
     86 + 
     87 + private void startRebuild() throws RepositoryException {
     88 + final String[] allNodesId = dao.getNodesList();
     89 + final Set<String> rootCollection = new HashSet<String>();
     90 + final Map<String, AbstractNodeConfig> nodeConfigs = new HashMap<String, AbstractNodeConfig>();
     91 + for (String nodeName : allNodesId) {
     92 + AbstractNodeConfig nodeConfig = dao.getNodeConfig(nodeName);
     93 + nodeConfigs.put(nodeName, nodeConfig);
     94 + if (nodeConfig instanceof CollectionNodeConfig) {
     95 + CollectionNodeConfig collectionNodeConfig = (CollectionNodeConfig) nodeConfig;
     96 + collectionNodeConfig.setChildren(null);
     97 + }
     98 + }
     99 + 
     100 + for (Entry<String, AbstractNodeConfig> entry : nodeConfigs.entrySet()) {
     101 + final AbstractNodeConfig nodeConfig = entry.getValue();
     102 + final String nodeName = entry.getKey();
     103 + final String collectionNodeName = nodeConfig.getCollection();
     104 + if (collectionNodeName == null || collectionNodeName.equals("")) {
     105 + nodeConfig.setCollection("");
     106 + rootCollection.add(nodeName);
     107 + } else {
     108 + AbstractNodeConfig potentialParent = nodeConfigs.get(collectionNodeName);
     109 + if (potentialParent != null && potentialParent instanceof CollectionNodeConfig) {
     110 + CollectionNodeConfig collectionConfig = (CollectionNodeConfig) potentialParent;
     111 + collectionConfig.addChildren(nodeName);
     112 + } else {
     113 + nodeConfig.setCollection("");
     114 + rootCollection.add(nodeName);
     115 + }
     116 + 
     117 + }
     118 + }
     119 + 
     120 + for (Entry<String, AbstractNodeConfig> entry : nodeConfigs.entrySet()) {
     121 + final AbstractNodeConfig nodeConfig = entry.getValue();
     122 + final String nodeName = entry.getKey();
     123 + dao.update(nodeName, nodeConfig);
     124 + }
     125 + 
     126 + dao.removeAllFromRootCollection();
     127 + for (String nodeName : rootCollection) {
     128 + dao.addToRootCollection(nodeName);
     129 + }
     130 + 
     131 + }
     132 + 
     133 +}
     134 + 
  • src/main/java/tigase/pubsub/repository/IPubSubDAO.java
    ■ ■ ■ ■ ■
    skipped 36 lines
    37 37   public abstract String addSubscriberJid(final String nodeName, final String jid, final Affiliation affiliation,
    38 38   final Subscription subscription) throws RepositoryException;
    39 39   
     40 + public void addToRootCollection(String nodeName) throws RepositoryException;
     41 + 
    40 42   void changeAffiliation(String node, String subscriberJid, Affiliation affiliation) throws RepositoryException;
    41 43   
    42 44   public abstract void changeSubscription(final String nodeName, final String jid, final Subscription subscription)
    skipped 26 lines
    69 71   
    70 72   public abstract Date getItemUpdateDate(final String nodeName, final String id) throws RepositoryException;
    71 73   
    72  - public abstract AccessModel getNodeAccessModel(String nodeName) throws RepositoryException;
    73  - 
    74 74   // public abstract void readNodeConfig(LeafNodeConfig nodeConfig, String
    75 75   // nodeName) throws RepositoryException;
     76 + 
     77 + public abstract AccessModel getNodeAccessModel(String nodeName) throws RepositoryException;
    76 78   
    77 79   public AbstractNodeConfig getNodeConfig(final String nodeName) throws RepositoryException;
    78 80   
    skipped 1 lines
    80 82   
    81 83   public abstract NodeType getNodeType(String nodeName) throws RepositoryException;
    82 84   
     85 + public String[] getRootNodes() throws RepositoryException;
     86 + 
    83 87   public abstract Affiliation getSubscriberAffiliation(final String nodeName, final String jid) throws RepositoryException;
    84 88   
    85 89   public abstract Subscription getSubscription(String nodeName, String jid) throws RepositoryException;
    skipped 5 lines
    91 95   String[] getUserRoster(String owner) throws RepositoryException;
    92 96   
    93 97   public void init();
     98 + 
     99 + public void removeFromRootCollection(String nodeName) throws RepositoryException;
    94 100   
    95 101   void removeListener(PubSubRepositoryListener listener);
    96 102   
    skipped 10 lines
  • src/main/java/tigase/pubsub/repository/IPubSubRepository.java
    ■ ■ ■ ■ ■ ■
    skipped 16 lines
    17 17   public abstract String addSubscriberJid(String nodeName, String jid, Affiliation affiliation, Subscription subscription)
    18 18   throws RepositoryException;
    19 19   
     20 + public void addToRootCollection(String nodeName) throws RepositoryException;
     21 + 
    20 22   public abstract void changeAffiliation(String nodeName, String jid, Affiliation affiliation) throws RepositoryException;
    21 23   
    22 24   public abstract void changeSubscription(String nodeName, String jid, Subscription subscription) throws RepositoryException;
    skipped 23 lines
    46 48   
    47 49   public abstract AbstractNodeConfig getNodeConfig(String nodeName) throws RepositoryException;
    48 50   
     51 + @Deprecated
    49 52   public abstract String[] getNodesList() throws RepositoryException;
    50 53   
    51 54   public abstract IPubSubDAO getPubSubDAO();
     55 + 
     56 + public abstract String[] getRootCollection() throws RepositoryException;
    52 57   
    53 58   public abstract NodeAffiliation getSubscriberAffiliation(String nodeName, String jid) throws RepositoryException;
    54 59   
    skipped 6 lines
    61 66   public abstract String[] getUserRoster(String owner) throws RepositoryException;
    62 67   
    63 68   public abstract void init();
     69 + 
     70 + public void removeFromRootCollection(String nodeName) throws RepositoryException;
    64 71   
    65 72   public abstract void removeListener(PubSubRepositoryListener listener);
    66 73   
    skipped 10 lines
  • src/main/java/tigase/pubsub/repository/PubSubDAO.java
    ■ ■ ■ ■ ■
    skipped 20 lines
    21 21   */
    22 22  package tigase.pubsub.repository;
    23 23   
     24 +import java.lang.reflect.Constructor;
    24 25  import java.util.Date;
    25 26  import java.util.Queue;
    26 27  import java.util.logging.Level;
    skipped 31 lines
    58 59   
    59 60   private static final String ITEMS_KEY = "items";
    60 61   
    61  - private final static long MAX_CACHE_TIME = 5000;
     62 + private final static long MAX_CACHE_TIME = 2000;
    62 63   
    63 64   private static final String NODE_TYPE_KEY = "pubsub#node_type";
    64 65   
    65 66   static final String NODES_KEY = "nodes/";
     67 + 
     68 + private static final String ROOT_COLLECTION_KEY = "root-collection";
    66 69   
    67 70   private static final String SUBSCRIPTIONS_KEY = "subscriptions";
    68 71   
    skipped 2 lines
    71 74   final PubSubConfig config;
    72 75   
    73 76   private Logger log = Logger.getLogger(this.getClass().getName());
     77 + 
     78 + private final ListCache<String, AbstractNodeConfig> nodesConfigCache = new ListCache<String, AbstractNodeConfig>(1000,
     79 + MAX_CACHE_TIME);
    74 80   
    75 81   private final ElementCache<String[]> nodesListCache = new ElementCache<String[]>(MAX_CACHE_TIME);
    76 82   
    skipped 52 lines
    129 135   throw new RepositoryException("Subscriber adding error", e);
    130 136   }
    131 137   
     138 + }
     139 + 
     140 + public void addToRootCollection(String nodeName) throws RepositoryException {
     141 + try {
     142 + repository.setData(config.getServiceName(), ROOT_COLLECTION_KEY, nodeName, "root");
     143 + } catch (Exception e) {
     144 + throw new RepositoryException("Adding to root collection error", e);
     145 + }
    132 146   }
    133 147   
    134 148   @Override
    skipped 231 lines
    366 380   public <T extends AbstractNodeConfig> T getNodeConfig(final Class<T> nodeConfigClass, final String nodeName)
    367 381   throws RepositoryException {
    368 382   try {
    369  - T nodeConfig = nodeConfigClass.newInstance();
     383 + Constructor<T> constructor = nodeConfigClass.getConstructor(String.class);
     384 + T nodeConfig = constructor.newInstance(nodeName);
    370 385   nodeConfig.read(repository, config, NODES_KEY + nodeName + "/configuration");
    371  - // Field f = Field.fieldTextMulti("pubsub#children",
    372  - // getNodeChildren(nodeName), null);
    373  - // nodeConfig.add(f);
    374 386   return nodeConfig;
    375 387   } catch (Exception e) {
    376 388   throw new RepositoryException("Node configuration reading error", e);
    skipped 2 lines
    379 391   }
    380 392   
    381 393   public AbstractNodeConfig getNodeConfig(final String nodeName) throws RepositoryException {
     394 + AbstractNodeConfig nc = nodesConfigCache.get(nodeName);
     395 + if (nc != null) {
     396 + return nc;
     397 + }
    382 398   try {
    383 399   NodeType type = getNodeType(nodeName);
    384 400   if (type == null)
    skipped 9 lines
    394 410   default:
    395 411   throw new RepositoryException("Unknown node type " + type);
    396 412   }
    397  - return getNodeConfig(cl, nodeName);
     413 + nc = getNodeConfig(cl, nodeName);
     414 + if (nc != null)
     415 + nodesConfigCache.put(nodeName, nc);
     416 + return nc;
    398 417   } catch (RepositoryException e) {
    399 418   throw e;
    400 419   } catch (Exception e) {
    skipped 53 lines
    454 473   }
    455 474   } catch (Exception e) {
    456 475   throw new RepositoryException("Owner getting error", e);
     476 + }
     477 + }
     478 + 
     479 + public String[] getRootNodes() throws RepositoryException {
     480 + try {
     481 + String[] ids = repository.getKeys(config.getServiceName(), ROOT_COLLECTION_KEY);
     482 + return ids;
     483 + } catch (Exception e) {
     484 + throw new RepositoryException("Getting root collection error", e);
    457 485   }
    458 486   }
    459 487   
    skipped 82 lines
    542 570   
    543 571   @Override
    544 572   public void init() {
     573 + }
     574 + 
     575 + public void removeAllFromRootCollection() throws RepositoryException {
     576 + try {
     577 + repository.removeSubnode(config.getServiceName(), ROOT_COLLECTION_KEY);
     578 + } catch (Exception e) {
     579 + throw new RepositoryException("Removing root collection error", e);
     580 + }
     581 + }
     582 + 
     583 + public void removeFromRootCollection(String nodeName) throws RepositoryException {
     584 + try {
     585 + repository.removeData(config.getServiceName(), ROOT_COLLECTION_KEY, nodeName);
     586 + } catch (Exception e) {
     587 + throw new RepositoryException("Removing from root collection error", e);
     588 + }
    545 589   }
    546 590   
    547 591   @Override
    skipped 75 lines
  • src/main/java/tigase/pubsub/repository/inmemory/InMemoryPubSubRepository.java
    ■ ■ ■ ■ ■ ■
    1  -/*
    2  - * Tigase Jabber/XMPP Publish Subscribe Component
    3  - * Copyright (C) 2007 "Bartosz M. Małkowski" <bartosz.malkowski@tigase.org>
    4  - *
    5  - * This program is free software: you can redistribute it and/or modify
    6  - * it under the terms of the GNU General Public License as published by
    7  - * the Free Software Foundation, either version 3 of the License.
    8  - *
    9  - * This program is distributed in the hope that it will be useful,
    10  - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  - * GNU General Public License for more details.
    13  - *
    14  - * You should have received a copy of the GNU General Public License
    15  - * along with this program. Look for COPYING file in the top folder.
    16  - * If not, see http://www.gnu.org/licenses/.
    17  - *
    18  - * $Rev$
    19  - * Last modified by $Author$
    20  - * $Date$
    21  - */
    22  -package tigase.pubsub.repository.inmemory;
    23  - 
    24  -import java.util.ArrayList;
    25  -import java.util.Date;
    26  -import java.util.HashMap;
    27  -import java.util.List;
    28  -import java.util.logging.Level;
    29  -import java.util.logging.Logger;
    30  - 
    31  -import tigase.pubsub.AbstractNodeConfig;
    32  -import tigase.pubsub.Affiliation;
    33  -import tigase.pubsub.NodeType;
    34  -import tigase.pubsub.PubSubConfig;
    35  -import tigase.pubsub.Subscription;
    36  -import tigase.pubsub.repository.IPubSubDAO;
    37  -import tigase.pubsub.repository.IPubSubRepository;
    38  -import tigase.pubsub.repository.PubSubDAO;
    39  -import tigase.pubsub.repository.PubSubRepositoryListener;
    40  -import tigase.pubsub.repository.RepositoryException;
    41  -import tigase.xml.Element;
    42  - 
    43  -public class InMemoryPubSubRepository implements IPubSubRepository {
    44  - 
    45  - public final static String CREATION_DATE_KEY = "creation-date";
    46  - 
    47  - protected static final String NODE_TYPE_KEY = "pubsub#node_type";
    48  - 
    49  - protected static final String NODES_KEY = "nodes/";
    50  - 
    51  - private final List<PubSubRepositoryListener> listeners = new ArrayList<PubSubRepositoryListener>();
    52  - 
    53  - protected Logger log = Logger.getLogger(this.getClass().getName());
    54  - 
    55  - private final HashMap<String, Entry> nodes = new HashMap<String, Entry>();
    56  - 
    57  - private final PubSubDAO pubSubDB;
    58  - 
    59  - public InMemoryPubSubRepository(PubSubDAO pubSubDB, PubSubConfig pubSubConfig) {
    60  - this.pubSubDB = pubSubDB;
    61  - }
    62  - 
    63  - /*
    64  - * (non-Javadoc)
    65  - *
    66  - * @see
    67  - * tigase.pubsub.repository.inmemory.IPubSubRepository#addListener(tigase
    68  - * .pubsub.repository.PubSubRepositoryListener)
    69  - */
    70  - public void addListener(PubSubRepositoryListener listener) {
    71  - this.listeners.add(listener);
    72  - }
    73  - 
    74  - /*
    75  - * (non-Javadoc)
    76  - *
    77  - * @see
    78  - * tigase.pubsub.repository.inmemory.IPubSubRepository#addSubscriberJid(
    79  - * java.lang.String, java.lang.String, tigase.pubsub.Affiliation,
    80  - * tigase.pubsub.Subscription)
    81  - */
    82  - public String addSubscriberJid(String nodeName, String jid, Affiliation affiliation, Subscription subscription)
    83  - throws RepositoryException {
    84  - Entry entry = readNodeEntry(nodeName);
    85  - String subid = this.pubSubDB.addSubscriberJid(nodeName, jid, affiliation, subscription);
    86  - Subscriber subscriber = new Subscriber(jid, subid, subscription);
    87  - NodeAffiliation nodeAffiliation = new NodeAffiliation(jid, affiliation);
    88  - entry.add(subscriber);
    89  - entry.add(nodeAffiliation);
    90  - return subid;
    91  - }
    92  - 
    93  - private Date asDate(String d) {
    94  - Long x = Long.valueOf(d);
    95  - return new Date(x.longValue());
    96  - }
    97  - 
    98  - /*
    99  - * (non-Javadoc)
    100  - *
    101  - * @see
    102  - * tigase.pubsub.repository.inmemory.IPubSubRepository#changeAffiliation
    103  - * (java.lang.String, java.lang.String, tigase.pubsub.Affiliation)
    104  - */
    105  - public void changeAffiliation(String nodeName, String jid, Affiliation affiliation) throws RepositoryException {
    106  - Entry entry = readNodeEntry(nodeName);
    107  - this.pubSubDB.changeAffiliation(nodeName, jid, affiliation);
    108  - entry.changeAffiliation(jid, affiliation);
    109  - }
    110  - 
    111  - /*
    112  - * (non-Javadoc)
    113  - *
    114  - * @see
    115  - * tigase.pubsub.repository.inmemory.IPubSubRepository#changeSubscription
    116  - * (java.lang.String, java.lang.String, tigase.pubsub.Subscription)
    117  - */
    118  - public void changeSubscription(String nodeName, String jid, Subscription subscription) throws RepositoryException {
    119  - Entry entry = readNodeEntry(nodeName);
    120  - this.pubSubDB.changeSubscription(nodeName, jid, subscription);
    121  - entry.changeSubscription(jid, subscription);
    122  - }
    123  - 
    124  - /*
    125  - * (non-Javadoc)
    126  - *
    127  - * @see
    128  - * tigase.pubsub.repository.inmemory.IPubSubRepository#createNode(java.lang
    129  - * .String, java.lang.String, tigase.pubsub.AbstractNodeConfig,
    130  - * tigase.pubsub.NodeType, java.lang.String)
    131  - */
    132  - public void createNode(String nodeName, String ownerJid, AbstractNodeConfig nodeConfig, NodeType nodeType, String collection)
    133  - throws RepositoryException {
    134  - this.pubSubDB.createNode(nodeName, ownerJid, nodeConfig, nodeType, collection);
    135  - Entry cnf = readNodeEntry(nodeName);
    136  - if (!"".equals(cnf.getConfig().getCollection())) {
    137  - fireNewNodeCollection(nodeName, null, collection);
    138  - }
    139  - }
    140  - 
    141  - /*
    142  - * (non-Javadoc)
    143  - *
    144  - * @see
    145  - * tigase.pubsub.repository.inmemory.IPubSubRepository#deleteItem(java.lang
    146  - * .String, java.lang.String)
    147  - */
    148  - public void deleteItem(String nodeName, String id) throws RepositoryException {
    149  - this.pubSubDB.deleteItem(nodeName, id);
    150  - Entry entry = readNodeEntry(nodeName);
    151  - entry.deleteItem(id);
    152  - }
    153  - 
    154  - /*
    155  - * (non-Javadoc)
    156  - *
    157  - * @see
    158  - * tigase.pubsub.repository.inmemory.IPubSubRepository#deleteNode(java.lang
    159  - * .String)
    160  - */
    161  - public void deleteNode(String nodeName) throws RepositoryException {
    162  - this.pubSubDB.deleteNode(nodeName);
    163  - this.nodes.remove(nodeName);
    164  - }
    165  - 
    166  - private void fireNewNodeCollection(String nodeName, String oldCollectionName, String newCollectionName) {
    167  - for (PubSubRepositoryListener listener : this.listeners) {
    168  - listener.onChangeCollection(nodeName, oldCollectionName, newCollectionName);
    169  - }
    170  - }
    171  - 
    172  - /*
    173  - * (non-Javadoc)
    174  - *
    175  - * @see
    176  - * tigase.pubsub.repository.inmemory.IPubSubRepository#forgetConfiguration
    177  - * (java.lang.String)
    178  - */
    179  - public void forgetConfiguration(String nodeName) throws RepositoryException {
    180  - this.nodes.remove(nodeName);
    181  - readNodeEntry(nodeName);
    182  - }
    183  - 
    184  - /*
    185  - * (non-Javadoc)
    186  - *
    187  - * @see
    188  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getAffiliations(java
    189  - * .lang.String)
    190  - */
    191  - public NodeAffiliation[] getAffiliations(final String nodeName) throws RepositoryException {
    192  - Entry entry = readNodeEntry(nodeName);
    193  - return entry.getAffiliations();
    194  - }
    195  - 
    196  - /*
    197  - * (non-Javadoc)
    198  - *
    199  - * @see
    200  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getBuddyGroups(java
    201  - * .lang.String, java.lang.String)
    202  - */
    203  - public String[] getBuddyGroups(String owner, String bareJid) throws RepositoryException {
    204  - return this.pubSubDB.getBuddyGroups(owner, bareJid);
    205  - }
    206  - 
    207  - /*
    208  - * (non-Javadoc)
    209  - *
    210  - * @see
    211  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getBuddySubscription
    212  - * (java.lang.String, java.lang.String)
    213  - */
    214  - public String getBuddySubscription(String owner, String buddy) throws RepositoryException {
    215  - return this.pubSubDB.getBuddySubscription(owner, buddy);
    216  - }
    217  - 
    218  - /*
    219  - * (non-Javadoc)
    220  - *
    221  - * @see
    222  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getItem(java.lang
    223  - * .String, java.lang.String)
    224  - */
    225  - public Element getItem(String nodeName, String id) throws RepositoryException {
    226  - Entry entry = readNodeEntry(nodeName);
    227  - Item item = entry.getItemData(id);
    228  - return item != null ? item.getData() : null;
    229  - }
    230  - 
    231  - /*
    232  - * (non-Javadoc)
    233  - *
    234  - * @see
    235  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getItemCreationDate
    236  - * (java.lang.String, java.lang.String)
    237  - */
    238  - public Date getItemCreationDate(String nodeName, String id) throws RepositoryException {
    239  - Entry entry = readNodeEntry(nodeName);
    240  - if (entry == null)
    241  - return null;
    242  - return entry.getItemCreationDate(id);
    243  - }
    244  - 
    245  - /*
    246  - * (non-Javadoc)
    247  - *
    248  - * @see
    249  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getItemsIds(java.
    250  - * lang.String)
    251  - */
    252  - public String[] getItemsIds(String nodeName) throws RepositoryException {
    253  - Entry entry = readNodeEntry(nodeName);
    254  - return entry == null ? null : entry.getSortedItemsId();
    255  - }
    256  - 
    257  - @Override
    258  - public Date getItemUpdateDate(String nodeName, String id) throws RepositoryException {
    259  - return this.pubSubDB.getItemUpdateDate(nodeName, id);
    260  - }
    261  - 
    262  - /*
    263  - * (non-Javadoc)
    264  - *
    265  - * @see
    266  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getNodeConfig(java
    267  - * .lang.String)
    268  - */
    269  - public AbstractNodeConfig getNodeConfig(String nodeName) throws RepositoryException {
    270  - Entry entry = readNodeEntry(nodeName);
    271  - return entry == null ? null : entry.getConfig();
    272  - }
    273  - 
    274  - /*
    275  - * (non-Javadoc)
    276  - *
    277  - * @see tigase.pubsub.repository.inmemory.IPubSubRepository#getNodesList()
    278  - */
    279  - public String[] getNodesList() throws RepositoryException {
    280  - return this.pubSubDB.getNodesList();
    281  - }
    282  - 
    283  - /*
    284  - * (non-Javadoc)
    285  - *
    286  - * @see tigase.pubsub.repository.inmemory.IPubSubRepository#getPubSubDAO()
    287  - */
    288  - public IPubSubDAO getPubSubDAO() {
    289  - return this.pubSubDB;
    290  - }
    291  - 
    292  - /*
    293  - * (non-Javadoc)
    294  - *
    295  - * @see
    296  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getSubscriberAffiliation
    297  - * (java.lang.String, java.lang.String)
    298  - */
    299  - public NodeAffiliation getSubscriberAffiliation(String nodeName, String jid) throws RepositoryException {
    300  - Entry entry = readNodeEntry(nodeName);
    301  - return entry.getSubscriberAffiliation(jid);
    302  - }
    303  - 
    304  - /*
    305  - * (non-Javadoc)
    306  - *
    307  - * @see
    308  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getSubscription(java
    309  - * .lang.String, java.lang.String)
    310  - */
    311  - public Subscription getSubscription(String nodeName, String jid) throws RepositoryException {
    312  - Entry entry = readNodeEntry(nodeName);
    313  - if (entry == null)
    314  - return Subscription.none;
    315  - return entry.getSubscriberSubscription(jid);
    316  - }
    317  - 
    318  - /*
    319  - * (non-Javadoc)
    320  - *
    321  - * @see
    322  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getSubscriptionId
    323  - * (java.lang.String, java.lang.String)
    324  - */
    325  - public String getSubscriptionId(String nodeName, String jid) throws RepositoryException {
    326  - Entry entry = readNodeEntry(nodeName);
    327  - return entry.getSubscriptionId(jid);
    328  - }
    329  - 
    330  - /*
    331  - * (non-Javadoc)
    332  - *
    333  - * @see
    334  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getSubscriptions(
    335  - * java.lang.String)
    336  - */
    337  - public Subscriber[] getSubscriptions(String nodeName) throws RepositoryException {
    338  - Entry entry = readNodeEntry(nodeName);
    339  - return entry == null ? null : entry.getSubscribersJid();
    340  - }
    341  - 
    342  - /*
    343  - * (non-Javadoc)
    344  - *
    345  - * @see
    346  - * tigase.pubsub.repository.inmemory.IPubSubRepository#getUserRoster(java
    347  - * .lang.String)
    348  - */
    349  - public String[] getUserRoster(String owner) throws RepositoryException {
    350  - return this.pubSubDB.getUserRoster(owner);
    351  - }
    352  - 
    353  - /*
    354  - * (non-Javadoc)
    355  - *
    356  - * @see tigase.pubsub.repository.inmemory.IPubSubRepository#init()
    357  - */
    358  - public void init() {
    359  - try {
    360  - log.config("InMemory PS Repository initializing...");
    361  - this.nodes.clear();
    362  - String[] nodes = this.pubSubDB.getNodesList();
    363  - if (nodes != null)
    364  - for (String nodeName : nodes) {
    365  - readNodeEntry(nodeName);
    366  - }
    367  - } catch (Exception e) {
    368  - log.log(Level.SEVERE, "Can't initialize InMemory PS!", e);
    369  - throw new RuntimeException("Can't initialize InMemory PS!", e);
    370  - }
    371  - }
    372  - 
    373  - protected List<Item> readItems(String nodeName) throws RepositoryException {
    374  - String[] ids = this.pubSubDB.getItemsIds(nodeName);
    375  - if (ids != null) {
    376  - List<Item> result = new ArrayList<Item>();
    377  - for (String id : ids) {
    378  - final Element data = this.pubSubDB.getItem(nodeName, id);
    379  - final Date creationDate = this.pubSubDB.getItemCreationDate(nodeName, id);
    380  - final Date updateDate = this.pubSubDB.getItemUpdateDate(nodeName, id);
    381  - final String pubslisher = this.pubSubDB.getItemPublisher(nodeName, id);
    382  - Item item = new Item(id, data, creationDate, updateDate, pubslisher);
    383  - result.add(item);
    384  - }
    385  - return result;
    386  - }
    387  - return null;
    388  - }
    389  - 
    390  - protected List<NodeAffiliation> readNodeAffiliations(final String nodeName) throws RepositoryException {
    391  - String[] jids = this.pubSubDB.getAffiliations(nodeName);
    392  - List<NodeAffiliation> result = new ArrayList<NodeAffiliation>();
    393  - if (jids != null) {
    394  - for (String jid : jids) {
    395  - Affiliation affiliation = this.pubSubDB.getSubscriberAffiliation(nodeName, jid);
    396  - NodeAffiliation na = new NodeAffiliation(jid, affiliation);
    397  - result.add(na);
    398  - }
    399  - }
    400  - return result;
    401  - }
    402  - 
    403  - protected AbstractNodeConfig readNodeConfig(final String nodeName) throws RepositoryException {
    404  - return this.pubSubDB.getNodeConfig(nodeName);
    405  - }
    406  - 
    407  - protected Entry readNodeEntry(final String nodeName) throws RepositoryException {
    408  - Entry entry = this.nodes.get(nodeName);
    409  - if (entry == null) {
    410  - log.fine("Reading '" + nodeName + "' node entry...");
    411  - AbstractNodeConfig cnf = readNodeConfig(nodeName);
    412  - if (cnf == null)
    413  - return null;
    414  - Date creationDate = this.pubSubDB.getNodeCreationDate(nodeName);
    415  - 
    416  - List<Subscriber> subscribers = readSubscribers(nodeName);
    417  - List<NodeAffiliation> affiliations = readNodeAffiliations(nodeName);
    418  - List<Item> items = readItems(nodeName);
    419  - entry = new Entry(nodeName, creationDate, cnf, subscribers, affiliations, items);
    420  - this.nodes.put(nodeName, entry);
    421  - }
    422  - return entry;
    423  - }
    424  - 
    425  - protected List<Subscriber> readSubscribers(final String nodeName) throws RepositoryException {
    426  - String[] jids = this.pubSubDB.getSubscriptions(nodeName);
    427  - List<Subscriber> result = new ArrayList<Subscriber>();
    428  - if (jids != null)
    429  - for (String jid : jids) {
    430  - String subId = this.pubSubDB.getSubscriptionId(nodeName, jid);
    431  - Subscription subscription = this.pubSubDB.getSubscription(nodeName, jid);
    432  - Subscriber s = new Subscriber(jid, subId, subscription);
    433  - result.add(s);
    434  - }
    435  - 
    436  - return result;
    437  - }
    438  - 
    439  - /*
    440  - * (non-Javadoc)
    441  - *
    442  - * @see
    443  - * tigase.pubsub.repository.inmemory.IPubSubRepository#removeListener(tigase
    444  - * .pubsub.repository.PubSubRepositoryListener)
    445  - */
    446  - public void removeListener(PubSubRepositoryListener listener) {
    447  - this.listeners.remove(listener);
    448  - }
    449  - 
    450  - /*
    451  - * (non-Javadoc)
    452  - *
    453  - * @see
    454  - * tigase.pubsub.repository.inmemory.IPubSubRepository#removeSubscriber(
    455  - * java.lang.String, java.lang.String)
    456  - */
    457  - public void removeSubscriber(String nodeName, String jid) throws RepositoryException {
    458  - this.pubSubDB.removeSubscriber(nodeName, jid);
    459  - Entry entry = readNodeEntry(nodeName);
    460  - entry.removeSubscriber(jid);
    461  - }
    462  - 
    463  - /*
    464  - * (non-Javadoc)
    465  - *
    466  - * @see
    467  - * tigase.pubsub.repository.inmemory.IPubSubRepository#setNewNodeCollection
    468  - * (java.lang.String, java.lang.String)
    469  - */
    470  - public void setNewNodeCollection(String nodeName, String collectionNew) throws RepositoryException {
    471  - Entry entry = readNodeEntry(nodeName);
    472  - this.pubSubDB.setNewNodeCollection(nodeName, collectionNew);
    473  - final String oldCollectionName = entry.getConfig().getCollection();
    474  - entry.getConfig().setCollection(collectionNew);
    475  - fireNewNodeCollection(nodeName, oldCollectionName, collectionNew);
    476  - }
    477  - 
    478  - /*
    479  - * (non-Javadoc)
    480  - *
    481  - * @see
    482  - * tigase.pubsub.repository.inmemory.IPubSubRepository#update(java.lang.
    483  - * String, tigase.pubsub.AbstractNodeConfig)
    484  - */
    485  - public void update(String nodeName, AbstractNodeConfig nodeConfig) throws RepositoryException {
    486  - Entry entry = readNodeEntry(nodeName);
    487  - final String oldCollectionName = entry.getConfig().getCollection();
    488  - this.pubSubDB.update(nodeName, nodeConfig);
    489  - entry.getConfig().copyFrom(nodeConfig);
    490  - final String newCollectionName = entry.getConfig().getCollection();
    491  - if (!oldCollectionName.equals(newCollectionName)) {
    492  - fireNewNodeCollection(nodeName, oldCollectionName, newCollectionName);
    493  - }
    494  - 
    495  - }
    496  - 
    497  - /*
    498  - * (non-Javadoc)
    499  - *
    500  - * @see
    501  - * tigase.pubsub.repository.inmemory.IPubSubRepository#writeItem(java.lang
    502  - * .String, long, java.lang.String, java.lang.String, tigase.xml.Element)
    503  - */
    504  - public void writeItem(String nodeName, long timeInMilis, String id, String publisher, Element item) throws RepositoryException {
    505  - this.pubSubDB.writeItem(nodeName, timeInMilis, id, publisher, item);
    506  - Item it = new Item(id, item, new Date(timeInMilis), new Date(timeInMilis), publisher);
    507  - Entry entry = readNodeEntry(nodeName);
    508  - entry.add(it);
    509  - }
    510  - 
    511  -}
    512  - 
  • src/main/java/tigase/pubsub/repository/stateless/StatelessPubSubRepository.java
    ■ ■ ■ ■ ■
    skipped 21 lines
    22 22  package tigase.pubsub.repository.stateless;
    23 23   
    24 24  import java.util.ArrayList;
    25  -import java.util.Collections;
    26  -import java.util.Comparator;
    27 25  import java.util.Date;
    28 26  import java.util.List;
    29 27   
    skipped 29 lines
    59 57   public String addSubscriberJid(String nodeName, String jid, Affiliation affiliation, Subscription subscription)
    60 58   throws RepositoryException {
    61 59   return this.dao.addSubscriberJid(nodeName, jid, affiliation, subscription);
     60 + }
     61 + 
     62 + public void addToRootCollection(String nodeName) throws RepositoryException {
     63 + dao.addToRootCollection(nodeName);
    62 64   }
    63 65   
    64 66   @Override
    skipped 95 lines
    160 162   }
    161 163   
    162 164   @Override
     165 + public String[] getRootCollection() throws RepositoryException {
     166 + String[] result = this.dao.getRootNodes();
     167 + return result == null ? new String[] {} : result;
     168 + }
     169 + 
     170 + @Override
    163 171   public NodeAffiliation getSubscriberAffiliation(String nodeName, String jid) throws RepositoryException {
    164 172   Affiliation affiliation = this.dao.getSubscriberAffiliation(nodeName, jid);
    165 173   NodeAffiliation na = new NodeAffiliation(jid, affiliation);
    skipped 33 lines
    199 207   public void init() {
    200 208   }
    201 209   
     210 + public void removeFromRootCollection(String nodeName) throws RepositoryException {
     211 + dao.removeFromRootCollection(nodeName);
     212 + }
     213 + 
    202 214   @Override
    203 215   public void removeListener(PubSubRepositoryListener listener) {
    204 216   this.listeners.remove(listener);
    skipped 25 lines
    230 242   public void writeItem(String nodeName, long timeInMilis, String id, String publisher, Element item) throws RepositoryException {
    231 243   this.dao.writeItem(nodeName, timeInMilis, id, publisher, item);
    232 244   }
    233  - 
    234 245   
    235 246  }
    236 247   
Please wait...
Page is in error, reload to recover