下面列出了org.osgi.framework.ServiceListener#org.restlet.Application 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void start(BundleContext context) throws Exception {
context.addServiceListener(new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.REGISTERED){
component = new Component();
Server server = new Server(Protocol.HTTP, 8182);
component.getServers().add(server);
component.setLogService(new LogService(false));
final Application app = new ApiApplication();
component.getDefaultHost().attachDefault(app);
try {
component.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, "(objectclass=" + ApiStartServiceToken.class.getName() +")");
}
@Before
public void setUp() throws UnknownHostException {
mongo = new Mongo();
helper = new ApiHelper();
PongoFactory.getInstance().getContributors().add(new OsgiPongoFactoryContributor());
platform = Platform.getInstance();
platform.setMongo(mongo);
platform.initialize();
WORKER_ID = Configuration.getInstance().getSlaveIdentifier();
// Register Worker
platform.getAnalysisRepositoryManager().getWorkerService().registerWorker(WORKER_ID);
Component component = new Component();
Server server = new Server(Protocol.HTTP, 8182);
component.getServers().add(server);
final Application app = new ApiApplication();
component.getDefaultHost().attachDefault(app);
try {
component.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Before
public void setUp() throws UnknownHostException {
mongo = new Mongo();
helper = new ApiHelper();
PongoFactory.getInstance().getContributors().add(new OsgiPongoFactoryContributor());
platform = Platform.getInstance();
platform.setMongo(mongo);
platform.initialize();
WORKER_ID = Configuration.getInstance().getSlaveIdentifier();
// Register Worker
platform.getAnalysisRepositoryManager().getWorkerService().registerWorker(WORKER_ID);
Component component = new Component();
Server server = new Server(Protocol.HTTP, 8182);
component.getServers().add(server);
final Application app = new ApiApplication();
component.getDefaultHost().attachDefault(app);
try {
component.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void runRestApplication() throws Exception {
if (workerConf.getWorkerPort() == 0) {
return;
}
Component _component = new Component();
_component.getServers().add(Protocol.HTTP, workerConf.getWorkerPort());
_component.getClients().add(Protocol.FILE);
_component.getClients().add(Protocol.JAR);
Context applicationContext = _component.getContext().createChildContext();
LOGGER.info("Injecting workerInstance to the api context, port {}", workerConf.getWorkerPort());
applicationContext.getAttributes().put(WorkerInstance.class.toString(), workerInstance);
Application restletApplication = new RestletApplication(null);
restletApplication.setContext(applicationContext);
_component.getDefaultHost().attach(restletApplication);
_component.start();
}
public void start() throws Exception {
_component.getServers().add(Protocol.HTTP, _config.getManagerPort());
_component.getClients().add(Protocol.FILE);
_component.getClients().add(Protocol.JAR);
Context applicationContext = _component.getContext().createChildContext();
LOGGER.info("Injecting conf and helix to the api context");
applicationContext.getAttributes().put(ManagerConf.class.toString(), _config);
applicationContext.getAttributes()
.put(ControllerHelixManager.class.toString(), _controllerHelixManager);
applicationContext.getAttributes()
.put(KafkaClusterValidationManager.class.toString(), _kafkaValidationManager);
Application managerRestApp = new ManagerRestApplication(null);
managerRestApp.setContext(applicationContext);
_component.getDefaultHost().attach(managerRestApp);
try {
LOGGER.info("Starting helix manager");
_controllerHelixManager.start();
LOGGER.info("Starting src and dst kafka cluster validation manager");
_kafkaValidationManager.start();
LOGGER.info("Starting API component");
_component.start();
} catch (final Exception e) {
LOGGER.error("Caught exception while starting uReplicator-Manager", e);
throw e;
}
}
@Inject
public RestComponent(@Hello Application helloApp, @Car Application carApp, Verifier authTokenVerifier) {
getClients().add(Protocol.HTTPS);
Server secureServer = getServers().add(Protocol.HTTPS, 8043);
Series<Parameter> parameters = secureServer.getContext().getParameters();
parameters.add("sslContextFactory", "org.restlet.engine.ssl.DefaultSslContextFactory");
parameters.add("keyStorePath", System.getProperty("javax.net.ssl.keyStorePath"));
getDefaultHost().attach("/api/hello", secure(helloApp, authTokenVerifier, "ame"));
getDefaultHost().attach("/api/cars", secure(carApp, authTokenVerifier, "ame"));
replaceConverter(JacksonConverter.class, new JacksonCustomConverter());
}
private Restlet secure(Application app, Verifier verifier, String realm) {
ChallengeAuthenticator guard = new ChallengeAuthenticator(getContext().createChildContext(),
ChallengeScheme.HTTP_OAUTH_BEARER, realm);
guard.setVerifier(verifier);
guard.setNext(app);
return guard;
}
public static Context getCurrentApplicationContext() {
Application application = Application.getCurrent();
if (application == null) {
return null;
}
return application.getContext();
}
@Override
protected Application createApplication(Context parentContext) {
Application application = super.createApplication(parentContext);
if (application == null) {
application = new OntopiaRestApplication(parentContext.createChildContext());
}
return application;
}
public void start(BundleContext context) throws Exception {
System.err.println("Starting Admin bundle");
context.addServiceListener(new ServiceListener() {
@Override
public void serviceChanged(ServiceEvent event) {
System.err.println(event);
if (event.getType() == ServiceEvent.REGISTERED){
Application application = new AdminApplication();
component = new Component();
component.getServers().add(Protocol.HTTP, 8183);
component.getClients().add(Protocol.FILE);
boolean useAuth = Boolean.valueOf(Configuration.getInstance().getProperty("adminapi.use_authentication", "false"));
if (useAuth) {
String username = Configuration.getInstance().getProperty("adminapi.username", null);
String password = Configuration.getInstance().getProperty("adminapi.password", null);
ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "myRealm");
MapVerifier verifier = new MapVerifier();
verifier.getLocalSecrets().put(username, password.toCharArray());
guard.setVerifier(verifier);
guard.setNext(application);
component.getDefaultHost().attachDefault(guard);
} else {
component.getDefaultHost().attachDefault(application);
}
try {
component.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, "(objectclass=" + ApiStartServiceToken.class.getName() +")");
}
@Override
protected void configure() {
bind(Application.class).annotatedWith(Car.class).to(CarApplication.class);
bind(CarRepository.class).to(InMemoryCarRepository.class);
}
@Override
protected void configure() {
bind(Application.class).annotatedWith(Hello.class).to(HelloApplication.class);
bind(CarService.class).to(RemoteCarService.class);
}
@Override
@SuppressWarnings( "unchecked" )
protected Application createApplication( Context context )
{
return module.newObject( Application.class, context.createChildContext(), getServletConfig(), getServletContext() );
}