SPARK-1284 now the privacylists are also stored locally to increase the performance.

git-svn-id: http://svn.igniterealtime.org/svn/repos/spark/trunk@12256 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Holger Bergunde
2011-04-18 11:36:35 +00:00
committed by holger.bergunde
parent c39a4e7f46
commit a04e2421cd
2 changed files with 70 additions and 36 deletions

View File

@ -24,7 +24,9 @@ import org.jivesoftware.sparkimpl.plugin.privacy.list.SparkPrivacyListListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.jivesoftware.resource.SparkRes; import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.smack.PrivacyList; import org.jivesoftware.smack.PrivacyList;
@ -46,7 +48,9 @@ public class PrivacyManager implements SparkPrivacyListListener {
private static PrivacyManager singleton; private static PrivacyManager singleton;
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static SparkPrivacyList _activeList = null;
private List<String> _nameList = new ArrayList<String>();
private Map<String,PrivacyList> _privacyLists = new HashMap<String, PrivacyList>();
private PrivacyListManager privacyManager; private PrivacyListManager privacyManager;
private PrivacyListBlackList blackList; private PrivacyListBlackList blackList;
@ -64,6 +68,7 @@ public class PrivacyManager implements SparkPrivacyListListener {
Log.error("Privacy plugin: Connection not initialized."); Log.error("Privacy plugin: Connection not initialized.");
} }
privacyManager = PrivacyListManager.getInstanceFor(conn); privacyManager = PrivacyListManager.getInstanceFor(conn);
forceReloadLists();
} }
/** /**
@ -77,8 +82,10 @@ public class PrivacyManager implements SparkPrivacyListListener {
synchronized (LOCK) { synchronized (LOCK) {
if (null == singleton) { if (null == singleton) {
singleton = new PrivacyManager(); singleton = new PrivacyManager();
} }
} }
return singleton; return singleton;
} }
@ -91,11 +98,12 @@ public class PrivacyManager implements SparkPrivacyListListener {
Log.warning("Could not remove PrivacyList "+listName); Log.warning("Could not remove PrivacyList "+listName);
e.printStackTrace(); e.printStackTrace();
} }
singleton.forceReloadLists();
} }
private SparkPrivacyList getSparkListFromPrivacyList (String listName) {
public SparkPrivacyList getPrivacyList(String listName) {
SparkPrivacyList privacyList = new SparkPrivacyList(listName) { SparkPrivacyList privacyList = new SparkPrivacyList(listName) {
@Override @Override
@ -132,23 +140,57 @@ public class PrivacyManager implements SparkPrivacyListListener {
* Returns the active PrivacyList * Returns the active PrivacyList
* @return the list if there is one, else null * @return the list if there is one, else null
*/ */
public SparkPrivacyList getActiveList() public SparkPrivacyList getActiveList() {
{
if (!hasActiveList()) if (!hasActiveList())
return null; return null;
PrivacyList list = null; if (_activeList == null) {
try { try {
list = getPrivacyListManager().getActiveList(); _activeList = this.getSparkListFromPrivacyList(privacyManager.getActiveList().toString());
} catch (XMPPException e) } catch (XMPPException e) {
Log.warning("server reportet there is an active list available, but could not load id",e);
}
}
return _activeList;
}
/**
* this forces the PrivacyManager to reload the active list. maybe another
* resource changed the active list, we can check this by updating the
* active list
*/
public void forceActiveListReload() {
try {
_activeList = this.getSparkListFromPrivacyList(privacyManager.getActiveList().toString());
} catch (XMPPException e) {
Log.warning("force reload active list failed", e);
}
}
public SparkPrivacyList getPrivacyList(String s)
{ {
return getSparkListFromPrivacyList(_privacyLists.get(s).toString());
}
public void forceReloadLists() {
_nameList = new ArrayList<String>();
try {
for (PrivacyList pl : privacyManager.getPrivacyLists()) {
_nameList.add(pl.toString());
_privacyLists.put(pl.toString(), pl);
}
} catch (XMPPException e) {
Log.warning("Error load privaylist names");
e.printStackTrace(); e.printStackTrace();
} }
;
return getPrivacyList(list.toString());
} }
/** /**
* Check if active list exist * Check if active list exist
* *
@ -207,7 +249,7 @@ public class PrivacyManager implements SparkPrivacyListListener {
} catch (XMPPException e) { } catch (XMPPException e) {
if (!(e.getXMPPError().getCode() == 404)) if (!(e.getXMPPError().getCode() == 404))
{ {
System.out.println(e.getXMPPError().getCode()); Log.warning("error creating list", e);
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -223,17 +265,7 @@ public class PrivacyManager implements SparkPrivacyListListener {
public List<String> getPrivacyListNames() public List<String> getPrivacyListNames()
{ {
List<String> nameList = new ArrayList<String>(); return _nameList;
try {
for (PrivacyList pl :privacyManager.getPrivacyLists())
{
nameList.add(pl.toString());
}
} catch (XMPPException e) {
Log.warning("Error load privaylist names");
e.printStackTrace();
}
return nameList;
} }
/** /**

View File

@ -404,6 +404,7 @@ public abstract class SparkPrivacyList {
*/ */
public void setListAsActive() throws XMPPException { public void setListAsActive() throws XMPPException {
PrivacyManager.getInstance().getPrivacyListManager().setActiveListName( getListName() ); PrivacyManager.getInstance().getPrivacyListManager().setActiveListName( getListName() );
PrivacyManager.getInstance().forceActiveListReload();
if (listName.equals(PrivacyManager.getInstance().getBlackList().getListName())) if (listName.equals(PrivacyManager.getInstance().getBlackList().getListName()))
{ {
final Presence myPresence = SparkManager.getWorkspace().getStatusBar() final Presence myPresence = SparkManager.getWorkspace().getStatusBar()
@ -470,6 +471,7 @@ public abstract class SparkPrivacyList {
getPrivacyItems().add(defItem); //Add default privacy action getPrivacyItems().add(defItem); //Add default privacy action
PrivacyManager.getInstance().getPrivacyListManager().createPrivacyList(getListName(), getPrivacyItems()); PrivacyManager.getInstance().getPrivacyListManager().createPrivacyList(getListName(), getPrivacyItems());
getPrivacyItems().remove(defItem); // remove Default item it will be added on next save operation getPrivacyItems().remove(defItem); // remove Default item it will be added on next save operation
PrivacyManager.getInstance().forceReloadLists();
} }
/** /**