001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.lang.reflect.Constructor;
028: import java.text.SimpleDateFormat;
029: import java.util.Date;
030:
031: import com.db4o.Db4o;
032: import com.db4o.Deploy;
033: import com.db4o.reflect.Reflector;
034: import com.db4o.reflect.generic.GenericReflector;
035: import com.db4o.reflect.jdk.JdkReflector;
036:
037: /**
038: *
039: * package and class name are hard-referenced in JavaOnly#jdk()
040: *
041: * TODO: may need to use this on instead of JDK on .NET. Check!
042: * @sharpen.ignore
043: */
044: public class JDKReflect extends JDK {
045: Class constructorClass() {
046: return Constructor.class;
047: }
048:
049: Object deserialize(byte[] bytes) {
050: try {
051: return new ObjectInputStream(
052: new ByteArrayInputStream(bytes)).readObject();
053: } catch (Exception e) {
054: }
055: return null;
056: }
057:
058: String format(Date date, boolean showTime) {
059: String fmt = "yyyy-MM-dd";
060: if (showTime) {
061: fmt += " HH:mm:ss";
062: }
063: return new SimpleDateFormat(fmt).format(date);
064: }
065:
066: public Class loadClass(String className, Object loader)
067: throws ClassNotFoundException {
068: return (loader != null ? ((ClassLoader) loader)
069: .loadClass(className) : Class.forName(className));
070: }
071:
072: /**
073: * use for system classes only, since not ClassLoader
074: * or Reflector-aware
075: */
076: final boolean methodIsAvailable(String className,
077: String methodName, Class[] params) {
078: return Reflection4.getMethod(className, methodName, params) != null;
079: }
080:
081: public void registerCollections(GenericReflector reflector) {
082: if (!Deploy.csharp) {
083: reflector.registerCollection(java.util.Vector.class);
084: reflector.registerCollection(java.util.Hashtable.class);
085: reflector.registerCollectionUpdateDepth(
086: java.util.Hashtable.class, 3);
087: }
088: }
089:
090: byte[] serialize(Object obj) throws Exception {
091: ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
092: new ObjectOutputStream(byteStream).writeObject(obj);
093: return byteStream.toByteArray();
094: }
095:
096: public Reflector createReflector(Object classLoader) {
097: if (classLoader == null) {
098:
099: // FIXME: The new reflector does not like the ContextCloader at all.
100: // Resolve hierarchies.
101: // classLoader=getContextClassLoader();
102:
103: // if (cl == null || classloaderName.indexOf("eclipse") >= 0) {
104: classLoader = Db4o.class.getClassLoader();
105: //
106: }
107: return new JdkReflector((ClassLoader) classLoader);
108: }
109:
110: public Reflector reflectorForType(Class clazz) {
111: return createReflector(clazz.getClassLoader());
112: }
113: }
|