import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.ibm.websphere.objectgrid.ClientClusterContext; import com.ibm.websphere.objectgrid.ObjectGrid; import com.ibm.websphere.objectgrid.ObjectGridException; import com.ibm.websphere.objectgrid.ObjectGridManagerFactory; import com.ibm.websphere.objectgrid.ObjectGridRuntimeException; import com.ibm.websphere.objectgrid.ObjectMap; import com.ibm.websphere.objectgrid.Session; import com.ibm.websphere.objectgrid.security.config.ClientSecurityConfiguration; import com.ibm.websphere.objectgrid.security.config.ClientSecurityConfigurationFactory; import com.ibm.websphere.objectgrid.security.plugins.CredentialGenerator; import com.ibm.websphere.objectgrid.security.plugins.builtins.UserPasswordCredentialGenerator; /** * Servlet implementation class TestMultiMaster */ public class XC10Servlet extends HttpServlet { private static final long serialVersionUID = 1L; static String username = "tefremenko"; // //"xcadmin"; static String password = "eval4webid"; //"xcadmin"; /** * @see HttpServlet#HttpServlet() */ public XC10Servlet() { super(); // TODO Auto-generated constructor stub } public ObjectGrid connectClient(String cep, String gridName) { try { // Connect to the Catalog Server. The security and client override // XML are not specified. ClientSecurityConfiguration clientSC = getAdminClientConfig(); ClientClusterContext ccc = ObjectGridManagerFactory .getObjectGridManager().connect(cep, clientSC, null); // Retrieve the ObjectGrid client connection and return it. ObjectGrid grid = ObjectGridManagerFactory.getObjectGridManager() .getObjectGrid(ccc, gridName); System.out.println("Connected"); return grid; } catch (Exception e) { throw new ObjectGridRuntimeException( "Unable to connect to catalog server at endpoints:" + cep, e); } } public static ClientSecurityConfiguration getAdminClientConfig() { // Creates a ClientSecurityConfiguration object using the specified file ClientSecurityConfiguration clientSC = ClientSecurityConfigurationFactory .getClientSecurityConfiguration(); clientSC.setSecurityEnabled(true); // Creates a CredentialGenerator using the passed-in user and password. CredentialGenerator credGen = new UserPasswordCredentialGenerator( username, password); clientSC.setCredentialGenerator(credGen); return clientSC; } public void showParameters(HttpServletRequest request, HttpServletResponse response, PrintWriter pout) { pout.println("
\n" + "\n" + "
Parameter NameParameter Value(s)"); Enumeration paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = (String) paramNames.nextElement(); pout.println("
" + paramName + "\n"); String[] paramValues = request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) pout.print("No Value"); else pout.print(paramValue); } else { pout.println("
    "); for (int i = 0; i < paramValues.length; i++) { pout.println("
  • " + paramValues[i]); } pout.println("
"); } } pout.println("
"); pout .println("









"); } public void printHTMLTop(PrintWriter pout) { pout.println(""); pout.println(""); pout.println(""); String title = ("Sessions Test"); pout.println("" + title + ""); pout.println(""); pout.println(""); } public void printHTMLBottom(PrintWriter pout) { pout.println(""); pout.println(""); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/html"); this.printHTMLTop(out); out.println("
"); this.showParameters(request, response, out); HttpSession httpSession = request.getSession(); String sessionID = httpSession.getId(); out.println("
"); out.println("

HTTP Session ID is " + sessionID + "

"); String catalogToUse = request.getParameter("catalog"); if (catalogToUse == null) { catalogToUse = "9.38.189.129"; } System.out.println("TJE: Catalog to use is " + catalogToUse); String portToUse = request.getParameter("port"); if (portToUse == null) { portToUse = "6000"; } String gridToUse = request.getParameter("grid"); if (gridToUse == null) { gridToUse = "test"; } String mapToUse = request.getParameter("mapname"); if (mapToUse == null) { mapToUse = "test"; } String getOrPut = request.getParameter("GetOrPut"); String sessionKey = request.getParameter("key"); String sessionValue = request.getParameter("data"); out.println("

Using Catalog >>> " + catalogToUse + ":"+ portToUse + "

grid Name >>> " +gridToUse + "

API >>> " + getOrPut + "

"); ObjectGrid grid = this.connectClient(catalogToUse + ":" + portToUse, gridToUse); // Get a Grid session Session gridSession = null; byte[] ba = new byte[64000]; if (getOrPut.equals("Get")) { try { gridSession = grid.getSession(); ObjectMap map = gridSession.getMap(gridToUse); gridSession.begin(); out.println("got a session with grid..." + gridSession.getObjectGrid().getName()); Object o = map.get(sessionKey); ByteArrayOutputStream bStream = new ByteArrayOutputStream(); ObjectOutputStream oStream = new ObjectOutputStream( bStream ); oStream.writeObject ( o ); byte[] byteVal = bStream. toByteArray(); // String result = (String)map.get("WzqoqrV_1GH1wXiReKd8pJO"); System.out.println("Got results of " + byteVal[0] + " of size " + byteVal.length); if (o != null) { out.println("

Got these results back >>>>>> " + byteVal[0] + " with size of " + byteVal.length + "

"); } gridSession.commit(); } catch (Exception e) { e.printStackTrace(); } } else { // API is "put" boolean error = false; try { gridSession = grid.getSession(); ObjectMap map = gridSession.getMap(mapToUse); gridSession.begin(); out.println("got a session..."); for (int j = 0;j < 64000; j++) { ba[j] = '1'; } map.put(sessionKey, ba); gridSession.commit(); } catch (Exception e) { error = true; if (e instanceof ObjectGridException) { ObjectGridException e1 = (ObjectGridException) e; out.println("Got an exception..." + e1.getCause()); } else { e.printStackTrace(); } } if (!error) { out.println("

Just persisted in the grid this key... > " + sessionKey + " and data...> " + ba[0] + " for length of " + ba.length + "

"); } } out.println("
"); this.printHTMLBottom(out); out.flush(); out.close(); } } ut.flush(); out.close();