-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from cryptomator/feature/service-loading
Integrated Service Loading
- Loading branch information
Showing
8 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import org.cryptomator.integrations.autostart.AutoStartProvider; | ||
import org.cryptomator.integrations.keychain.KeychainAccessProvider; | ||
import org.cryptomator.integrations.tray.TrayIntegrationProvider; | ||
import org.cryptomator.integrations.uiappearance.UiAppearanceProvider; | ||
|
||
module org.cryptomator.integrations.api { | ||
exports org.cryptomator.integrations.autostart; | ||
exports org.cryptomator.integrations.keychain; | ||
exports org.cryptomator.integrations.tray; | ||
exports org.cryptomator.integrations.uiappearance; | ||
|
||
uses AutoStartProvider; | ||
uses KeychainAccessProvider; | ||
uses TrayIntegrationProvider; | ||
uses UiAppearanceProvider; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/org/cryptomator/integrations/autostart/AutoStartProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.cryptomator.integrations.common; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.Optional; | ||
import java.util.ServiceLoader; | ||
import java.util.stream.Stream; | ||
|
||
public class IntegrationsLoader { | ||
|
||
/** | ||
* Loads the best suited service, i.e. the one with the highest priority that is supported. | ||
* <p> | ||
* If two services are available with the same priority, it is unspecified which one will be returned. | ||
* | ||
* @param clazz Service class | ||
* @param <T> Type of the service | ||
* @return Highest priority service or empty if no supported service was found | ||
*/ | ||
public static <T> Optional<T> load(Class<T> clazz) { | ||
return loadAll(clazz).findFirst(); | ||
} | ||
|
||
/** | ||
* Loads all suited services ordered by priority in descending order. | ||
* | ||
* @param clazz Service class | ||
* @param <T> Type of the service | ||
* @return An ordered stream of all suited service candidates | ||
*/ | ||
public static <T> Stream<T> loadAll(Class<T> clazz) { | ||
return ServiceLoader.load(clazz) | ||
.stream() | ||
.filter(IntegrationsLoader::isSupportedOperatingSystem) | ||
.sorted(Comparator.comparingInt(IntegrationsLoader::getPriority).reversed()) | ||
.map(ServiceLoader.Provider::get); | ||
} | ||
|
||
private static int getPriority(ServiceLoader.Provider<?> provider) { | ||
var prio = provider.type().getAnnotation(Priority.class); | ||
return prio == null ? Priority.DEFAULT : prio.value(); | ||
} | ||
|
||
private static boolean isSupportedOperatingSystem(ServiceLoader.Provider<?> provider) { | ||
var annotations = provider.type().getAnnotationsByType(OperatingSystem.class); | ||
return annotations.length == 0 || Arrays.stream(annotations).anyMatch(OperatingSystem.Value::isCurrent); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/cryptomator/integrations/common/OperatingSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.cryptomator.integrations.common; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Restricts the annotated integration provider to one or more operating system(s). | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
@Repeatable(OperatingSystem.OperatingSystems.class) | ||
public @interface OperatingSystem { | ||
Value value() default Value.UNKNOWN; | ||
|
||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
@interface OperatingSystems { | ||
OperatingSystem[] value(); | ||
} | ||
|
||
enum Value { | ||
LINUX, | ||
MAC, | ||
WINDOWS, | ||
UNKNOWN; | ||
|
||
private static final String OS_NAME = System.getProperty("os.name", "").toLowerCase(); | ||
|
||
public static Value current() { | ||
if (OS_NAME.contains("linux")) { | ||
return LINUX; | ||
} else if (OS_NAME.contains("mac")) { | ||
return MAC; | ||
} else if (OS_NAME.contains("windows")) { | ||
return WINDOWS; | ||
} else { | ||
return UNKNOWN; | ||
} | ||
} | ||
|
||
public static boolean isCurrent(OperatingSystem os) { | ||
return current().equals(os.value()); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/cryptomator/integrations/common/Priority.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.cryptomator.integrations.common; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Integration Priority. | ||
* <p> | ||
* If multiple implementations for an integration can be provided, the provider with the highest priority will be used. | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
public @interface Priority { | ||
int DEFAULT = 0; | ||
int FALLBACK = Integer.MIN_VALUE; | ||
|
||
int value() default DEFAULT; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/org/cryptomator/integrations/keychain/KeychainAccessProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/org/cryptomator/integrations/tray/TrayIntegrationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/org/cryptomator/integrations/uiappearance/UiAppearanceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters