Skip to content

Commit

Permalink
Refactoring of distribution, core, example-tests refactored. Removed …
Browse files Browse the repository at this point in the history
…version warning from start script.
  • Loading branch information
predic8 committed Jan 15, 2023
1 parent 2476b7f commit a248d17
Show file tree
Hide file tree
Showing 128 changed files with 2,890 additions and 2,291 deletions.
30 changes: 30 additions & 0 deletions DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,33 @@ To run the integration tests in an isolated environment, run
docker build .

if you have a Docker Engine available.


Examples Tests
==============

The example tests try to run the service-proxy.sh/bat scripts in the examples folders of a
distribution build in the target folder. Remember to build after any change otherwise old code
is tested:

Build a distribution in target/ . Then run the example tests.
```sh
mvn clean install -DskipTests
```


## Prequisites

On Mac OS the `setsid` command is needed. It can be installed with:

```sh
brew install util-linux
```

## Running

Run Testclass `ExampleTests`

## Troubleshooting

-
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import com.predic8.membrane.annot.model.OtherAttributesInfo;
import com.predic8.membrane.annot.model.doc.Doc;

import static java.util.Comparator.comparing;

public class HelpReference {

private final ProcessingEnvironment processingEnv;
Expand All @@ -63,7 +65,7 @@ public void writeHelp(Model m) {
String path = System.getenv("MEMBRANE_GENERATE_DOC_DIR");
if (path == null)
return;
path = path.replace("%VERSION%", "4.8");
path = path.replace("%VERSION%", "5.0");

sw = new StringWriter();
XMLOutputFactory output = XMLOutputFactory.newInstance();
Expand All @@ -87,7 +89,7 @@ public void writeHelp(Model m) {
}

private String getFileName(Model m) {
ArrayList<String> packages = new ArrayList<String>();
ArrayList<String> packages = new ArrayList<>();
for (MainInfo mi : m.getMains())
packages.add(mi.getAnnotation().outputPackage());
Collections.sort(packages);
Expand All @@ -111,15 +113,7 @@ private void handle(Model m, MainInfo main) throws XMLStreamException {
xew.writeStartElement("namespace");
xew.writeAttribute("package", main.getAnnotation().outputPackage());
xew.writeAttribute("targetNamespace", main.getAnnotation().targetNamespace());
Collections.sort(main.getIis(), new Comparator<ElementInfo>() {
@Override
public int compare(ElementInfo o1, ElementInfo o2) {
int res = o1.getAnnotation().name().compareTo(o2.getAnnotation().name());
if (res == 0)
res = o1.getElement().getQualifiedName().toString().compareTo(o2.getElement().getQualifiedName().toString());
return res;
}
});
main.getIis().sort(comparing((ElementInfo o) -> o.getAnnotation().name()).thenComparing(o -> o.getElement().getQualifiedName().toString()));
for (ElementInfo ei : main.getIis())
handle(m, main, ei);
xew.writeEndElement();
Expand All @@ -141,14 +135,7 @@ private void handle(Model m, MainInfo main, ElementInfo ei) throws XMLStreamExce
handleDoc(ei);

List<AttributeInfo> ais = ei.getAis();
Collections.sort(ais, new Comparator<AttributeInfo>() {

@Override
public int compare(AttributeInfo o1, AttributeInfo o2) {
return o1.getXMLName().compareTo(o2.getXMLName());
}

});
ais.sort(comparing(AttributeInfo::getXMLName));
OtherAttributesInfo oai = ei.getOai();

if (ais.size() > 0 && ais.get(0).getXMLName().equals("id"))
Expand Down Expand Up @@ -179,7 +166,7 @@ public int compare(AttributeInfo o1, AttributeInfo o2) {

private String getPrimaryParentId(Model m, MainInfo mi, ElementInfo ei) {
// choose a random parent (TODO: choose a better one)
Set<ElementInfo> possibleParents = new HashSet<ElementInfo>();
Set<ElementInfo> possibleParents = new HashSet<>();
for (Map.Entry<TypeElement, ChildElementDeclarationInfo> e : mi.getChildElementDeclarations().entrySet())
if (e.getValue().getElementInfo().contains(ei)) {
for (ChildElementInfo usedBy : e.getValue().getUsedBy()) {
Expand Down Expand Up @@ -217,7 +204,7 @@ private void handle(Model m, MainInfo main, ChildElementInfo cei) throws XMLStre

handleDoc(cei);

SortedSet<String> possibilities = new TreeSet<String>();
SortedSet<String> possibilities = new TreeSet<>();
for (ElementInfo ei : main.getChildElementDeclarations().get(cei.getTypeDeclaration()).getElementInfo()) {
possibilities.add(ei.getId());
}
Expand Down
30 changes: 29 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand All @@ -74,7 +79,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20211205</version>
<version>20220924</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -155,6 +160,29 @@
<artifactId>spring-web</artifactId>
</dependency>

<!-- Rhino Javascript Engine, for tests only. Will not be shipped! -->
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino-engine</artifactId>
<version>1.7.14</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>22.3.0</version>
<scope>provided</scope>
</dependency>

<!-- GraalVM Javascript Engine, for tests only. Will not be shipped! -->
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>22.3.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.*;
import java.security.SecureRandom;
import java.util.Properties;

Expand All @@ -38,6 +39,10 @@
import com.predic8.membrane.core.transport.http.HttpClient;
import com.predic8.membrane.core.util.MessageUtil;

import static java.nio.charset.StandardCharsets.*;
import static javax.crypto.Cipher.ENCRYPT_MODE;
import static org.apache.commons.codec.binary.Base64.encodeBase64;

public class LBNotificationClient {

private static Logger log = LoggerFactory.getLogger(LBNotificationClient.class.getName());
Expand Down Expand Up @@ -81,8 +86,7 @@ private Response notifiyClusterManager() throws Exception {
r.setBodyContent(new byte[0]);
exc.setRequest(r);
exc.getDestinations().add(getRequestURL());
Response res = client.call(exc).getResponse();
return res;
return client.call(exc).getResponse();
}

private void parseArguments(CommandLine cl) throws Exception {
Expand Down Expand Up @@ -114,11 +118,8 @@ private String getArgument(CommandLine cl, int clArgPos, char option,

if (prop != null && new File(propertiesFile).exists()) {
Properties props = new Properties();
InputStream is = new FileInputStream(propertiesFile);
try {
try (InputStream is = new FileInputStream(propertiesFile)) {
props.load(is);
} finally {
is.close();
}
if (props.containsKey(prop)) {
return props.getProperty(prop);
Expand All @@ -141,7 +142,7 @@ private String getQueryString() {
private String getRequestURL() throws Exception {
if (skeySpec!=null) {
return cmURL + "/" + cmd + "?data="+
URLEncoder.encode(getEncryptedQueryString(),"UTF-8");
URLEncoder.encode(getEncryptedQueryString(), UTF_8);
}
String time = String.valueOf(System.currentTimeMillis());
return cmURL + "/" + cmd + "?balancer=" + balancer + "&cluster=" + cluster + "&host=" + host + "&port=" + port + "&time=" + time;
Expand All @@ -150,8 +151,8 @@ private String getRequestURL() throws Exception {
private String getEncryptedQueryString() throws Exception {
Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
return new String(Base64.encodeBase64(cipher.doFinal(getQueryString().getBytes("UTF-8"))),"UTF-8");
cipher.init(ENCRYPT_MODE, skeySpec);
return new String(encodeBase64(cipher.doFinal(getQueryString().getBytes(UTF_8))), UTF_8);
}

private void logArguments() {
Expand Down
36 changes: 16 additions & 20 deletions core/src/main/java/com/predic8/membrane/core/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@

import javax.annotation.concurrent.GuardedBy;

import static com.predic8.membrane.core.jmx.JmxExporter.JMX_EXPORTER_NAME;
import static java.util.stream.Collectors.toList;

/**
* @description <p>
* Membrane Service Proxy's main object.
Expand Down Expand Up @@ -93,7 +96,7 @@ public class Router implements Lifecycle, ApplicationContextAware, BeanNameAware
* app context, we track them here, so they start only one
* HotDeploymentThread.
*/
protected static final HashSet<ApplicationContext> hotDeployingContexts = new HashSet<ApplicationContext>();
protected static final HashSet<ApplicationContext> hotDeployingContexts = new HashSet<>();

private ApplicationContext beanFactory;
private String baseLocation;
Expand Down Expand Up @@ -158,6 +161,7 @@ public static Router init(String resource, ClassLoader classLoader) {
return (Router) beanFactory.getBean("router");
}

@SuppressWarnings("NullableProblems")
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
beanFactory = applicationContext;
Expand Down Expand Up @@ -202,7 +206,7 @@ public HttpClientConfiguration getHttpClientConfig() {
return resolverMap.getHTTPSchemaResolver().getHttpClientConfig();
}

@MCChildElement(order=0)
@MCChildElement()
public void setHttpClientConfig(HttpClientConfiguration httpClientConfig) {
resolverMap.getHTTPSchemaResolver().setHttpClientConfig(httpClientConfig);
}
Expand Down Expand Up @@ -270,14 +274,12 @@ public void init() throws Exception {
}

private void initRemainingRules() throws Exception {
List<Rule> otherRules = getRuleManager().getRules().stream().filter(r -> !(r instanceof InternalProxy)).collect(Collectors.toList());
for (Rule rule : otherRules)
for (Rule rule : getRuleManager().getRules().stream().filter(r -> !(r instanceof InternalProxy)).collect(toList()))
rule.init(this);
}

private void initInternalProxies() throws Exception {
List<Rule> internalProxies = getRuleManager().getRules().stream().filter(r -> r instanceof InternalProxy).collect(Collectors.toList());
for (Rule rule : internalProxies)
for (Rule rule : getRuleManager().getRules().stream().filter(r -> r instanceof InternalProxy).collect(toList()))
rule.init(this);
}

Expand Down Expand Up @@ -321,26 +323,19 @@ public void start() {
private void startJmx() {
if(getBeanFactory() != null) {
try{
Object exporterObj = getBeanFactory().getBean(JmxExporter.JMX_EXPORTER_NAME);
if (exporterObj != null) {
((JmxExporter) exporterObj).initAfterBeansAdded();
}
((JmxExporter) getBeanFactory().getBean(JMX_EXPORTER_NAME)).initAfterBeansAdded();
}catch(NoSuchBeanDefinitionException ignored){
// If bean is not available, then dont start jmx
// If bean is not available, then don't start jmx
}
}
}

private void initJmx() {
if (beanFactory != null) {
try {
Object exporterObj = beanFactory.getBean(JmxExporter.JMX_EXPORTER_NAME);
if (exporterObj != null) {
JmxExporter exporter = (JmxExporter) exporterObj;
String prefix = "org.membrane-soa:00=routers, name=";
//exporter.removeBean(prefix + jmxRouterName);
exporter.addBean(prefix + jmxRouterName, new JmxRouter(this, exporter));
}
JmxExporter exporter = (JmxExporter) beanFactory.getBean(JMX_EXPORTER_NAME);
//exporter.removeBean(prefix + jmxRouterName);
exporter.addBean("org.membrane-soa:00=routers, name=" + jmxRouterName, new JmxRouter(this, exporter));
}catch(NoSuchBeanDefinitionException ignored){
// If bean is not available, then dont init jmx
}
Expand Down Expand Up @@ -459,7 +454,7 @@ public boolean isRunning() {
* is created by a Spring Application Context which supports monitoring.
* </p>
* @default true
* @param hotDeploy
* @param hotDeploy If true the hot deploy feature is activated
*/
@MCAttribute
public void setHotDeploy(boolean hotDeploy) {
Expand Down Expand Up @@ -490,7 +485,7 @@ public void setRetryInitInterval(int retryInitInterval) {
}

private ArrayList<Rule> getInactiveRules() {
ArrayList<Rule> inactive = new ArrayList<Rule>();
ArrayList<Rule> inactive = new ArrayList<>();
for (Rule rule : getRuleManager().getRules())
if (!rule.isActive())
inactive.add(rule);
Expand Down Expand Up @@ -558,6 +553,7 @@ public String getJmx(){
return jmxRouterName;
}

@SuppressWarnings("NullableProblems")
@Override
public void setBeanName(String s) {
this.id = s;
Expand Down
Loading

0 comments on commit a248d17

Please sign in to comment.