diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/Enigma.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/Enigma.java | 121 |
1 files changed, 0 insertions, 121 deletions
diff --git a/src/main/java/cuchaz/enigma/Enigma.java b/src/main/java/cuchaz/enigma/Enigma.java deleted file mode 100644 index f5f0649..0000000 --- a/src/main/java/cuchaz/enigma/Enigma.java +++ /dev/null | |||
| @@ -1,121 +0,0 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * <p> | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | |||
| 12 | package cuchaz.enigma; | ||
| 13 | |||
| 14 | import com.google.common.base.Preconditions; | ||
| 15 | import com.google.common.collect.ImmutableListMultimap; | ||
| 16 | import cuchaz.enigma.analysis.ClassCache; | ||
| 17 | import cuchaz.enigma.analysis.index.JarIndex; | ||
| 18 | import cuchaz.enigma.api.EnigmaPlugin; | ||
| 19 | import cuchaz.enigma.api.EnigmaPluginContext; | ||
| 20 | import cuchaz.enigma.api.service.EnigmaService; | ||
| 21 | import cuchaz.enigma.api.service.EnigmaServiceFactory; | ||
| 22 | import cuchaz.enigma.api.service.EnigmaServiceType; | ||
| 23 | import cuchaz.enigma.api.service.JarIndexerService; | ||
| 24 | import cuchaz.enigma.utils.Utils; | ||
| 25 | |||
| 26 | import java.io.IOException; | ||
| 27 | import java.nio.file.Path; | ||
| 28 | import java.util.List; | ||
| 29 | import java.util.ServiceLoader; | ||
| 30 | |||
| 31 | public class Enigma { | ||
| 32 | private final EnigmaProfile profile; | ||
| 33 | private final EnigmaServices services; | ||
| 34 | |||
| 35 | private Enigma(EnigmaProfile profile, EnigmaServices services) { | ||
| 36 | this.profile = profile; | ||
| 37 | this.services = services; | ||
| 38 | } | ||
| 39 | |||
| 40 | public static Enigma create() { | ||
| 41 | return new Builder().build(); | ||
| 42 | } | ||
| 43 | |||
| 44 | public static Builder builder() { | ||
| 45 | return new Builder(); | ||
| 46 | } | ||
| 47 | |||
| 48 | public EnigmaProject openJar(Path path, ProgressListener progress) throws IOException { | ||
| 49 | ClassCache classCache = ClassCache.of(path); | ||
| 50 | JarIndex jarIndex = classCache.index(progress); | ||
| 51 | |||
| 52 | services.get(JarIndexerService.TYPE).forEach(indexer -> indexer.acceptJar(classCache, jarIndex)); | ||
| 53 | |||
| 54 | return new EnigmaProject(this, classCache, jarIndex, Utils.zipSha1(path)); | ||
| 55 | } | ||
| 56 | |||
| 57 | public EnigmaProfile getProfile() { | ||
| 58 | return profile; | ||
| 59 | } | ||
| 60 | |||
| 61 | public EnigmaServices getServices() { | ||
| 62 | return services; | ||
| 63 | } | ||
| 64 | |||
| 65 | public static class Builder { | ||
| 66 | private EnigmaProfile profile = EnigmaProfile.EMPTY; | ||
| 67 | private Iterable<EnigmaPlugin> plugins = ServiceLoader.load(EnigmaPlugin.class); | ||
| 68 | |||
| 69 | private Builder() { | ||
| 70 | } | ||
| 71 | |||
| 72 | public Builder setProfile(EnigmaProfile profile) { | ||
| 73 | Preconditions.checkNotNull(profile, "profile cannot be null"); | ||
| 74 | this.profile = profile; | ||
| 75 | return this; | ||
| 76 | } | ||
| 77 | |||
| 78 | public Builder setPlugins(Iterable<EnigmaPlugin> plugins) { | ||
| 79 | Preconditions.checkNotNull(plugins, "plugins cannot be null"); | ||
| 80 | this.plugins = plugins; | ||
| 81 | return this; | ||
| 82 | } | ||
| 83 | |||
| 84 | public Enigma build() { | ||
| 85 | PluginContext pluginContext = new PluginContext(profile); | ||
| 86 | for (EnigmaPlugin plugin : plugins) { | ||
| 87 | plugin.init(pluginContext); | ||
| 88 | } | ||
| 89 | |||
| 90 | EnigmaServices services = pluginContext.buildServices(); | ||
| 91 | return new Enigma(profile, services); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | private static class PluginContext implements EnigmaPluginContext { | ||
| 96 | private final EnigmaProfile profile; | ||
| 97 | |||
| 98 | private final ImmutableListMultimap.Builder<EnigmaServiceType<?>, EnigmaService> services = ImmutableListMultimap.builder(); | ||
| 99 | |||
| 100 | PluginContext(EnigmaProfile profile) { | ||
| 101 | this.profile = profile; | ||
| 102 | } | ||
| 103 | |||
| 104 | @Override | ||
| 105 | public <T extends EnigmaService> void registerService(String id, EnigmaServiceType<T> serviceType, EnigmaServiceFactory<T> factory) { | ||
| 106 | List<EnigmaProfile.Service> serviceProfiles = profile.getServiceProfiles(serviceType); | ||
| 107 | |||
| 108 | for (EnigmaProfile.Service serviceProfile : serviceProfiles) { | ||
| 109 | if (serviceProfile.matches(id)) { | ||
| 110 | T service = factory.create(serviceProfile::getArgument); | ||
| 111 | services.put(serviceType, service); | ||
| 112 | break; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | EnigmaServices buildServices() { | ||
| 118 | return new EnigmaServices(services.build()); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||