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.java114
1 files changed, 114 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..9f88f77
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/Enigma.java
@@ -0,0 +1,114 @@
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;
23
24import java.io.IOException;
25import java.nio.file.Path;
26import java.util.ServiceLoader;
27
28public class Enigma {
29 private final EnigmaProfile profile;
30 private final EnigmaServices services;
31
32 private Enigma(EnigmaProfile profile, EnigmaServices services) {
33 this.profile = profile;
34 this.services = services;
35 }
36
37 public static Enigma create() {
38 return new Builder().build();
39 }
40
41 public static Builder builder() {
42 return new Builder();
43 }
44
45 public EnigmaProject openJar(Path path, ProgressListener progress) throws IOException {
46 ClassCache classCache = ClassCache.of(path);
47 JarIndex jarIndex = classCache.index(progress);
48
49 return new EnigmaProject(this, classCache, jarIndex);
50 }
51
52 public EnigmaProfile getProfile() {
53 return profile;
54 }
55
56 public EnigmaServices getServices() {
57 return services;
58 }
59
60 public static class Builder {
61 private EnigmaProfile profile = EnigmaProfile.EMPTY;
62 private Iterable<EnigmaPlugin> plugins = ServiceLoader.load(EnigmaPlugin.class);
63
64 private Builder() {
65 }
66
67 public Builder setProfile(EnigmaProfile profile) {
68 Preconditions.checkNotNull(profile, "profile cannot be null");
69 this.profile = profile;
70 return this;
71 }
72
73 public Builder setPlugins(Iterable<EnigmaPlugin> plugins) {
74 Preconditions.checkNotNull(plugins, "plugins cannot be null");
75 this.plugins = plugins;
76 return this;
77 }
78
79 public Enigma build() {
80 PluginContext pluginContext = new PluginContext(profile);
81 for (EnigmaPlugin plugin : plugins) {
82 plugin.init(pluginContext);
83 }
84
85 EnigmaServices services = pluginContext.buildServices();
86 return new Enigma(profile, services);
87 }
88 }
89
90 private static class PluginContext implements EnigmaPluginContext {
91 private final EnigmaProfile profile;
92
93 private final ImmutableMap.Builder<EnigmaServiceType<?>, EnigmaService> services = ImmutableMap.builder();
94
95 PluginContext(EnigmaProfile profile) {
96 this.profile = profile;
97 }
98
99 @Override
100 public <T extends EnigmaService> void registerService(String id, EnigmaServiceType<T> serviceType, EnigmaServiceFactory<T> factory) {
101 EnigmaProfile.Service serviceProfile = profile.getServiceProfile(serviceType);
102
103 // if this service type is not configured, or it is configured to use a different service id, skip
104 if (serviceProfile == null || !serviceProfile.matches(id)) return;
105
106 T service = factory.create(serviceProfile::getArgument);
107 services.put(serviceType, service);
108 }
109
110 EnigmaServices buildServices() {
111 return new EnigmaServices(services.build());
112 }
113 }
114}