下面列出了org.osgi.framework.ServiceListener#org.restlet.Component 实例代码,或者点击链接到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 ControllerInstance(ManagerControllerHelix managerControllerHelix, ControllerConf conf) {
LOGGER.info("Trying to init ControllerStarter with config: {}", conf);
_managerControllerHelix = managerControllerHelix;
_config = conf;
initializeMetricsReporter(conf);
_component = new Component();
_controllerRestApp = new ControllerRestApplication(null);
_helixMirrorMakerManager = new HelixMirrorMakerManager(_config);
_validationManager = new ValidationManager(_helixMirrorMakerManager);
_srcKafkaValidationManager = getSourceKafkaClusterValidationManager();
_autoTopicWhitelistingManager = getAutoTopicWhitelistingManager();
if (_config.getBackUpToGit()) {
_clusterInfoBackupManager = new ClusterInfoBackupManager(_helixMirrorMakerManager,
new GitBackUpHandler(conf.getRemoteBackupRepo(), conf.getLocalGitRepoPath()), _config);
} else {
_clusterInfoBackupManager = new ClusterInfoBackupManager(_helixMirrorMakerManager,
new FileBackUpHandler(conf.getLocalBackupFilePath()), _config);
}
}
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();
}
@Override
protected Component createComponent() {
Component component = super.createComponent();
// a bit more configuration by initParams
LogService log = component.getLogService();
// allow access to the LogService in the application via the context
component.getContext().getAttributes().put(LogService.class.getName(), log);
// modify the logger name
String loggerName = getInitParameter(LOGGER_NAME_ATTRIBUTE, LOGGER_NAME_ATTRIBUTE_DEFAULT_VALUE);
log.setLoggerName(loggerName);
// modify the logger pattern
// note: overridden to be smaller than the restlet default value
log.setResponseLogFormat(getInitParameter(LOGGER_FORMAT_ATTRIBUTE, LOGGER_FORMAT_ATTRIBUTE_DEFAULT_VALUE));
return component;
}
public static void main(String[] args) {
try {
// Create a new Component.
Component component = new Component();
// Add a new HTTP server listening on port 8082.
component.getServers().add(Protocol.HTTP, 8080);
// Attach the sample application.
component.getDefaultHost().attach(new RestletApplication());
// Start the component.
component.start();
} catch (Exception e) {
// Something is wrong.
e.printStackTrace();
}
}
public ManagerStarter(ManagerConf conf) {
LOGGER.info("Trying to init ManagerStarter with config: {}", conf);
_config = conf;
initializeMetricsReporter(conf);
_component = new Component();
_kafkaValidationManager = new KafkaClusterValidationManager(_config);
_controllerHelixManager = new ControllerHelixManager(_kafkaValidationManager, _config);
}
public void start(final BundleContext context) throws Exception {
component = new Component();
component.getServers().add(Protocol.HTTP, 8888);
component.getClients().add(Protocol.CLAP);
component.getDefaultHost().attach("", new RestService(context));
component.start();
}
public void run(FloodlightModuleContext fmlContext, String restHost, int restPort) {
setStatusService(new StatusService() {
@Override
public Representation getRepresentation(Status status,
Request request,
Response response) {
return new JacksonRepresentation<Status>(status);
}
});
// Add everything in the module context to the rest
for (Class<? extends IFloodlightService> s : fmlContext.getAllServices()) {
if (logger.isTraceEnabled()) {
logger.trace("Adding {} for service {} into context",
s.getCanonicalName(), fmlContext.getServiceImpl(s));
}
context.getAttributes().put(s.getCanonicalName(),
fmlContext.getServiceImpl(s));
}
// Start listening for REST requests
try {
final Component component = new Component();
if (restHost == null) {
component.getServers().add(Protocol.HTTP, restPort);
} else {
component.getServers().add(Protocol.HTTP, restHost, restPort);
}
component.getClients().add(Protocol.CLAP);
component.getDefaultHost().attach(this);
component.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void startServer()
throws Exception
{
configuration.refresh();
component = new Component();
component.getServers().add( Protocol.HTTP, configuration.get().port().get() );
RestApplication application = module.newObject( RestApplication.class, component.getContext() );
component.getDefaultHost().attach( application );
component.start();
}
public static void main(String[] args) throws Exception {
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8082);
component.getDefaultHost().attach(new FoxbpmRestApplication());
component.start();
System.out.println("The restlet server started ...");
}
@Override
public void start() throws UnknownHostException {
if (isEnabled) {
final boolean isRunning = this.isRunning.getAndSet(true);
if (!isRunning) {
if (isEnabled && port <= 0) {
this.isEnabled = false;
TestServerConsole.log("Could not start RestService. Parameter missing: 'server.service.rest.port'", 1, TestServerServiceEnum.TEST_SERVER);
}
Component component = new Component();
Server s = component.getServers().add(isSsl ? Protocol.HTTPS : Protocol.HTTP, InetAddress.getLocalHost().getHostAddress(), port);
if (isSsl) {
Series<Parameter> parameters = s.getContext().getParameters();
parameters.add("keystorePath", QOS_KEY_FILE_ABSOLUTE);
parameters.add("keystorePassword", TestServer.QOS_KEY_PASSWORD);
parameters.add("keyPassword", TestServer.QOS_KEY_PASSWORD);
parameters.add("keystoreType", TestServer.QOS_KEY_TYPE);
}
component.getDefaultHost().attach("", new RestletApplication());
try {
component.start();
TestServerConsole.log("[" + getName() + "] started: " + toString(), 1, TestServerServiceEnum.TEST_SERVER);
} catch (Exception e) {
TestServerConsole.error(getName(), e, 0, TestServerServiceEnum.TEST_SERVER);
}
}
}
}
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
workspaceResourceServerManager = WorkspaceResourceServerManager.getInstance();
port = SocketUtil.findFreePort();
workspaceResourceServerManager.start(port);
final Component component = workspaceResourceServerManager.getComponent();
assertThat(component).isNotNull();
assertThat(component.isStarted()).isTrue();
assertThat(component.getServers()).hasSize(1);
server = component.getServers().get(0);
defaultHost = component.getDefaultHost();
}
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() +")");
}
protected Component getComponent() {
return component;
}