11package javaxt .express ;
22import javaxt .express .cms .Content ;
3- import javaxt .express .cms .WebSite ;
3+
4+ import javaxt .http .servlet .HttpServlet ;
45import javaxt .http .servlet .HttpServletRequest ;
5- import javaxt .utils .Console ;
6+ import javaxt .http .servlet .HttpServletResponse ;
7+ import javaxt .http .servlet .ServletException ;
8+
9+ import javaxt .io .Jar ;
10+ import static javaxt .utils .Console .*;
11+
12+ import java .util .*;
13+ import java .io .IOException ;
14+
615
716//******************************************************************************
817//** Express Server
@@ -20,64 +29,95 @@ public class Server {
2029 /** Command line interface used to start the server.
2130 */
2231 public static void main (String [] arr ) {
23- java .util .HashMap <String , String > args = Console .parseArgs (arr );
24-
25-
26- if (args .containsKey ("-deploy" )){
32+ HashMap <String , String > args = console .parseArgs (arr );
33+
34+
35+ //Get jar file
36+ Jar jar = new Jar (Server .class );
37+ javaxt .io .File jarFile = new javaxt .io .File (jar .getFile ());
38+ String version = jar .getVersion ();
39+
40+
41+
42+ //Process command line args
43+ if (args .containsKey ("-version" )){
44+ if (version ==null ) version = "Unknown" ;
45+ System .out .println (version );
46+ }
47+ else if (args .containsKey ("-deploy" )){
2748 Deploy .main (arr );
2849 }
29- else {
30-
31- //Get port
32- int port ;
33- try { port = Integer .parseInt (args .get ("-p" )); }
34- catch (Exception e ){
35- System .err .println ("Port (\" -p\" ) is required." );
36- return ;
37- }
38-
39-
50+ else if (args .containsKey ("-start" )){
51+
52+
4053 //Get directory
41- javaxt .io .Directory dir ;
54+ String dir = getValue (args , "-d" , "-dir" , "-directory" , "-web" ).toString ();
55+ javaxt .io .Directory web ;
4256 try {
43- dir = new javaxt .io .Directory (args .get ("-d" ));
44- if (!dir .exists ()) throw new Exception ();
57+ if (dir .endsWith ("\" " )) dir = dir .substring (0 , dir .length ()-1 );
58+
59+ java .io .File f = new java .io .File (dir );
60+ if (f .isFile ()) f = f .getParentFile ();
61+ web = new javaxt .io .Directory (f );
62+ if (!web .exists ()) throw new Exception ();
4563 }
4664 catch (Exception e ){
47- System .err .println ("Directory (\" -d \" ) is required." );
65+ System .err .println ("Directory (\" -dir \" ) is required." );
4866 return ;
4967 }
50-
51-
52- //Get number of threads
53- int numThreads = 50 ;
54- try { numThreads = Integer .parseInt (args .get ("-t" )); }
55- catch (Exception e ){}
56-
57-
68+
69+
70+ //Get config file
71+ javaxt .io .File configFile = (args .containsKey ("-config" )) ?
72+ getFile (args .get ("-config" ), jarFile ) :
73+ new javaxt .io .File (jar .getFile ().getParentFile (), "config.json" );
74+
75+
76+ //Get servlet
77+ HttpServlet servlet ;
78+ javaxt .utils .Value start = getValue (args , "-start" );
79+ if (start .equals ("cms" )) servlet = new WebSite (web , configFile );
80+ else servlet = new WebApp (web );
81+
82+
83+
84+ //Get port (optional)
85+ Integer port = getValue (args , "-p" , "-port" ).toInteger ();
86+ if (port ==null ) port = 8080 ;
87+
88+
89+
90+ //Get number of threads (optional)
91+ Integer numThreads = getValue (args , "-t" , "-threads" ).toInteger ();
92+ if (numThreads ==null ) numThreads = 250 ;
93+
94+
95+
5896 //Start server
5997 try {
60- javaxt .http .Server server = new javaxt .http .Server (port , numThreads , new Demo ( dir ) );
98+ javaxt .http .Server server = new javaxt .http .Server (port , numThreads , servlet );
6199 server .start ();
62100 }
63101 catch (Exception e ) {
64102 System .out .println ("Server could not start because of an " + e .getClass ());
65103 System .exit (1 );
66104 }
67105 }
106+ else {
107+
108+ }
68109 }
69110
70111
71112 //**************************************************************************
72- //** Demo WebSite
113+ //** WebSite
73114 //**************************************************************************
74- private static class Demo extends WebSite {
75- private Demo (javaxt .io .Directory dir ){
115+ private static class WebSite extends javaxt . express . cms . WebSite {
116+ private WebSite (javaxt .io .Directory dir , javaxt . io . File configFile ){
76117 super (dir );
77- super .setAuthor ("ACME Inc" );
78118 }
79-
80- /** Returns an html snippet found in the given file. Overrides the native
119+
120+ /** Returns an html snippet found in the given file. Overrides the native
81121 * getContent method to support custom tags (e.g. "index").
82122 */
83123 public Content getContent (HttpServletRequest request , javaxt .io .File file ){
@@ -94,7 +134,7 @@ public Content getContent(HttpServletRequest request, javaxt.io.File file){
94134 //Return content
95135 if (path .equals ("wiki" )){
96136 String html = file .getText ();
97- java . util . Date date = file .getDate ();
137+ Date date = file .getDate ();
98138 if (file .getName (false ).equals ("index" )){
99139 Content content = getIndex (file );
100140 javaxt .utils .Date d = new javaxt .utils .Date (content .getDate ());
@@ -111,4 +151,48 @@ public Content getContent(HttpServletRequest request, javaxt.io.File file){
111151 }
112152 }
113153 }
154+
155+
156+ //**************************************************************************
157+ //** WebApp
158+ //**************************************************************************
159+ private static class WebApp extends HttpServlet {
160+ private FileManager fileManager ;
161+ public WebApp (javaxt .io .Directory web ){
162+ fileManager = new FileManager (web );
163+ }
164+ public void processRequest (HttpServletRequest request , HttpServletResponse response )
165+ throws ServletException , IOException {
166+ //response.setHeader("Server", server);
167+ fileManager .sendFile (request , response );
168+ }
169+ }
170+
171+
172+ //**************************************************************************
173+ //** getValue
174+ //**************************************************************************
175+ private static javaxt .utils .Value getValue (HashMap <String , String > args , String ...keys ){
176+ for (String key : keys ){
177+ if (args .containsKey (key )){
178+ return new javaxt .utils .Value (args .get (key ));
179+ }
180+ }
181+ return new javaxt .utils .Value (null );
182+ }
183+
184+ //**************************************************************************
185+ //** getFile
186+ //**************************************************************************
187+ /** Returns a File for a given path
188+ * @param path Full canonical path to a file or a relative path (relative
189+ * to the jarFile)
190+ */
191+ public static javaxt .io .File getFile (String path , javaxt .io .File jarFile ){
192+ javaxt .io .File file = new javaxt .io .File (path );
193+ if (!file .exists ()){
194+ file = new javaxt .io .File (jarFile .MapPath (path ));
195+ }
196+ return file ;
197+ }
114198}
0 commit comments