1 11 12 package org.jivesoftware.messenger.muc; 13 14 import org.jivesoftware.util.JiveGlobals; 15 import org.jivesoftware.util.Log; 16 import org.xmpp.packet.Message; 17 18 import java.util.Iterator ; 19 import java.util.LinkedList ; 20 import java.util.ListIterator ; 21 22 32 public class HistoryStrategy { 33 34 37 private Type type = Type.number; 38 39 42 private LinkedList history = new LinkedList (); 43 46 private static final int DEFAULT_MAX_NUMBER = 25; 47 50 private int maxNumber; 51 55 private HistoryStrategy parent; 56 59 private Message roomSubject = null; 60 64 private String contextPrefix = null; 65 66 72 public HistoryStrategy(HistoryStrategy parentStrategy) { 73 this.parent = parentStrategy; 74 if (parent == null) { 75 maxNumber = DEFAULT_MAX_NUMBER; 76 } 77 else { 78 type = Type.defaulType; 79 maxNumber = parent.getMaxNumber(); 80 } 81 } 82 83 88 public int getMaxNumber() { 89 return maxNumber; 90 } 91 92 97 public void setMaxNumber(int max) { 98 this.maxNumber = max; 99 if (contextPrefix != null){ 100 JiveGlobals.setProperty(contextPrefix + ".maxNumber", 101 Integer.toString(maxNumber)); 102 } 103 } 104 105 110 public void setType(Type newType){ 111 if (newType != null){ 112 type = newType; 113 } 114 if (contextPrefix != null){ 115 JiveGlobals.setProperty(contextPrefix + ".type", type.toString()); 116 } 117 } 118 119 124 public Type getType(){ 125 return type; 126 } 127 128 134 public void addMessage(Message packet){ 135 Type strategyType; 137 int strategyMaxNumber; 138 if (type == Type.defaulType && parent != null) { 139 strategyType = parent.getType(); 140 strategyMaxNumber = parent.getMaxNumber(); 141 } 142 else { 143 strategyType = type; 144 strategyMaxNumber = maxNumber; 145 } 146 147 boolean subjectChange = false; 149 if (packet.getSubject() != null && packet.getSubject().length() > 0){ 150 subjectChange = true; 151 roomSubject = packet; 152 } 153 154 if (strategyType == Type.none){ 156 if (subjectChange) { 157 history.clear(); 158 history.add(packet); 159 } 160 } 161 else if (strategyType == Type.all) { 162 history.addLast(packet); 163 } 164 else if (strategyType == Type.number) { 165 if (history.size() >= strategyMaxNumber) { 166 ListIterator historyIter = history.listIterator(); 173 while (historyIter.hasNext() && history.size() > strategyMaxNumber) { 174 if (historyIter.next() != roomSubject) { 175 historyIter.remove(); 176 } 177 } 178 } 179 history.addLast(packet); 180 } 181 } 182 183 188 public Iterator getMessageHistory(){ 189 LinkedList list = new LinkedList (history); 190 return list.iterator(); 191 } 192 193 200 public ListIterator getReverseMessageHistory(){ 201 LinkedList list = new LinkedList (history); 202 return list.listIterator(list.size()); 203 } 204 205 208 public enum Type { 209 defaulType, none, all, number; 210 } 211 212 219 public void setTypeFromString(String typeName) { 220 try { 221 setType(Type.valueOf(typeName)); 222 } 223 catch (Exception e) { 224 if (parent != null) { 225 setType(Type.defaulType); 226 } 227 else { 228 setType(Type.number); 229 } 230 } 231 } 232 233 239 public void setContext(String prefix) { 240 this.contextPrefix = prefix; 241 setTypeFromString(JiveGlobals.getProperty(prefix + ".type")); 242 String maxNumberString = JiveGlobals.getProperty(prefix + ".maxNumber"); 243 if (maxNumberString != null && maxNumberString.trim().length() > 0){ 244 try { 245 setMaxNumber(Integer.parseInt(maxNumberString)); 246 }catch (Exception e){ 247 Log.info("Jive property " 248 + prefix + ".maxNumber not a valid number."); 249 } 250 } 251 } 252 253 260 public boolean hasChangedSubject() { 261 return roomSubject != null; 262 } 263 } 264 | Popular Tags |