Skip to content

Commit

Permalink
Merge pull request #880 from stokito/empty_string
Browse files Browse the repository at this point in the history
SPARK-2337 refactoring, cleanup and minor perf improvement
  • Loading branch information
Plyha authored Sep 7, 2024
2 parents 8419213 + 74dec45 commit 3e43f74
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 91 deletions.
13 changes: 7 additions & 6 deletions core/src/main/java/org/jivesoftware/spark/SoundManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ public SoundManager() {
* @return the AudioClip found. If no audio clip was found, returns null.
*/
public AudioClip getClip(String clip) {
if (!clipMap.containsKey(clip)) {
AudioClip audioClip = clipMap.get(clip);
if (audioClip == null) {
// Add new clip
final AudioClip newClip = loadClipForURL(clip);
if (newClip != null) {
clipMap.put(clip, newClip);
audioClip = loadClipForURL(clip);
if (audioClip != null) {
clipMap.put(clip, audioClip);
}
}

return clipMap.get(clip);
return audioClip;
}

/**
Expand Down Expand Up @@ -140,4 +141,4 @@ private AudioClip loadClipForURL(String clipOfURL) {
}


}
}
3 changes: 1 addition & 2 deletions core/src/main/java/org/jivesoftware/spark/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,8 @@ public void searchContacts(String contact, final JFrame parent) {
for (ContactGroup contactGroup : contactList.getContactGroups()) {
contactGroup.clearSelection();
for (ContactItem contactItem : contactGroup.getContactItems()) {
if (!contactMap.containsKey(contactItem.getJid().toString())) {
if (contactMap.putIfAbsent(contactItem.getJid().toString(), contactItem) == null) {
contacts.add(contactItem);
contactMap.put(contactItem.getJid().toString(), contactItem);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public void insertString(int offs, String str, AttributeSet a)
}

public String autoComplete(String text) {
for (Object aDictionary : dictionary) {
String word = (String) aDictionary;
for (String word : dictionary) {
if (word.startsWith(text)) {
return word.substring(text.length());
}
Expand Down
47 changes: 22 additions & 25 deletions core/src/main/java/org/jivesoftware/spark/ui/ContactList.java
Original file line number Diff line number Diff line change
Expand Up @@ -1705,37 +1705,34 @@ private void clearSelectionList(ContactItem selectedItem) {


private void sendMessages(Collection<ContactItem> items) {
StringBuilder buf = new StringBuilder();
InputDialog dialog = new InputDialog();
final String messageText = dialog.getInput(Res.getString("title.broadcast.message"), Res.getString("message.enter.broadcast.message"), SparkRes.getImageIcon(SparkRes.BLANK_IMAGE), SparkManager.getMainWindow());
if (ModelUtil.hasLength(messageText)) {

final Map<String, Message> broadcastMessages = new HashMap<>();
for (ContactItem item : items) {
final Message message = new Message();
message.setTo(item.getJid());
final Map<String, Object> properties = new HashMap<>();
properties.put("broadcast", true);
message.addExtension(new JivePropertiesExtension(properties));
message.setBody(messageText);
if (!broadcastMessages.containsKey(item.getJid().toString())) {
buf.append(item.getDisplayName()).append("\n");
broadcastMessages.put(item.getJid().toString(), message);
}
if (!ModelUtil.hasLength(messageText)) {
return;
}
StringBuilder buf = new StringBuilder();
final Map<String, Message> broadcastMessages = new HashMap<>();
for (ContactItem item : items) {
final Message message = new Message();
message.setTo(item.getJid());
final Map<String, Object> properties = new HashMap<>();
properties.put("broadcast", true);
message.addExtension(new JivePropertiesExtension(properties));
message.setBody(messageText);
if (broadcastMessages.putIfAbsent(item.getJid().toString(), message) == null) {
buf.append(item.getDisplayName()).append('\n');
}
}

for (Message message : broadcastMessages.values()) {
try {
SparkManager.getConnection().sendStanza(message);
} catch (SmackException.NotConnectedException | InterruptedException e) {
Log.warning("Unable to send broadcast to " + message.getTo(), e);
}
for (Message message : broadcastMessages.values()) {
try {
SparkManager.getConnection().sendStanza(message);
} catch (SmackException.NotConnectedException | InterruptedException e) {
Log.warning("Unable to send broadcast to " + message.getTo(), e);
}
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.hasbeenbroadcast.to", buf.toString()), Res.getString("title.notification"), JOptionPane.INFORMATION_MESSAGE);
}


UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.hasbeenbroadcast.to", buf.toString()), Res.getString("title.notification"), JOptionPane.INFORMATION_MESSAGE);
}

// For plugin use only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ private void init( boolean config_from_file )
public AppConfigurationEntry[] getAppConfigurationEntry( String name )
{
AppConfigurationEntry[] a = new AppConfigurationEntry[ 1 ];
if ( configs.containsKey( name ) )
Vector<AppConfigurationEntry> v = configs.get( name );
if ( v != null )
{
Vector<AppConfigurationEntry> v = configs.get( name );
a = v.toArray( a );
return a;
}
Expand All @@ -81,16 +81,7 @@ public AppConfigurationEntry[] getAppConfigurationEntry( String name )

public boolean putAppConfigurationEntry( String name, String module, AppConfigurationEntry.LoginModuleControlFlag controlFlag, Map<String, String> options )
{
Vector<AppConfigurationEntry> v;
if ( configs.containsKey( name ) )
{
v = configs.get( name );
}
else
{
v = new Vector<>();
configs.put( name, v );
}
Vector<AppConfigurationEntry> v = configs.computeIfAbsent(name, k -> new Vector<>());

return v.add( new AppConfigurationEntry( module, controlFlag, options ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ public final class OIDTranslator {
}

public static String getDescription(String oid) {
if (OIDtoDescriptionMap.containsKey(oid)) {
return OIDtoDescriptionMap.get(oid);
String description = OIDtoDescriptionMap.get(oid);
if (description != null) {
return description;
} else {
Log.warning("Unknown description for Object ID (OID: " + oid + ")");
return Res.getString("cert.unknown.oid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.awt.event.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -285,9 +286,7 @@ public void shutdown() {
// Write out new File
try {
File conFile = new File(transcriptDir, "conversations.xml");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(conFile), StandardCharsets.UTF_8));
out.write(builder.toString());
out.close();
Files.write(conFile.toPath(), builder.toString().getBytes(StandardCharsets.UTF_8));
}
catch (IOException e) {
Log.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@
import org.jxmpp.jid.impl.JidCreate;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -122,9 +120,7 @@ private static void writeToFile(File transcriptFile, Collection<HistoryMessage>
// Write out new File
try {
transcriptFile.getParentFile().mkdirs();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(transcriptFile), StandardCharsets.UTF_8));
out.write(builder.toString());
out.close();
Files.write(transcriptFile.toPath(), builder.toString().getBytes(StandardCharsets.UTF_8));
}
catch (IOException e) {
Log.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -134,9 +135,8 @@ private void loadInstalledPlugins()
{
PluginManager pluginManager = PluginManager.getInstance();
List<PublicPlugin> plugins = pluginManager.getPublicPlugins();
for ( Object plugin1 : plugins )
for ( PublicPlugin plugin : plugins )
{
PublicPlugin plugin = (PublicPlugin) plugin1;
final SparkPlugUI ui = new SparkPlugUI( plugin );
ui.useLocalIcon();
installedPanel.add( ui );
Expand Down Expand Up @@ -504,37 +504,27 @@ public Collection<PublicPlugin> getPluginList( InputStream response )
catch ( DocumentException | SAXException e )
{
Log.error( e );
return Collections.emptyList();
}

String sparkVersion = JiveInfo.getVersion();
List<? extends Node> plugins = pluginXML.selectNodes( "/plugins/plugin" );

for ( Node plugin1 : plugins )
{
PublicPlugin publicPlugin = new PublicPlugin();

String clazz;
String name = null;
try
{
Element plugin = (Element) plugin1;

try
{
String version = plugin.selectSingleNode( "minSparkVersion" ).getText();
if ( !isGreaterOrEqual( JiveInfo.getVersion(), version ) )
{
Log.error( "Unable to load plugin " + name + " due to min version incompatibility." );
continue;
}
}
catch ( Exception e )
String name = plugin.selectSingleNode("name").getText();
String minSparkVersion = plugin.selectSingleNode( "minSparkVersion" ).getText();
if ( !isGreaterOrEqual( sparkVersion, minSparkVersion ) )
{
Log.error( "Unable to load plugin " + name + " due to no minSparkVersion." );
Log.error( "Unable to load plugin " + name + " due to min version incompatibility." );
continue;
}

name = plugin.selectSingleNode( "name" ).getText();
clazz = plugin.selectSingleNode( "class" ).getText();
String clazz = plugin.selectSingleNode("class").getText();
PublicPlugin publicPlugin = new PublicPlugin();
publicPlugin.setPluginClass( clazz );
publicPlugin.setName( name );

Expand Down Expand Up @@ -605,10 +595,8 @@ public Collection<PublicPlugin> getPluginList( InputStream response )
}
catch ( Exception ex )
{
ex.printStackTrace();
Log.error(ex);
}


}
return pluginList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
Expand Down Expand Up @@ -430,8 +431,9 @@ public VCard getVCard(BareJid jid) {
*/
public VCard getVCardFromMemory(BareJid jid) {
// Check in memory first.
if (vcards.containsKey(jid)) {
return vcards.get(jid);
VCard currentVcard = vcards.get(jid);
if (currentVcard != null) {
return currentVcard;
}

// if not in memory
Expand All @@ -448,9 +450,9 @@ public VCard getVCardFromMemory(BareJid jid) {
}

/**
* Returns the VCard. You should always use useChachedVCards. VCardManager
* will keep the VCards up to date. If you wan't to force a network reload
* of the VCard you can set useChachedVCards to false. That means that you
* Returns the VCard. You should always use useCachedVCards. VCardManager
* will keep the VCards up to date. If you want to force a network reload
* of the VCard you can set useCachedVCards to false. That means that you
* have to wait for the vcard response. The method will block until the
* result is available or a timeout occurs (like reloadVCard(String jid)).
* If there is no response from server this method a dummy vcard with an
Expand Down Expand Up @@ -540,7 +542,8 @@ public void addVCard(BareJid jid, VCard vcard) {
if (vcard == null)
return;
vcard.setJabberId(jid.toString());
if (vcards.containsKey(jid) && vcards.get(jid).getError() == null && vcard.getError()!= null)
VCard currentVcard = vcards.get(jid);
if (currentVcard != null && currentVcard.getError() == null && vcard.getError()!= null)
{
return;

Expand Down Expand Up @@ -757,9 +760,7 @@ private void persistVCard(BareJid jid, VCard vcard) {

// write xml to file
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(vcardFile), StandardCharsets.UTF_8));
out.write(xml);
out.close();
Files.write(vcardFile.toPath(), xml.getBytes(StandardCharsets.UTF_8));
}
catch (IOException e) {
Log.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static String getVersion() {
return version.trim();
}

return null;
return "3.0.3"; // avoid null and return at least some current version
}

public static String getOS() {
Expand Down

0 comments on commit 3e43f74

Please sign in to comment.