Skip to content

Commit

Permalink
Updates of service-providers loading
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Aug 6, 2015
1 parent 67c5e84 commit dc6f2a7
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.jd.gui.view.PreferencesView
class PreferencesController {
PreferencesView preferencesView

PreferencesController(SwingBuilder swing, Configuration configuration, API api, List<PreferencesPanel> panels) {
PreferencesController(SwingBuilder swing, Configuration configuration, API api, Collection<PreferencesPanel> panels) {
// Create UI
preferencesView = new PreferencesView(swing, configuration, panels)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ package org.jd.gui.service.actions
import groovy.transform.CompileStatic
import org.jd.gui.api.API
import org.jd.gui.api.model.Container
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.ContextualActionsFactory

import javax.swing.Action

@Singleton(lazy = true)
class ContextualActionsFactoryService {
static final ActionNameComparator COMPARATOR = new ActionNameComparator()
protected static final ActionNameComparator COMPARATOR = new ActionNameComparator()

final List<ContextualActionsFactory> providers = ServiceLoader.load(ContextualActionsFactory).toList()
protected final Collection<ContextualActionsFactory> providers = ExtensionService.instance.load(ContextualActionsFactory)

@CompileStatic
Collection<Action> get(API api, Container.Entry entry, String fragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
package org.jd.gui.service.container

import org.jd.gui.api.API
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.ContainerFactory

import java.nio.file.Path

@Singleton(lazy = true)
class ContainerFactoryService {
private List<ContainerFactory> providers = ServiceLoader.load(ContainerFactory).toList()
protected final Collection<ContainerFactory> providers = ExtensionService.instance.load(ContainerFactory)

ContainerFactory get(API api, Path rootPath) {
return providers.find { it.accept(api, rootPath) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2008-2015 Emmanuel Dupuy
* This program is made available under the terms of the GPLv3 License.
*/

package org.jd.gui.service.extension

import groovy.transform.CompileStatic

@Singleton
@CompileStatic
class ExtensionService {
protected ClassLoader extensionClassLoader = initClassLoader()

protected ClassLoader initClassLoader() {
def extDirectory = new File("ext");

if (extDirectory.exists() && extDirectory.isDirectory()) {
List<URL> urls = []

searchJarAndMetaInf(urls, extDirectory)

if (! urls.isEmpty()) {
URL[] array = urls.sort { u1, u2 -> u1.path.compareTo(u2.path) }
return new URLClassLoader(array, ExtensionService.class.classLoader)
}
}

return ExtensionService.class.classLoader
}

protected void searchJarAndMetaInf(List<URL> urls, File directory) {
def metaInf = new File(directory, 'META-INF')

if (metaInf.exists() && metaInf.isDirectory()) {
urls.add(directory.toURI().toURL())
} else {
directory.eachFile { File child ->
if (child.isDirectory()) {
searchJarAndMetaInf(urls, child)
} else if (child.name.toLowerCase().endsWith('.jar')) {
urls.add(new URL('jar', '', child.toURI().toURL().toString() + '!/'))
}
}
}
}

public <T> Collection<T> load(Class<T> service) {
return ServiceLoader.load(service, extensionClassLoader).toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
package org.jd.gui.service.fileloader

import org.jd.gui.api.API
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.FileLoader

@Singleton(lazy = true)
class FileLoaderService {
final List<FileLoader> providers = ServiceLoader.load(FileLoader).toList()
protected final Collection<FileLoader> providers = ExtensionService.instance.load(FileLoader)

private Map<String, FileLoader> mapProviders = providers.collectEntries { provider ->
protected Map<String, FileLoader> mapProviders = providers.collectEntries { provider ->
provider.extensions.collectEntries { [it, provider] }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ package org.jd.gui.service.indexer

import groovy.transform.CompileStatic
import org.jd.gui.api.model.Container
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.Indexer

@CompileStatic
@Singleton(lazy = true)
class IndexerService {
protected List<Indexer> providers = ServiceLoader.load(Indexer).toList()
protected Map<String, Indexers> mapProviders = populate()

protected Map<String, Indexers> mapProviders = populate(providers)

protected Map<String, Indexers> populate(List<Indexer> providers) {
protected Map<String, Indexers> populate() {
Collection<Indexer> providers = ExtensionService.instance.load(Indexer)
Map<String, Indexers> mapProviders = [:]

def mapProvidersWithDefault = mapProviders.withDefault { new Indexers() }
Expand Down Expand Up @@ -63,19 +63,19 @@ class IndexerService {
}

static class Indexers {
ArrayList<Indexer> indexers = []
HashMap<String, Indexer> indexers = [:]
Indexer defaultIndexer

void add(Indexer indexer) {
if (indexer.pathPattern) {
indexers << indexer
indexers.put(indexer.pathPattern.pattern(), indexer)
} else {
defaultIndexer = indexer
}
}

Indexer match(String path) {
for (def indexer : indexers) {
for (def indexer : indexers.values()) {
if (path ==~ indexer.pathPattern) {
return indexer
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
package org.jd.gui.service.mainpanel

import org.jd.gui.api.model.Container
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.PanelFactory

@Singleton(lazy = true)
class PanelFactoryService {
protected List<PanelFactory> providers = ServiceLoader.load(PanelFactory).toList()
protected Map<String, PanelFactory> mapProviders = populate()

protected Map<String, PanelFactory> mapProviders = populate(providers)

protected Map<String, PanelFactory> populate(List<PanelFactory> providers) {
protected Map<String, PanelFactory> populate() {
Collection<PanelFactory> providers = ExtensionService.instance.load(PanelFactory)
Map<String, PanelFactory> mapProviders = [:]

for (def provider : providers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

package org.jd.gui.service.pastehandler

import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.PasteHandler

@Singleton(lazy = true)
class PasteHandlerService {
protected List<PasteHandler> providers = ServiceLoader.load(PasteHandler).toList()
protected final Collection<PasteHandler> providers = ExtensionService.instance.load(PasteHandler)

PasteHandler get(Object obj) {
for (def provider : providers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@

package org.jd.gui.service.preferencespanel

import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.PreferencesPanel

@Singleton(lazy = true)
class PreferencesPanelService {
final List<PreferencesPanel> providers = ServiceLoader.load(PreferencesPanel).toList().grep { it.isActivated() }
final Collection<PreferencesPanel> providers = populate()

protected Collection<PreferencesPanel> populate() {
Collection<PreferencesPanel> list = ExtensionService.instance.load(PreferencesPanel).grep { it.isActivated() }
Map<String, PreferencesPanel> map = [:]

for (def provider : list) {
map.put(provider.preferencesGroupTitle + '$' + provider.preferencesPanelTitle, provider)
}

return map.values()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ package org.jd.gui.service.sourcesaver

import groovy.transform.CompileStatic
import org.jd.gui.api.model.Container
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.SourceSaver

@CompileStatic
@Singleton(lazy = true)
class SourceSaverService {
protected List<SourceSaver> providers = ServiceLoader.load(SourceSaver).toList()
protected Map<String, SourceSavers> mapProviders = populate()

protected Map<String, SourceSavers> mapProviders = populate(providers)

protected Map<String, SourceSavers> populate(List<SourceSaver> providers) {
protected Map<String, SourceSavers> populate() {
Collection<SourceSaver> providers = ExtensionService.instance.load(SourceSaver)
Map<String, SourceSavers> mapProviders = [:]

def mapProvidersWithDefault = mapProviders.withDefault { new SourceSavers() }
Expand Down Expand Up @@ -63,19 +63,19 @@ class SourceSaverService {
}

static class SourceSavers {
ArrayList<SourceSaver> savers = []
HashMap<String, SourceSaver> savers = [:]
SourceSaver defaultSaver

void add(SourceSaver saver) {
if (saver.pathPattern) {
savers << saver
savers.put(saver.pathPattern.pattern(), saver)
} else {
defaultSaver = saver
}
}

SourceSaver match(String path) {
for (def saver : savers) {
for (def saver : savers.values()) {
if (path ==~ saver.pathPattern) {
return saver
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ package org.jd.gui.service.treenode

import groovy.transform.CompileStatic
import org.jd.gui.api.model.Container
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.TreeNodeFactory

@CompileStatic
@Singleton(lazy = true)
class TreeNodeFactoryService {
protected List<TreeNodeFactory> providers = ServiceLoader.load(TreeNodeFactory).toList()
protected Map<String, TreeNodeFactories> mapProviders = populate()

protected Map<String, TreeNodeFactories> mapProviders = populate(providers)

protected Map<String, TreeNodeFactories> populate(List<TreeNodeFactory> providers) {
protected Map<String, TreeNodeFactories> populate() {
Collection<TreeNodeFactory> providers = ExtensionService.instance.load(TreeNodeFactory)
Map<String, TreeNodeFactories> mapProviders = [:]

def mapProvidersWithDefault = mapProviders.withDefault { new TreeNodeFactories() }
Expand Down Expand Up @@ -64,19 +64,19 @@ class TreeNodeFactoryService {
}

static class TreeNodeFactories {
ArrayList<TreeNodeFactory> factories = []
HashMap<String, TreeNodeFactory> factories = [:]
TreeNodeFactory defaultFactory

void add(TreeNodeFactory factory) {
if (factory.pathPattern) {
factories << factory
factories.put(factory.pathPattern.pattern(), factory)
} else {
defaultFactory = factory
}
}

TreeNodeFactory match(String path) {
for (def factory : factories) {
for (def factory : factories.values()) {
if (path ==~ factory.pathPattern) {
return factory
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ package org.jd.gui.service.type
import groovy.transform.CompileStatic
import org.jd.gui.api.model.Container
import org.jd.gui.api.model.Type
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.TypeFactory

@Singleton(lazy = true)
class TypeFactoryService {
protected List<TypeFactory> providers = ServiceLoader.load(TypeFactory).toList()
protected Map<String, TypeFactories> mapProviders = populate()

protected Map<String, TypeFactories> mapProviders = populate(providers)

protected Map<String, TypeFactories> populate(List<TypeFactory> providers) {
protected Map<String, TypeFactories> populate() {
Collection<TypeFactory> providers = ExtensionService.instance.load(TypeFactory)
Map<String, TypeFactories> mapProviders = [:]

def mapProvidersWithDefault = mapProviders.withDefault { new TypeFactories() }
Expand Down Expand Up @@ -70,19 +70,19 @@ class TypeFactoryService {
}

static class TypeFactories {
ArrayList<TypeFactory> factories = []
HashMap<String, TypeFactory> factories = [:]
TypeFactory defaultFactory

void add(TypeFactory factory) {
if (factory.pathPattern) {
factories << factory
factories.put(factory.pathPattern.pattern(), factory)
} else {
defaultFactory = factory
}
}

TypeFactory match(String path) {
for (def factory : factories) {
for (def factory : factories.values()) {
if (path ==~ factory.pathPattern) {
return factory
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
package org.jd.gui.service.uriloader

import org.jd.gui.api.API
import org.jd.gui.service.extension.ExtensionService
import org.jd.gui.spi.UriLoader

@Singleton(lazy = true)
class UriLoaderService {
final List<UriLoader> providers = ServiceLoader.load(UriLoader).toList()
protected final Collection<UriLoader> providers = ExtensionService.instance.load(UriLoader)

private Map<String, UriLoader> mapProviders = providers.collectEntries { provider ->
protected Map<String, UriLoader> mapProviders = providers.collectEntries { provider ->
provider.schemes.collectEntries { [it, provider] }
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/groovy/org/jd/gui/view/PreferencesView.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import java.awt.event.KeyEvent
class PreferencesView implements PreferencesPanel.PreferencesPanelChangeListener {
SwingBuilder swing
Map<String, String> preferences
List<PreferencesPanel> panels
Collection<PreferencesPanel> panels
Map<PreferencesPanel, Boolean> valids

PreferencesView(SwingBuilder swing, Configuration configuration, List<PreferencesPanel> panels) {
PreferencesView(SwingBuilder swing, Configuration configuration, Collection<PreferencesPanel> panels) {
this.swing = swing
this.preferences = configuration.preferences
this.panels = panels
Expand Down

0 comments on commit dc6f2a7

Please sign in to comment.