[mud4j-commit] SF.net SVN: mud4j: [102] trunk/mud4j-web
Status: Pre-Alpha
Brought to you by:
mpurland
From: <mpu...@us...> - 2007-01-14 00:37:12
|
Revision: 102 http://mud4j.svn.sourceforge.net/mud4j/?rev=102&view=rev Author: mpurland Date: 2007-01-13 16:29:38 -0800 (Sat, 13 Jan 2007) Log Message: ----------- Add basic server functionality. Added Paths: ----------- trunk/mud4j-web/resources/ trunk/mud4j-web/resources/faces-config.xml trunk/mud4j-web/resources/spring-config.xml trunk/mud4j-web/resources/web.xml trunk/mud4j-web/src/java/net/sf/mud4j/web/Mud4jServer.java trunk/mud4j-web/src/java/net/sf/mud4j/web/server/ trunk/mud4j-web/src/java/net/sf/mud4j/web/server/JettyWebServer.java trunk/mud4j-web/src/java/net/sf/mud4j/web/server/WebServer.java Added: trunk/mud4j-web/resources/faces-config.xml =================================================================== --- trunk/mud4j-web/resources/faces-config.xml (rev 0) +++ trunk/mud4j-web/resources/faces-config.xml 2007-01-14 00:29:38 UTC (rev 102) @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!DOCTYPE faces-config PUBLIC + "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" + "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> +<!-- JSF Myfaces configuration --> +<faces-config> +</faces-config> \ No newline at end of file Added: trunk/mud4j-web/resources/spring-config.xml =================================================================== --- trunk/mud4j-web/resources/spring-config.xml (rev 0) +++ trunk/mud4j-web/resources/spring-config.xml 2007-01-14 00:29:38 UTC (rev 102) @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- mud4j spring configuration --> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:util="http://www.springframework.org/schema/util" + xmlns:aop="http://www.springframework.org/schema/aop" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd + http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> + + <bean id="webServer" class="net.sf.mud4j.web.server.JettyWebServer"> + <constructor-arg type="int" value="8081" /> + </bean> +</beans> \ No newline at end of file Added: trunk/mud4j-web/resources/web.xml =================================================================== --- trunk/mud4j-web/resources/web.xml (rev 0) +++ trunk/mud4j-web/resources/web.xml 2007-01-14 00:29:38 UTC (rev 102) @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> +<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" id="WebApp_ID"> + + <listener> + <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> + </listener> + <listener> + <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> + </listener> + + <description>web.xml</description> + <display-name>mud4j web application</display-name> + + <context-param> + <param-name>contextConfigLocation</param-name> + <param-value>/WEB-INF/spring-config.xml</param-value> + </context-param> + +</web-app> \ No newline at end of file Added: trunk/mud4j-web/src/java/net/sf/mud4j/web/Mud4jServer.java =================================================================== --- trunk/mud4j-web/src/java/net/sf/mud4j/web/Mud4jServer.java (rev 0) +++ trunk/mud4j-web/src/java/net/sf/mud4j/web/Mud4jServer.java 2007-01-14 00:29:38 UTC (rev 102) @@ -0,0 +1,52 @@ +/** + * Copyright 2006 Matthew Purland (m.p...@gm...) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.mud4j.web; + +import java.io.IOException; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import net.sf.mud4j.web.server.JettyWebServer; +import net.sf.mud4j.web.server.WebServer; + +/** + * Provides web by using embedded web server. + * + * @author Matthew Purland + */ +public class Mud4jServer { + + public static void main(String[] args) { + AbstractApplicationContext appContext = new ClassPathXmlApplicationContext( + new String[] { "spring-config.xml" } ); + BeanFactory beanFactory = (BeanFactory) appContext; + + appContext.registerShutdownHook(); + + // Get our WebServer bean from our Spring beans + WebServer webServer = (WebServer) beanFactory.getBean("webServer", WebServer.class); + + try { + webServer.start(); + } + catch (IOException e) { + System.out.println(e); + } + } +} Added: trunk/mud4j-web/src/java/net/sf/mud4j/web/server/JettyWebServer.java =================================================================== --- trunk/mud4j-web/src/java/net/sf/mud4j/web/server/JettyWebServer.java (rev 0) +++ trunk/mud4j-web/src/java/net/sf/mud4j/web/server/JettyWebServer.java 2007-01-14 00:29:38 UTC (rev 102) @@ -0,0 +1,130 @@ +/** + * Copyright 2006 Matthew Purland (m.p...@gm...) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.mud4j.web.server; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.mortbay.jetty.Connector; +import org.mortbay.jetty.Handler; +import org.mortbay.jetty.Request; +import org.mortbay.jetty.Server; +import org.mortbay.jetty.handler.AbstractHandler; +import org.mortbay.jetty.nio.SelectChannelConnector; +import org.mortbay.thread.BoundedThreadPool; + +/** + * Provide a Jetty webserver that will use classes on the classpath or as + * specified to configure the container. + * + * @author Matthew Purland + */ +public class JettyWebServer implements WebServer { + + // Jetty server + private Server server; + + // Port to listen on + private int port; + + /** + * Constructor to provide configurable port listener. + * + * @param port Port to listen on. + */ + public JettyWebServer(int port) { + this.port = port; + } + + /** + * Configure server servlets and configurations. + * + * @throws IOException in case of Jetty configuration setup problem + */ + protected void configureAndStartServer() throws IOException { + try { + server = new Server(); + Connector connector = new SelectChannelConnector(); + connector.setPort(port); + + // Create a thread pool to control number of threads for requests + BoundedThreadPool threadPool = new BoundedThreadPool(); + threadPool.setMaxThreads(100); + server.setThreadPool(threadPool); + + Handler handler = new AbstractHandler() + { + + public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { + response.setContentType("text/html"); + response.setStatus(HttpServletResponse.SC_OK); + response.getWriter().println("<h1>testing</h1>"); + ((Request)request).setHandled(true); + + } + }; + server.setHandler(handler); + + server.setStopAtShutdown(true); + server.setConnectors(new Connector[] { connector }); + + server.start(); + //server.join(); + } + catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + /** + * {@inheritDoc} + */ + public void start() throws IOException { + configureAndStartServer(); + } + + /** + * {@inheritDoc} + */ + public void stop() throws IOException { + if (server != null) { + try { + server.stop(); + } + catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + } + + /** + * {@inheritDoc} + */ + public boolean isStarted() { + return server.isStarted(); + } + + /** + * {@inheritDoc} + */ + public boolean isStopped() { + return server.isStopped(); + } + +} Added: trunk/mud4j-web/src/java/net/sf/mud4j/web/server/WebServer.java =================================================================== --- trunk/mud4j-web/src/java/net/sf/mud4j/web/server/WebServer.java (rev 0) +++ trunk/mud4j-web/src/java/net/sf/mud4j/web/server/WebServer.java 2007-01-14 00:29:38 UTC (rev 102) @@ -0,0 +1,53 @@ +/** + * Copyright 2006 Matthew Purland (m.p...@gm...) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.sf.mud4j.web.server; + +import java.io.IOException; + +/** + * Provide interface for configuring a web server. + * + * @author Matthew Purland + */ +public interface WebServer { + /** + * Start the webserver. + * + * @throws IOException in case of I/O problems or the server won't start. + */ + void start() throws IOException; + + /** + * Stop the webserver. + * + * @throws IOException in case of I/O problems or if it can't stop the server. + */ + void stop() throws IOException; + + /** + * Whether the web server is started. + * + * @return Returns whether the web server is currently started. + */ + boolean isStarted(); + + /** + * Whether the web server is stopped. + * + * @returns Returns whether the web server is stopped. + */ + boolean isStopped(); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |