|
2 | 2 |
|
3 | 3 | package com.mongodb;
|
4 | 4 |
|
5 |
| -import java.util.*; |
6 |
| -import java.util.logging.*; |
7 | 5 |
|
8 | 6 | import org.bson.*;
|
9 |
| -import org.bson.types.*; |
10 | 7 |
|
11 | 8 | /**
|
12 |
| - * This class overrides BasicBSONCallback to implement some extra features specific to the Database. |
13 |
| - * For example DBRef type. |
14 |
| - * @author antoine |
| 9 | + * The DB callback interface. |
15 | 10 | */
|
16 |
| -public class DBCallback extends BasicBSONCallback { |
| 11 | +public interface DBCallback extends BSONCallback { |
17 | 12 |
|
18 |
| - public static interface Factory { |
19 |
| - public DBCallback create( DBCollection collection ); |
20 |
| - } |
21 |
| - |
22 |
| - static class DefaultFactory implements Factory { |
23 |
| - @Override |
24 |
| - public DBCallback create( DBCollection collection ){ |
25 |
| - return new DBCallback( collection ); |
26 |
| - } |
27 |
| - } |
28 |
| - |
29 |
| - public static Factory FACTORY = new DefaultFactory(); |
30 |
| - |
31 |
| - public DBCallback( DBCollection coll ){ |
32 |
| - _collection = coll; |
33 |
| - _db = _collection == null ? null : _collection.getDB(); |
34 |
| - } |
35 |
| - |
36 |
| - @Override |
37 |
| - @SuppressWarnings("deprecation") |
38 |
| - public void gotDBRef( String name , String ns , ObjectId id ){ |
39 |
| - if ( id.equals( Bytes.COLLECTION_REF_ID ) ) |
40 |
| - cur().put( name , _collection ); |
41 |
| - else |
42 |
| - cur().put( name , new DBPointer( (DBObject)cur() , name , _db , ns , id ) ); |
43 |
| - } |
44 |
| - |
45 |
| - @Override |
46 |
| - public void objectStart(boolean array, String name){ |
47 |
| - _lastName = name; |
48 |
| - super.objectStart( array , name ); |
49 |
| - } |
50 |
| - |
51 |
| - @Override |
52 |
| - public Object objectDone(){ |
53 |
| - BSONObject o = (BSONObject)super.objectDone(); |
54 |
| - if ( ! ( o instanceof List ) && |
55 |
| - o.containsField( "$ref" ) && |
56 |
| - o.containsField( "$id" ) ){ |
57 |
| - return cur().put( _lastName , new DBRef( _db, o ) ); |
58 |
| - } |
59 |
| - |
60 |
| - return o; |
61 |
| - } |
62 |
| - |
63 |
| - @Override |
64 |
| - public BSONObject create(){ |
65 |
| - return _create( null ); |
66 |
| - } |
67 |
| - |
68 |
| - @Override |
69 |
| - public BSONObject create( boolean array , List<String> path ){ |
70 |
| - if ( array ) |
71 |
| - return new BasicDBList(); |
72 |
| - return _create( path ); |
73 |
| - } |
74 |
| - |
75 |
| - private DBObject _create( List<String> path ){ |
76 |
| - |
77 |
| - Class c = null; |
78 |
| - |
79 |
| - if ( _collection != null && _collection._objectClass != null){ |
80 |
| - if ( path == null || path.size() == 0 ){ |
81 |
| - c = _collection._objectClass; |
82 |
| - } |
83 |
| - else { |
84 |
| - StringBuilder buf = new StringBuilder(); |
85 |
| - for ( int i=0; i<path.size(); i++ ){ |
86 |
| - if ( i > 0 ) |
87 |
| - buf.append("."); |
88 |
| - buf.append( path.get(i) ); |
89 |
| - } |
90 |
| - c = _collection.getInternalClass( buf.toString() ); |
91 |
| - } |
92 |
| - |
93 |
| - } |
94 |
| - |
95 |
| - if ( c != null ){ |
96 |
| - try { |
97 |
| - return (DBObject)c.newInstance(); |
98 |
| - } |
99 |
| - catch ( InstantiationException ie ){ |
100 |
| - LOGGER.log( Level.FINE , "can't create a: " + c , ie ); |
101 |
| - throw new MongoInternalException( "can't instantiate a : " + c , ie ); |
102 |
| - } |
103 |
| - catch ( IllegalAccessException iae ){ |
104 |
| - LOGGER.log( Level.FINE , "can't create a: " + c , iae ); |
105 |
| - throw new MongoInternalException( "can't instantiate a : " + c , iae ); |
106 |
| - } |
107 |
| - } |
108 |
| - |
109 |
| - if ( _collection != null && _collection._name.equals( "$cmd" ) ) |
110 |
| - return new CommandResult(); |
111 |
| - return new BasicDBObject(); |
112 |
| - } |
113 |
| - |
114 |
| - DBObject dbget(){ |
115 |
| - DBObject o = (DBObject)get(); |
116 |
| - return o; |
117 |
| - } |
118 |
| - |
119 |
| - @Override |
120 |
| - public void reset(){ |
121 |
| - _lastName = null; |
122 |
| - super.reset(); |
123 |
| - } |
124 |
| - |
125 |
| - private String _lastName; |
126 |
| - final DBCollection _collection; |
127 |
| - final DB _db; |
128 |
| - static final Logger LOGGER = Logger.getLogger( "com.mongo.DECODING" ); |
129 | 13 | }
|
| 14 | + |
0 commit comments