001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JBIServlet.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework.websphere;
030:
031: import java.io.IOException;
032: import java.io.PrintWriter;
033: import java.util.Enumeration;
034: import java.util.Properties;
035: import javax.servlet.ServletException;
036: import javax.servlet.http.HttpServlet;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import com.sun.jbi.framework.websphere.WebSphereJBIFramework;
041: import com.sun.jbi.framework.websphere.WebSphereJBIBootstrap;
042:
043: /**
044: * JBIServlet handles the lifecycle of JBI inside WebSphere
045: *
046: * @author Sun Microsystems, Inc.
047: */
048: public class JBIServlet extends HttpServlet {
049: /**
050: * an instance of WebSphereJBIBootstrap which only holds the
051: * JBI system jars jbi.jar, jbi-ext.jar
052: */
053: private static WebSphereJBIBootstrap jbiBootstrap;
054:
055: /**
056: * Constructor
057: */
058: public JBIServlet() {
059: }
060:
061: /**
062: * destroy method stops the JBI framework
063: */
064: public void destroy() {
065: super .destroy();
066: try {
067: jbiBootstrap.stop(getJBIProperties());
068: } catch (Exception ex) {
069: ex.printStackTrace();
070: }
071: }
072:
073: /**
074: * init method is used to init JBIFramework
075: * @throws javax.servlet.ServletException a ServletException is thrown if JBIFramework did not init successfully
076: */
077: public void init() throws ServletException {
078: super .init();
079: initFramework();
080: }
081:
082: /**
083: * the processRequest method that handles both get and post requests
084: * the response is a simple page that confirms that JBIServlet is running
085: * @param request the request
086: * @param response the response
087: * @throws ServletException ServletException is thrown
088: * @throws IOException IOException is thrown
089: */
090: protected void processRequest(HttpServletRequest request,
091: HttpServletResponse response) throws ServletException,
092: IOException {
093: response.setContentType("text/html;charset=UTF-8");
094: PrintWriter out = response.getWriter();
095: out.println("<html>");
096: out.println("<head>");
097: out.println("<title>Servlet JBIServlet</title>");
098: out.println("</head>");
099: out.println("<body>");
100: out.println((new StringBuilder()).append(
101: "<h1>Servlet JBIServlet at ").append(
102: request.getContextPath()).append("</h1>").toString());
103: out.println("</body>");
104: out.println("</html>");
105: out.close();
106: }
107:
108: /**
109: * This method is used to get required properties from the servlet
110: * context and pass it to the framework
111: * @return Properties the properties from servlet context
112: */
113: private Properties getJBIProperties() {
114: Properties props = new Properties();
115: Enumeration propNames = getInitParameterNames();
116: props.put("install.root", getServletConfig()
117: .getServletContext().getRealPath("/"));
118: String name;
119: for (; propNames.hasMoreElements(); props.put(name,
120: getInitParameter(name)))
121: name = (String) propNames.nextElement();
122:
123: return props;
124: }
125:
126: /**
127: * This method initializes the JBI Bootstrap
128: */
129: private synchronized void initFramework() {
130: try {
131: if (jbiBootstrap == null)
132: jbiBootstrap = new WebSphereJBIBootstrap(
133: getJBIProperties());
134: if (!jbiBootstrap.isStarted())
135: jbiBootstrap.start(getJBIProperties());
136: } catch (Exception ex) {
137: ex.printStackTrace();
138: }
139: }
140:
141: /**
142: * doGet method handles the get requests
143: * the response is a simple page that confirms that JBIServlet is running
144: * @param request the request
145: * @param response the response
146: * @throws ServletException ServletException is thrown
147: * @throws IOException IOException is thrown
148: */
149: protected void doGet(HttpServletRequest request,
150: HttpServletResponse response) throws ServletException,
151: IOException {
152: processRequest(request, response);
153: }
154:
155: /**
156: * doPost method handles the post requests
157: * the response is a simple page that confirms that JBIServlet is running
158: * @param request the request
159: * @param response the response
160: * @throws ServletException ServletException is thrown
161: * @throws IOException IOException is thrown
162: */
163: protected void doPost(HttpServletRequest request,
164: HttpServletResponse response) throws ServletException,
165: IOException {
166: processRequest(request, response);
167: }
168:
169: /**
170: * This method returns a short description
171: * @return String the description
172: */
173: public String getServletInfo() {
174: return "Short description";
175: }
176:
177: }
|