Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SpEL Keys to RateLimiter, Handle error when duration for Limite… #448

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,14 @@
<artifactId>groovy-templates</artifactId>
</dependency>


<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.17.3</version>
<scope>test</scope>
</dependency>

<!-- ntlm support-->
<!-- ntlm support-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@

package com.predic8.membrane.core;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.predic8.membrane.core.config.spring.*;
import com.predic8.membrane.core.config.spring.CheckableBeanFactory.*;
import com.predic8.membrane.core.exceptions.*;
import org.slf4j.*;
import org.springframework.context.support.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractRefreshableApplicationContext;

import com.predic8.membrane.core.config.spring.CheckableBeanFactory;
import com.predic8.membrane.core.config.spring.CheckableBeanFactory.InvalidConfigurationException;
import java.io.*;
import java.util.*;

public class HotDeploymentThread extends Thread {

private static Logger log = LoggerFactory.getLogger(HotDeploymentThread.class.getName());
private static final Logger log = LoggerFactory.getLogger(HotDeploymentThread.class.getName());

private List<HotDeploymentThread.FileInfo> files = new ArrayList<HotDeploymentThread.FileInfo>();
private final List<HotDeploymentThread.FileInfo> files = new ArrayList<>();
protected AbstractRefreshableApplicationContext applicationContext;
private boolean reloading;

Expand Down Expand Up @@ -72,6 +70,7 @@ public void run() {
while (!isInterrupted()) {
try {
while (!configurationChanged()) {
//noinspection BusyWait
sleep(1000);
}

Expand All @@ -92,15 +91,17 @@ public void run() {
// InterruptedException clears interrupt flag. see javadoc Thread.interrupt();
// So reset it.
interrupt();
} catch (Exception e) {
log.error("Could not redeploy.", e);
}
catch (Exception e) {
log.error("Could not redeploy, there are errors in the configuration.");
SpringConfigurationErrorHandler.handleRootCause(e,log);
updateLastModified();
}
}
log.debug("Spring Hot Deployment Thread interrupted.");
}

protected void reload() throws Exception {
protected void reload() {
synchronized(this) {
reloading = true;
}
Expand Down
82 changes: 31 additions & 51 deletions core/src/main/java/com/predic8/membrane/core/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,36 @@

package com.predic8.membrane.core;

import java.io.IOException;
import java.net.MalformedURLException;
import com.predic8.membrane.annot.*;
import com.predic8.membrane.core.RuleManager.*;
import com.predic8.membrane.core.config.spring.*;
import com.predic8.membrane.core.exchangestore.*;
import com.predic8.membrane.core.interceptor.*;
import com.predic8.membrane.core.interceptor.administration.*;
import com.predic8.membrane.core.jmx.*;
import com.predic8.membrane.core.kubernetes.*;
import com.predic8.membrane.core.kubernetes.client.*;
import com.predic8.membrane.core.resolver.*;
import com.predic8.membrane.core.rules.*;
import com.predic8.membrane.core.transport.*;
import com.predic8.membrane.core.transport.http.*;
import com.predic8.membrane.core.transport.http.client.*;
import com.predic8.membrane.core.util.*;
import org.slf4j.*;
import org.springframework.beans.*;
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.context.support.*;

import javax.annotation.concurrent.*;
import java.io.*;
import java.net.*;
import java.util.Timer;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import com.predic8.membrane.core.jmx.JmxExporter;
import com.predic8.membrane.core.jmx.JmxRouter;
import com.predic8.membrane.core.kubernetes.KubernetesWatcher;
import com.predic8.membrane.core.kubernetes.client.KubernetesClientFactory;
import com.predic8.membrane.core.rules.InternalProxy;
import com.predic8.membrane.core.transport.http.HttpClientFactory;
import com.predic8.membrane.core.util.TimerManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.Lifecycle;
import org.springframework.context.support.AbstractRefreshableApplicationContext;

import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.annot.MCMain;
import com.predic8.membrane.core.RuleManager.RuleDefinitionSource;
import com.predic8.membrane.core.config.spring.BaseLocationApplicationContext;
import com.predic8.membrane.core.config.spring.TrackingApplicationContext;
import com.predic8.membrane.core.config.spring.TrackingFileSystemXmlApplicationContext;
import com.predic8.membrane.core.exchangestore.ExchangeStore;
import com.predic8.membrane.core.exchangestore.LimitedMemoryExchangeStore;
import com.predic8.membrane.core.interceptor.ExchangeStoreInterceptor;
import com.predic8.membrane.core.interceptor.Interceptor;
import com.predic8.membrane.core.interceptor.administration.AdminConsoleInterceptor;
import com.predic8.membrane.core.resolver.ResolverMap;
import com.predic8.membrane.core.rules.Rule;
import com.predic8.membrane.core.transport.Transport;
import com.predic8.membrane.core.transport.http.HttpServerThreadFactory;
import com.predic8.membrane.core.transport.http.HttpTransport;
import com.predic8.membrane.core.transport.http.client.HttpClientConfiguration;
import com.predic8.membrane.core.util.DNSCache;
import com.predic8.membrane.core.util.URIFactory;

import javax.annotation.concurrent.GuardedBy;

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

import static com.predic8.membrane.core.Constants.*;
import static com.predic8.membrane.core.jmx.JmxExporter.*;

/**
* @description <p>
Expand Down Expand Up @@ -274,7 +254,7 @@ public void init() throws Exception {
}

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

Expand Down Expand Up @@ -317,7 +297,7 @@ public void start() {
synchronized (lock) {
running = true;
}
log.info(Constants.PRODUCT_NAME + " " + Constants.VERSION + " up and running!");
log.info(PRODUCT_NAME + " " + VERSION + " up and running!");
}

private void startJmx() {
Expand Down
91 changes: 13 additions & 78 deletions core/src/main/java/com/predic8/membrane/core/RouterCLI.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,36 @@

package com.predic8.membrane.core;

import java.io.*;

import com.predic8.membrane.core.transport.*;
import com.predic8.membrane.core.util.*;
import com.predic8.membrane.core.exceptions.*;
import com.predic8.membrane.core.resolver.*;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException;
import org.slf4j.*;
import org.springframework.beans.factory.xml.*;

import com.predic8.membrane.core.config.spring.CheckableBeanFactory.InvalidConfigurationException;
import com.predic8.membrane.core.resolver.ResolverMap;
import com.predic8.membrane.core.resolver.ResourceRetrievalException;
import java.io.*;

import static com.predic8.membrane.core.Constants.MEMBRANE_HOME;
import static com.predic8.membrane.core.config.spring.TrackingFileSystemXmlApplicationContext.handleXmlBeanDefinitionStoreException;
import static com.predic8.membrane.core.util.OSUtil.fixBackslashes;
import static com.predic8.membrane.core.util.OSUtil.getOS;
import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCause;
import static com.predic8.membrane.core.Constants.*;
import static com.predic8.membrane.core.config.spring.TrackingFileSystemXmlApplicationContext.*;
import static com.predic8.membrane.core.util.OSUtil.*;

public class RouterCLI {

private static final Logger LOG = LoggerFactory.getLogger(RouterCLI.class);
private static final Logger log = LoggerFactory.getLogger(RouterCLI.class);

public static void main(String[] args) {

MembraneCommandLine cl = getMembraneCommandLine(args);

Router router = null;
try {
try {
router = Router.init(getRulesFile(cl), RouterCLI.class.getClassLoader());
router = Router.init(getRulesFile(getMembraneCommandLine(args)), RouterCLI.class.getClassLoader());
} catch (XmlBeanDefinitionStoreException e) {
handleXmlBeanDefinitionStoreException(e);
}
} catch (InvalidConfigurationException e) {
System.err.println("Fatal error: " + e.getMessage());
log.error("Fatal error: " + e.getMessage());
System.exit(1);
} catch (Exception ex) {
Throwable rootCause = getRootCause(ex);
if (rootCause instanceof ConfigurationException ee)
handleConfigurationException(ee);
else if (rootCause instanceof PortOccupiedException poe)
handlePortOccupiedException(poe);
else
ex.printStackTrace();
SpringConfigurationErrorHandler.handleRootCause(ex,log);
System.exit(1);
}

Expand Down Expand Up @@ -88,54 +73,6 @@ private static MembraneCommandLine getMembraneCommandLine(String[] args) {
return cl;
}

private static void handlePortOccupiedException(PortOccupiedException poe) {
printStars();
System.err.println();
System.err.printf("Membrane is configured to open port %d. But this port is alreay in\n", poe.getPort());
System.err.println("""
use by a different program. To start Membrane do one of the following:

1. Find and stop the program that is occupying the port. Then restart Membrane.""");
System.err.println();
switch (getOS()) {
case WINDOWS -> printHowToFindPortWindows();
case LINUX, MAC -> printHowToFindPortLinux(poe.getPort());
}
System.err.println("""
2. Configure Membrane to use a different port. Propably in the conf/proxies.xml
file. Then restart Membrane.
""");
}

private static void printHowToFindPortWindows() {
System.err.println("""
netstat -aon | find /i "listening"
""");
}

private static void printHowToFindPortLinux(int port) {
System.err.println("""
e.g.:
> lsof -i :""" + port + """

COMMAND PID USER TYPE
java 80910 predic8 IPv6 TCP (LISTEN)
> kill -9 80910
""");
}

private static void handleConfigurationException(ConfigurationException ce) {
printStars();
System.err.println();
System.err.println(ce.getMessage());
System.err.println();
System.err.println("giving up.");
}

private static void printStars() {
System.err.println("**********************************************************************************");
}

private static String getRulesFile(MembraneCommandLine line) throws IOException {
ResolverMap rm = new ResolverMap();
if (line.hasConfiguration()) {
Expand Down Expand Up @@ -169,8 +106,6 @@ private static boolean shouldResolveFile(String s) {
return s.startsWith("file:") || s.startsWith("/") || s.length() > 3 && s.startsWith(":/", 1);
}



private static String getRulesFileFromRelativeSpec(ResolverMap rm, String relativeFile, String errorNotice) {

String try1 = ResolverMap.combine(prefix(getUserDir()), relativeFile);
Expand Down Expand Up @@ -209,4 +144,4 @@ private static String prefix(String dir) {
}
return dir;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
package com.predic8.membrane.core;


import java.io.*;

public class TerminateException extends Exception {

/**
*
*/
@Serial
private static final long serialVersionUID = 6330549161301272316L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
limitations under the License. */
package com.predic8.membrane.core.config.spring;

import java.io.*;

public interface CheckableBeanFactory {

/**
* Checks whether this application context's configuration file(s) contain valid bean definitions.
* @throws InvalidConfigurationException if the configuration is not valid
*/
public void checkForInvalidBeanDefinitions() throws InvalidConfigurationException;
void checkForInvalidBeanDefinitions() throws InvalidConfigurationException;

public static class InvalidConfigurationException extends Exception {
class InvalidConfigurationException extends Exception {

@Serial
private static final long serialVersionUID = 1L;

public InvalidConfigurationException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
/**
* Delegates everything to {@link FileSystemXmlApplicationContext}.
*
* Additionally adds aspects of {@link TrackingApplicationContext}, {@link BaseLocationApplicationContext} and
* Additionally, adds aspects of {@link TrackingApplicationContext}, {@link BaseLocationApplicationContext} and
* {@link CheckableBeanFactory}.
*/
public class TrackingFileSystemXmlApplicationContext extends FileSystemXmlApplicationContext implements
TrackingApplicationContext, BaseLocationApplicationContext, CheckableBeanFactory {
private static final Logger log = LoggerFactory.getLogger(TrackingFileSystemXmlApplicationContext.class.getName());

private List<File> files = new ArrayList<File>();
private final List<File> files = new ArrayList<>();

public TrackingFileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
super(configLocations, refresh);
Expand All @@ -62,7 +62,7 @@ public Resource getResource(String location) {
log.debug("",e);
}
return new Resource() {
Resource r2 = r;
final Resource r2 = r;

public boolean exists() {
return r2.exists();
Expand Down
Loading