Source code: org/media/mn8/servlet/mn8Runner.java
1 /*
2 * $COPYRIGHT$
3 * $Id: mn8Runner.java,v 1.4 2002/06/05 00:19:45 neuro Exp $
4 *
5 * Date Author Changes
6 * May 16 2002 Remus Pereni Created
7 */
8 package org.media.mn8.servlet;
9
10
11 import org.media.mn8.concepts.*;
12 import org.media.mn8.event.*;
13 import org.media.mn8.*;
14
15 import javax.servlet.http.*;
16 import javax.servlet.*;
17 import javax.naming.NamingException;
18 import java.util.Enumeration;
19 import java.net.MalformedURLException;
20 import java.io.PrintWriter;
21 import java.io.PrintStream;
22 import java.io.IOException;
23 import java.io.File;
24
25 /**
26 * The servlet implementation which will run mn8 scripts or concepts.
27 * from any servlet aware web server (Apache, IIS, ...).
28 * @author <a href="mailto:remus@nolimits.ro">Remus Pereni</a>
29 * @version $Revision: 1.4 $ $Date: 2002/06/05 00:19:45 $
30 */
31 public class mn8Runner extends HttpServlet {
32 static final String ENV_PATH = "env:/servlet/";
33 private ServletContext _servletContext = null;
34 private ServletConfig _servletConfig = null;
35 private mn8RequestMapper _requestMapper = new mn8RequestMapper();
36 private boolean _enableConceptLookup = false;
37 private boolean _enableMapInfo = false;
38
39 public void init(ServletConfig config ) throws ServletException {
40 System.setProperty("java.protocol.handler.pkgs", "org.media.mn8.protocol");
41 _servletConfig = config;
42 mn8Interpreter.initialize();
43 try {
44 _servletContext = config.getServletContext();
45 NamingManager.envBind(ENV_PATH + "server.info", new StringConcept(_servletContext.getServerInfo()));
46 NamingManager.envBind(ENV_PATH + "servlet.name", new StringConcept(config.getServletName()));
47 NamingManager.envBind(ENV_PATH + "servlet.api.version", new StringConcept("" + _servletContext.getMajorVersion() + "." +_servletContext.getMinorVersion()));
48
49 Enumeration paramNames = config.getInitParameterNames();
50 String name;
51 while( paramNames.hasMoreElements() ) {
52 name = (String)paramNames.nextElement();
53 NamingManager.envBind(ENV_PATH + "/parameters/" + name,
54 new StringConcept( Helper.fillWithValues(config.getInitParameter(name))));
55 }
56 if( config.getInitParameter("debug") != null && config.getInitParameter("debug").equalsIgnoreCase("true")) {
57 mn8RuntimeFlags.setDebug(true);
58 }
59
60 if( config.getInitParameter("verbose") != null && config.getInitParameter("verbose").equalsIgnoreCase("true")) {
61 mn8RuntimeFlags.setVerbose(true);
62 }
63
64 if( config.getInitParameter("EnableConceptLookup") != null && config.getInitParameter("EnableConceptLookup").equalsIgnoreCase("true")) {
65 _enableConceptLookup = true;
66 }
67
68 if( config.getInitParameter("MapInfo") != null) {
69 String path = config.getInitParameter("MapInfo");
70 File fpath;
71 path = Helper.fillWithValues( path);
72 if( path.trim().equals("")) {
73 try {
74 if( _servletContext.getResource("/mn8MapInfo.xml") != null ) {
75 path = _servletContext.getResource("/mn8MapInfo.xml").toString();
76 }
77 } catch ( MalformedURLException mex) {
78 System.err.println(mex.getMessage());
79 }
80 }
81 if( path != null && !path.trim().equals("")) {
82 fpath = new File(path);
83 try {
84 System.err.println("Loading MN8 Mapping Info from: " + fpath.toURL().toString());
85 } catch ( MalformedURLException mex) {
86 System.err.println(mex.getMessage());
87 }
88 _enableMapInfo = true;
89 _requestMapper.parseMapInfo( fpath );
90 if( mn8RuntimeFlags.isDebug() || mn8RuntimeFlags.isVerbose() ) {
91 _requestMapper.printDebugInfo();
92 }
93 }
94 }
95
96 } catch (NamingException nex) {
97 throw new ServletException( nex.getMessage());
98 }
99 }
100
101
102
103 public String getServletInfo() {
104 return "mn8Runner is a Java servlet which allows MN8 based scripts or concepts"
105 + " to be run server side, for more information on MN8 please"
106 + " visit: http://mappa.mundi.net/spacemapper";
107 }
108
109
110
111 /**
112 * Handle the HTTP GET method by building a simple web page.
113 */
114 public void service (HttpServletRequest request, HttpServletResponse response)
115 throws ServletException, IOException {
116
117 ServletRequestConcept requestConc = new ServletRequestConcept(request, _servletContext);
118 ServletResponseConcept responseConc = new ServletResponseConcept(response);
119
120 mn8Interpreter.setOut(new PrintStream(response.getOutputStream()) , Thread.currentThread());
121 mn8Interpreter.setErr(System.err , Thread.currentThread());
122 try {
123 if( request.getPathTranslated() != null ) {
124 File path = new File(request.getPathTranslated());
125 SeriesConcept paramVal = new SeriesConcept();
126 paramVal.add( requestConc );
127 paramVal.add( responseConc );
128
129 if( path.exists() && path.isFile() && path.getName().toLowerCase().endsWith(".mn8")) {
130 SeriesConcept param = new SeriesConcept();
131 param.add( paramVal );
132 mn8Interpreter.threadExecuteURLRun(path.toURL().toString(), null, param);
133 } else {
134 if( mn8RuntimeFlags.isDebug() || mn8RuntimeFlags.isVerbose()) {
135 System.err.println("MN8 Servlet: not direct call");
136 }
137
138 MapInfoResult result = _requestMapper.getMatch( request.getPathInfo());
139
140 if( _enableMapInfo && result != null ) {
141
142 if( mn8RuntimeFlags.isDebug() || mn8RuntimeFlags.isVerbose()) {
143 System.err.println("MN8 Servlet: map enabled");
144 }
145
146 if( mn8RuntimeFlags.isDebug() || mn8RuntimeFlags.isVerbose()) {
147 System.err.println("MN8 Servlet: mapped to : " + result.conceptName);
148 }
149
150 if( ! result.conceptName.equalsIgnoreCase("_SYSTEM_")) {
151 paramVal.addSeries( result.conceptParams );
152 SeriesConcept param = new SeriesConcept();
153 param.add(paramVal);
154 mn8Interpreter.threadExecuteURLRun(result.conceptName, null, param);
155 } else {
156 (new mn8SystemServlet(_servletConfig, request, response)).serve();
157 }
158
159 } else if ( _enableConceptLookup ) {
160 String conceptName = request.getPathInfo();
161 if( conceptName.startsWith("/")) {
162 conceptName = conceptName.substring(1);
163 }
164
165 if( conceptName.indexOf("/") != -1 ) {
166 conceptName = conceptName.substring(0, conceptName.indexOf("/"));
167 }
168
169 if( mn8RuntimeFlags.isDebug() || mn8RuntimeFlags.isVerbose()) {
170 System.err.println("MN8 Servlet: Concept Lookup : " + conceptName );
171 }
172
173 try {
174 String fileName = (new mn8Loader()).findConcept(conceptName);
175
176 if( mn8RuntimeFlags.isDebug() || mn8RuntimeFlags.isVerbose()) {
177 System.err.println("MN8 Servlet: Concept Lookup found: " + fileName );
178 }
179 SeriesConcept param = new SeriesConcept();
180 param.add(paramVal);
181 mn8Interpreter.threadExecuteURLRun(fileName, null, param);
182 } catch ( ConceptNotFoundException cex) {
183 response.sendError(404, cex.getMessage());
184 cex.printStackTrace();
185 }
186 } else if( path.exists() && path.isFile() ) {
187 response.sendError(501);
188 } else if( path.exists() && path.isDirectory() ) {
189 response.sendError(403, "Directory listing forbidden on this server.");
190 } else {
191 response.sendError(404, "The requested URL was not found on this server.");
192 }
193 }
194 }
195 } catch (RuntimeException rex) {
196 response.sendError(500, rex.getMessage());
197 throw rex;
198 } finally {
199 PrintStream out = mn8Interpreter.getOut(Thread.currentThread());
200 PrintStream err = mn8Interpreter.getErr(Thread.currentThread());
201 if( out != System.out ) {
202 out.flush();
203 out.close();
204 }
205 if( err != System.err ) {
206 err.flush();
207 err.close();
208 }
209 mn8Interpreter.cleanStreams( Thread.currentThread());
210 }
211 }
212 }