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.*;
024: import java.lang.reflect.*;
025: import java.util.*;
026:
027: import com.db4o.*;
028: import com.db4o.foundation.*;
029:
030: class JDK_1_4 extends JDK_1_3 {
031:
032: private Hashtable fileLocks;
033:
034: private Object _reflectionFactory;
035: private Constructor _objectConstructor;
036: private Method _factoryMethod;
037:
038: synchronized void lockFile(String path, Object file) {
039: // Conversion to canonical is already done by RandomAccessFileAdapter, but it's probably
040: // not safe to rely on that for other file-based adapters.
041:
042: String canonicalPath;
043: try {
044: canonicalPath = new File(path).getCanonicalPath();
045: } catch (IOException e) {
046: throw new Db4oIOException(e);
047: }
048:
049: if (fileLocks == null) {
050: fileLocks = new Hashtable();
051: }
052: if (fileLocks.containsKey(canonicalPath)) {
053: throw new DatabaseFileLockedException(canonicalPath);
054: }
055:
056: Object lock = null;
057: Object channel = Reflection4.invoke(file, "getChannel");
058: lock = Reflection4.invoke(channel, "tryLock");
059: if (lock == null) {
060: throw new DatabaseFileLockedException(canonicalPath);
061: }
062: fileLocks.put(canonicalPath, lock);
063: }
064:
065: synchronized void unlockFile(String path, Object file) {
066: if (fileLocks == null) {
067: return;
068: }
069: Object fl = fileLocks.get(path);
070: if (fl == null) {
071: return;
072: }
073: Reflection4.invoke("java.nio.channels.FileLock", "release",
074: null, null, fl);
075: fileLocks.remove(path);
076: }
077:
078: public Constructor serializableConstructor(Class clazz) {
079: if (_reflectionFactory == null) {
080: if (!initSerializableConstructor()) {
081: Platform4.callConstructorCheck = TernaryBool.YES;
082: return null;
083: }
084: }
085: return (Constructor) Reflection4.invoke(new Object[] { clazz,
086: _objectConstructor }, _reflectionFactory,
087: _factoryMethod);
088: }
089:
090: private boolean initSerializableConstructor() {
091: try {
092: _reflectionFactory = Reflection4.invoke(
093: Platform4.REFLECTIONFACTORY,
094: "getReflectionFactory", null, null, null);
095: _factoryMethod = Reflection4.getMethod(
096: Platform4.REFLECTIONFACTORY,
097: "newConstructorForSerialization", new Class[] {
098: Class.class, Constructor.class });
099: if (_factoryMethod == null) {
100: return false;
101: }
102: } catch (ReflectException e) {
103: return false;
104: }
105:
106: try {
107: _objectConstructor = Object.class
108: .getDeclaredConstructor((Class[]) null);
109: return true;
110: } catch (Exception e) {
111: if (Debug.atHome) {
112: e.printStackTrace();
113: }
114: return false;
115: }
116: }
117:
118: public int ver() {
119: return 4;
120: }
121:
122: }
|