Merge pull request elastic/elasticsearch#51 from elastic/plugin-api
Refactored api for plugins into it's own module Original commit: elastic/x-pack-elasticsearch@8136be7e53
This commit is contained in:
commit
ba9202a5de
|
@ -0,0 +1 @@
|
|||
/eclipse-build/
|
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>elasticsearch-license</artifactId>
|
||||
<groupId>org.elasticsearch</groupId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>elasticsearch-license-plugin-api</artifactId>
|
||||
<properties>
|
||||
<license.basedir combine.self="override">${project.parent.basedir}</license.basedir>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch</groupId>
|
||||
<artifactId>elasticsearch-license-licensor</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch</groupId>
|
||||
<artifactId>elasticsearch-license-core</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,163 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.license.core.License;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
|
||||
public interface LicensesClientService {
|
||||
|
||||
public interface Listener {
|
||||
|
||||
/**
|
||||
* Called to enable a feature
|
||||
*/
|
||||
public void onEnabled(License license);
|
||||
|
||||
/**
|
||||
* Called to disable a feature
|
||||
*/
|
||||
public void onDisabled(License license);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a feature for licensing
|
||||
*
|
||||
* @param feature - name of the feature to register (must be in sync with license Generator feature name)
|
||||
* @param trialLicenseOptions - Trial license specification used to generate a one-time trial license for the feature;
|
||||
* use <code>null</code> if no trial license should be generated for the feature
|
||||
* @param expirationCallbacks - A collection of Pre and/or Post expiration callbacks
|
||||
* @param listener - used to notify on feature enable/disable
|
||||
*/
|
||||
void register(String feature, TrialLicenseOptions trialLicenseOptions, Collection<ExpirationCallback> expirationCallbacks, Listener listener);
|
||||
|
||||
public static class TrialLicenseOptions {
|
||||
final TimeValue duration;
|
||||
final int maxNodes;
|
||||
|
||||
public TrialLicenseOptions(TimeValue duration, int maxNodes) {
|
||||
this.duration = duration;
|
||||
this.maxNodes = maxNodes;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static interface LicenseCallback {
|
||||
void on(License license, ExpirationStatus status);
|
||||
}
|
||||
|
||||
public static abstract class ExpirationCallback implements LicenseCallback {
|
||||
|
||||
public enum Orientation { PRE, POST }
|
||||
|
||||
public static abstract class Pre extends ExpirationCallback {
|
||||
|
||||
/**
|
||||
* Callback schedule prior to license expiry
|
||||
*
|
||||
* @param min latest relative time to execute before license expiry
|
||||
* @param max earliest relative time to execute before license expiry
|
||||
* @param frequency interval between execution
|
||||
*/
|
||||
public Pre(TimeValue min, TimeValue max, TimeValue frequency) {
|
||||
super(Orientation.PRE, min, max, frequency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(long expirationDate, long now) {
|
||||
long expiryDuration = expirationDate - now;
|
||||
if (expiryDuration > 0l) {
|
||||
if (expiryDuration <= max().getMillis()) {
|
||||
return expiryDuration >= min().getMillis();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Post extends ExpirationCallback {
|
||||
|
||||
/**
|
||||
* Callback schedule after license expiry
|
||||
*
|
||||
* @param min earliest relative time to execute after license expiry
|
||||
* @param max latest relative time to execute after license expiry
|
||||
* @param frequency interval between execution
|
||||
*/
|
||||
public Post(TimeValue min, TimeValue max, TimeValue frequency) {
|
||||
super(Orientation.POST, min, max, frequency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(long expirationDate, long now) {
|
||||
long postExpiryDuration = now - expirationDate;
|
||||
if (postExpiryDuration > 0l) {
|
||||
if (postExpiryDuration <= max().getMillis()) {
|
||||
return postExpiryDuration >= min().getMillis();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private final Orientation orientation;
|
||||
private final TimeValue min;
|
||||
private final TimeValue max;
|
||||
private final TimeValue frequency;
|
||||
|
||||
private ExpirationCallback(Orientation orientation, TimeValue min, TimeValue max, TimeValue frequency) {
|
||||
this.orientation = orientation;
|
||||
this.min = (min == null) ? TimeValue.timeValueMillis(0) : min;
|
||||
this.max = (max == null) ? TimeValue.timeValueMillis(Long.MAX_VALUE) : max;
|
||||
this.frequency = frequency;
|
||||
if (frequency == null) {
|
||||
throw new IllegalArgumentException("frequency can not be null");
|
||||
}
|
||||
}
|
||||
|
||||
public Orientation orientation() {
|
||||
return orientation;
|
||||
}
|
||||
|
||||
public TimeValue min() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public TimeValue max() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public TimeValue frequency() {
|
||||
return frequency;
|
||||
}
|
||||
|
||||
public abstract boolean matches(long expirationDate, long now);
|
||||
}
|
||||
|
||||
public static class ExpirationStatus {
|
||||
private final boolean expired;
|
||||
private final TimeValue time;
|
||||
|
||||
ExpirationStatus(boolean expired, TimeValue time) {
|
||||
this.expired = expired;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public boolean expired() {
|
||||
return expired;
|
||||
}
|
||||
|
||||
public TimeValue time() {
|
||||
return time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
/eclipse-build/
|
|
@ -12,7 +12,8 @@
|
|||
<artifactId>elasticsearch-license-plugin</artifactId>
|
||||
|
||||
<properties>
|
||||
<!-- TODO: do we want to always enforce non-default keys -->
|
||||
<!-- TODO: do we want to always enforce non-default keys
|
||||
-->
|
||||
<keys.path>${basedir}/src/test/resources</keys.path>
|
||||
<license.basedir combine.self="override">${project.parent.basedir}</license.basedir>
|
||||
</properties>
|
||||
|
@ -30,6 +31,12 @@
|
|||
<version>2.0.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch</groupId>
|
||||
<artifactId>elasticsearch-license-plugin-api</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -8,6 +8,7 @@ package org.elasticsearch.license.plugin;
|
|||
import org.elasticsearch.common.inject.AbstractModule;
|
||||
import org.elasticsearch.common.inject.Scopes;
|
||||
import org.elasticsearch.license.core.LicenseVerifier;
|
||||
import org.elasticsearch.license.plugin.core.LicensesClientService;
|
||||
import org.elasticsearch.license.plugin.core.LicensesService;
|
||||
|
||||
public class LicenseModule extends AbstractModule {
|
||||
|
@ -15,5 +16,6 @@ public class LicenseModule extends AbstractModule {
|
|||
protected void configure() {
|
||||
bind(LicenseVerifier.class).in(Scopes.SINGLETON);
|
||||
bind(LicensesService.class).in(Scopes.SINGLETON);
|
||||
bind(LicensesClientService.class).to(LicensesService.class).in(Scopes.SINGLETON);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
package org.elasticsearch.license.plugin.core;
|
||||
|
||||
import org.elasticsearch.common.inject.ImplementedBy;
|
||||
import org.elasticsearch.license.core.License;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.elasticsearch.license.plugin.core.LicensesService.*;
|
||||
import static org.elasticsearch.license.plugin.core.LicensesService.TrialLicenseOptions;
|
||||
|
||||
@ImplementedBy(LicensesService.class)
|
||||
public interface LicensesClientService {
|
||||
|
||||
public interface Listener {
|
||||
|
||||
/**
|
||||
* Called to enable a feature
|
||||
*/
|
||||
public void onEnabled(License license);
|
||||
|
||||
/**
|
||||
* Called to disable a feature
|
||||
*/
|
||||
public void onDisabled(License license);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a feature for licensing
|
||||
*
|
||||
* @param feature - name of the feature to register (must be in sync with license Generator feature name)
|
||||
* @param trialLicenseOptions - Trial license specification used to generate a one-time trial license for the feature;
|
||||
* use <code>null</code> if no trial license should be generated for the feature
|
||||
* @param expirationCallbacks - A collection of Pre and/or Post expiration callbacks
|
||||
* @param listener - used to notify on feature enable/disable
|
||||
*/
|
||||
void register(String feature, TrialLicenseOptions trialLicenseOptions, Collection<ExpirationCallback> expirationCallbacks, Listener listener);
|
||||
}
|
|
@ -741,125 +741,8 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
|
|||
}
|
||||
}
|
||||
|
||||
public static class TrialLicenseOptions {
|
||||
final TimeValue duration;
|
||||
final int maxNodes;
|
||||
|
||||
public TrialLicenseOptions(TimeValue duration, int maxNodes) {
|
||||
this.duration = duration;
|
||||
this.maxNodes = maxNodes;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExpirationStatus {
|
||||
private final boolean expired;
|
||||
private final TimeValue time;
|
||||
|
||||
private ExpirationStatus(boolean expired, TimeValue time) {
|
||||
this.expired = expired;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public boolean expired() {
|
||||
return expired;
|
||||
}
|
||||
|
||||
public TimeValue time() {
|
||||
return time;
|
||||
}
|
||||
}
|
||||
|
||||
public static interface LicenseCallback {
|
||||
void on(License license, ExpirationStatus status);
|
||||
}
|
||||
|
||||
public static abstract class ExpirationCallback implements LicenseCallback {
|
||||
|
||||
public enum Orientation { PRE, POST }
|
||||
|
||||
public static abstract class Pre extends ExpirationCallback {
|
||||
|
||||
/**
|
||||
* Callback schedule prior to license expiry
|
||||
*
|
||||
* @param min latest relative time to execute before license expiry
|
||||
* @param max earliest relative time to execute before license expiry
|
||||
* @param frequency interval between execution
|
||||
*/
|
||||
public Pre(TimeValue min, TimeValue max, TimeValue frequency) {
|
||||
super(Orientation.PRE, min, max, frequency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(long expirationDate, long now) {
|
||||
long expiryDuration = expirationDate - now;
|
||||
if (expiryDuration > 0l) {
|
||||
if (expiryDuration <= max().getMillis()) {
|
||||
return expiryDuration >= min().getMillis();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class Post extends ExpirationCallback {
|
||||
|
||||
/**
|
||||
* Callback schedule after license expiry
|
||||
*
|
||||
* @param min earliest relative time to execute after license expiry
|
||||
* @param max latest relative time to execute after license expiry
|
||||
* @param frequency interval between execution
|
||||
*/
|
||||
public Post(TimeValue min, TimeValue max, TimeValue frequency) {
|
||||
super(Orientation.POST, min, max, frequency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(long expirationDate, long now) {
|
||||
long postExpiryDuration = now - expirationDate;
|
||||
if (postExpiryDuration > 0l) {
|
||||
if (postExpiryDuration <= max().getMillis()) {
|
||||
return postExpiryDuration >= min().getMillis();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private final Orientation orientation;
|
||||
private final TimeValue min;
|
||||
private final TimeValue max;
|
||||
private final TimeValue frequency;
|
||||
|
||||
private ExpirationCallback(Orientation orientation, TimeValue min, TimeValue max, TimeValue frequency) {
|
||||
this.orientation = orientation;
|
||||
this.min = (min == null) ? TimeValue.timeValueMillis(0) : min;
|
||||
this.max = (max == null) ? TimeValue.timeValueMillis(Long.MAX_VALUE) : max;
|
||||
this.frequency = frequency;
|
||||
if (frequency == null) {
|
||||
throw new IllegalArgumentException("frequency can not be null");
|
||||
}
|
||||
}
|
||||
|
||||
public Orientation orientation() {
|
||||
return orientation;
|
||||
}
|
||||
|
||||
public TimeValue min() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public TimeValue max() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public TimeValue frequency() {
|
||||
return frequency;
|
||||
}
|
||||
|
||||
public abstract boolean matches(long expirationDate, long now);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores configuration and listener for a feature
|
||||
|
@ -993,6 +876,7 @@ public class LicensesService extends AbstractLifecycleComponent<LicensesService>
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "(feature: " + feature + ", enabled: " + enabled + ")";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue