|
1 |
| -# Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved. |
2 |
| -# Licensed under the Universal Permissive License v 1.0 as shown at |
3 |
| -# http://oss.oracle.com/licenses/upl. |
| 1 | +// Copyright 2019, Oracle Corporation and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at |
| 3 | +// http://oss.oracle.com/licenses/upl. |
4 | 4 |
|
5 | 5 | package com.examples.web;
|
6 | 6 |
|
7 | 7 | import java.io.IOException;
|
8 | 8 | import java.io.PrintWriter;
|
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.Set; |
9 | 12 | import javax.servlet.ServletException;
|
10 | 13 | import javax.servlet.annotation.WebServlet;
|
11 | 14 | import javax.servlet.http.HttpServlet;
|
12 | 15 | import javax.servlet.http.HttpServletRequest;
|
13 | 16 | import javax.servlet.http.HttpServletResponse;
|
| 17 | + |
14 | 18 | import com.examples.pof.Contact;
|
15 | 19 | import com.examples.pof.ContactId;
|
16 | 20 | import com.tangosol.net.CacheFactory;
|
17 | 21 | import com.tangosol.net.NamedCache;
|
18 |
| -import java.util.ArrayList; |
19 |
| -import java.util.Iterator; |
20 |
| -import java.util.Map; |
21 |
| -import java.util.Set; |
22 | 22 |
|
23 | 23 | /**
|
24 |
| - * |
25 |
| - * This web application is used for following with respect to Coherence cache depending on user action. |
26 |
| - * 1. Populating the cahce using "add" , first name and second name used for populating the data. |
27 |
| - * 2. To get the cache size using "count" |
28 |
| - * 3. Clear the cache using "clear" |
29 |
| - * 4. Get all cached data details using "get" |
30 |
| - * |
| 24 | + * This web application is used for following with respect to Coherence cache depending on user |
| 25 | + * action. 1. Populating the cahce using "add" , first name and second name used for populating the |
| 26 | + * data. 2. To get the cache size using "count" 3. Clear the cache using "clear" 4. Get all cached |
| 27 | + * data details using "get" |
31 | 28 | */
|
32 |
| -@WebServlet(name = "CoherenceApp", urlPatterns = { "/CoherenceApp" }) |
| 29 | +@WebServlet( |
| 30 | + name = "CoherenceApp", |
| 31 | + urlPatterns = {"/CoherenceApp"}) |
33 | 32 | public class CoherenceApp extends HttpServlet {
|
34 | 33 |
|
35 |
| - private ArrayList keyList = new ArrayList<ContactId>(); |
36 |
| - |
37 |
| - public CoherenceApp() { |
38 |
| - super(); |
39 |
| - } |
40 |
| - |
41 |
| - /** |
42 |
| - * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
43 |
| - * methods. |
44 |
| - * |
45 |
| - * @param request |
46 |
| - * servlet request |
47 |
| - * @param response |
48 |
| - * servlet response |
49 |
| - * @throws ServletException |
50 |
| - * if a servlet-specific error occurs |
51 |
| - * @throws IOException |
52 |
| - * if an I/O error occurs |
53 |
| - */ |
54 |
| - protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
55 |
| - throws ServletException, IOException { |
56 |
| - response.setContentType("text/html;charset=UTF-8"); |
57 |
| - PrintWriter out = response.getWriter(); |
58 |
| - |
59 |
| - String action = request.getParameter("action"); |
60 |
| - String firstName = request.getParameter("first"); |
61 |
| - String secondName = request.getParameter("second"); |
62 |
| - |
63 |
| - // Call respective methods based on user action |
64 |
| - if (action.equals("add")) { |
65 |
| - add(request, response); |
66 |
| - } else if (action.equals("size")) { |
67 |
| - count(request, response); |
68 |
| - } else if (action.endsWith("clear")) { |
69 |
| - clear(request, response); |
70 |
| - } else if (action.endsWith("get")) { |
71 |
| - get(request, response); |
72 |
| - } |
73 |
| - |
74 |
| - } |
75 |
| - |
76 |
| - /* |
77 |
| - * Clea the cache and updates the size after cache |
78 |
| - */ |
79 |
| - private void clear(HttpServletRequest request, HttpServletResponse response) throws IOException { |
80 |
| - NamedCache cache = CacheFactory.getCache("contacts"); |
81 |
| - cache.clear(); |
82 |
| - PrintWriter out = response.getWriter(); |
83 |
| - if (cache.size() == 0) { |
84 |
| - out.println("Cache is cleared and current size is :" + cache.size()); |
85 |
| - } else { |
86 |
| - out.println("Cache is not cleared and current size is :" + cache.size()); |
87 |
| - } |
88 |
| - |
89 |
| - } |
90 |
| - |
91 |
| - /* |
92 |
| - * Returns the current cache size |
93 |
| - */ |
94 |
| - private void count(HttpServletRequest request, HttpServletResponse response) throws IOException { |
95 |
| - NamedCache cache = CacheFactory.getCache("contacts"); |
96 |
| - PrintWriter out = response.getWriter(); |
97 |
| - out.println(cache.size()); |
98 |
| - } |
99 |
| - |
100 |
| - /* |
101 |
| - * Add first name and second name as cache data |
102 |
| - */ |
103 |
| - private void add(HttpServletRequest request, HttpServletResponse response) throws IOException { |
104 |
| - NamedCache cache = CacheFactory.getCache("contacts"); |
105 |
| - String firstName = request.getParameter("first"); |
106 |
| - String secondName = request.getParameter("second"); |
107 |
| - PrintWriter out = response.getWriter(); |
108 |
| - Contact contact = new Contact(firstName, secondName); |
109 |
| - |
110 |
| - ContactId contactID = new ContactId(firstName, secondName); |
111 |
| - keyList.add(contactID); |
112 |
| - cache.put(contactID, contact); |
113 |
| - Contact contactGet = (Contact) cache.get(contactID); |
114 |
| - out.println("\nContact added:" + contactGet); |
115 |
| - } |
116 |
| - |
117 |
| - /* |
118 |
| - * Retrieve all cached data as of now |
119 |
| - */ |
120 |
| - private void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
121 |
| - NamedCache cacheMap = CacheFactory.getCache("contacts"); |
122 |
| - Set keys = cacheMap.keySet(); |
123 |
| - PrintWriter out = response.getWriter(); |
124 |
| - Iterator iterate = keys.iterator(); |
125 |
| - while (iterate.hasNext()) { |
126 |
| - ContactId key = (ContactId) iterate.next(); |
127 |
| - Contact contactGet = (Contact) cacheMap.get(key); |
128 |
| - out.println(contactGet); |
129 |
| - } |
130 |
| - } |
131 |
| - |
132 |
| - // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on |
133 |
| - // the + sign on the left to edit the code."> |
134 |
| - /** |
135 |
| - * Handles the HTTP <code>GET</code> method. |
136 |
| - * |
137 |
| - * @param request |
138 |
| - * servlet request |
139 |
| - * @param response |
140 |
| - * servlet response |
141 |
| - * @throws ServletException |
142 |
| - * if a servlet-specific error occurs |
143 |
| - * @throws IOException |
144 |
| - * if an I/O error occurs |
145 |
| - */ |
146 |
| - @Override |
147 |
| - protected void doGet(HttpServletRequest request, HttpServletResponse response) |
148 |
| - throws ServletException, IOException { |
149 |
| - processRequest(request, response); |
150 |
| - } |
151 |
| - |
152 |
| - /** |
153 |
| - * Handles the HTTP <code>POST</code> method. |
154 |
| - * |
155 |
| - * @param request |
156 |
| - * servlet request |
157 |
| - * @param response |
158 |
| - * servlet response |
159 |
| - * @throws ServletException |
160 |
| - * if a servlet-specific error occurs |
161 |
| - * @throws IOException |
162 |
| - * if an I/O error occurs |
163 |
| - */ |
164 |
| - @Override |
165 |
| - protected void doPost(HttpServletRequest request, HttpServletResponse response) |
166 |
| - throws ServletException, IOException { |
167 |
| - processRequest(request, response); |
168 |
| - } |
169 |
| - |
170 |
| - /** |
171 |
| - * Returns a short description of the servlet. |
172 |
| - * |
173 |
| - * @return a String containing servlet description |
174 |
| - */ |
175 |
| - @Override |
176 |
| - public String getServletInfo() { |
177 |
| - return "Short description"; |
178 |
| - }// </editor-fold> |
| 34 | + private ArrayList keyList = new ArrayList<ContactId>(); |
| 35 | + |
| 36 | + public CoherenceApp() { |
| 37 | + super(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. |
| 42 | + * |
| 43 | + * @param request servlet request |
| 44 | + * @param response servlet response |
| 45 | + * @throws ServletException if a servlet-specific error occurs |
| 46 | + * @throws IOException if an I/O error occurs |
| 47 | + */ |
| 48 | + protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
| 49 | + throws ServletException, IOException { |
| 50 | + response.setContentType("text/html;charset=UTF-8"); |
| 51 | + PrintWriter out = response.getWriter(); |
| 52 | + |
| 53 | + String action = request.getParameter("action"); |
| 54 | + String firstName = request.getParameter("first"); |
| 55 | + String secondName = request.getParameter("second"); |
| 56 | + |
| 57 | + // Call respective methods based on user action |
| 58 | + if (action.equals("add")) { |
| 59 | + add(request, response); |
| 60 | + } else if (action.equals("size")) { |
| 61 | + count(request, response); |
| 62 | + } else if (action.endsWith("clear")) { |
| 63 | + clear(request, response); |
| 64 | + } else if (action.endsWith("get")) { |
| 65 | + get(request, response); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + * Clea the cache and updates the size after cache |
| 71 | + */ |
| 72 | + private void clear(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 73 | + NamedCache cache = CacheFactory.getCache("contacts"); |
| 74 | + cache.clear(); |
| 75 | + PrintWriter out = response.getWriter(); |
| 76 | + if (cache.size() == 0) { |
| 77 | + out.println("Cache is cleared and current size is :" + cache.size()); |
| 78 | + } else { |
| 79 | + out.println("Cache is not cleared and current size is :" + cache.size()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + * Returns the current cache size |
| 85 | + */ |
| 86 | + private void count(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 87 | + NamedCache cache = CacheFactory.getCache("contacts"); |
| 88 | + PrintWriter out = response.getWriter(); |
| 89 | + out.println(cache.size()); |
| 90 | + } |
| 91 | + |
| 92 | + /* |
| 93 | + * Add first name and second name as cache data |
| 94 | + */ |
| 95 | + private void add(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 96 | + NamedCache cache = CacheFactory.getCache("contacts"); |
| 97 | + String firstName = request.getParameter("first"); |
| 98 | + String secondName = request.getParameter("second"); |
| 99 | + PrintWriter out = response.getWriter(); |
| 100 | + Contact contact = new Contact(firstName, secondName); |
| 101 | + |
| 102 | + ContactId contactID = new ContactId(firstName, secondName); |
| 103 | + keyList.add(contactID); |
| 104 | + cache.put(contactID, contact); |
| 105 | + Contact contactGet = (Contact) cache.get(contactID); |
| 106 | + out.println("\nContact added:" + contactGet); |
| 107 | + } |
| 108 | + |
| 109 | + /* |
| 110 | + * Retrieve all cached data as of now |
| 111 | + */ |
| 112 | + private void get(HttpServletRequest request, HttpServletResponse response) |
| 113 | + throws ServletException, IOException { |
| 114 | + NamedCache cacheMap = CacheFactory.getCache("contacts"); |
| 115 | + Set keys = cacheMap.keySet(); |
| 116 | + PrintWriter out = response.getWriter(); |
| 117 | + Iterator iterate = keys.iterator(); |
| 118 | + while (iterate.hasNext()) { |
| 119 | + ContactId key = (ContactId) iterate.next(); |
| 120 | + Contact contactGet = (Contact) cacheMap.get(key); |
| 121 | + out.println(contactGet); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on |
| 126 | + // the + sign on the left to edit the code."> |
| 127 | + /** |
| 128 | + * Handles the HTTP <code>GET</code> method. |
| 129 | + * |
| 130 | + * @param request servlet request |
| 131 | + * @param response servlet response |
| 132 | + * @throws ServletException if a servlet-specific error occurs |
| 133 | + * @throws IOException if an I/O error occurs |
| 134 | + */ |
| 135 | + @Override |
| 136 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) |
| 137 | + throws ServletException, IOException { |
| 138 | + processRequest(request, response); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Handles the HTTP <code>POST</code> method. |
| 143 | + * |
| 144 | + * @param request servlet request |
| 145 | + * @param response servlet response |
| 146 | + * @throws ServletException if a servlet-specific error occurs |
| 147 | + * @throws IOException if an I/O error occurs |
| 148 | + */ |
| 149 | + @Override |
| 150 | + protected void doPost(HttpServletRequest request, HttpServletResponse response) |
| 151 | + throws ServletException, IOException { |
| 152 | + processRequest(request, response); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Returns a short description of the servlet. |
| 157 | + * |
| 158 | + * @return a String containing servlet description |
| 159 | + */ |
| 160 | + @Override |
| 161 | + public String getServletInfo() { |
| 162 | + return "Short description"; |
| 163 | + } // </editor-fold> |
179 | 164 | }
|
0 commit comments