KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > JDK_1_4


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import java.io.*;
24 import java.lang.reflect.*;
25 import java.util.*;
26
27 import com.db4o.ext.*;
28
29 class JDK_1_4 extends JDK_1_3 {
30     
31     private Hashtable fileLocks;
32     
33     private Object JavaDoc reflectionFactory;
34     private Constructor objectConstructor;
35     private Method factoryMethod;
36     
37     synchronized void lockFile(Object JavaDoc file) {
38         Object JavaDoc channel = invoke(file, "getChannel", null, null);
39         Object JavaDoc fl = invoke(channel, "tryLock", null, null);
40         if(fl == null){
41             throw new DatabaseFileLockedException();
42         }
43         if(fileLocks == null){
44             fileLocks = new Hashtable();
45         }
46         fileLocks.put(file, fl);
47     }
48     
49     synchronized void unlockFile(Object JavaDoc file) {
50         if(fileLocks != null){
51             Object JavaDoc fl = fileLocks.get(file);
52             if(fl != null){
53                 invoke(fl, "release", null, null);
54                 fileLocks.remove(file);
55             }
56         }
57     }
58     
59     public Constructor serializableConstructor(Class JavaDoc clazz){
60         if(reflectionFactory == null){
61             if(! initSerializableConstructor()){
62                 Platform4.callConstructorCheck = YapConst.YES;
63                 return null;
64             }
65         }
66         try{
67             return (Constructor) factoryMethod.invoke(reflectionFactory, new Object JavaDoc[]{clazz, objectConstructor});
68         }catch(Exception JavaDoc e){
69         }
70         return null;
71     }
72     
73     boolean initSerializableConstructor(){
74         reflectionFactory = invoke(Platform4.REFLECTIONFACTORY, "getReflectionFactory", null,null, null);
75         if(reflectionFactory == null){
76             return false;
77         }
78         factoryMethod = getMethod(Platform4.REFLECTIONFACTORY, "newConstructorForSerialization", new Class JavaDoc[]{Class JavaDoc.class, Constructor.class});
79         if(factoryMethod == null){
80             return false;
81         }
82         try{
83             objectConstructor = Object JavaDoc.class.getDeclaredConstructor((Class JavaDoc[])null);
84         }catch(Exception JavaDoc e){
85             e.printStackTrace();
86         }
87         return true;
88     }
89     
90     
91     
92     public int ver(){
93         return 4;
94     }
95     
96 }
97
Popular Tags