001: package com.sun.portal.sra.desktop;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.io.PrintWriter;
006: import java.util.HashMap;
007: import java.util.Iterator;
008:
009: import javax.servlet.RequestDispatcher;
010: import javax.servlet.ServletConfig;
011: import javax.servlet.ServletException;
012: import javax.servlet.http.HttpServlet;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015: import javax.xml.parsers.ParserConfigurationException;
016:
017: import org.xml.sax.SAXException;
018:
019: import com.sun.portal.sra.config.CommandList;
020: import com.sun.portal.sra.config.SAX2Reader;
021: import com.sun.portal.sra.config.SraDesktop;
022: import com.sun.portal.sra.desktop.commands.Command;
023: import com.sun.portal.sra.desktop.commands.CommandExecutionException;
024:
025: public class SRADesktop extends HttpServlet {
026: private static final String CONTENT_TYPE = "text/html";
027: private static final String FIRST_PAGE = "/index.jsp";
028: private static ServletConfig _config;
029: private static HashMap _commandMap;
030: private static HashMap _nextViewMap;
031: private static HashMap _providerMap;
032: private static SAX2Reader _sax;
033:
034: /**
035: * @web.servlet display-name="SRA Desktop Servlet" load-on-startup="1"
036: * name="SRADesktop" @web.servlet-init-param name="CONFIG_FILE"
037: * value="config.xml" @web.servlet-init-param name="NETLET_PROVIDER_CLASS"
038: * value="SOME VALUE HERE" To change the template for this generated type
039: * comment go to Window - Preferences - Java - Code Generation - Code and
040: * Comments
041: */
042:
043: public void init(ServletConfig config) throws ServletException {
044: super .init(config);
045: _config = config;
046:
047: try {
048: _sax = new SAX2Reader();
049: _commandMap = new HashMap();
050: _nextViewMap = new HashMap();
051: _providerMap = new HashMap();
052:
053: String configFile = _config.getServletContext()
054: .getRealPath(
055: _config.getInitParameter("CONFIG_FILE"));
056:
057: getAllProviders();
058:
059: SraDesktop sdesk = (SraDesktop) _sax.parse(new File(
060: configFile));
061: CommandList command_list = sdesk.getCommandList();
062: Iterator iter = command_list.iterator();
063:
064: while (iter.hasNext()) {
065: com.sun.portal.sra.config.Command command = (com.sun.portal.sra.config.Command) iter
066: .next();
067: _commandMap.put(command.getName().getPCData(), command
068: .getCommandclass().getPCData());
069: _nextViewMap.put(command.getName().getPCData(), command
070: .getView().getPCData());
071: }
072:
073: } catch (Exception e) {
074: throw new ServletException(e.toString());
075: }
076:
077: }
078:
079: /**
080: *
081: */
082: private void getAllProviders() {
083: _providerMap.put("SRA_PROXYLET_PROVIDER", _config
084: .getServletContext().getInitParameter(
085: "SRA_PROXYLET_PROVIDER"));
086: _providerMap.put("SRA_SEPARATION_PROVIDER", _config
087: .getServletContext().getInitParameter(
088: "SRA_SEPARATION_PROVIDER"));
089:
090: }
091:
092: public void doGet(HttpServletRequest request,
093: HttpServletResponse response) throws ServletException,
094: IOException {
095: try {
096: doGetPost(request, response);
097: } catch (CommandExecutionException e) {
098: //e.printStackTrace();
099: System.out.println(e.returnStackTrace().toString());
100:
101: }
102: }
103:
104: public void doPost(HttpServletRequest request,
105: HttpServletResponse response) throws ServletException,
106: IOException {
107: try {
108: doGetPost(request, response);
109: } catch (CommandExecutionException e) {
110: System.out.println(e.returnStackTrace().toString());
111: }
112: }
113:
114: public void doPut(HttpServletRequest request,
115: HttpServletResponse response) throws ServletException,
116: IOException {
117: }
118:
119: public void doDelete(HttpServletRequest request,
120: HttpServletResponse response) throws ServletException,
121: IOException {
122: }
123:
124: private Command getCommand(String command)
125: throws ClassNotFoundException, IllegalAccessException,
126: InstantiationException {
127: String nextCommand = (String) _commandMap.get(command);
128:
129: return (Command) (Class.forName(nextCommand).newInstance());
130: }
131:
132: private String getNextView(String command) {
133: String nextView = (String) _nextViewMap.get(command);
134: if (nextView == null)
135: nextView = FIRST_PAGE;
136:
137: return nextView;
138: }
139:
140: public void doGetPost(HttpServletRequest request,
141: HttpServletResponse response) throws ServletException,
142: IOException, CommandExecutionException {
143: PrintWriter out = response.getWriter();
144: out
145: .println("<html><body>The value for the provider is "
146: + (String) _providerMap.get("NETLET_PROVIDER")
147: + "<br>");
148:
149: String comm = request.getParameter("action") == null ? "getDefault"
150: : request.getParameter("action");
151:
152: try {
153: //Get the new command and next view which will generate the HTML
154: // fragment
155: Command command = getCommand(comm);
156: String nextView = getNextView(comm);
157:
158: request.setAttribute("DSAME_LOGOUT_URL", _config
159: .getServletContext().getInitParameter(
160: "DSAME_LOGOUT_URL"));
161: request.setAttribute("staticContext", _config
162: .getServletContext().getInitParameter(
163: "staticContext"));
164:
165: executeRequestedCommand(request, response, command);
166:
167: // Forward processing to the next view.
168: out.println("Next View is " + nextView + "<br>");
169: RequestDispatcher dispatcher = _config.getServletContext()
170: .getRequestDispatcher(nextView);
171:
172: if (dispatcher != null)
173: dispatcher.forward(request, response);
174: else
175: out.println("No Dispatcher Found for next view "
176: + nextView);
177: } catch (Exception e) {
178: e.printStackTrace();
179: throw new CommandExecutionException(e);
180: }
181:
182: out.println("</body></html>");
183: }
184:
185: private void executeRequestedCommand(HttpServletRequest request,
186: HttpServletResponse response, Command command)
187: throws IOException, CommandExecutionException {
188: // Execute the command associated and get the outout HTML and put it in
189: // the request session.
190: command.execute(request, response, _providerMap);
191:
192: }
193:
194: public void destroy() {
195: _commandMap.clear();
196: _nextViewMap.clear();
197: _providerMap.clear();
198: }
199:
200: public static void main(String[] args)
201: throws ParserConfigurationException, SAXException,
202: IOException {
203: String configFile = "D:\\SunProjects\\SRAWeb\\Web Root\\WEB-INF\\config.xml";
204:
205: SAX2Reader _sax = new SAX2Reader();
206: SraDesktop sdesk = (SraDesktop) _sax
207: .parse(new File(configFile));
208: CommandList command_list = sdesk.getCommandList();
209: Iterator iter = command_list.iterator();
210:
211: while (iter.hasNext()) {
212: com.sun.portal.sra.config.Command command = (com.sun.portal.sra.config.Command) iter
213: .next();
214: System.out.println("|" + command.getName().getPCData()
215: + "|:::|" + command.getCommandclass().getPCData()
216: + "|");
217: System.out.println("|" + command.getName().getPCData()
218: + "|:::|" + command.getView().getPCData() + "|");
219: }
220: }
221:
222: }
|