From 977499c4cc51999806dfd8779cc8bc1f9ad725cf Mon Sep 17 00:00:00 2001 From: Ben Kelsey Date: Sat, 12 Aug 2017 18:22:05 -0700 Subject: [PATCH] Refactor to remove serviceLocator - This removes all dependencies on deprecated ServiceLocator class. - Also does some cleanup in the settingsService Signed-off-by: Ben Kelsey --- .../player/controller/CoverArtController.java | 10 +-- .../player/service/MediaFileService.java | 6 +- .../player/service/ServiceLocator.java | 40 ----------- .../player/service/SettingsService.java | 70 ++++++------------- .../metadata/DefaultMetaDataParser.java | 11 +++ .../player/service/metadata/FFmpegParser.java | 14 +++- .../service/metadata/JaudiotaggerParser.java | 11 +++ .../service/metadata/MetaDataParser.java | 9 +-- .../resources/applicationContext-service.xml | 14 +++- .../metadata/MetaDataParserTestCase.java | 6 ++ 10 files changed, 87 insertions(+), 104 deletions(-) delete mode 100644 airsonic-main/src/main/java/org/airsonic/player/service/ServiceLocator.java diff --git a/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java b/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java index 97b56b25..711d0a50 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java +++ b/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java @@ -82,6 +82,8 @@ public class CoverArtController implements LastModified { private ArtistDao artistDao; @Autowired private AlbumDao albumDao; + @Autowired + private JaudiotaggerParser jaudiotaggerParser; private Semaphore semaphore; @PostConstruct @@ -217,8 +219,7 @@ public class CoverArtController implements LastModified { private void sendUnscaled(CoverArtRequest coverArtRequest, HttpServletResponse response) throws IOException { File file = coverArtRequest.getCoverArt(); - JaudiotaggerParser parser = new JaudiotaggerParser(); - if (!parser.isApplicable(file)) { + if (!jaudiotaggerParser.isApplicable(file)) { response.setContentType(StringUtil.getMimeType(FilenameUtils.getExtension(file.getName()))); } InputStream in = null; @@ -274,10 +275,9 @@ public class CoverArtController implements LastModified { * the embedded album art is returned. */ private InputStream getImageInputStream(File file) throws IOException { - JaudiotaggerParser parser = new JaudiotaggerParser(); - if (parser.isApplicable(file)) { + if (jaudiotaggerParser.isApplicable(file)) { MediaFile mediaFile = mediaFileService.getMediaFile(file); - return new ByteArrayInputStream(parser.getImageData(mediaFile)); + return new ByteArrayInputStream(jaudiotaggerParser.getImageData(mediaFile)); } else { return new FileInputStream(file); } diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/MediaFileService.java b/airsonic-main/src/main/java/org/airsonic/player/service/MediaFileService.java index d1e58540..37ad3fa7 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/MediaFileService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/MediaFileService.java @@ -52,6 +52,7 @@ public class MediaFileService { private SettingsService settingsService; private MediaFileDao mediaFileDao; private AlbumDao albumDao; + private JaudiotaggerParser parser; private MetaDataParserFactory metaDataParserFactory; private boolean memoryCacheEnabled = true; @@ -600,7 +601,6 @@ public class MediaFileService { } // Look for embedded images in audiofiles. (Only check first audio file encountered). - JaudiotaggerParser parser = new JaudiotaggerParser(); for (File candidate : candidates) { if (parser.isApplicable(candidate)) { if (parser.isImageAvailable(getMediaFile(candidate))) { @@ -706,4 +706,8 @@ public class MediaFileService { public void setAlbumDao(AlbumDao albumDao) { this.albumDao = albumDao; } + + public void setParser(JaudiotaggerParser parser) { + this.parser = parser; + } } diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/ServiceLocator.java b/airsonic-main/src/main/java/org/airsonic/player/service/ServiceLocator.java deleted file mode 100644 index 750493ef..00000000 --- a/airsonic-main/src/main/java/org/airsonic/player/service/ServiceLocator.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - This file is part of Airsonic. - - Airsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Airsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Airsonic. If not, see . - - Copyright 2016 (C) Airsonic Authors - Based upon Subsonic, Copyright 2009 (C) Sindre Mehus - */ -package org.airsonic.player.service; - -/** - * Locates services for objects that are not part of the Spring context. - * - * @author Sindre Mehus - */ -@Deprecated -public class ServiceLocator { - - private static SettingsService settingsService; - - public static SettingsService getSettingsService() { - return settingsService; - } - - public static void setSettingsService(SettingsService settingsService) { - ServiceLocator.settingsService = settingsService; - } -} - diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java b/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java index 728ecb89..a5e9e121 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/SettingsService.java @@ -87,7 +87,6 @@ public class SettingsService { private static final String KEY_LDAP_SEARCH_FILTER = "LdapSearchFilter"; private static final String KEY_LDAP_AUTO_SHADOWING = "LdapAutoShadowing"; private static final String KEY_GETTING_STARTED_ENABLED = "GettingStartedEnabled"; - private static final String KEY_SERVER_ID = "ServerId"; private static final String KEY_SETTINGS_CHANGED = "SettingsChanged"; private static final String KEY_LAST_SCANNED = "LastScanned"; private static final String KEY_ORGANIZE_BY_FOLDER_STRUCTURE = "OrganizeByFolderStructure"; @@ -164,7 +163,6 @@ public class SettingsService { private static final String DEFAULT_LDAP_SEARCH_FILTER = "(sAMAccountName={0})"; private static final boolean DEFAULT_LDAP_AUTO_SHADOWING = false; private static final boolean DEFAULT_GETTING_STARTED_ENABLED = true; - private static final String DEFAULT_SERVER_ID = null; private static final long DEFAULT_SETTINGS_CHANGED = 0L; private static final boolean DEFAULT_ORGANIZE_BY_FOLDER_STRUCTURE = true; private static final boolean DEFAULT_SORT_ALBUMS_BY_YEAR = true; @@ -222,9 +220,9 @@ public class SettingsService { private String[] cachedMusicFileTypesArray; private String[] cachedVideoFileTypesArray; private List cachedMusicFolders; - private final ConcurrentMap> cachedMusicFoldersPerUser = new ConcurrentHashMap>(); + private final ConcurrentMap> cachedMusicFoldersPerUser = new ConcurrentHashMap<>(); - private void removeObseleteProperties() { + private void removeObsoleteProperties() { OBSOLETE_KEYS.forEach( oKey -> { if(configurationService.containsKey(oKey)) { @@ -254,7 +252,7 @@ public class SettingsService { return home; } - public static String getFileSystemAppName() { + private static String getFileSystemAppName() { String home = getAirsonicHome().getPath(); return home.contains("libresonic") ? "libresonic" : "airsonic"; } @@ -275,7 +273,6 @@ public class SettingsService { */ public void init() { logServerInfo(); - ServiceLocator.setSettingsService(this); } private void logServerInfo() { @@ -289,7 +286,7 @@ public class SettingsService { public void save(boolean updateSettingsChanged) { if(updateSettingsChanged) { - removeObseleteProperties(); + removeObsoleteProperties(); this.setLong(KEY_SETTINGS_CHANGED, System.currentTimeMillis()); } configurationService.save(); @@ -308,7 +305,7 @@ public class SettingsService { } } - public static File getPropertyFile() { + static File getPropertyFile() { File propertyFile = getAirsonicHome(); return new File(propertyFile, getFileSystemAppName() + ".properties"); } @@ -361,7 +358,7 @@ public class SettingsService { return getProperty(KEY_IGNORED_ARTICLES, DEFAULT_IGNORED_ARTICLES); } - public String[] getIgnoredArticlesAsArray() { + String[] getIgnoredArticlesAsArray() { return getIgnoredArticles().split("\\s+"); } @@ -373,7 +370,7 @@ public class SettingsService { return getProperty(KEY_SHORTCUTS, DEFAULT_SHORTCUTS); } - public String[] getShortcutsAsArray() { + String[] getShortcutsAsArray() { return StringUtil.split(getShortcuts()); } @@ -398,7 +395,7 @@ public class SettingsService { cachedMusicFileTypesArray = null; } - public synchronized String[] getMusicFileTypesAsArray() { + synchronized String[] getMusicFileTypesAsArray() { if (cachedMusicFileTypesArray == null) { cachedMusicFileTypesArray = toStringArray(getMusicFileTypes()); } @@ -430,7 +427,7 @@ public class SettingsService { cachedCoverArtFileTypesArray = null; } - public synchronized String[] getCoverArtFileTypesAsArray() { + synchronized String[] getCoverArtFileTypesAsArray() { if (cachedCoverArtFileTypesArray == null) { cachedCoverArtFileTypesArray = toStringArray(getCoverArtFileTypes()); } @@ -613,7 +610,7 @@ public class SettingsService { setProperty(KEY_HLS_COMMAND, command); } - public String getJukeboxCommand() { + String getJukeboxCommand() { return getProperty(KEY_JUKEBOX_COMMAND, DEFAULT_JUKEBOX_COMMAND); } public String getVideoImageCommand() { @@ -687,14 +684,6 @@ public class SettingsService { setBoolean(KEY_GETTING_STARTED_ENABLED, isGettingStartedEnabled); } - public String getServerId() { - return getProperty(KEY_SERVER_ID, DEFAULT_SERVER_ID); - } - - public void setServerId(String serverId) { - setProperty(KEY_SERVER_ID, serverId); - } - public long getSettingsChanged() { return getLong(KEY_SETTINGS_CHANGED, DEFAULT_SETTINGS_CHANGED); } @@ -704,7 +693,7 @@ public class SettingsService { return lastScanned == null ? null : new Date(Long.parseLong(lastScanned)); } - public void setLastScanned(Date date) { + void setLastScanned(Date date) { if (date == null) { setProperty(KEY_LAST_SCANNED, null); } else { @@ -732,7 +721,7 @@ public class SettingsService { return MediaLibraryStatistics.parse(getString(KEY_MEDIA_LIBRARY_STATISTICS, DEFAULT_MEDIA_LIBRARY_STATISTICS)); } - public void setMediaLibraryStatistics(MediaLibraryStatistics statistics) { + void setMediaLibraryStatistics(MediaLibraryStatistics statistics) { setString(KEY_MEDIA_LIBRARY_STATISTICS, statistics.format()); } @@ -785,7 +774,7 @@ public class SettingsService { */ public synchronized Theme[] getAvailableThemes() { if (themes == null) { - themes = new ArrayList(); + themes = new ArrayList<>(); try { InputStream in = SettingsService.class.getResourceAsStream(THEMES_FILE); String[] lines = StringUtil.readLines(in); @@ -814,7 +803,7 @@ public class SettingsService { */ public synchronized Locale[] getAvailableLocales() { if (locales == null) { - locales = new ArrayList(); + locales = new ArrayList<>(); try { InputStream in = SettingsService.class.getResourceAsStream(LOCALES_FILE); String[] lines = StringUtil.readLines(in); @@ -876,7 +865,7 @@ public class SettingsService { cachedMusicFolders = musicFolderDao.getAllMusicFolders(); } - List result = new ArrayList(cachedMusicFolders.size()); + List result = new ArrayList<>(cachedMusicFolders.size()); for (MusicFolder folder : cachedMusicFolders) { if ((includeDisabled || folder.isEnabled()) && (includeNonExisting || FileUtil.exists(folder.getPath()))) { result.add(folder); @@ -913,7 +902,7 @@ public class SettingsService { return allowed; } MusicFolder selected = getMusicFolderById(selectedMusicFolderId); - return allowed.contains(selected) ? Arrays.asList(selected) : Collections.emptyList(); + return allowed.contains(selected) ? Collections.singletonList(selected) : Collections.emptyList(); } /** @@ -993,21 +982,6 @@ public class SettingsService { return getAllInternetRadios(false); } - /** - * Returns the internet radio station with the given ID. - * - * @param id The ID. - * @return The internet radio station with the given ID, or null if not found. - */ - public InternetRadio getInternetRadioById(Integer id) { - for (InternetRadio radio : getAllInternetRadios()) { - if (id.equals(radio.getId())) { - return radio; - } - } - return null; - } - /** * Returns all internet radio stations. * @@ -1016,7 +990,7 @@ public class SettingsService { */ public List getAllInternetRadios(boolean includeAll) { List all = internetRadioDao.getAllInternetRadios(); - List result = new ArrayList(all.size()); + List result = new ArrayList<>(all.size()); for (InternetRadio folder : all) { if (includeAll || folder.isEnabled()) { result.add(folder); @@ -1190,14 +1164,10 @@ public class SettingsService { setString(KEY_SONOS_SERVICE_NAME, sonosServiceName); } - public int getSonosServiceId() { + int getSonosServiceId() { return getInt(KEY_SONOS_SERVICE_ID, DEFAULT_SONOS_SERVICE_ID); } - public void setSonosServiceId(int sonosServiceid) { - setInt(KEY_SONOS_SERVICE_ID, sonosServiceid); - } - private void setProperty(String key, Object value) { if (value == null) { configurationService.clearProperty(key); @@ -1207,7 +1177,7 @@ public class SettingsService { } private String[] toStringArray(String s) { - List result = new ArrayList(); + List result = new ArrayList<>(); StringTokenizer tokenizer = new StringTokenizer(s, " "); while (tokenizer.hasMoreTokens()) { result.add(tokenizer.nextToken()); @@ -1378,7 +1348,7 @@ public class SettingsService { setDatabaseConfigType(DEFAULT_DATABASE_CONFIG_TYPE); } - public String getPlaylistExportFormat() { + String getPlaylistExportFormat() { return getProperty(KEY_EXPORT_PLAYLIST_FORMAT, DEFAULT_EXPORT_PLAYLIST_FORMAT); } } diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/DefaultMetaDataParser.java b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/DefaultMetaDataParser.java index 147667e2..c7bc4fbe 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/DefaultMetaDataParser.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/DefaultMetaDataParser.java @@ -20,6 +20,7 @@ package org.airsonic.player.service.metadata; import org.airsonic.player.domain.MediaFile; +import org.airsonic.player.service.SettingsService; import java.io.File; @@ -30,6 +31,11 @@ import java.io.File; */ public class DefaultMetaDataParser extends MetaDataParser { + private final SettingsService settingsService; + + public DefaultMetaDataParser(SettingsService settingsService) { + this.settingsService = settingsService; + } /** * Parses meta data for the given file. * @@ -65,6 +71,11 @@ public class DefaultMetaDataParser extends MetaDataParser { return false; } + @Override + SettingsService getSettingsService() { + return settingsService; + } + /** * Returns whether this parser is applicable to the given file. * diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/FFmpegParser.java b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/FFmpegParser.java index 314e5329..af036cd9 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/FFmpegParser.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/FFmpegParser.java @@ -21,7 +21,7 @@ package org.airsonic.player.service.metadata; import org.airsonic.player.domain.MediaFile; import org.airsonic.player.io.InputStreamReaderThread; -import org.airsonic.player.service.ServiceLocator; +import org.airsonic.player.service.SettingsService; import org.airsonic.player.service.TranscodingService; import org.airsonic.player.util.StringUtil; import org.apache.commons.io.FilenameUtils; @@ -49,6 +49,7 @@ public class FFmpegParser extends MetaDataParser { private static final Pattern PAR_PATTERN = Pattern.compile("PAR (\\d+):(\\d+)"); private TranscodingService transcodingService; + private SettingsService settingsService; /** * Parses meta data for the given music file. No guessing or reformatting is done. @@ -148,6 +149,11 @@ public class FFmpegParser extends MetaDataParser { return false; } + @Override + SettingsService getSettingsService() { + return settingsService; + } + /** * Returns whether this parser is applicable to the given file. * @@ -158,7 +164,7 @@ public class FFmpegParser extends MetaDataParser { public boolean isApplicable(File file) { String format = FilenameUtils.getExtension(file.getName()).toLowerCase(); - for (String s : ServiceLocator.getSettingsService().getVideoFileTypesAsArray()) { + for (String s : settingsService.getVideoFileTypesAsArray()) { if (format.equals(s)) { return true; } @@ -169,4 +175,8 @@ public class FFmpegParser extends MetaDataParser { public void setTranscodingService(TranscodingService transcodingService) { this.transcodingService = transcodingService; } + + public void setSettingsService(SettingsService settingsService) { + this.settingsService = settingsService; + } } \ No newline at end of file diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/JaudiotaggerParser.java b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/JaudiotaggerParser.java index 32f4534e..9105c2c2 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/JaudiotaggerParser.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/JaudiotaggerParser.java @@ -20,6 +20,7 @@ package org.airsonic.player.service.metadata; import org.airsonic.player.domain.MediaFile; +import org.airsonic.player.service.SettingsService; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; import org.jaudiotagger.audio.AudioFile; @@ -51,6 +52,11 @@ public class JaudiotaggerParser extends MetaDataParser { private static final Pattern GENRE_PATTERN = Pattern.compile("\\((\\d+)\\).*"); private static final Pattern TRACK_NUMBER_PATTERN = Pattern.compile("(\\d+)/\\d+"); private static final Pattern YEAR_NUMBER_PATTERN = Pattern.compile("(\\d{4}).*"); + private final SettingsService settingsService; + + public JaudiotaggerParser(SettingsService settingsService) { + this.settingsService = settingsService; + } static { try { @@ -265,6 +271,11 @@ public class JaudiotaggerParser extends MetaDataParser { return true; } + @Override + SettingsService getSettingsService() { + return settingsService; + } + /** * Returns whether this parser is applicable to the given file. * diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/MetaDataParser.java b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/MetaDataParser.java index fc76ada0..0bb69daa 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/metadata/MetaDataParser.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/metadata/MetaDataParser.java @@ -21,7 +21,6 @@ package org.airsonic.player.service.metadata; import org.airsonic.player.domain.MediaFile; import org.airsonic.player.domain.MusicFolder; -import org.airsonic.player.service.ServiceLocator; import org.airsonic.player.service.SettingsService; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; @@ -108,7 +107,7 @@ public abstract class MetaDataParser { /** * Guesses the artist for the given file. */ - public String guessArtist(File file) { + String guessArtist(File file) { File parent = file.getParentFile(); if (isRoot(parent)) { return null; @@ -120,7 +119,7 @@ public abstract class MetaDataParser { /** * Guesses the album for the given file. */ - public String guessAlbum(File file, String artist) { + String guessAlbum(File file, String artist) { File parent = file.getParentFile(); String album = isRoot(parent) ? null : parent.getName(); if (artist != null && album != null) { @@ -137,7 +136,7 @@ public abstract class MetaDataParser { } private boolean isRoot(File file) { - SettingsService settings = ServiceLocator.getSettingsService(); + SettingsService settings = getSettingsService(); List folders = settings.getAllMusicFolders(false, true); for (MusicFolder folder : folders) { if (file.equals(folder.getPath())) { @@ -147,6 +146,8 @@ public abstract class MetaDataParser { return false; } + abstract SettingsService getSettingsService(); + /** * Removes any prefixed track number from the given title string. * diff --git a/airsonic-main/src/main/resources/applicationContext-service.xml b/airsonic-main/src/main/resources/applicationContext-service.xml index 1afc71de..7af6285f 100644 --- a/airsonic-main/src/main/resources/applicationContext-service.xml +++ b/airsonic-main/src/main/resources/applicationContext-service.xml @@ -82,6 +82,7 @@ + @@ -233,14 +234,23 @@ + + + + - + + + + + + + - diff --git a/airsonic-main/src/test/java/org/airsonic/player/service/metadata/MetaDataParserTestCase.java b/airsonic-main/src/test/java/org/airsonic/player/service/metadata/MetaDataParserTestCase.java index 01959380..48740186 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/service/metadata/MetaDataParserTestCase.java +++ b/airsonic-main/src/test/java/org/airsonic/player/service/metadata/MetaDataParserTestCase.java @@ -21,6 +21,7 @@ package org.airsonic.player.service.metadata; import junit.framework.TestCase; import org.airsonic.player.domain.MediaFile; +import org.airsonic.player.service.SettingsService; import java.io.File; @@ -45,6 +46,11 @@ public class MetaDataParserTestCase extends TestCase { return false; } + @Override + SettingsService getSettingsService() { + return null; + } + public boolean isApplicable(File file) { return false; }