summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/Enigma.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/Enigma.java')
-rw-r--r--src/main/java/cuchaz/enigma/Enigma.java119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/Enigma.java b/src/main/java/cuchaz/enigma/Enigma.java
new file mode 100644
index 0000000..fd23b47
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/Enigma.java
@@ -0,0 +1,119 @@
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
12package cuchaz.enigma;
13
14import com.google.common.base.Preconditions;
15import com.google.common.collect.ImmutableMap;
16import cuchaz.enigma.analysis.ClassCache;
17import cuchaz.enigma.analysis.index.JarIndex;
18import cuchaz.enigma.api.EnigmaPlugin;
19import cuchaz.enigma.api.EnigmaPluginContext;
20import cuchaz.enigma.api.service.EnigmaService;
21import cuchaz.enigma.api.service.EnigmaServiceFactory;
22import cuchaz.enigma.api.service.EnigmaServiceType;
23import cuchaz.enigma.api.service.JarIndexerService;
24
25import java.io.IOException;
26import java.nio.file.Path;
27import java.util.ServiceLoader;
28
29public class Enigma {
30 private final EnigmaProfile profile;
31 private final EnigmaServices services;
32
33 private Enigma(EnigmaProfile profile, EnigmaServices services) {
34 this.profile = profile;
35 this.services = services;
36 }
37
38 public static Enigma create() {
39 return new Builder().build();
40 }
41
42 public static Builder builder() {
43 return new Builder();
44 }
45
46 public EnigmaProject openJar(Path path, ProgressListener progress) throws IOException {
47 ClassCache classCache = ClassCache.of(path);
48 JarIndex jarIndex = classCache.index(progress);
49
50 services.get(JarIndexerService.TYPE).ifPresent(indexer -> {
51 indexer.acceptJar(classCache, jarIndex);
52 });
53
54 return new EnigmaProject(this, classCache, jarIndex);
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 ImmutableMap.Builder<EnigmaServiceType<?>, EnigmaService> services = ImmutableMap.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 EnigmaProfile.Service serviceProfile = profile.getServiceProfile(serviceType);
107
108 // if this service type is not configured, or it is configured to use a different service id, skip
109 if (serviceProfile == null || !serviceProfile.matches(id)) return;
110
111 T service = factory.create(serviceProfile::getArgument);
112 services.put(serviceType, service);
113 }
114
115 EnigmaServices buildServices() {
116 return new EnigmaServices(services.build());
117 }
118 }
119}