/src/main/com/mongodb/DefaultDBCallback.java

https://github.com/dixcychaitanya/mongo-java-driver · Java · 143 lines · 98 code · 23 blank · 22 comment · 20 complexity · 06463253d145d93c5e8b11db56cba9e6 MD5 · raw file

  1. // DBCallback.java
  2. /**
  3. * Copyright (C) 2008 10gen Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.mongodb;
  18. // Bson
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import org.bson.BSONObject;
  23. import org.bson.BasicBSONCallback;
  24. import org.bson.types.ObjectId;
  25. /**
  26. * This class overrides BasicBSONCallback to implement some extra features specific to the Database.
  27. * For example DBRef type.
  28. * @author antoine
  29. */
  30. public class DefaultDBCallback extends BasicBSONCallback implements DBCallback {
  31. static class DefaultFactory implements DBCallbackFactory {
  32. @Override
  33. public DBCallback create( DBCollection collection ){
  34. return new DefaultDBCallback( collection );
  35. }
  36. }
  37. public static DBCallbackFactory FACTORY = new DefaultFactory();
  38. public DefaultDBCallback( DBCollection coll ){
  39. _collection = coll;
  40. _db = _collection == null ? null : _collection.getDB();
  41. }
  42. @Override
  43. @SuppressWarnings("deprecation")
  44. public void gotDBRef( String name , String ns , ObjectId id ){
  45. if ( id.equals( Bytes.COLLECTION_REF_ID ) )
  46. cur().put( name , _collection );
  47. else
  48. cur().put( name , new DBPointer( (DBObject)cur() , name , _db , ns , id ) );
  49. }
  50. @Override
  51. public void objectStart(boolean array, String name){
  52. _lastName = name;
  53. super.objectStart( array , name );
  54. }
  55. @Override
  56. public Object objectDone(){
  57. BSONObject o = (BSONObject)super.objectDone();
  58. if ( ! ( o instanceof List ) &&
  59. o.containsField( "$ref" ) &&
  60. o.containsField( "$id" ) ){
  61. return cur().put( _lastName , new DBRef( _db, o ) );
  62. }
  63. return o;
  64. }
  65. @Override
  66. public BSONObject create(){
  67. return _create( null );
  68. }
  69. @Override
  70. public BSONObject create( boolean array , List<String> path ){
  71. if ( array )
  72. return new BasicDBList();
  73. return _create( path );
  74. }
  75. private DBObject _create( List<String> path ){
  76. Class c = null;
  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. if ( c != null ){
  92. try {
  93. return (DBObject)c.newInstance();
  94. }
  95. catch ( InstantiationException ie ){
  96. LOGGER.log( Level.FINE , "can't create a: " + c , ie );
  97. throw new MongoInternalException( "can't instantiate a : " + c , ie );
  98. }
  99. catch ( IllegalAccessException iae ){
  100. LOGGER.log( Level.FINE , "can't create a: " + c , iae );
  101. throw new MongoInternalException( "can't instantiate a : " + c , iae );
  102. }
  103. }
  104. if ( _collection != null && _collection._name.equals( "$cmd" ) )
  105. return new CommandResult();
  106. return new BasicDBObject();
  107. }
  108. DBObject dbget(){
  109. return (DBObject)get();
  110. }
  111. @Override
  112. public void reset(){
  113. _lastName = null;
  114. super.reset();
  115. }
  116. private String _lastName;
  117. final DBCollection _collection;
  118. final DB _db;
  119. static final Logger LOGGER = Logger.getLogger( "com.mongo.DECODING" );
  120. }