I needed it to be light weight because it is an agent program, that
might be run on a standard user machine without a web server.
Regardless here is the code that works.
private void setupWebServices(Properties props) throws Exception {
System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME,
BusFactory.DEFAULT_BUS_FACTORY);
try {
// Start up the jetty embedded server
int port = 8080;
try {
port =
Integer.parseInt(props.getProperty("slx.http.port"));
}
catch(NumberFormatException nfe){
// do nothing default to 8080
}
server = new Server(port);
ContextHandlerCollection contexts = new
ContextHandlerCollection();
server.setHandler(contexts);
Context root = new Context(contexts, "/",
Context.SESSIONS);
root.setResourceBase("../webServerRoot");
root.addServlet(DefaultServlet.class, "/*");
CXFNonSpringServlet cxf = new CXFNonSpringServlet();
ServletHolder servlet = new ServletHolder(cxf);
servlet.setName("ws");
servlet.setForcedPath("ws");
root.addServlet(servlet, "/ws/*");
server.start();
Bus bus = cxf.getBus();
BusFactory.setDefaultBus(bus);
Enumeration<?> keys = props.keys();
while(keys.hasMoreElements()){
String key = (String)keys.nextElement();
if (key.startsWith(WEB_SERVICE_URL)){
String url = props.getProperty(key);
String subKey = key.replaceFirst(WEB_SERVICE_URL,
"");
String implKey = WEB_SERVICE_IMPL + subKey;
String implClassName =
props.getProperty(implKey);
Class<?> klass = Class.forName(implClassName);
Constructor<?> konstr =
klass.getConstructor(new Class[]{});
Object svcImpl = konstr.newInstance(new
Object[]{});
Endpoint.publish(url, svcImpl);
}
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
finally {
// clean up the system properties
System.clearProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME);
}
}
Thanks
Garry
On Feb 23, 2009, at 2:33 PM, Andrew Clegg wrote:
Err... Why not host your cxf services as a servlet in Tomcat, and
run your other web site as the ROOT webapp?
Or insert favourite servlet container above.
Andrew.
On 23 Feb 2009, at 19:06, Garry Watkins <[email protected]>
wrote:
I have an application that I need both a web server, and webservices
to run together on the same port. How do I configure cxf to work?
Example
I need to run on http://192.168.10.100:9000/
From this directory I will serve misc html and resources for a flex
application
Then I need to run two different web services
http://192.168.10.100:9000/ws/SomeWebService1
http://192.168.10.100:9000/ws/SomeWebService2
How do I set this up?
This is what I have so far. Please help I have been beating myself
up
all morning. I would also prefer to do this without the spring
configuration. I already have a zillion jars so I don't really want
that many more.
TIA
Garry
private void setupHTTPServer(Properties props) throws Exception {
SelectChannelConnector con = new SelectChannelConnector();
String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
con.setHost(ipAddress);
int port = 8080;
try {
port =
Integer.parseInt(props.getProperty("slx.http.port"));
}
catch(NumberFormatException nfe){
// do nothing default to 8080
}
con.setPort(port);
JettyHTTPServerEngineFactory sef = new
JettyHTTPServerEngineFactory();
JettyHTTPServerEngine eng =
sef.createJettyHTTPServerEngine(port, "http");
eng.setConnector(con);
List<Handler> handlers = new ArrayList<Handler>();
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setResourceBase("../webServerRoot");
handlers.add(resource_handler);
ServletHandler servlet_handler = new ServletHandler();
servlet_handler.addServletWithMapping(, "/MasterStatus");
servlet_handler.addServletWithMapping(SendEmployeesServlet.class, "/
SendEmployees");
handlers.add(servlet_handler);
eng.setHandlers(handlers);
}
private void setupServices(Properties props) throws Exception {
String ipAddress = props.getProperty(HTTP_IP_ADDRESS);
String port = props.getProperty(HTTP_PORT);
Enumeration<?> keys = props.keys();
while(keys.hasMoreElements()){
String key = (String)keys.nextElement();
if (key.startsWith(WEB_SERVICE_URL)){
String url =
props.getProperty(key).replace("{http.ipAddress}",
ipAddress).replace("{http.port}", port);
String subKey = key.replaceFirst(WEB_SERVICE_URL, "");
String implKey = WEB_SERVICE_IMPL + subKey;
String implClassName = props.getProperty(implKey);
Class<?> klass = Class.forName(implClassName);
Constructor<?> konstr = klass.getConstructor(new
Class[]{});
Object svcImpl = konstr.newInstance(new Object[]{});
JaxWsServerFactoryBean svrFactory = new
JaxWsServerFactoryBean();
svrFactory.setServiceClass(klass);
svrFactory.setAddress(url);
svrFactory.setServiceBean(svcImpl);
svrFactory.create();
}
}
}