/hazelcast/src/main/java/com/hazelcast/impl/management/GetMapEntryRequest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 111 lines · 75 code · 20 blank · 16 comment · 10 complexity · 52e5b512be2efc4a8a42f609ee34bbda MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl.management;
  17. import com.hazelcast.core.IMap;
  18. import com.hazelcast.core.MapEntry;
  19. import java.io.DataInput;
  20. import java.io.DataOutput;
  21. import java.io.IOException;
  22. import java.util.LinkedHashMap;
  23. import java.util.Map;
  24. import java.util.TreeMap;
  25. // author: sancar - 12.12.2012
  26. public class GetMapEntryRequest implements ConsoleRequest {
  27. private String mapName;
  28. private String type;
  29. private String key;
  30. public GetMapEntryRequest(){
  31. super();
  32. }
  33. public GetMapEntryRequest(String type, String mapName, String key){
  34. this.type = type;
  35. this.mapName = mapName;
  36. this.key = key;
  37. }
  38. public int getType() {
  39. return ConsoleRequestConstants.REQUEST_TYPE_MAP_ENTRY;
  40. }
  41. public Object readResponse(DataInput in) throws IOException {
  42. Map<String,String> properties = new LinkedHashMap<String, String>();
  43. int size = in.readInt();
  44. String[] temp;
  45. for(int i = 0; i < size ; i++){
  46. temp = in.readUTF().split(":#");
  47. properties.put(temp[0],temp.length == 1 ? "" : temp[1] );
  48. }
  49. return properties;
  50. }
  51. public void writeResponse(ManagementCenterService mcs, DataOutput dos) throws Exception {
  52. IMap map = mcs.getHazelcastInstance().getMap(mapName);
  53. MapEntry entry = null;
  54. if(type.equals("string")) {
  55. entry = map.getMapEntry(key);
  56. }else if(type.equals("long")) {
  57. entry = map.getMapEntry(Long.valueOf(key));
  58. }else if(type.equals("integer")) {
  59. entry = map.getMapEntry(Integer.valueOf(key));
  60. }
  61. TreeMap result = new TreeMap();
  62. if(entry == null ){
  63. result.put("No Value Found!", " ");
  64. }
  65. Object value = entry.getValue();
  66. result.put("browse_value", value != null ? value.toString() : "null");
  67. result.put("browse_class", value != null ? value.getClass().getName() : "null");
  68. result.put("memory_cost", Long.toString(entry.getCost()));
  69. result.put("date_creation_time", Long.toString(entry.getCreationTime()));
  70. result.put("date_expiration_time", Long.toString(entry.getExpirationTime()));
  71. result.put("browse_hits", Integer.toString(entry.getHits()));
  72. result.put("date_access_time", Long.toString(entry.getLastAccessTime()));
  73. result.put("date_update_time", Long.toString(entry.getLastUpdateTime( )));
  74. result.put("browse_version", Long.toString(entry.getVersion()));
  75. result.put("boolean_valid", Boolean.toString(entry.isValid()));
  76. dos.writeInt(result.size());
  77. for (Object property : result.keySet()) {
  78. dos.writeUTF((String)property + ":#" + (String)result.get(property) );
  79. }
  80. }
  81. public void writeData(DataOutput out) throws IOException {
  82. out.writeUTF(type);
  83. out.writeUTF(mapName);
  84. out.writeUTF(key);
  85. }
  86. public void readData(DataInput in) throws IOException {
  87. type = in.readUTF();
  88. mapName = in.readUTF();
  89. key = in.readUTF();
  90. }
  91. }