001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.util;
020:
021: import java.io.DataInput;
022: import java.io.DataOutput;
023: import java.io.IOException;
024: import java.lang.reflect.Array;
025: import java.lang.reflect.Field;
026: import java.nio.ByteBuffer;
027: import java.rmi.registry.LocateRegistry;
028: import java.rmi.registry.Registry;
029: import java.util.logging.Handler;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: import com.mobixess.jodb.core.JODBConstants;
034: import com.mobixess.jodb.util.logging.SimpleConsoleHandler;
035: import com.mobixess.jodb.util.logging.SimpleFileFormatter;
036:
037: //TODO move primitive methods to corresponding util class
038: public class Utils {
039:
040: private static boolean _logInit;
041:
042: private static boolean _regInitialized = false;
043:
044: public static void ensureRegistryExist() {
045: if (_regInitialized)
046: return;
047: try {
048: LocateRegistry
049: .createRegistry(JODBConstants.DEFAULT_RMI_PORT);
050: } catch (Exception e) {
051: e.printStackTrace();
052: }
053: _regInitialized = true;
054: }
055:
056: public static void fatalError(Throwable th) {
057: th.printStackTrace();
058: System.exit(-1);
059: }
060:
061: public static void writePrimitive(Object obj, Field field,
062: DataOutput out) throws IllegalArgumentException,
063: IOException, IllegalAccessException {
064: String type = field.getType().getName();
065: if (type.equals("byte")) {
066: out.writeByte(field.getByte(obj));
067: } else if (type.equals("int")) {
068: out.writeInt(field.getInt(obj));
069: } else if (type.equals("short")) {
070: out.writeShort(field.getShort(obj));
071: } else if (type.equals("boolean")) {
072: out.writeBoolean(field.getBoolean(obj));
073: } else if (type.equals("long")) {
074: out.writeLong(field.getLong(obj));
075: } else if (type.equals("char")) {
076: out.writeChar(field.getChar(obj));
077: } else if (type.equals("float")) {
078: out.writeFloat(field.getFloat(obj));
079: } else if (type.equals("double")) {
080: out.writeDouble(field.getDouble(obj));
081: }
082: }
083:
084: public static void writePrimitiveArray(Object array,
085: Class arrayType, int from, int to, DataOutput out)
086: throws IllegalArgumentException, IOException,
087: IllegalAccessException {
088: String type = arrayType.getName();
089: if (type.equals("byte")) {
090: for (int i = from; i < to; i++) {
091: out.writeByte(Array.getByte(array, i));
092: }
093: } else if (type.equals("int")) {
094: for (int i = from; i < to; i++) {
095: out.writeInt(Array.getInt(array, i));
096: }
097: } else if (type.equals("short")) {
098: for (int i = from; i < to; i++) {
099: out.writeShort(Array.getShort(array, i));
100: }
101: } else if (type.equals("boolean")) {
102: for (int i = from; i < to; i++) {
103: out.writeBoolean(Array.getBoolean(array, i));
104: }
105: } else if (type.equals("long")) {
106: for (int i = from; i < to; i++) {
107: out.writeLong(Array.getLong(array, i));
108: }
109: } else if (type.equals("char")) {
110: for (int i = from; i < to; i++) {
111: out.writeChar(Array.getChar(array, i));
112: }
113: } else if (type.equals("float")) {
114: for (int i = from; i < to; i++) {
115: out.writeFloat(Array.getFloat(array, i));
116: }
117: } else if (type.equals("double")) {
118: for (int i = from; i < to; i++) {
119: out.writeDouble(Array.getDouble(array, i));
120: }
121: }
122: }
123:
124: public static boolean readPrimitiveAndCompare(Object obj,
125: Field field, DataInput in) throws IOException {
126: String type = field.getType().getName();
127: try {
128: if (type.equals("byte")) {
129: return in.readByte() == field.getByte(obj);
130: } else if (type.equals("int")) {
131: return in.readInt() == field.getInt(obj);
132: } else if (type.equals("short")) {
133: return in.readShort() == field.getShort(obj);
134: } else if (type.equals("boolean")) {
135: return in.readBoolean() == field.getBoolean(obj);
136: } else if (type.equals("long")) {
137: return in.readLong() == field.getLong(obj);
138: } else if (type.equals("char")) {
139: return in.readChar() == field.getChar(obj);
140: } else if (type.equals("float")) {
141: return in.readFloat() == field.getFloat(obj);
142: } else if (type.equals("double")) {
143: return in.readDouble() == field.getDouble(obj);
144: }
145: } catch (IllegalArgumentException e) {
146: e.printStackTrace();
147: throw new IOException(e.getMessage());
148: } catch (IllegalAccessException e) {
149: e.printStackTrace();
150: throw new IOException(e.getMessage());
151: }
152: throw new IOException("unknown type "
153: + field.getType().getName());
154: }
155:
156: public static Object readPrimitive(String type, DataInput in)
157: throws IOException {
158: //TODO rework based on enumerated types
159: if (type.equals("byte")) {
160: return in.readByte();
161: } else if (type.equals("int")) {
162: return in.readInt();
163: } else if (type.equals("short")) {
164: return in.readShort();
165: } else if (type.equals("boolean")) {
166: return in.readBoolean();
167: } else if (type.equals("long")) {
168: return in.readLong();
169: } else if (type.equals("char")) {
170: return in.readChar();
171: } else if (type.equals("float")) {
172: return in.readFloat();
173: } else if (type.equals("double")) {
174: return in.readDouble();
175: }
176: throw new IOException("unknown primitive type " + type);
177: }
178:
179: /**
180: * Bob Jenkins' one-at-a-time hash.
181: * See http://burtleburtle.net/bob/hash/doobs.html
182: *
183: * @param key array containing data to be hashed
184: * @param offset the first element of the array to be hashed
185: * @param len data endpoint (excluded)
186: *
187: * @return a 32-bit hash number
188: */
189: public final static int one_at_a_time(byte[] key, int offset,
190: int len) {
191: int hash;
192: int i;
193:
194: for (hash = 0, i = offset; i < len; ++i) {
195: hash += (((int) key[i]) & 0xFF);
196: hash += (hash << 10);
197: hash ^= (hash >> 6);
198: }
199:
200: hash += (hash << 3);
201: hash ^= (hash >> 11);
202: hash += (hash << 15);
203:
204: return hash;
205: }
206:
207: public static boolean primitiveEquals(Field field, Object obj,
208: long value) throws IOException {
209: String type = field.getType().getName();
210: try {
211: if (type.equals("byte")) {
212: return value == field.getByte(obj);
213: } else if (type.equals("int")) {
214: return value == field.getInt(obj);
215: } else if (type.equals("short")) {
216: return value == field.getShort(obj);
217: } else if (type.equals("boolean")) {
218: return (value == 0 ? false : true) == field
219: .getBoolean(obj);
220: } else if (type.equals("long")) {
221: return value == field.getLong(obj);
222: } else if (type.equals("char")) {
223: return value == field.getChar(obj);
224: } else if (type.equals("float")) {
225: return value == field.getFloat(obj);
226: } else if (type.equals("double")) {
227: return value == field.getDouble(obj);
228: } else {
229: throw new IOException("unknown type "
230: + field.getType().getName());
231: }
232: } catch (IllegalArgumentException e) {
233: e.printStackTrace();
234: throw new IOException(e.getMessage());
235: } catch (IllegalAccessException e) {
236: e.printStackTrace();
237: throw new IOException(e.getMessage());
238: }
239: }
240:
241: public static int comparePrimitive(Field field, Object obj,
242: ByteBuffer value) throws IOException {
243: String type = field.getType().getName();
244: try {
245: if (type.equals("byte")) {
246: return field.getByte(obj) - value.get();
247: } else if (type.equals("int")) {
248: return field.getInt(obj) - value.getInt();
249: } else if (type.equals("short")) {
250: return field.getShort(obj) - value.getShort();
251: } else if (type.equals("boolean")) {
252: boolean val1 = field.getBoolean(obj);
253: boolean val2 = value.get() != 0;
254: return val1 == val2 ? 0 : (val1 ? 1 : -1);
255: } else if (type.equals("long")) {
256: long val1 = field.getLong(obj);
257: long val2 = value.getLong();
258: return (val1 < val2 ? -1 : (val1 == val2 ? 0 : 1));
259: } else if (type.equals("char")) {
260: return field.getChar(obj) - value.getChar();
261: } else if (type.equals("float")) {
262: float val1 = field.getFloat(obj);
263: float val2 = value.getFloat();
264: return Float.compare(val1, val2);
265: } else if (type.equals("double")) {
266: double val1 = field.getDouble(obj);
267: double val2 = value.getDouble();
268: return Double.compare(val1, val2);
269: } else {
270: throw new IOException("unknown type "
271: + field.getType().getName());
272: }
273: } catch (IllegalArgumentException e) {
274: e.printStackTrace();
275: throw new IOException(e.getMessage());
276: } catch (IllegalAccessException e) {
277: e.printStackTrace();
278: throw new IOException(e.getMessage());
279: }
280: }
281:
282: public static void skipPrimitive(Field field, DataInput in)
283: throws IOException {
284: String type = field.getType().getName();
285: if (type.equals("byte")) {
286: in.readByte();
287: } else if (type.equals("int")) {
288: in.readInt();
289: } else if (type.equals("short")) {
290: in.readShort();
291: } else if (type.equals("boolean")) {
292: in.readBoolean();
293: } else if (type.equals("long")) {
294: in.readLong();
295: } else if (type.equals("char")) {
296: in.readChar();
297: } else if (type.equals("float")) {
298: in.readFloat();
299: } else if (type.equals("double")) {
300: in.readDouble();
301: } else {
302: throw new IOException("unknown type "
303: + field.getType().getName());
304: }
305: }
306:
307: public static Logger getLogger(String name) {
308: if (!_logInit) {
309: Logger main = Logger.getLogger("");
310: Handler[] handlers = main.getHandlers();
311: for (int i = 0; i < handlers.length; i++) {
312: main.removeHandler(handlers[i]);
313: }
314: synchronized (main) {
315: if (!_logInit) {
316: main.setLevel(Level.INFO);
317: try {
318: SimpleConsoleHandler mHandler = new SimpleConsoleHandler();
319: main.addHandler(mHandler);
320: mHandler
321: .setFormatter(new SimpleFileFormatter());
322: mHandler.setLevel(Level.FINEST);
323: } catch (Exception e) {
324: e.printStackTrace();
325: }
326: _logInit = true;
327: }
328: }
329: }
330: return Logger.getLogger(name);
331: }
332: }
|