PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.hazelcast.impl.management;
 18
 19import com.hazelcast.core.IMap;
 20import com.hazelcast.core.MapEntry;
 21
 22import java.io.DataInput;
 23import java.io.DataOutput;
 24import java.io.IOException;
 25import java.util.LinkedHashMap;
 26import java.util.Map;
 27import java.util.TreeMap;
 28
 29// author: sancar - 12.12.2012
 30public class GetMapEntryRequest implements ConsoleRequest {
 31
 32    private String mapName;
 33    private String type;
 34    private String key;
 35
 36    public GetMapEntryRequest(){
 37        super();
 38    }
 39
 40    public GetMapEntryRequest(String type, String mapName, String key){
 41        this.type = type;
 42        this.mapName = mapName;
 43        this.key = key;
 44    }
 45
 46    public int getType() {
 47        return ConsoleRequestConstants.REQUEST_TYPE_MAP_ENTRY;
 48    }
 49
 50    public Object readResponse(DataInput in) throws IOException {
 51        Map<String,String> properties = new LinkedHashMap<String, String>();
 52        int size = in.readInt();
 53        String[] temp;
 54        for(int i = 0; i < size ; i++){
 55            temp = in.readUTF().split(":#");
 56            properties.put(temp[0],temp.length == 1 ? "" : temp[1] );
 57        }
 58        return properties;
 59    }
 60
 61    public void writeResponse(ManagementCenterService mcs, DataOutput dos) throws Exception {
 62        IMap map = mcs.getHazelcastInstance().getMap(mapName);
 63
 64        MapEntry entry = null;
 65        if(type.equals("string")) {
 66            entry = map.getMapEntry(key);
 67        }else if(type.equals("long")) {
 68            entry = map.getMapEntry(Long.valueOf(key));
 69        }else if(type.equals("integer")) {
 70            entry = map.getMapEntry(Integer.valueOf(key));
 71        }
 72
 73        TreeMap result = new TreeMap();
 74
 75        if(entry == null ){
 76            result.put("No Value Found!", " ");
 77        }
 78        Object value = entry.getValue();
 79
 80        result.put("browse_value", value != null ? value.toString() : "null");
 81        result.put("browse_class", value != null ? value.getClass().getName() : "null");
 82
 83        result.put("memory_cost",  Long.toString(entry.getCost()));
 84        result.put("date_creation_time",  Long.toString(entry.getCreationTime()));
 85        result.put("date_expiration_time",  Long.toString(entry.getExpirationTime()));
 86        result.put("browse_hits", Integer.toString(entry.getHits()));
 87        result.put("date_access_time",  Long.toString(entry.getLastAccessTime()));
 88        result.put("date_update_time",  Long.toString(entry.getLastUpdateTime( )));
 89        result.put("browse_version", Long.toString(entry.getVersion()));
 90        result.put("boolean_valid", Boolean.toString(entry.isValid()));
 91
 92        dos.writeInt(result.size());
 93
 94        for (Object property : result.keySet()) {
 95            dos.writeUTF((String)property + ":#" + (String)result.get(property) );
 96        }
 97
 98    }
 99
100    public void writeData(DataOutput out) throws IOException {
101        out.writeUTF(type);
102        out.writeUTF(mapName);
103        out.writeUTF(key);
104    }
105
106    public void readData(DataInput in) throws IOException {
107        type = in.readUTF();
108        mapName = in.readUTF();
109        key = in.readUTF();
110    }
111}