Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.

Commit cbe050b

Browse files
committed
Doing work
1 parent 0d8dcf2 commit cbe050b

File tree

10 files changed

+170
-80
lines changed

10 files changed

+170
-80
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ hs_err_pid*
7676
# temporary files which can be created if a process still has a handle open of a deleted file
7777
.fuse_hidden*
7878

79-
# KDE directory preferences
80-
.directory
79+
# KDE registryDir preferences
80+
.registryDir
8181

8282
# Linux trash folder which might appear on any partition or disk
8383
.Trash-*

gradlew

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ if [ -n "$JAVA_HOME" ] ; then
7575
JAVACMD="$JAVA_HOME/bin/java"
7676
fi
7777
if [ ! -x "$JAVACMD" ] ; then
78-
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
78+
die "ERROR: JAVA_HOME is set to an invalid registryDir: $JAVA_HOME
7979
8080
Please set the JAVA_HOME variable in your environment to match the
8181
location of your Java installation."

gradlew.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
3838
if exist "%JAVA_EXE%" goto init
3939

4040
echo.
41-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
41+
echo ERROR: JAVA_HOME is set to an invalid registryDir: %JAVA_HOME%
4242
echo.
4343
echo Please set the JAVA_HOME variable in your environment to match the
4444
echo location of your Java installation.

proteus-httpgateway/src/main/java/com/netifi/proteus/httpgateway/config/HttpGatewayConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.netifi.proteus.httpgateway.config;
1717

18-
import com.netifi.proteus.httpgateway.registry.FileSystemProteusRegistry;
1918
import com.netifi.proteus.httpgateway.registry.ProteusRegistry;
2019
import io.netifi.proteus.Proteus;
2120
import org.springframework.context.annotation.Bean;
@@ -25,8 +24,8 @@
2524
public class HttpGatewayConfiguration {
2625

2726
@Bean
28-
public ProteusRegistry serviceRegistry(HttpGatewaySettings settings) {
29-
return new FileSystemProteusRegistry(settings);
27+
public ProteusRegistry proteusRegistry(HttpGatewaySettings settings) throws Exception {
28+
return new ProteusRegistry(settings);
3029
}
3130

3231
@Bean

proteus-httpgateway/src/main/java/com/netifi/proteus/httpgateway/config/HttpGatewaySettings.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,24 @@ public class HttpGatewaySettings {
3232
public static final String DEFAULT_GROUP = "proteus.httpgateway";
3333

3434
private String group;
35-
private String directory;
35+
private String registryDir;
3636

3737
@PostConstruct
3838
public void init() {
3939
if (StringUtils.isEmpty(group)) {
4040
group = DEFAULT_GROUP;
4141
}
4242

43-
if (StringUtils.isEmpty(directory)) {
43+
if (StringUtils.isEmpty(registryDir)) {
4444
ApplicationHome home = new ApplicationHome(Main.class);
45-
directory = home.getDir().getAbsolutePath();
45+
registryDir = home.getDir().getAbsolutePath() + "/registry";
46+
47+
if (!Files.isDirectory(Paths.get(registryDir))) {
48+
Paths.get(registryDir).toFile().mkdirs();
49+
}
4650
} else {
47-
if (!Files.isDirectory(Paths.get(directory))) {
48-
new RuntimeException(String.format("The 'netifi.httpgateway.directory' property is not a valid directory! [value='%s']", directory), new FileNotFoundException());
51+
if (!Files.isDirectory(Paths.get(registryDir))) {
52+
new RuntimeException(String.format("The 'netifi.httpgateway.registryDir' property is not a valid registryDir! [value='%s']", registryDir), new FileNotFoundException());
4953
}
5054
}
5155
}
@@ -58,11 +62,11 @@ public void setGroup(String group) {
5862
this.group = group;
5963
}
6064

61-
public String getDirectory() {
62-
return directory;
65+
public String getRegistryDir() {
66+
return registryDir;
6367
}
6468

65-
public void setDirectory(String directory) {
66-
this.directory = directory;
69+
public void setRegistryDir(String registryDir) {
70+
this.registryDir = registryDir;
6771
}
6872
}

proteus-httpgateway/src/main/java/com/netifi/proteus/httpgateway/invocation/ServiceInvocationFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
public class ServiceInvocationFactory {
2727
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInvocationFactory.class);
2828

29-
private final ProteusRegistry serviceRegistry;
29+
private final ProteusRegistry proteusRegistry;
3030
private final ProteusSettings proteusSettings;
3131

3232
@Autowired
33-
public ServiceInvocationFactory(ProteusRegistry serviceRegistry, ProteusSettings proteusSettings) {
34-
this.serviceRegistry = serviceRegistry;
33+
public ServiceInvocationFactory(ProteusRegistry proteusRegistry, ProteusSettings proteusSettings) {
34+
this.proteusRegistry = proteusRegistry;
3535
this.proteusSettings = proteusSettings;
3636
}
3737

proteus-httpgateway/src/main/java/com/netifi/proteus/httpgateway/registry/FileSystemProteusRegistry.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

proteus-httpgateway/src/main/java/com/netifi/proteus/httpgateway/registry/ProteusRegistry.java

Lines changed: 125 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,144 @@
1515
*/
1616
package com.netifi.proteus.httpgateway.registry;
1717

18+
import com.netifi.proteus.httpgateway.config.HttpGatewaySettings;
19+
import com.netifi.proteus.httpgateway.util.JarUtil;
20+
import io.netifi.proteus.annotations.internal.ProteusGenerated;
21+
import io.netifi.proteus.annotations.internal.ProteusResourceType;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import reactor.core.publisher.Flux;
25+
import reactor.core.publisher.Mono;
26+
27+
import java.lang.reflect.Method;
28+
import java.net.URL;
29+
import java.net.URLClassLoader;
30+
import java.nio.file.Path;
31+
import java.nio.file.Paths;
32+
import java.util.ArrayList;
33+
import java.util.HashSet;
34+
import java.util.List;
35+
import java.util.Map;
1836
import java.util.Objects;
37+
import java.util.Set;
38+
import java.util.concurrent.ConcurrentHashMap;
1939

20-
public interface ProteusRegistry {
40+
public class ProteusRegistry {
41+
private static final Logger LOGGER = LoggerFactory.getLogger(ProteusRegistry.class);
42+
private static final Map<Key, List<Method>> MAPPINGS = new ConcurrentHashMap<>();
2143

22-
boolean isRegisteredService(String service, String method);
44+
private final Path registryDir;
2345

24-
boolean isRegisteredService(Key key);
46+
public ProteusRegistry(HttpGatewaySettings settings) throws Exception {
47+
this.registryDir = Paths.get(settings.getRegistryDir());
48+
init();
49+
}
2550

2651
/**
2752
*
53+
* @param service
54+
* @param method
55+
* @return
2856
*/
29-
class Key {
30-
public final String service;
31-
public final String method;
57+
public boolean contains(String service, String method) {
58+
return MAPPINGS.containsKey(Key.from(service, method));
59+
}
3260

33-
public Key(final String service, final String method) {
34-
this.service = service;
35-
this.method = method;
61+
/**
62+
*
63+
* @param service
64+
* @param method
65+
* @return
66+
*/
67+
public List<Method> get(String service, String method) {
68+
Key mappingKey = Key.from(service, method);
69+
if (MAPPINGS.containsKey(mappingKey)) {
70+
return MAPPINGS.get(mappingKey);
71+
} else {
72+
throw new ServiceNotFoundException(mappingKey.service, mappingKey.method);
73+
}
74+
}
75+
76+
private void init() throws Exception {
77+
// Find all jars in the registry directory
78+
List<URL> foundJars = JarUtil.findJars(registryDir);
79+
80+
// Find all classes in the jars
81+
Set<String> foundClasses = new HashSet<>();
82+
foundJars.forEach(url -> {
83+
try {
84+
foundClasses.addAll(JarUtil.getClassNames(url));
85+
} catch (Exception e) {
86+
throw new RuntimeException(String.format("Unable to load classes from url! [url='%s']", url), e);
87+
}
88+
});
89+
90+
// Create a classloader containing jars in registry directory
91+
URL[] searchUrls = new URL[foundJars.size()];
92+
ClassLoader classLoader = new URLClassLoader(foundJars.toArray(searchUrls));
93+
94+
// Find all Proteus clients
95+
foundClasses.forEach(className -> {
96+
try {
97+
Class<?> clazz = classLoader.loadClass(className);
98+
99+
if (isProteusClient(clazz)) {
100+
for (Method method : clazz.getDeclaredMethods()) {
101+
if (isProteusMethod(method)) {
102+
if (MAPPINGS.containsKey(Key.from(className, method.getName()))) {
103+
MAPPINGS.get(Key.from(className, method.getName())).add(method);
104+
} else {
105+
List<Method> methods = new ArrayList<>();
106+
methods.add(method);
107+
108+
MAPPINGS.put(Key.from(className, method.getName()), methods);
109+
}
110+
}
111+
}
112+
}
113+
} catch (ClassNotFoundException e) {
114+
throw new RuntimeException(String.format("Unable to load class! [class='%s']", className), e);
115+
}
116+
});
117+
}
118+
119+
private boolean isProteusClient(Class<?> clazz) {
120+
if (clazz != null) {
121+
if (clazz.isAnnotationPresent(ProteusGenerated.class)) {
122+
ProteusGenerated annotation = clazz.getAnnotation(ProteusGenerated.class);
123+
124+
if (annotation.type() == ProteusResourceType.CLIENT) {
125+
if (!clazz.getSimpleName().startsWith("Blocking")) {
126+
return true;
127+
}
128+
}
129+
}
130+
}
131+
132+
return false;
133+
}
134+
135+
private boolean isProteusMethod(Method method) {
136+
if (method != null) {
137+
if (Mono.class.isAssignableFrom(method.getReturnType()) || Flux.class.isAssignableFrom(method.getReturnType())) {
138+
return true;
139+
}
36140
}
37141

38-
public String getService() {
39-
return service;
142+
return false;
143+
}
144+
145+
private static class Key {
146+
private final String service;
147+
private final String method;
148+
149+
static Key from(final String service, final String method) {
150+
return new Key(service, method);
40151
}
41152

42-
public String getMethod() {
43-
return method;
153+
private Key(final String service, final String method) {
154+
this.service = service;
155+
this.method = method;
44156
}
45157

46158
@Override
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.netifi.proteus.httpgateway.registry;
2+
3+
public class ServiceNotFoundException extends RuntimeException {
4+
private final String service;
5+
private final String method;
6+
7+
public ServiceNotFoundException(String service, String method) {
8+
super(String.format("The service/method is not registered. [service='%s', method='%s']", service, method));
9+
10+
this.service = service;
11+
this.method = method;
12+
}
13+
14+
public String getService() {
15+
return service;
16+
}
17+
18+
public String getMethod() {
19+
return method;
20+
}
21+
}

proteus-httpgateway/src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ netifi.proteus.accesskey=3006839580103245170
2121
netifi.proteus.accesstoken=SkOlZxqQcTboZE3fni4OVXVC0e0=
2222

2323
netifi.httpgateway.group=proteus.httpgateway
24-
#netifi.httpgateway.directory=
24+
25+
#netifi.httpgateway.registryDir=

0 commit comments

Comments
 (0)