Created
December 12, 2012 10:42
-
-
Save njbartlett/4266809 to your computer and use it in GitHub Desktop.
Gatekeeper component example
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
package org.example.exchange.gatekeeper; | |
import java.io.File; | |
import java.util.List; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
import org.example.osgi.utils.components.ComponentHelper; | |
import org.osgi.service.component.ComponentContext; | |
import aQute.bnd.annotation.component.Activate; | |
import aQute.bnd.annotation.component.Component; | |
import aQute.bnd.annotation.component.Deactivate; | |
@Component(name = GatekeeperComp.COMP_NAME) | |
public class GatekeeperComp { | |
static final String COMP_NAME = "org.example.exchange.gatekeeper"; | |
private ScheduledExecutorService executor; | |
@Activate | |
void activate(final ComponentContext context) { | |
executor = Executors.newScheduledThreadPool(1); | |
Runnable check = new Runnable() { | |
public void run() { | |
if (new File("licence.txt").exists()) { | |
context.enableComponent(null); | |
} else { | |
List<String> names = ComponentHelper.getComponentNames(context.getBundleContext()); | |
for (String name : names) { | |
if (!COMP_NAME.equals(name)) { | |
context.disableComponent(name); | |
} | |
} | |
} | |
} | |
}; | |
executor.scheduleAtFixedRate(check, 2, 2, TimeUnit.SECONDS); | |
} | |
@Deactivate | |
void deactivate() { | |
executor.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment