Skip to content

Commit 49dd05e

Browse files
author
Ryan
committed
was DBCallback... now default impl.
1 parent 61ae33c commit 49dd05e

File tree

1 file changed

+127
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)