My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package nl.gridshore.samples.bundles.exampleclient.impl;

import org.osgi.framework.*;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nl.gridshore.samples.bundles.trainingservice.api.TrainingService;

/**
* Created by IntelliJ IDEA.
* User: jettro
* Date: Feb 9, 2008
* Time: 10:35:42 PM
* Activator class for the client
*/
public class Activator implements BundleActivator, ServiceListener {
private Logger logger = LoggerFactory.getLogger(Activator.class);
private Server server;
private ServletHolder servletHolder;
private BundleContext bundleContext;
private static final int JETTY_PORT = 8091;
private static final String JETTY_CONTEXT_PATH = "/";
private static final String MAIN_SERVLET_PATH_SPEC = "/*";
private static final String TRAININGS_SERVLET_PATH_SPEC = "/trainings";

/**
* Initializes the bundle by adding the service listener, starting the jetty servlet container and add the servlet
* to the web container.
* @param bundleContext BundleContext in which this bundle is running
* @throws Exception thrown when one of the methods throws an exception
*/
public void start(BundleContext bundleContext) throws Exception {
logger.debug("Activator for webcontext is started");
this.bundleContext = bundleContext;

startJettyServer();


synchronized (this) {
bundleContext.addServiceListener(this,"(&(objectClass=" + TrainingService.class.getName() + "))");
}

initializeServlet(bundleContext);
}

/**
* Is called when the container stopsm, we use it to stop the server
* @param bundleContext BundleContext in which this bundle is running
* @throws Exception thrown when one of the actions throws an exception
*/
public void stop(BundleContext bundleContext) throws Exception {
// is done by the framework
server.stop();
}

/**
* Method called when an event we listen to takes place. We expect only to receive events with a relation to the
* TrainingService class.
* @param event ServiceEvent passed with details about the event that took place
*/
public void serviceChanged(ServiceEvent event) {
if (event.getType() == ServiceEvent.REGISTERED) {
logger.info("Training service is registered");
servletHolder.setServlet(new TrainingsServlet((TrainingService)bundleContext.getService(event.getServiceReference())));
} else if (event.getType() == ServiceEvent.UNREGISTERING) {
logger.info("Training service is unregistered");
servletHolder.setServlet(new NonAvailableServlet());
} else if (event.getType() == ServiceEvent.MODIFIED) {
logger.info("Training service is modified");
servletHolder.setServlet(new TrainingsServlet((TrainingService)bundleContext.getService(event.getServiceReference())));
}
}

private void initializeServlet(BundleContext bundleContext) throws InvalidSyntaxException {
ServiceReference[] references = bundleContext.getServiceReferences(TrainingService.class.getName(),null);

if (references != null) {
TrainingService trainingService = (TrainingService) bundleContext.getService(references[0]);
servletHolder.setServlet(new TrainingsServlet(trainingService));
}
}

private void startJettyServer() throws Exception {
server = new Server(JETTY_PORT);
Context root = new Context(server, JETTY_CONTEXT_PATH, Context.SESSIONS);
root.addServlet(new ServletHolder(new HelloServlet()), MAIN_SERVLET_PATH_SPEC);
servletHolder = new ServletHolder();
servletHolder.setServlet(new NonAvailableServlet());
root.addServlet(servletHolder, TRAININGS_SERVLET_PATH_SPEC);
server.start();
}
}

Change log

r106 by jettro.coenradie on Feb 15, 2008   Diff
added the support for lifecycle management
of taining service bundle
Go to: 
Project members, sign in to write a code review

Older revisions

r104 by jettro.coenradie on Feb 10, 2008   Diff
created a servlet and embedded jetty
succesfully into the felix container
r102 by jettro.coenradie on Feb 10, 2008   Diff
made some refactorings, now we have
included a logging library
r101 by jettro.coenradie on Feb 10, 2008   Diff
Added the initial version of
FelixTryout project
All revisions of this file

File info

Size: 3989 bytes, 94 lines
Powered by Google Project Hosting