Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Evolveum/midpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaHonchar committed Mar 2, 2020
2 parents df26e92 + 46a8c06 commit b79ccf1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
Expand Up @@ -59,14 +59,14 @@ public static class EmbeddedTomcat {
private int port;

@Value("${server.servlet.context-path}")
private String servletPath;
private String contextPath;

@Autowired
private SystemObjectCache systemObjectCache;

@Bean
public TomcatServletWebServerFactory tomcatEmbeddedServletContainerFactory() {
MidPointTomcatServletWebServerFactory tomcat = new MidPointTomcatServletWebServerFactory(servletPath, systemObjectCache);
MidPointTomcatServletWebServerFactory tomcat = new MidPointTomcatServletWebServerFactory(contextPath, systemObjectCache);

if(enableAjp) {
Connector ajpConnector = new Connector("AJP/1.3");
Expand Down
Expand Up @@ -41,12 +41,12 @@ public class MidPointTomcatServletWebServerFactory extends TomcatServletWebServe

private int backgroundProcessorDelay;

private String servletPath;
private String contextPath;

private SystemObjectCache systemObjectCache;

public MidPointTomcatServletWebServerFactory(String servletPath, SystemObjectCache systemObjectCache){
this.servletPath = servletPath;
public MidPointTomcatServletWebServerFactory(String contextPath, SystemObjectCache systemObjectCache){
this.contextPath = contextPath;
this.systemObjectCache = systemObjectCache;
}

Expand Down Expand Up @@ -94,9 +94,9 @@ public Response createResponse() {
if (protocolHandler instanceof AbstractAjpProtocol<?>) {
int packetSize = ((AbstractAjpProtocol<?>) protocolHandler).getPacketSize();
return new MidpointResponse(packetSize - org.apache.coyote.ajp.Constants.SEND_HEAD_LEN,
servletPath, systemObjectCache);
contextPath, systemObjectCache);
} else {
return new MidpointResponse(servletPath, systemObjectCache);
return new MidpointResponse(contextPath, systemObjectCache);
}
}
};
Expand Down
Expand Up @@ -28,17 +28,17 @@ public class MidpointResponse extends Response {

private static final Trace LOGGER = TraceManager.getTrace(MidpointResponse.class);

private String servletPath;
private String contextPath;
private SystemObjectCache systemObjectCache;

public MidpointResponse(String servletPath, SystemObjectCache systemObjectCache) {
this(OutputBuffer.DEFAULT_BUFFER_SIZE, servletPath, systemObjectCache);
}

public MidpointResponse(int outputBufferSize, String servletPath, SystemObjectCache systemObjectCache) {
public MidpointResponse(int outputBufferSize, String contextPath, SystemObjectCache systemObjectCache) {
super(outputBufferSize);

this.servletPath = servletPath;
this.contextPath = contextPath;
this.systemObjectCache = systemObjectCache;
}

Expand All @@ -49,15 +49,17 @@ public void setHeader(String name, String value) {
if (publicUrlPrefix != null && StringUtils.isNotBlank(value)) {
if (value.startsWith(".")) {
value = publicUrlPrefix + value.substring(1);
} else if (StringUtils.isBlank(servletPath)) {
} else if (StringUtils.isBlank(contextPath)) {
if (value.startsWith("/")) {
value = publicUrlPrefix + value;
} else {
String partAfterSchema = value.substring(value.indexOf("://") + 3);
value = publicUrlPrefix + partAfterSchema.substring(partAfterSchema.indexOf("/"));
}
} else if (value.contains(servletPath + "/")) {
value = publicUrlPrefix + value.substring(value.indexOf(servletPath) + servletPath.length());
} else if (value.contains(contextPath + "/")) {
String partAfterHostname = value.substring(value.indexOf("://") + 3);
partAfterHostname = partAfterHostname.substring(partAfterHostname.indexOf("/"));
value = publicUrlPrefix + partAfterHostname.substring(partAfterHostname.indexOf(contextPath) + contextPath.length());
}
}
}
Expand Down
Expand Up @@ -19,32 +19,34 @@
@Service
public class LightweightIdentifierGeneratorImpl implements LightweightIdentifierGenerator {

long lastTimestamp;
int lastSequence;
int hostIdentifier;
private static final long BACKWARD_TIME_ALLOWANCE = 10 * 1000L;

private long lastTimestamp; // monotonic increasing sequence
private int lastSequence; // incremented by 1, occasionally reset to 0
private int hostIdentifier; // currently unused

public LightweightIdentifierGeneratorImpl() {
lastTimestamp = 0;
lastSequence = 0;
hostIdentifier = 0;
}

/* (non-Javadoc)
* @see com.evolveum.midpoint.task.api.LightweightIdentifierGenerator#generate()
*/
@Override
public synchronized LightweightIdentifier generate() {
long timestamp = System.currentTimeMillis();
if (timestamp == lastTimestamp) {
// Nothing to do
} else if (timestamp > lastTimestamp) {
// reset the last timestamp and sequence conunter
if (timestamp > lastTimestamp) {
// update the last timestamp and reset sequence counter
lastTimestamp = timestamp;
lastSequence = 0;
} else if (timestamp < lastTimestamp - BACKWARD_TIME_ALLOWANCE) {
throw new IllegalStateException("The time has moved back more than " + BACKWARD_TIME_ALLOWANCE
+ " milliseconds, possible consistency violation. Current time = " + timestamp + ", last time = "
+ lastTimestamp + ", difference is " + (lastTimestamp - timestamp) + ".");
} else {
throw new IllegalStateException("The time has moved back, possible consistency violation");
// Usually timestamp == lastTimestamp here. But even if the time moved back a few seconds we stay calm
// and simply keep lastTimestamp unchanged. We will probably get a few identifiers with increasing sequence
// numbers and nothing wrong will happen.
}
return new LightweightIdentifier(timestamp, hostIdentifier, ++lastSequence);
}

}

0 comments on commit b79ccf1

Please sign in to comment.