You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by tu...@apache.org on 2015/11/03 12:26:21 UTC
[59/79] incubator-geode git commit: GEODE-287: Remove old gfsh code
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/67085172/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/pogo/MapLiteSerializer.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/pogo/MapLiteSerializer.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/pogo/MapLiteSerializer.java
deleted file mode 100644
index fe9b09a..0000000
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/pogo/MapLiteSerializer.java
+++ /dev/null
@@ -1,338 +0,0 @@
-package com.gemstone.gemfire.internal.tools.gfsh.app.pogo;
-
-import java.io.DataInput;
-import java.io.DataOutput;
-import java.io.IOException;
-import java.util.Date;
-import java.util.zip.DataFormatException;
-import java.util.zip.Deflater;
-import java.util.zip.Inflater;
-
-import com.gemstone.gemfire.DataSerializer;
-import com.gemstone.gemfire.internal.DSCODE;
-import com.gemstone.gemfire.internal.InternalDataSerializer;
-
-/**
- * MapLiteSerializer serializes MapLite specifics. It is for internal use only.
- * @author dpark
- *
- */
-abstract class MapLiteSerializer extends DataSerializer
-{
- public static void write(Class cls, Object object, DataOutput out) throws IOException
- {
- if (cls == String.class) {
- writeUTF((String) object, out);
- } else if (cls == boolean.class || cls == Boolean.class) {
- writeBoolean((Boolean) object, out);
- } else if (cls == byte.class || cls == Byte.class) {
- writeByte((Byte) object, out);
- } else if (cls == char.class || cls == Character.class) {
- writeCharacter((Character) object, out);
- } else if (cls == double.class || cls == Double.class) {
- writeDouble((Double) object, out);
- } else if (cls == float.class || cls == Float.class) {
- writeFloat((Float) object, out);
- } else if (cls == int.class || cls == Integer.class) {
- writeInteger((Integer) object, out);
- } else if (cls == long.class || cls == Long.class) {
- writeLong((Long) object, out);
- } else if (cls == short.class || cls == Short.class) {
- writeShort((Short) object, out);
- } else if (cls == MapLite.class) {
- writeMapLite((MapLite) object, out);
- } else if (cls == Date.class) {
- writeObject((Date) object, out);
- } else if (cls == boolean[].class) {
- writeBooleanArray((boolean[]) object, out);
- } else if (cls == byte[].class) {
- writeByteArray((byte[]) object, out);
- } else if (cls == char[].class) {
- writeCharArray((char[]) object, out);
- } else if (cls == double[].class) {
- writeDoubleArray((double[]) object, out);
- } else if (cls == float[].class) {
- writeFloatArray((float[]) object, out);
- } else if (cls == int[].class) {
- writeIntArray((int[]) object, out);
- } else if (cls == long[].class) {
- writeLongArray((long[]) object, out);
- } else if (cls == short[].class) {
- writeShortArray((short[]) object, out);
- } else {
- writeObject(object, out);
- }
- }
-
- public static Object read(Class cls, DataInput in) throws IOException, ClassNotFoundException
- {
- Object value;
- if (cls == String.class) {
- value = readUTF(in);
- } else if (cls == boolean.class || cls == Boolean.class) {
- value = readBoolean(in);
- } else if (cls == byte.class || cls == Byte.class) {
- value = readByte(in);
- } else if (cls == char.class || cls == Character.class) {
- value = readCharacter(in);
- } else if (cls == double.class || cls == Double.class) {
- value = readDouble(in);
- } else if (cls == float.class || cls == Float.class) {
- value = readFloat(in);
- } else if (cls == int.class || cls == Integer.class) {
- value = readInteger(in);
- } else if (cls == long.class || cls == Long.class) {
- value = readLong(in);
- } else if (cls == short.class || cls == Short.class) {
- value = readShort(in);
- } else if (cls == MapLite.class) {
- value = readMapLite(in);
- } else if (cls == Date.class) {
- value = readObject(in);
- } else if (cls == boolean[].class) {
- value = DataSerializer.readBooleanArray(in);
- } else if (cls == byte[].class) {
- value = DataSerializer.readByteArray(in);
- } else if (cls == char[].class) {
- value = DataSerializer.readCharacter(in);
- } else if (cls == double[].class) {
- value = DataSerializer.readByteArray(in);
- } else if (cls == float[].class) {
- value = DataSerializer.readByteArray(in);
- } else if (cls == int[].class) {
- value = DataSerializer.readByteArray(in);
- } else if (cls == long[].class) {
- value = DataSerializer.readByteArray(in);
- } else if (cls == short[].class) {
- value = DataSerializer.readByteArray(in);
- } else {
- value = DataSerializer.readObject(in);
- }
- return value;
- }
-
- /**
- * Writes the specified byte array to the output stream. This method is
- * not thread safe.
- *
- * @param array The byte array to compress
- * @param buffer The byte array buffer used as input to the deflater. This
- * buffer must have enough space to hold the compressed data.
- * @param compressor java.util.Deflater.
- * @param output The data output stream.
- * @throws IOException Thrown if unable to write to the output stream.
- */
- public static void writeByteArray(byte array[], byte buffer[], Deflater compressor, DataOutput output) throws IOException
- {
- // Compress the bytes
- compressor.setInput(array);
- compressor.finish();
- int compressedDataLength = compressor.deflate(buffer);
- DataSerializer.writeByteArray(buffer, compressedDataLength, output);
- }
-
- /**
- * Reads byte array from the input stream. This method is not thread safe.
- *
- * @param buffer The buffer to hold the decompressed data. This buffer
- * must be large enough to hold the decompressed data.
- * @param decompressor java.util.Inflater
- * @param input The data input stream.
- * @return Returns the actual byte array (not compressed) read from the
- * input stream.
- * @throws IOException Thrown if unable to read from the input stream or
- * unable to decompress the data.
- */
- public static byte[] readByteArray(byte buffer[], Inflater decompressor, DataInput input) throws IOException
- {
- byte compressedBuffer[] = DataSerializer.readByteArray(input);
- // Decompress the bytes
- decompressor.setInput(compressedBuffer, 0, compressedBuffer.length);
- byte retval[] = null;
- try {
- int resultLength = decompressor.inflate(buffer);
- retval = new byte[resultLength];
- System.arraycopy(compressedBuffer, 0, retval, 0, resultLength);
- } catch (DataFormatException e) {
- throw new IOException("Unable to decompress the byte array due to a data format error. " + e.getMessage());
- }
- return retval;
- }
-
- /**
- * Reads UTF string from the input. This method is analogous to
- * DataInput.readUTF() except that it supports null string.
- * @param input The data input stream.
- * @return Returns null or non-null string value.
- * @throws IOException Thrown if unable to read from the input stream.
- */
- public static String readUTF(DataInput input) throws IOException
- {
- byte header = input.readByte();
- if (header == DSCODE.NULL_STRING) {
- return null;
- }
- return input.readUTF();
- }
-
- /**
- * Writes the specified sting value to the output stream. This method
- * is analogous to DataOutput.writeUTF() except that it supports null
- * string.
- * @param value The string value to write to the output stream.
- * @param output The data output stream.
- * @throws IOException Thrown if unable to write to the output stream.
- */
- public static void writeUTF(String value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeByte(DSCODE.NULL_STRING);
- } else {
- output.writeByte(DSCODE.STRING);
- output.writeUTF(value);
- }
- }
-
- /**
- * Reads boolean value;
- */
- public static Boolean readBoolean(DataInput input) throws IOException
- {
- return input.readBoolean();
- }
-
- /**
- * Writes the specified boolean value to the stream. If the value is null
- * then it write false.
- * @param value The value to write
- * @param output
- * @throws IOException
- */
- public static void writeBoolean(Boolean value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeBoolean(false);
- } else {
- output.writeBoolean(value);
- }
- }
-
- public static Byte readByte(DataInput input) throws IOException
- {
- return input.readByte();
- }
-
- public static void writeByte(Byte value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeByte(0);
- } else {
- output.writeByte(value);
- }
- }
-
- public static Character readCharacter(DataInput input) throws IOException
- {
- return input.readChar();
- }
-
- public static void writeCharacter(Character value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeChar(0);
- } else {
- output.writeChar(value);
- }
- }
-
- public static Double readDouble(DataInput input) throws IOException
- {
- return input.readDouble();
- }
-
- public static void writeDouble(Double value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeDouble(0);
- } else {
- output.writeDouble(value);
- }
- }
-
- public static Float readFloat(DataInput input) throws IOException
- {
- return input.readFloat();
- }
-
- public static void writeFloat(Float value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeFloat(0);
- } else {
- output.writeFloat(value);
- }
- }
-
- public static Integer readInteger(DataInput input) throws IOException
- {
- return input.readInt();
- }
-
- public static void writeInteger(Integer value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeInt(0);
- } else {
- output.writeInt(value);
- }
- }
-
- public static Long readLong(DataInput input) throws IOException
- {
- return input.readLong();
- }
-
- public static void writeLong(Long value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeLong(0);
- } else {
- output.writeLong(value);
- }
- }
-
- public static Short readShort(DataInput input) throws IOException
- {
- return input.readShort();
- }
-
- public static void writeShort(Short value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeShort(0);
- } else {
- output.writeShort(value);
- }
- }
-
- public static MapLite readMapLite(DataInput input) throws ClassNotFoundException, IOException
- {
- byte header = input.readByte();
- if (header == DSCODE.NULL){
- return null;
- }
- MapLite fo = new MapLite();
- InternalDataSerializer.invokeFromData(fo, input);
- return fo;
- }
-
- public static void writeMapLite(MapLite value, DataOutput output) throws IOException
- {
- if (value == null) {
- output.writeByte(DSCODE.NULL);
- } else {
- output.writeByte(DSCODE.DS_NO_FIXED_ID);
- InternalDataSerializer.invokeToData(value, output);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/67085172/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtil.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtil.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtil.java
deleted file mode 100644
index 42ee2c7..0000000
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtil.java
+++ /dev/null
@@ -1,1094 +0,0 @@
-/*=========================================================================
- * Copyright (c) 2008, Pivotal Software, Inc. All Rights Reserved.
- * This product is protected by U.S. and international copyright
- * and intellectual property laws. Pivotal products are covered by
- * more patents listed at http://www.pivotal.io/patents.
- *========================================================================
- */
-package com.gemstone.gemfire.internal.tools.gfsh.app.util;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.Driver;
-import java.sql.DriverManager;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-import java.util.Set;
-
-import com.gemstone.gemfire.cache.Region;
-import com.gemstone.gemfire.cache.query.SelectResults;
-import com.gemstone.gemfire.cache.query.Struct;
-import com.gemstone.gemfire.cache.query.types.CollectionType;
-import com.gemstone.gemfire.cache.query.types.ObjectType;
-import com.gemstone.gemfire.cache.query.types.StructType;
-import com.gemstone.gemfire.internal.tools.gfsh.app.Gfsh;
-import com.gemstone.gemfire.internal.tools.gfsh.app.command.task.QueryResults;
-import com.gemstone.gemfire.internal.tools.gfsh.app.command.task.QueryTask;
-import com.gemstone.gemfire.internal.tools.gfsh.app.misc.util.ReflectionUtil;
-import com.gemstone.gemfire.internal.tools.gfsh.command.CommandResults;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-/**
- * DBUtil is a simple database utility class that updates the database
- * with the RBC TradeCapture specific objects.
- *
- * @author dpark
- */
-public class DBUtil
-{
- public static final int TYPE_KEYS = 0;
- public static final int TYPE_VALUES = 1;
- public static final int TYPE_KEYS_VALUES = 2;
-
-
- private static DBUtil dbutil;
-
- static SimpleDateFormat dateFormat = new SimpleDateFormat(
- "MM/dd/yyyy HH:mm:ss.SSS");
- static SimpleDateFormat smallDateFormat = new SimpleDateFormat(
- "MM/dd/yy");
-
- protected Connection conn;
- private String driverName;
- private String url;
- private String userName;
-
-
- /**
- * Creates a DBUtil object that provides access to the database.
- *
- * @param driverName The JDBC dirver name. For example,
- * com.nywe.db.sybaseImpl.SybaseDBConn.
- * @throws DBUtilException Thrown if the driver does not exist.
- */
- private DBUtil(String driverName) throws DBUtilException
- {
- init(driverName);
- }
-
- @SuppressFBWarnings(value="LI_LAZY_INIT_UPDATE_STATIC",justification="This looks like singleton and at present looks like desired functionality")
- public static DBUtil initialize(String driverName, String url, String userName,
- String password) throws DBUtilException
- {
- if (dbutil != null) {
- dbutil.close();
- }
- dbutil = new DBUtil(driverName);
- dbutil.connect(url, userName, password);
- return dbutil;
- }
-
- public static DBUtil getDBUtil()
- {
- return dbutil;
- }
-
- /**
- * Initializes DB.
- * @param driverName The name of the JDBC Driver. For example,
- * com.nywe.db.sybaseImpl.SybaseDBConn.
- */
- private void init(String driverName) throws DBUtilException
- {
- try {
- // finds and loads the driver dynamically
- Driver driver = (Driver) Class.forName(driverName).newInstance();
- DriverManager.registerDriver(driver);
- this.driverName = driverName;
- } catch (Exception ex) {
- throw new DBUtilException(ex);
- }
- }
-
- /**
- * Establishes connection to the database server.
- * @param url The database URL.
- * @param userName The user name used to login to the database.
- * @param password The password used to login to the database.
- * @throws DBUtilException Thrown if it encounters a database connection error.
- */
- private synchronized void connect(String url, String userName,
- String password) throws DBUtilException
- {
- if (conn != null) {
- throw new DBUtilException(DBUtilException.ERROR_CONNECTION_ALREADY_ESTABLISHED,
- "The database connection has already been established. To establish a new connection, close it first.");
- }
-
- Properties props = new Properties();
- props.put("user", userName);
- props.put("password", password);
-
- try {
- conn = DriverManager.getConnection(url, props);
- this.url = url;
- this.userName = userName;
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println("Error Code: " + ex.getErrorCode());
- } catch (Exception ex) {
- ex.printStackTrace();
- throw new DBUtilException(ex);
- }
- }
-
- public String getDriverName()
- {
- return driverName;
- }
-
- public String getUrl()
- {
- return url;
- }
-
- public String getUserName()
- {
- return userName;
- }
-
- /**
- * Cloases the database connection and frees all system resources used
- * by DBUtil.
- *
- * @throws DBUtilException Thrown if it encounters a database communcations
- * error.
- */
- public synchronized void close() throws DBUtilException
- {
- try {
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (Exception ex) {
- throw new DBUtilException(ex);
- }
- }
-
-
- /**
- * Returns true if DBUtil has been closed, otherwise, false. If DBUtil is
- * closed then DBUtil is no longer usable. To reconnect, call connect() again.
- */
- public boolean isClosed()
- {
- return (conn == null);
- }
-
- public int loadDB(Gfsh gfsh, Region region,
- Class keyClass,
- Class valueClass,
- String sql,
- String primaryColumn) throws DBUtilException
- {
- int count = 0;
- PreparedStatement statement = null;
-
- try {
- Map<String, Method> keySettersMap = ReflectionUtil.getAllSettersMap(keyClass);
- Map<String, Method> valueSettersMap = ReflectionUtil.getAllSettersMap(valueClass);
-
- // realMap is for mapping methods to case-insensitive table columns
- Map<String, Method> realMap = new HashMap();
-
- if (sql.startsWith("select ") == false) {
- // it's table name. create sql
- sql = "select * from " + sql;
- }
-
- statement = conn.prepareStatement(sql);
- ResultSet resultSet = statement.executeQuery();
-
- while (resultSet.next()) {
-
- // key
- Object key = updateObject(gfsh, keyClass, keySettersMap, realMap, resultSet, primaryColumn);
-
- // value
- Object value = updateObject(gfsh, valueClass, valueSettersMap, realMap, resultSet, null);
-
- region.put(key, value);
-
- count++;
- }
-
- } catch (Exception ex) {
- throw new DBUtilException(ex);
- } finally {
- if (statement != null) {
- try {
- statement.close();
- } catch (SQLException e) {
- throw new DBUtilException(e);
- }
- }
- }
-
- return count;
- }
-
- public int storeDB(Gfsh gfsh, Region region, String tableName,
- int storeType, String primaryColumn,
- boolean isKeyPrimary) throws DBUtilException
- {
- int count = 0;
- try {
- DatabaseMetaData meta = conn.getMetaData();
- ResultSet resultSet = meta.getColumns(null, null, tableName, null);
- ArrayList<ColumnInfo> columnList = new ArrayList();
- while (resultSet.next()) {
- ColumnInfo info = new ColumnInfo();
- info.name = (String)resultSet.getObject("COLUMN_NAME");
- info.type = (Integer)resultSet.getObject("DATA_TYPE"); // java.sql.Types
- columnList.add(info);
- }
-
- // Collect all matching field names from the region object
- Set<Region.Entry> entrySet = region.entrySet();
-
- Map<String, Method> keyGettersMap = new HashMap(0);
- Map<String, Method> valueGettersMap = new HashMap(0);
- int columnCount = 0;
- String keyKeys[] = new String[0];
- String valueKeys[] = new String[0];
- for (Region.Entry entry : entrySet) {
- Object key = entry.getKey();
- Object value = entry.getValue();
- switch (storeType) {
- case TYPE_KEYS:
- keyGettersMap = ReflectionUtil.getAllGettersMap(key.getClass());
- keyGettersMap = getGettersMap(keyGettersMap, columnList);
- columnCount = keyGettersMap.size();
- keyKeys = keyGettersMap.keySet().toArray(new String[0]);
- break;
- case TYPE_VALUES:
- valueGettersMap = ReflectionUtil.getAllGettersMap(value.getClass());
- valueGettersMap = getGettersMap(valueGettersMap, columnList);
- columnCount = valueGettersMap.size();
- valueKeys = valueGettersMap.keySet().toArray(new String[0]);
- break;
- case TYPE_KEYS_VALUES:
- keyGettersMap = ReflectionUtil.getAllGettersMap(key.getClass());
- keyGettersMap = getGettersMap(keyGettersMap, columnList);
- valueGettersMap = ReflectionUtil.getAllGettersMap(value.getClass());
- valueGettersMap = getGettersMap(valueGettersMap, columnList);
- columnCount = keyGettersMap.size() + valueGettersMap.size();
- keyKeys = keyGettersMap.keySet().toArray(new String[0]);
- valueKeys = valueGettersMap.keySet().toArray(new String[0]);
- default:
- break;
- }
-
- Set<String> keySet = valueGettersMap.keySet();
- // take care of case senstiveness
- if (primaryColumn != null) {
- for (String keyStr : keySet) {
- if (primaryColumn.equalsIgnoreCase(keyStr)) {
- primaryColumn = keyStr;
- break;
- }
- }
- }
-
- // Remove all duplicate entries from keyGettersMap
- // keep only primary key (column)
- for (String keyStr : keySet) {
- if (isKeyPrimary == false || primaryColumn == null || primaryColumn.equalsIgnoreCase(keyStr) == false) {
- keyGettersMap.remove(keyStr);
- }
- }
- break;
- }
-
- try {
-
- String insertQuery = createInsertQuery(keyKeys, valueKeys, tableName);
- String updateQuery = createUpdateQuery(keyKeys, valueKeys, tableName, primaryColumn, isKeyPrimary);
-//
-// System.out.println(insertQuery);
-// System.out.println(updateQuery);
-
- // tries insert followed by update (upsert)
- for (Region.Entry entry : entrySet) {
- Object key = entry.getKey();
- Object value = entry.getValue();
-
- // try insert first. if it fails then do update
- PreparedStatement statement = createInsertPreparedStatement(keyGettersMap, valueGettersMap, keyKeys, valueKeys, key, value, insertQuery);
-// System.out.println(statement.toString());
- try {
- statement.executeUpdate();
- } catch (SQLException ex) {
-
- // try update
- statement = createUpdatePreparedStatement(keyGettersMap, valueGettersMap, keyKeys, valueKeys, key, value, updateQuery, primaryColumn, isKeyPrimary);
- statement.executeUpdate();
-// System.out.println(statement.toString());
- }
- count++;
- }
- } catch (SQLException ex) {
- throw new DBUtilException(ex);
- }
-
- } catch (Exception ex) {
- throw new DBUtilException(ex);
- }
- return count;
- }
-
- private List<ColumnInfo> getColumnInfoList(String tableName) throws SQLException
- {
- String lowercase = tableName.toLowerCase();
- ArrayList<ColumnInfo> columnList = new ArrayList();
- if (lowercase.startsWith("insert ")) {
- // insert
-
-
- } else if (lowercase.startsWith("update ")) {
-
- // update
-
- // TODO: hack
- // Replace the following with real parser
- // This breaks if string literals that has '?', ',', or '=' character
- String split[] = tableName.split("= *\\?");
- for (int i = 0; i < split.length; i++) {
- String token = split[i].trim();
- int index = token.indexOf('=');
- if (index >= 0) {
- String x = token.substring(index);
- x = x.trim();
- index = x.lastIndexOf(',');
- if (index > 0) {
- token = x.substring(index+1);
- } else {
- index = x.lastIndexOf(' ');
- if (index > 0) {
- token = x.substring(index+1);
- } else {
- continue;
- }
- }
- }
- index = token.lastIndexOf(' ');
- if (index != -1) {
- token = token.substring(index);
- }
- token = token.replace(',', ' ');
- token = token.trim();
- ColumnInfo info = new ColumnInfo();
- info.name = token;
- columnList.add(info);
- }
-
- } else {
- // table name
- DatabaseMetaData meta = conn.getMetaData();
- ResultSet resultSet = meta.getColumns(null, null, tableName, null);
-
- while (resultSet.next()) {
- ColumnInfo info = new ColumnInfo();
- info.name = (String)resultSet.getObject("COLUMN_NAME");
- info.type = (Integer)resultSet.getObject("DATA_TYPE"); // java.sql.Types
- columnList.add(info);
- }
- }
-
- return columnList;
- }
-
- private String[] getColumnNames(List<ColumnInfo> columnList)
- {
- String columnNames[] = new String[columnList.size()];
- for (int i = 0; i < columnList.size(); i++) {
- columnNames[i] = ((ColumnInfo)columnList.get(i)).name;
- }
- return columnNames;
- }
-
- private int storeMapDB(Gfsh gfsh, Map map, String tableName,
- int storeType, String primaryColumn,
- boolean isKeyPrimary, List<ColumnInfo> columnList, boolean isQuery) throws DBUtilException
- {
- int count = 0;
- try {
-
- // Collect all matching field names from the region object
- Set<Map.Entry> entrySet = map.entrySet();
-
- Map<String, Method> keyGettersMap = new HashMap(0);
- Map<String, Method> valueGettersMap = new HashMap(0);
- int columnCount = 0;
- String keyKeys[] = new String[0];
- String valueKeys[] = new String[0];
- for (Map.Entry entry : entrySet) {
- Object key = entry.getKey();
- Object value = entry.getValue();
- switch (storeType) {
- case TYPE_KEYS:
- keyGettersMap = ReflectionUtil.getAllGettersMap(key.getClass());
- keyGettersMap = getGettersMap(keyGettersMap, columnList);
- columnCount = keyGettersMap.size();
- if (isQuery) {
- keyKeys = getColumnNames(columnList);
- } else {
- keyKeys = keyGettersMap.keySet().toArray(new String[0]);
- }
-
- break;
- case TYPE_VALUES:
- valueGettersMap = ReflectionUtil.getAllGettersMap(value.getClass());
- valueGettersMap = getGettersMap(valueGettersMap, columnList);
- columnCount = valueGettersMap.size();
- if (isQuery) {
- valueKeys = getColumnNames(columnList);
- } else {
- valueKeys = valueGettersMap.keySet().toArray(new String[0]);
- }
- break;
- case TYPE_KEYS_VALUES:
- keyGettersMap = ReflectionUtil.getAllGettersMap(key.getClass());
- keyGettersMap = getGettersMap(keyGettersMap, columnList);
- valueGettersMap = ReflectionUtil.getAllGettersMap(value.getClass());
- valueGettersMap = getGettersMap(valueGettersMap, columnList);
- columnCount = keyGettersMap.size() + valueGettersMap.size();
-
- if (isQuery) {
- keyKeys = getColumnNames(columnList);
- valueKeys = getColumnNames(columnList);
- } else {
- keyKeys = keyGettersMap.keySet().toArray(new String[0]);
- valueKeys = valueGettersMap.keySet().toArray(new String[0]);
- }
- default:
- break;
- }
-
- Set<String> keySet = valueGettersMap.keySet();
- // take care of case senstiveness
- if (primaryColumn != null) {
- for (String keyStr : keySet) {
- if (primaryColumn.equalsIgnoreCase(keyStr)) {
- primaryColumn = keyStr;
- break;
- }
- }
- }
-
- // Remove all duplicate entries from keyGettersMap
- // keep only primary key (column)
- for (String keyStr : keySet) {
- if (isKeyPrimary == false || primaryColumn == null || primaryColumn.equalsIgnoreCase(keyStr) == false) {
- keyGettersMap.remove(keyStr);
- }
- }
- break;
- }
-
- try {
-
- String insertQuery = createInsertQuery(keyKeys, valueKeys, tableName);
- String updateQuery = createUpdateQuery(keyKeys, valueKeys, tableName, primaryColumn, isKeyPrimary);
- if (gfsh.isDebug()) {
- System.out.println("insert: " + insertQuery);
- System.out.println("update: " + updateQuery);
- }
-
- // tries insert followed by update (upsert)
- for (Map.Entry entry : entrySet) {
- Object key = entry.getKey();
- Object value = entry.getValue();
-
- // try insert first. if it fails then do update
- PreparedStatement statement;
- if (insertQuery != null) {
- try {
- statement = createInsertPreparedStatement(keyGettersMap, valueGettersMap, keyKeys, valueKeys, key, value, insertQuery);
- statement.executeUpdate();
- } catch (SQLException ex) {
-
- // try update
- if (updateQuery != null) {
- statement = createUpdatePreparedStatement(keyGettersMap, valueGettersMap, keyKeys, valueKeys, key, value, updateQuery, primaryColumn, isKeyPrimary);
- statement.executeUpdate();
- }
-
- }
- } else {
- statement = createUpdatePreparedStatement(keyGettersMap, valueGettersMap, keyKeys, valueKeys, key, value, updateQuery, primaryColumn, isKeyPrimary);
- statement.executeUpdate();
- }
-
- count++;
- }
- } catch (SQLException ex) {
- throw new DBUtilException(ex);
- }
-
- } catch (Exception ex) {
- throw new DBUtilException(ex);
- }
- return count;
- }
-
- public int storeDB(Gfsh gfsh, String oql, String tableName,
- int storeType, String primaryColumn,
- boolean isKeyPrimary, int batchSize) throws DBUtilException
- {
- String lowercase = tableName.toLowerCase();
- boolean isQuery = lowercase.startsWith("insert ") || lowercase.startsWith("update ");
- int count = 0;
- int actualSize;
- String insertQuery = null;
- String updateQuery = null;
- int columnCount = 0;
- Map<String, Method> valueGettersMap = null;
- String valueKeys[] = null;
- try {
-
- List<ColumnInfo>columnList = getColumnInfoList(tableName);
-
- // Use command client to fetch data from the server
- do {
- CommandResults cr = gfsh.getCommandClient().execute(new QueryTask(oql, batchSize, true));
- if (cr.getCode() == QueryTask.ERROR_QUERY) {
- gfsh.println(cr.getCodeMessage());
- return -1;
- }
- QueryResults results = (QueryResults) cr.getDataObject();
- if (results == null || results.getResults() == null) {
- return count;
- }
-
- if (results.getResults() instanceof Map) {
-
- // Map
-
- Map map = (Map)results.getResults();
- count += storeMapDB(gfsh, map, tableName, storeType, primaryColumn, isKeyPrimary, columnList, isQuery);
-
- } else {
-
- // SelectResults
-
- SelectResults sr = (SelectResults)results.getResults();
- CollectionType type = sr.getCollectionType();
- ObjectType elementType = type.getElementType();
- List srList = sr.asList();
- Object element = null;
- StructType structType = null;
- Struct struct = null;
-
- // Create insert prepared query strings
- if (insertQuery == null && srList.size() > 0) {
- element = srList.get(0);
-
- if (elementType.isStructType()) {
- // struct
- structType = (StructType) elementType;
- insertQuery = createInsertQuery(structType.getFieldNames(), tableName);
- updateQuery = createUpdateQuery(structType.getFieldNames(), tableName, primaryColumn);
- } else {
- // object
- valueGettersMap = new HashMap(0);
- valueGettersMap = ReflectionUtil.getAllGettersMap(element.getClass());
- valueGettersMap = getGettersMap(valueGettersMap, columnList);
- columnCount = valueGettersMap.size();
-
- if (isQuery) {
- valueKeys = getColumnNames(columnList);
- } else {
- valueKeys = valueGettersMap.keySet().toArray(new String[0]);
- }
-
- insertQuery = createInsertQuery(valueKeys, tableName);
- updateQuery = createUpdateQuery(valueKeys, tableName, primaryColumn);
- }
-
- if (gfsh.isDebug()) {
- System.out.println("insert: " + insertQuery);
- System.out.println("update: " + updateQuery);
- }
- }
-
- // Upsert
- for (int i = 0; i < srList.size(); i++) {
- element = srList.get(i);
-
- if (elementType.isStructType()) {
-
- structType = (StructType) elementType;
- struct = (Struct) element;
- PreparedStatement statement = createInsertPreparedStatement(struct, insertQuery);
-// System.out.println(statement.toString());
-
- try {
- statement.executeUpdate();
- } catch (SQLException ex) {
-
- // try update
- statement = createUpdatePreparedStatement(struct, updateQuery, primaryColumn);
- statement.executeUpdate();
-// System.out.println(statement.toString());
- }
-
- } else {
-
- PreparedStatement statement;
- if (insertQuery != null) {
- try {
- statement = createInsertPreparedStatement(valueGettersMap, valueKeys, element, insertQuery);
- statement.executeUpdate();
- } catch (SQLException ex) {
-
- // try update
- if (updateQuery != null) {
- statement = createUpdatePreparedStatement(valueGettersMap, valueKeys, element, updateQuery, primaryColumn);
- statement.executeUpdate();
- }
- }
- } else {
- statement = createUpdatePreparedStatement(valueGettersMap, valueKeys, element, updateQuery, primaryColumn);
- System.out.println(statement.toString());
- statement.executeUpdate();
- }
- }
- }
-
- count += sr.size();
- }
- actualSize = results.getActualSize();
- oql = null;
- } while (count < actualSize);
-
- } catch (Exception ex) {
- throw new DBUtilException(ex);
- }
- return count;
- }
-
- private PreparedStatement createUpdatePreparedStatement(
- Struct struct,
- String query,
- String primaryColumn) throws SQLException, InvocationTargetException, IllegalAccessException
- {
- Object primaryObj = null;
- PreparedStatement statement = conn.prepareStatement(query);
- String fieldNames[] = struct.getStructType().getFieldNames();
- Object fieldValues[] = struct.getFieldValues();
- int i;
- for (i = 0; i < fieldValues.length; i++) {
- statement.setObject(i+1, fieldValues[i]);
- Object obj = fieldValues[i];
- if (primaryColumn != null && primaryColumn.equals(fieldNames[i])) {
- primaryObj = obj;
- } else {
- statement.setObject(i+1, obj);
- }
- }
-
- if (primaryObj != null) {
- statement.setObject(i+1, primaryObj);
- }
-
- return statement;
- }
-
- private PreparedStatement createInsertPreparedStatement(Struct struct, String query)
- throws SQLException, InvocationTargetException, IllegalAccessException
- {
- PreparedStatement statement = conn.prepareStatement(query);
- Object fieldValues[] = struct.getFieldValues();
- for (int i = 0; i < fieldValues.length; i++) {
- statement.setObject(i+1, fieldValues[i]);
- }
- return statement;
- }
-
-
- private PreparedStatement createUpdatePreparedStatement(
- Map<String, Method> valueGettersMap,
- String valueKeys[],
- Object value,
- String query,
- String primaryColumn) throws SQLException, InvocationTargetException, IllegalAccessException
- {
- return createUpdatePreparedStatement(null, valueGettersMap, null, valueKeys, null, value, query, primaryColumn, false);
- }
-
- private PreparedStatement createInsertPreparedStatement(
- Map<String, Method> valueGettersMap,
- String valueKeys[],
- Object value,
- String query) throws SQLException, InvocationTargetException, IllegalAccessException
- {
- return createInsertPreparedStatement(null, valueGettersMap, null, valueKeys, null, value, query);
- }
-
- private PreparedStatement createUpdatePreparedStatement(Map<String, Method> keyGettersMap,
- Map<String, Method> valueGettersMap,
- String keyKeys[],
- String valueKeys[],
- Object key,
- Object value,
- String query,
- String primaryColumn,
- boolean isKeyPrimary) throws SQLException, InvocationTargetException, IllegalAccessException
- {
- Object primaryObj = null;
- PreparedStatement statement = conn.prepareStatement(query);
- int index = 1;
- if (keyKeys != null) {
- for (int i = 0; i < keyKeys.length; i++) {
- Method method = keyGettersMap.get(keyKeys[i]);
- Object obj = method.invoke(key, (Object[])null);
- if (primaryColumn != null && isKeyPrimary==false && primaryColumn.equals(keyKeys[i])) {
- primaryObj = obj;
- } else {
- statement.setObject(index++, obj);
- }
- }
- }
-
- if (valueKeys != null) {
- for (int i = 0; i < valueKeys.length; i++) {
- Method method = valueGettersMap.get(valueKeys[i]);
- Object obj = method.invoke(value, (Object[])null);
- if (primaryColumn != null && isKeyPrimary == false && primaryColumn.equals(valueKeys[i])) {
- primaryObj = obj;
- } else {
- statement.setObject(index++, obj);
- }
- }
- }
-
- if (primaryObj != null) {
- statement.setObject(index, primaryObj);
- }
-
- return statement;
- }
-
- private PreparedStatement createInsertPreparedStatement(Map<String, Method> keyGettersMap,
- Map<String, Method> valueGettersMap,
- String keyKeys[],
- String valueKeys[],
- Object key,
- Object value,
- String query) throws SQLException, InvocationTargetException, IllegalAccessException
- {
- PreparedStatement statement = conn.prepareStatement(query);
- if (keyKeys!= null) {
- for (int i = 0; i < keyKeys.length; i++) {
- Method method = keyGettersMap.get(keyKeys[i]);
- Object obj = method.invoke(key, (Object[])null);
- statement.setObject(i+1, obj);
- }
- }
- if (valueKeys != null) {
- for (int i = 0; i < valueKeys.length; i++) {
- Method method = valueGettersMap.get(valueKeys[i]);
- Object obj = method.invoke(value, (Object[])null);
- statement.setObject(i+1, obj);
- }
- }
-
- return statement;
- }
-
- private String createUpdateQuery(String fieldNames[],
- String tableName,
- String primaryColumn)
- {
- String query = null;
- String lowercase = tableName.trim().toLowerCase();
- if (lowercase.startsWith("insert ")) {
- // insert not honored for update operation
- query = null;
- } else if (lowercase.startsWith("update ")) {
- // use the passed in update statement
- query = tableName;
- } else {
-
- // build update
- query = "UPDATE " + tableName + " SET ";
- for (int i = 0; i < fieldNames.length; i++) {
- if (primaryColumn != null && primaryColumn.equals(fieldNames[i])) {
- // skip
- } else {
- query += fieldNames[i] + "=?,";
- }
- }
-
- int index = query.lastIndexOf(",");
- if (index != -1) {
- query = query.substring(0, index);
- }
- if (primaryColumn != null) {
- query += " WHERE " + primaryColumn + "=?";
- }
- }
-
- return query;
- }
-
- private String createInsertQuery(String fieldNames[], String tableName)
- {
-
- String lowercase = tableName.trim().toLowerCase();
- String query = null;
-
- if (lowercase.startsWith("insert ")) {
- // use the passed in insert statement
- query = tableName;
- } else if (lowercase.startsWith("update ")) {
- // update not honored for insert operation
- query = null;
- } else {
-
- // build insert
- query = "INSERT INTO " + tableName + " (";
- for (int i = 0; i < fieldNames.length; i++) {
- query += fieldNames[i] + ",";
- }
- int index = query.lastIndexOf(',');
- if (index != -1) {
- query = query.substring(0, index);
- }
- int columnCount = fieldNames.length;
- query += ") VALUES (";
- for (int i = 0; i < columnCount; i++) {
- query += "?,";
- }
- index = query.lastIndexOf(',');
- if (index != -1) {
- query = query.substring(0, index);
- }
- query += ")";
- }
-
- return query;
- }
-
- private String createUpdateQuery(String keyKeys[], String valueKeys[],
- String tableName,
- String primaryColumn,
- boolean isKeyPrimary)
- {
- String query = null;
- String lowercase = tableName.trim().toLowerCase();
- if (lowercase.startsWith("insert ")) {
- // insert not honored for update operation
- query = null;
- } else if (lowercase.startsWith("update ")) {
- // use the passed in update statement
- query = tableName;
- } else {
-
- // build update
- query = "UPDATE " + tableName + " SET ";
- for (int i = 0; i < keyKeys.length; i++) {
- if (primaryColumn != null && isKeyPrimary && primaryColumn.equals(keyKeys[i])) {
- // skip
- } else {
- query += keyKeys[i] + "=?,";
- }
- }
- for (int i = 0; i < valueKeys.length; i++) {
- if (primaryColumn != null && isKeyPrimary == false && primaryColumn.equals(valueKeys[i])) {
- // skip
- } else {
- query += valueKeys[i] + "=?,";
- }
- }
- int index = query.lastIndexOf(",");
- if (index != -1) {
- query = query.substring(0, index);
- }
- if (primaryColumn != null) {
- query += " WHERE " + primaryColumn + "=?";
- }
- }
-
- return query;
- }
-
- private String createInsertQuery(String keyKeys[], String valueKeys[], String tableName)
- {
- String lowercase = tableName.trim().toLowerCase();
- String query = null;
-
- if (lowercase.startsWith("insert ")) {
- // use the passed in insert statement
- query = tableName;
- } else if (lowercase.startsWith("update ")) {
- // update not honored for insert operation
- query = null;
- } else {
-
- // build insert
- int columnCount = 0;
- query = "INSERT INTO " + tableName + " (";
- if (keyKeys != null) {
- for (int i = 0; i < keyKeys.length; i++) {
- query += keyKeys[i] + ",";
- }
- columnCount += keyKeys.length;
- }
- if (valueKeys != null) {
- for (int i = 0; i < valueKeys.length; i++) {
- query += valueKeys[i] + ",";
- }
- columnCount += valueKeys.length;
- }
- int index = query.lastIndexOf(',');
- if (index != -1) {
- query = query.substring(0, index);
- }
-
- query += ") VALUES (";
- for (int i = 0; i < columnCount; i++) {
- query += "?,";
- }
- index = query.lastIndexOf(',');
- if (index != -1) {
- query = query.substring(0, index);
- }
- query += ")";
- }
-
- return query;
- }
-
- private Map getGettersMap(Map<String, Method> gettersMap, List<ColumnInfo> columnList)
- {
- HashMap map = new HashMap();
- Set<String> keySet = gettersMap.keySet();
- for (int i = 0; i < columnList.size(); i++) {
- ColumnInfo info = columnList.get(i);
- Method getterMethod = gettersMap.get("get" + info.name);
- if (getterMethod == null) {
- for (String key : keySet) {
- if (key.substring(3).equalsIgnoreCase(info.name)) {
- getterMethod = gettersMap.get(key);
- break;
- }
- }
- }
- if (getterMethod != null) {
- map.put(info.name, getterMethod);
- }
- }
- return map;
- }
-
- private Object updateObject(Gfsh gfsh, Class clazz,
- Map<String, Method> settersMap, Map<String, Method> realMap,
- ResultSet resultSet,
- String primaryColumn) throws Exception
- {
-
- // If primaryColumn is set then set the mapping method only
-
- if (primaryColumn != null) {
-
- // Set the primary column value only
- Object obj = resultSet.getObject(primaryColumn);
- if (clazz == null ||
- clazz == byte.class || clazz == Byte.class ||
- clazz == char.class || clazz == Character.class ||
- clazz == short.class || clazz == Short.class ||
- clazz == int.class || clazz == Integer.class ||
- clazz == long.class || clazz == Long.class ||
- clazz == float.class || clazz == Float.class ||
- clazz == double.class || clazz == Double.class ||
- clazz == Date.class || clazz == String.class)
- {
- return obj;
- }
-
- Object value = clazz.newInstance();
- Method setterMethod = realMap.get(primaryColumn);
- if (setterMethod == null) {
- setterMethod = settersMap.get("set" + primaryColumn);
- if (setterMethod == null) {
- Set<Entry<String, Method>> entrySet = settersMap.entrySet(); //FindBugs - entrySet efficient over keyset
- for (Entry<String, Method> entry : entrySet) {
- String key = entry.getKey();
- if (key.substring(3).equalsIgnoreCase(primaryColumn)) {
- setterMethod = entry.getValue();
- realMap.put(primaryColumn, settersMap.get(key));
- }
- }
- }
- }
- setterMethod.invoke(value, obj);
- return value;
- }
-
- // Set the entire object
-
- if (clazz == null ||
- clazz == byte.class || clazz == Byte.class ||
- clazz == char.class || clazz == Character.class ||
- clazz == short.class || clazz == Short.class ||
- clazz == int.class || clazz == Integer.class ||
- clazz == long.class || clazz == Long.class ||
- clazz == float.class || clazz == Float.class ||
- clazz == double.class || clazz == Double.class ||
- clazz == Date.class || clazz == String.class)
- {
-
- return resultSet.getObject(1);
-
- } else {
-
- Object value = clazz.newInstance();
- ResultSetMetaData meta = resultSet.getMetaData();
- for (int i = 1; i <= meta.getColumnCount(); i++) {
- String columnName = meta.getColumnName(i);
- Method setterMethod = realMap.get(columnName);
- if (setterMethod == null) {
- setterMethod = settersMap.get("set" + columnName);
- if (setterMethod == null) {
- Set<String> keySet = settersMap.keySet();
- for (String key : keySet) {
- if (key.substring(3).equalsIgnoreCase(columnName)) {
- setterMethod = settersMap.get(key);
- realMap.put(columnName, settersMap.get(key));
- }
- }
- }
- }
- if (setterMethod == null) {
-// throw new DBUtilException(DBUtilException.ERROR_NO_MATCHING_METHOD_FOR_COLUMN, "Undefined method for the column " + columnName);
- continue;
- }
-
- Object obj = resultSet.getObject(i);
- setterMethod.invoke(value, obj);
- }
- return value;
- }
-
- }
- //FindBugs - make static inner class
- static class ColumnInfo
- {
- String name;
- int type;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/67085172/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtilException.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtilException.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtilException.java
deleted file mode 100644
index 45c1c6a..0000000
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/DBUtilException.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*=========================================================================
- * Copyright (c) 2008, Pivotal Software, Inc. All Rights Reserved.
- * This product is protected by U.S. and international copyright
- * and intellectual property laws. Pivotal products are covered by
- * more patents listed at http://www.pivotal.io/patents.
- *========================================================================
- */
-package com.gemstone.gemfire.internal.tools.gfsh.app.util;
-
-/**
- * DBUtilException is thrown by DBUtil when it encounters DB errors.
- *
- * @author dpark
- *
- */
-public class DBUtilException extends Exception
-{
- private static final long serialVersionUID = 1L;
-
- public static final int ERROR_CONNECTION_CLOSED = 1;
- public static final int ERROR_CONNECTION_ALREADY_ESTABLISHED = 2;
- public static final int ERROR_NO_MATCHING_METHOD_FOR_COLUMN = 3;
- public static final int ERROR_UNDEFINED = -1;
-
- private int errorCode = ERROR_UNDEFINED;
-
- public DBUtilException()
- {
- }
-
- public DBUtilException(int errorCode, String message)
- {
- super(message);
- this.errorCode = errorCode;
- }
-
- public DBUtilException(String message, Throwable cause)
- {
- super(cause.getClass() + ": " + cause.getMessage() + ". " + message);
- initCause(cause);
- }
-
- public DBUtilException(Throwable cause)
- {
- super(cause.getClass() + ": " + cause.getMessage() + ".");
- initCause(cause);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/67085172/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/GfshResultsBag.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/GfshResultsBag.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/GfshResultsBag.java
deleted file mode 100644
index 7f88de7..0000000
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/GfshResultsBag.java
+++ /dev/null
@@ -1,600 +0,0 @@
-package com.gemstone.gemfire.internal.tools.gfsh.app.util;
-
-import it.unimi.dsi.fastutil.objects.Object2IntMap;
-import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
-import it.unimi.dsi.fastutil.objects.ObjectIterator;
-
-import java.io.DataInput;
-import java.io.DataOutput;
-import java.io.IOException;
-import java.util.AbstractCollection;
-import java.util.AbstractSet;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.NoSuchElementException;
-import java.util.Set;
-
-import com.gemstone.gemfire.DataSerializable;
-import com.gemstone.gemfire.DataSerializer;
-import com.gemstone.gemfire.cache.query.SelectResults;
-import com.gemstone.gemfire.cache.query.internal.ResultsCollectionWrapper;
-import com.gemstone.gemfire.cache.query.internal.types.CollectionTypeImpl;
-import com.gemstone.gemfire.cache.query.types.CollectionType;
-import com.gemstone.gemfire.cache.query.types.ObjectType;
-
-// @todo probably should assert element type when elements added
-// @todo support generics when no longer support Java 1.4
-/**
- * Implementation of SelectResults that allows duplicates and
- * extends HashMap. The keys store the elements of the collection and the
- * the values store the number of occurrances as an int.
- * If the elements are Structs, then use a StructBag instead.
- *
- * @author Eric Zoerner
- * @since 5.1
- */
-public class GfshResultsBag extends AbstractCollection implements SelectResults, DataSerializable {
- protected ObjectType elementType;
- protected Object2IntOpenHashMap map;
- protected int size = 0;
- /** adds support for null elements, since a TObjectIntHashMap does not
- support null elements
- */
- protected int numNulls = 0;
- //Asif: These fields are used for limiting the results based on the limit
- //clause in the query
- private int limit = -1;
- boolean hasLimitIterator = false;
- final Object limitLock = new Object();
-
- public GfshResultsBag() {
- this.map = new Object2IntOpenHashMap();
- }
-
- public void setElementType(ObjectType elementType) {
- this.elementType = elementType;
- }
-
- // @todo Currently does an iteration, could make this more efficient
- // by providing a ListView
- /**
- * Returns this bag as a list.
- */
- public List asList() {
- return new ArrayList(this);
- }
-
- /**
- * Return an unmodifiable Set view of this bag.
- * Does not require an iteration by using a lightweight wrapper.
- */
- public Set asSet() {
- return new SetView();
- }
-
- public CollectionType getCollectionType() {
- return new CollectionTypeImpl(Collection.class, this.elementType);
- }
-
- public boolean isModifiable() {
- return true;
- }
-
- public int occurrences(Object element) {
- if (this.hasLimitIterator) {
- // Asif: If limit iterator then occurence should be calculated
- // via the limit iterator
- int count = 0;
- boolean encounteredObject = false;
- for (Iterator itr = this.iterator()/* this.base.iterator() */; itr
- .hasNext();) {
- Object v = itr.next();
- if (element == null ? v == null : element.equals(v)) {
- count++;
- encounteredObject = true;
- }
- else if (encounteredObject) {
- // Asif: No possibility of its occurence again
- break;
- }
- }
- return count;
- }
- else {
- if (element == null) {
- return this.numNulls;
- }
- return this.map.getInt(element); // returns 0 if not found
- }
- }
-
- /**
- * Return an iterator over the elements in this collection. Duplicates will
- * show up the number of times it has occurrances.
- */
- @Override
- public Iterator iterator() {
- if (this.hasLimitIterator) {
- // Asif: Return a new LimitIterator in the block so that
- // the current Limit does not get changed by a remove
- // operation of another thread. The current limit is
- // set in the limit iterator. So the setting of the current limit
- // & creation of iterator should be atomic .If any subsequent
- // modifcation in limit occurs, the iterator will itself fail.
- // Similarly a remove called by a iterator should decrease the
- // current limit and underlying itr.remove atomically
- synchronized (this.limitLock) {
- return new LimitResultsBagIterator();
- }
- }
- else {
- return new ResultsBagIterator();
- }
- }
-
- @Override
- public boolean contains(Object element) {
- if (this.hasLimitIterator) {
- return super.contains(element);
- }
- else {
- if (element == null) {
- return this.numNulls > 0;
- }
- return this.map.containsKey(element);
- }
- }
-
- // not thread safe!
- @Override
- public boolean add(Object element) {
- if( this.limit > -1) {
- throw new UnsupportedOperationException("Addition to the SelectResults not allowed as the query result is constrained by LIMIT");
- }
- if (element == null) {
- numNulls++;
- }
- else {
- int count = this.map.getInt(element); // 0 if not found
- this.map.put(element, count + 1);
- }
- this.size++;
- assert this.size >= 0 : this.size;
- return true;
- }
-
- // Internal usage method
- // Asif :In case of StructBag , we will ensure that it
- // gets an Object [] indicating field values as parameter
- // from the CompiledSelect
- public int addAndGetOccurence(Object element) {
- int occurence;
- if (element == null) {
- numNulls++;
- occurence = numNulls;
- }
- else {
- occurence = this.map.getInt(element); // 0 if not found
- this.map.put(element, ++occurence);
- }
- this.size++;
- assert this.size >= 0: this.size;
- return occurence;
- }
-
- @Override
- public int size() {
- if (this.hasLimitIterator) {
- synchronized (this.limitLock) {
- return this.limit;
- }
- } else {
- return this.size;
- }
- }
-
- // not thread safe!
- @Override
- public boolean remove(Object element) {
- if(this.hasLimitIterator) {
- return super.remove(element);
- }else {
- if (element == null) {
- if (this.numNulls > 0) {
- this.numNulls--;
- this.size--;
- assert this.size >= 0 : this.size;
- return true;
- }
- else {
- return false;
- }
- }
- int count = this.map.getInt(element); // 0 if not found
- if (count == 0) {
- return false;
- }
- if (count == 1) {
- this.map.remove(element);
- }
- else {
- this.map.put(element, --count);
- }
- this.size--;
- assert this.size >= 0 : this.size;
- return true;
- }
- }
-
- // not thread safe!
- @Override
- public void clear() {
- this.map.clear();
- this.numNulls = 0;
- this.size = 0;
- if (this.hasLimitIterator) {
- synchronized (this.limitLock) {
- this.limit = 0;
- }
- }
- }
-
- // not thread safe!
- @Override
- public boolean equals(Object o) {
- if (!(o instanceof GfshResultsBag)) {
- return false;
- }
- GfshResultsBag otherBag = (GfshResultsBag)o;
- return this.size == otherBag.size
- && this.elementType.equals(otherBag.elementType)
- && this.map.equals(otherBag.map)
- && this.numNulls == otherBag.numNulls;
- }
-
- @Override // GemStoneAddition
- public int hashCode() {
- return this.map.hashCode();
- }
- public boolean addAll(Collection coll) {
- if(this.limit > -1) {
- throw new UnsupportedOperationException("Addition to the SelectResults not allowed as the query result is constrained by LIMIT");
- }else {
- return super.addAll(coll);
- }
- }
- protected Object2IntOpenHashMap createMapForFromData() {
- return new Object2IntOpenHashMap(this.size);
- }
-
- public void fromData(DataInput in) throws IOException, ClassNotFoundException {
- this.elementType = (ObjectType)DataSerializer.readObject(in);
- this.size = in.readInt();
- assert this.size >= 0: this.size;
- this.readNumNulls(in);
- // Asif: The size will be including null so the Map should at max contain
- // size - number of nulls
- int numLeft = this.size - this.numNulls;
-
- while (numLeft > 0) {
- Object key = DataSerializer.readObject(in);
- int occurence = in.readInt();
- this.map.put(key, occurence);
- numLeft -= occurence;
- }
- }
-//
-// public int getDSFID() {
-// return RESULTS_BAG;
-// }
-
- public void toData(DataOutput out) throws IOException {
- DataSerializer.writeObject(this.elementType, out);
- out.writeInt(this.size());
- this.writeNumNulls(out);
- // TODO:Asif: Should we actually pass the limit in serialization?
- // For the time being not passing , assuming PR Has parsed
- // it
- // out.writeInt(this.limit);
- int numLeft = this.size() - this.numNulls;
- for (ObjectIterator<Object2IntMap.Entry<?>> itr = this.map.object2IntEntrySet().fastIterator(); itr.hasNext()
- && numLeft > 0;) {
- Object2IntMap.Entry<?> entry = itr.next();
- Object key = entry.getKey();
- DataSerializer.writeObject(key, out);
- int occurence = entry.getValue();
- if (numLeft < occurence) {
- occurence = numLeft;
- }
- out.writeInt(occurence);
- numLeft -= occurence;
- }
- }
-
- /**
- *
- * @param out
- * @throws IOException
- */
- void writeNumNulls(DataOutput out) throws IOException {
- out.writeInt(this.numNulls);
- }
-
- /**
- *
- * @param in
- * @throws IOException
- */
- void readNumNulls(DataInput in) throws IOException {
- this.numNulls = in.readInt();
- }
-
- /**
- */
- void createObject2IntHashMap() {
- this.map = new Object2IntOpenHashMap(this.size - this.numNulls);
- }
-
- void applyLimit(int limit) {
- this.limit = limit;
- // Asif : From the code of TObjectIntHashMap, it appears that if no data is
- // going to be added , then the rehash does not occur & default code
- // of rehash does not appear to change the order of data . So we can assume
- // that this iterator will be returning data in order.
- // Limit Iterator is needed if the limit happens to be less than the size
- if (this.limit > -1 && this.size > this.limit) {
- this.hasLimitIterator = true;
- }
- }
-
- protected class ResultsBagIterator implements Iterator {
- final ObjectIterator<Object2IntMap.Entry<?>> mapIterator = GfshResultsBag.this.map.object2IntEntrySet().fastIterator();
- Object2IntMap.Entry<?> mapIteratorEntry;
- Object current = null;
-
- /**
- * duplicates are numbered from 1 to n;
- * 0 = no current, otherwise goes from 1 to dupLimit,
- * indicating the last dup that was emitted by next()
- */
- int currentDup = 0;
- /**
- * dupLimit is the total number of occurrences;
- * start by emitting the nulls
- */
- int dupLimit = GfshResultsBag.this.numNulls;
-
- public boolean hasNext() {
- return this.mapIterator.hasNext() || this.currentDup < this.dupLimit;
- }
-
- public Object next() {
- // see if there is another duplicate to emit
- if (this.currentDup < this.dupLimit) {
- this.currentDup++;
- return this.current;
- }
- //otherwise, go to next object
- this.mapIteratorEntry = this.mapIterator.next();
- this.dupLimit = this.mapIteratorEntry.getValue();
- this.currentDup = 1;
- this.current = this.mapIteratorEntry.getKey();
- return this.current;
- }
-
- public void remove() {
-// if (this.currentDup == 0) {
-// // next has not yet been called
-// throw new IllegalStateException(LocalizedStrings.ResultsBag_NEXT_MUST_BE_CALLED_BEFORE_REMOVE.toLocalizedString());
-// }
-
- this.dupLimit--;
- assert this.dupLimit >= 0 : this.dupLimit;
- if (this.current == null) {
- GfshResultsBag.this.numNulls = this.dupLimit;
- assert GfshResultsBag.this.numNulls >= 0 : GfshResultsBag.this.numNulls;
- }
- else {
- if (this.dupLimit > 0) {
- this.mapIteratorEntry.setValue(this.dupLimit);
- }
- else {
- this.mapIterator.remove();
- }
- }
- GfshResultsBag.this.size--;
- this.currentDup--;
- assert GfshResultsBag.this.size >= 0 : GfshResultsBag.this.size;
- assert this.currentDup >= 0 : this.currentDup;
- }
- }
-
- /** package visibility so ResultsCollectionWrapper can reference
- * it.
- * This SetView is serialized as a special case by a
- * ResultsCollectionWrapper.
- * Keith: Refactored to add consideration for LIMIT, April 1, 2009
- * @see ResultsCollectionWrapper#toData
- */
- class SetView extends AbstractSet {
-
- private int localLimit;
-
- SetView() {
- localLimit = GfshResultsBag.this.limit;
- }
-
- public Iterator iterator() {
- if (localLimit > -1) {
- return new LimitSetViewIterator();
- } else {
- return new SetViewIterator();
- }
- }
-
- @Override
- public boolean add(Object o) {
- if(contains(o)) {
- return false;
- }
- return GfshResultsBag.this.add(o);
- }
-
- @Override
- public void clear() {
- GfshResultsBag.this.clear();
- }
-
- @Override
- public int size() {
- int calculatedSize = GfshResultsBag.this.map.size() +
- (GfshResultsBag.this.numNulls > 0 ? 1 : 0);
- if (localLimit > -1) {
- return Math.min(localLimit, calculatedSize);
- }
- return calculatedSize;
- }
-
- @Override
- public boolean contains(Object o) {
- if (o == null) {
- return GfshResultsBag.this.numNulls > 0;
- }
- return GfshResultsBag.this.map.containsKey(o);
- }
-
- @Override
- public boolean isEmpty() {
- if(localLimit == 0) {
- return true;
- }
- if (GfshResultsBag.this.numNulls > 0) {
- return false;
- }
- return GfshResultsBag.this.map.isEmpty();
- }
-
- public class SetViewIterator implements Iterator {
- /** need to emit a null value if true */
- boolean emitNull = GfshResultsBag.this.numNulls > 0;
- final ObjectIterator<Object2IntMap.Entry<?>> it = GfshResultsBag.this.map.object2IntEntrySet().fastIterator();
- Object2IntMap.Entry<?> currEntry;
- boolean currentIsNull = false;
-
- public Object next() {
- if (this.emitNull) {
- this.emitNull = false;
- currentIsNull = true;
- return null;
- }
- currEntry = it.next();
- currentIsNull = false;
- return currEntry.getKey();
- }
-
- public boolean hasNext() {
- if (this.emitNull) {
- return true;
- }
- return it.hasNext();
- }
-
- public void remove() {
- if(currentIsNull) {
- GfshResultsBag.this.numNulls = 0;
- } else {
- it.remove();
- }
- }
- };
-
- class LimitSetViewIterator extends SetViewIterator {
- private int currPos = 0;
- @Override
- public Object next() {
- if (this.currPos == GfshResultsBag.SetView.this.localLimit) {
- throw new NoSuchElementException();
- }
- else {
- Object next = super.next();
- ++currPos;
- return next;
- }
- }
-
- @Override
- public boolean hasNext() {
- return (this.currPos < GfshResultsBag.SetView.this.localLimit)
- && super.hasNext();
- }
-
- @Override
- public void remove() {
- if (this.currPos == 0) {
- // next has not yet been called
- throw new IllegalStateException("next() must be called before remove()");
- }
- synchronized (GfshResultsBag.this.limitLock) {
- if(currentIsNull) {
- GfshResultsBag.this.limit -= GfshResultsBag.this.numNulls;
- GfshResultsBag.this.numNulls = 0;
- GfshResultsBag.SetView.this.localLimit--;
- } else {
- Object key = currEntry.getKey();
- int count = GfshResultsBag.this.map.removeInt(key);
- assert count != 0 : "Attempted to remove an element that was not in the map.";
- GfshResultsBag.this.limit -= count;
- GfshResultsBag.SetView.this.localLimit--;
- }
- }
- }
- }
- }
-
- /**
- * @author Asif
- *
- */
- protected class LimitResultsBagIterator extends GfshResultsBag.ResultsBagIterator {
- final private int localLimit;
-
- private int currPos = 0;
-
- /**
- *guarded by GfshResultsBag2.this.limitLock object
- */
- public LimitResultsBagIterator() {
- localLimit = GfshResultsBag.this.limit;
- }
-
- public boolean hasNext() {
- return this.currPos < this.localLimit;
- }
-
- public Object next() {
- if (this.currPos == this.localLimit) {
- throw new NoSuchElementException();
- }
- else {
- Object next = super.next();
- ++currPos;
- return next;
- }
-
- }
-
- public void remove() {
- if (this.currPos == 0) {
- // next has not yet been called
- throw new IllegalStateException("next() must be called before remove()");
- }
- synchronized (GfshResultsBag.this.limitLock) {
- super.remove();
- --GfshResultsBag.this.limit;
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/67085172/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/ObjectUtil.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/ObjectUtil.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/ObjectUtil.java
deleted file mode 100644
index 1dd66de..0000000
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/ObjectUtil.java
+++ /dev/null
@@ -1,164 +0,0 @@
-package com.gemstone.gemfire.internal.tools.gfsh.app.util;
-
-import java.lang.reflect.Method;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Map;
-
-import com.gemstone.gemfire.internal.tools.gfsh.app.Gfsh;
-import com.gemstone.gemfire.internal.tools.gfsh.app.misc.util.ReflectionUtil;
-
-public class ObjectUtil
-{
- public static Object updateObject(Gfsh gfsh,
- Map<String, Method> setterMap,
- Object obj,
- String setterMethodName,
- String value,
- SimpleDateFormat dateFormat, boolean isCsvFormat) throws Exception
- {
- Method setterMethod = setterMap.get(setterMethodName);
- if (setterMethod == null) {
- return obj;
- }
-
- Class types[] = setterMethod.getParameterTypes();
- Class arg = types[0];
- if (arg == byte.class || arg == Byte.class) {
- setterMethod.invoke(obj, Byte.parseByte(value));
- } else if (arg == char.class || arg == Character.class) {
- setterMethod.invoke(obj, value.charAt(0));
- } else if (arg == short.class || arg == Short.class) {
- setterMethod.invoke(obj, Short.parseShort(value));
- } else if (arg == int.class || arg == Integer.class) {
- setterMethod.invoke(obj, Integer.parseInt(value));
- } else if (arg == long.class || arg == Long.class) {
- setterMethod.invoke(obj, Long.parseLong(value));
- } else if (arg == float.class || arg == Float.class) {
- setterMethod.invoke(obj, Float.parseFloat(value));
- } else if (arg == double.class || arg == Double.class) {
- setterMethod.invoke(obj, Double.parseDouble(value));
- } else if (arg == Date.class) {
- Date date = dateFormat.parse(value);
- setterMethod.invoke(obj, date);
- } else if (arg == String.class) {
-
- if (isCsvFormat) {
- // Replace double quotes with single quotes
- value = value.replaceAll("\"\"", "\"");
-
- // if begins with a quote then remove it
- if (value.startsWith("\"")) {
- value = value.substring(1);
- }
- // if ends with a quote then remove it
- if (value.endsWith("\"")) {
- value = value.substring(0, value.length() - 1);
- }
- }
- setterMethod.invoke(obj, value);
- } else {
- gfsh.println("Error: unsupported type: " + setterMethod.getName() + "(" + arg.getName() + ")");
- }
- return obj;
- }
-
- public static Object getPrimitive(Gfsh gfsh, String value, boolean quoted) throws ParseException
- {
- if (quoted) {
- // string
- return value;
- }
-
-
- value = value.trim().toLowerCase();
- if (value.length() == 0) {
- return null;
- }
- char firstChar = value.charAt(0);
- if (!(firstChar == '.' || firstChar >= '0' && firstChar <= '9')) {
- // it's not number
- return null;
- }
-
- Object obj = null;
- if (value.endsWith("b")) {
- // byte
- obj = new Byte(value.substring(0, value.length() - 1));
- } else if (value.endsWith("c")) {
- // char
- obj = value.charAt(0);
- } else if (value.endsWith("s")) {
- // short
- obj = new Short(value.substring(0, value.length() - 1));
- } else if (value.endsWith("i")) {
- // int
- obj = new Integer(value.substring(0, value.length() - 1));
- } else if (value.endsWith("l")) {
- // long
- obj = new Long(value.substring(0, value.length() - 1));
- } else if (value.endsWith("f")) {
- // float
- obj = new Float(value.substring(0, value.length() - 1));
- } else if (value.endsWith("d")) {
- // double
- obj = new Double(value.substring(0, value.length() - 1));
- } else if (value.startsWith("to_date")) {
- obj = gfsh.getDate(value);
- } else {
- if (value.indexOf(".") != -1 || value.indexOf("e") != -1) {
- // use double by default
- obj = new Double(value);
- } else {
- // us int by default
- obj = new Integer(value);
- }
- }
-
- return obj;
- }
-
- public static Object updateObject(Gfsh gfsh,
- Map<String, Method> setterMap,
- Object obj,
- String tableColumnName,
- Object value) throws Exception
- {
- Method setterMethod = setterMap.get("set" + tableColumnName);
- if (setterMethod == null) {
- // TODO: need to search the map. column names can be case insensitive
- return obj;
- }
- setterMethod.invoke(obj, value);
-
- return obj;
- }
-
- public static Object getPrintableObject(Object object)
- {
- if (object == null) {
- return "null";
- }
- if (object instanceof String || object.getClass().isPrimitive() ||
- object.getClass() == Boolean.class ||
- object.getClass() == Byte.class ||
- object.getClass() == Character.class ||
- object.getClass() == Short.class ||
- object.getClass() == Integer.class ||
- object.getClass() == Long.class ||
- object.getClass() == Float.class ||
- object.getClass() == Double.class)
- {
- return object.toString();
-
- } else if (object instanceof Date) {
-
- return object.toString();
-
- } else {
- return ReflectionUtil.toStringGettersAnd(object);
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/67085172/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/OutputUtil.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/OutputUtil.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/OutputUtil.java
deleted file mode 100644
index 0f24d14..0000000
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/tools/gfsh/app/util/OutputUtil.java
+++ /dev/null
@@ -1,255 +0,0 @@
-package com.gemstone.gemfire.internal.tools.gfsh.app.util;
-
-import java.io.PrintWriter;
-import java.lang.reflect.Method;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-
-import com.gemstone.gemfire.internal.tools.gfsh.app.misc.util.ReflectionUtil;
-
-public class OutputUtil
-{
- public static final int TYPE_KEYS = 0;
- public static final int TYPE_VALUES = 1;
- public static final int TYPE_KEYS_VALUES = 2;
-
- public static final String TAG_COLUMN_SEPARATOR = "#|";
-
- public static final String TAG_KEY = "#%key";
- public static final String TAG_DATE_FORMAT = "#%date_format";
- public static final String TAG_VALUE = "#%value";
- public static final String TAG_VALUE_KEY = "#%value_key";
-
-
- public static void printEntries(PrintWriter writer, Map map,
- String fieldTerminator, String rowTerminator,
- int firstRow, int lastRow,
- int printType, boolean printHeader,
- SimpleDateFormat dateFormat, String valueKeyFieldName)
- {
- if (map == null) {
- System.out.println("Error: map is null");
- return;
- }
-
- // Get all getters
- Set<Map.Entry> entrySet = map.entrySet();
- Object key = null;
- Object value = null;
- Method keyGetters[] = null;
- Method valueGetters[] = null;
- for (Entry entry : entrySet) {
- key = entry.getKey();
- value = entry.getValue();
- keyGetters = ReflectionUtil.getAllGetters(key.getClass());
- if (value == null) {
- valueGetters = new Method[0];
- } else {
- valueGetters = ReflectionUtil.getAllGetters(value.getClass());
- }
- break;
- }
-
- if (value == null) {
- System.out.println("Error: value is null");
- return;
- }
-
- switch (printType) {
- case TYPE_KEYS:
- // Print keys
- if (printHeader) {
- writer.print(TAG_KEY + " " + key.getClass().getName());
- writer.print(rowTerminator);
- writer.print(TAG_DATE_FORMAT + " " + dateFormat.toPattern());
- writer.print(rowTerminator);
- printHeader(writer, key, keyGetters, fieldTerminator, rowTerminator);
- }
- for (Entry entry : entrySet) {
- key = entry.getKey();
- printObject(writer, keyGetters, key, fieldTerminator, rowTerminator, dateFormat);
- }
- break;
- case TYPE_VALUES:
- // Print values
- if (printHeader) {
- if (value != null) {
- writer.print(TAG_VALUE + " " + value.getClass().getName());
- }
- writer.print(rowTerminator);
- writer.print(TAG_VALUE_KEY + " " + valueKeyFieldName);
- writer.print(rowTerminator);
- writer.print(TAG_DATE_FORMAT + " " + dateFormat.toPattern());
- writer.print(rowTerminator);
- printHeader(writer, value, valueGetters, fieldTerminator, rowTerminator);
- }
- for (Entry entry : entrySet) {
- value = entry.getValue();
- printObject(writer, valueGetters, value, fieldTerminator, rowTerminator, dateFormat);
- }
- break;
- case TYPE_KEYS_VALUES:
- default:
- // Print keys and values
- if (printHeader) {
- writer.print(TAG_KEY + " " + key.getClass().getName());
- writer.print(rowTerminator);
- if (value != null) {
- writer.print(TAG_VALUE + " " + value.getClass().getName());
- }
- writer.print(rowTerminator);
- writer.print(TAG_DATE_FORMAT + " " + dateFormat.toPattern());
- writer.print(rowTerminator);
- printHeader(writer, key, keyGetters, fieldTerminator);
- writer.print(",");
- printHeader(writer, value, valueGetters, fieldTerminator, rowTerminator);
- }
- for (Entry entry : entrySet) {
- key = entry.getKey();
- value = entry.getValue();
- printObject(writer, keyGetters, key, fieldTerminator, dateFormat);
- writer.print(",");
- printObject(writer, valueGetters, value, fieldTerminator, dateFormat);
- writer.print(rowTerminator);
- }
- break;
-
- }
- }
-
- private static void printHeader(PrintWriter writer, Object object, Method methods[],
- String fieldTerminator, String rowTerminator)
- {
- printHeader(writer, object, methods, fieldTerminator);
- writer.print(rowTerminator);
- }
-
- private static void printHeader(PrintWriter writer, Object object, Method methods[],
- String fieldTerminator)
- {
- writer.print(TAG_COLUMN_SEPARATOR);
-
- if (object == null || object instanceof String || object.getClass().isPrimitive() ||
- object.getClass() == Boolean.class ||
- object.getClass() == Byte.class ||
- object.getClass() == Character.class ||
- object.getClass() == Short.class ||
- object.getClass() == Integer.class ||
- object.getClass() == Long.class ||
- object.getClass() == Float.class ||
- object.getClass() == Double.class ||
- object instanceof Date)
- {
- writer.print("Value");
- } else {
- for (int i = 0; i < methods.length; i++) {
- String name = methods[i].getName().substring(3);
- writer.print(name);
- if (i < methods.length - 1) {
- writer.print(fieldTerminator);
- }
- }
- }
- }
-
- public static void printObject(PrintWriter writer,
- Method methods[],
- Object object,
- String fieldTerminator, String rowTerminator,
- SimpleDateFormat dateFormat)
- {
- printObject(writer, methods, object, fieldTerminator, dateFormat);
- writer.print(rowTerminator);
- }
-
- private static void printObject(PrintWriter writer,
- Method methods[],
- Object object,
- String fieldTerminator,
- SimpleDateFormat dateFormat)
- {
- if (object == null) {
- writer.print("null");
- } else if (object instanceof String ) { //FindBugs (Below) - Possible null pointer dereference of object
- String value = object.toString();
-
- // For each quote add matching quote
- value = value.replaceAll("\"", "\"\"");
-
- // If contains a quote then enclose it with quotes
- if (value.indexOf("\"") != -1) {
- value = "\"" + value;
- value = value + "\"";
- } else {
-
- // If begins with a " then prepend a ".
- if (value.startsWith("\"")) {
- value = "\"" + value;
- }
-
- // If ends with a " then end it with a ".
- if (value.endsWith("\"")) {
- value = value + "\"";
- }
- }
- writer.print(value);
-
- } else if(object.getClass().isPrimitive() ||
- object.getClass() == Boolean.class ||
- object.getClass() == Byte.class ||
- object.getClass() == Character.class ||
- object.getClass() == Short.class ||
- object.getClass() == Integer.class ||
- object.getClass() == Long.class ||
- object.getClass() == Float.class ||
- object.getClass() == Double.class)
- {
- writer.print(object.toString());
-
- } else if (object instanceof Date) {
-
- writer.print(dateFormat.format((Date)object));
-
- } else if (methods != null) {
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
- String name = method.getName();
- try {
- Object value = method.invoke(object, (Object[])null);
- value = getPrintableValue(value);
- printObject(writer, null, value, fieldTerminator, dateFormat);
- if (i < methods.length - 1) {
- writer.print(fieldTerminator);
- }
- } catch (Exception ex) {
- }
- }
- }
- }
-
- private static Object getPrintableValue(Object value)
- {
- if (value instanceof Byte) {
- value = ((Byte) value).toString();
- } else if (value instanceof byte[]) {
- value = "[B " + ((byte[])value).length;
- } else if (value instanceof boolean[]) {
- value = "[Z " + ((boolean[])value).length;
- } else if (value instanceof short[]) {
- value = "[S " + ((short[])value).length;
- } else if (value instanceof int[]) {
- value = "[I " + ((int[])value).length;
- } else if (value instanceof long[]) {
- value = "[J " + ((long[])value).length;
- } else if (value instanceof float[]) {
- value = "[F " + ((float[])value).length;
- } else if (value instanceof double[]) {
- value = "[D " + ((double[])value).length;
- }
- return value;
- }
-
-}