incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1512146 - in /sling/trunk/contrib/extensions/mongodb/src/main: java/org/apache/sling/mongodb/impl/ resources/OSGI-INF/metatype/
Date Fri, 09 Aug 2013 06:13:24 GMT
Author: cziegeler
Date: Fri Aug  9 06:13:24 2013
New Revision: 1512146

URL: http://svn.apache.org/r1512146
Log:
SLING-3002 : Make the Mongo Resource Provider easier to subclass. Apply patch from Dan McWeeney

Modified:
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java
    sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
    sling/trunk/contrib/extensions/mongodb/src/main/resources/OSGI-INF/metatype/metatype.properties

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java?rev=1512146&r1=1512145&r2=1512146&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ChangeableValueMap.java
Fri Aug  9 06:13:24 2013
@@ -19,6 +19,7 @@ package org.apache.sling.mongodb.impl;
 import java.util.Map;
 
 import org.apache.sling.api.resource.ModifiableValueMap;
+import org.apache.sling.api.resource.Resource;
 
 public class ChangeableValueMap
     extends ReadableValueMap
@@ -79,4 +80,8 @@ public class ChangeableValueMap
         }
         return result;
     }
+    
+    protected MongoDBResource getResource() {
+        return this.resource;
+    }
 }

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java?rev=1512146&r1=1512145&r2=1512146&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBContext.java
Fri Aug  9 06:13:24 2013
@@ -116,5 +116,5 @@ public class MongoDBContext {
         props.put("event.distribute", "");
         final Event event = new Event(SlingConstants.TOPIC_RESOURCE_CHANGED, props);
         this.eventAdmin.postEvent(event);
-    }
+    }    
 }

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java?rev=1512146&r1=1512145&r2=1512146&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResource.java
Fri Aug  9 06:13:24 2013
@@ -38,7 +38,7 @@ public class MongoDBResource extends Abs
     private final ResourceResolver resourceResolver;
 
     /** Metadata. */
-    private final ResourceMetadata metadata = new ResourceMetadata();
+    protected ResourceMetadata metadata = new ResourceMetadata();
 
     /** The db object. */
     private DBObject dbObject;
@@ -136,7 +136,7 @@ public class MongoDBResource extends Abs
 
     @Override
     public String toString() {
-        return "MongoDBResource [resourcePath=" + resourcePath + ", dbPath=" + this.dbObject.get(MongoDBResourceProvider.PROP_PATH)
+ ", collection=" + collection
+        return "MongoDBResource [resourcePath=" + resourcePath + ", dbPath=" + this.dbObject.get(provider.getPROP_PATH())
+ ", collection=" + collection
                         + ", resourceResolver=" + resourceResolver + "]";
     }
 

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java?rev=1512146&r1=1512145&r2=1512146&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProvider.java
Fri Aug  9 06:13:24 2013
@@ -27,9 +27,11 @@ import javax.servlet.http.HttpServletReq
 
 import org.apache.sling.api.resource.ModifyingResourceProvider;
 import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.QueriableResourceProvider;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceProvider;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,13 +46,13 @@ import com.mongodb.QueryBuilder;
  * The MongoDB resource provider creates resources based on MongoDB entries.
  * The resources contain all properties stored in the MongoDB except those starting with
a "_".
  */
-public class MongoDBResourceProvider implements ResourceProvider, ModifyingResourceProvider
{
+public class MongoDBResourceProvider implements ResourceProvider, ModifyingResourceProvider,
QueriableResourceProvider {
 
     /** The special path property containing the (relative) path of the resource in the tree.
*/
-    public static final String PROP_PATH = "_path";
+    private static final String PROP_PATH = "_path";
 
     /** The id property. */
-    public static final String PROP_ID = "_id";
+    private static final String PROP_ID = "_id";
 
     /** Logger. */
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -95,7 +97,7 @@ public class MongoDBResourceProvider imp
                 throw new PersistenceException("Resource already exists at " + path, null,
path, null);
             }
             final DBObject dbObj = new BasicDBObject();
-            dbObj.put(PROP_PATH, info[1]);
+            dbObj.put(getPROP_PATH(), info[1]);
             if ( properties != null ) {
                 for(Map.Entry<String, Object> entry : properties.entrySet()) {
                     final String key = propNameToKey(entry.getKey());
@@ -135,11 +137,11 @@ public class MongoDBResourceProvider imp
                     final DBCollection col = this.getCollection(info[0]);
                     final String pattern = "^" + Pattern.quote(info[1]) + "/";
 
-                    final DBObject query = QueryBuilder.start(PROP_PATH).regex(Pattern.compile(pattern)).get();
+                    final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
                     final DBCursor cur = col.find(query);
                     while ( cur.hasNext() ) {
                         final DBObject dbObj = cur.next();
-                        final String childPath = info[0] + '/' + dbObj.get(PROP_PATH);
+                        final String childPath = info[0] + '/' + dbObj.get(getPROP_PATH());
                         this.deletedResources.add(childPath);
                         this.changedResources.remove(childPath);
                     }
@@ -183,7 +185,7 @@ public class MongoDBResourceProvider imp
                 // check if the collection still exists
                 final DBCollection col = this.getCollection(info[0]);
                 if ( col != null ) {
-                    if ( col.findAndRemove(QueryBuilder.start(PROP_PATH).is(info[1]).get())
!= null ) {
+                    if ( col.findAndRemove(QueryBuilder.start(getPROP_PATH()).is(info[1]).get())
!= null ) {
                         this.context.notifyRemoved(info);
                     }
                 }
@@ -193,10 +195,10 @@ public class MongoDBResourceProvider imp
                 final DBCollection col = this.context.getDatabase().getCollection(changed.getCollection());
                 if ( col != null ) {
                     final String[] info = new String[] {changed.getCollection(),
-                            changed.getProperties().get(PROP_PATH).toString()};
+                            changed.getProperties().get(getPROP_PATH()).toString()};
                     // create or update?
                     if ( changed.getProperties().get(PROP_ID) != null ) {
-                        col.update(QueryBuilder.start(PROP_PATH).is(changed.getProperties().get(PROP_PATH)).get(),
+                        col.update(QueryBuilder.start(getPROP_PATH()).is(changed.getProperties().get(getPROP_PATH())).get(),
                                 changed.getProperties());
                         this.context.notifyUpdated(info);
                     } else {
@@ -283,9 +285,9 @@ public class MongoDBResourceProvider imp
                     pattern = "^" + Pattern.quote(info[1]) + "/([^/])*$";
                 }
 
-                final DBObject query = QueryBuilder.start(PROP_PATH).regex(Pattern.compile(pattern)).get();
+                final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
                 final DBCursor cur = col.find(query).
-                        sort(BasicDBObjectBuilder.start(PROP_PATH, 1).get());
+                        sort(BasicDBObjectBuilder.start(getPROP_PATH(), 1).get());
                 return new Iterator<Resource>() {
 
                     public boolean hasNext() {
@@ -294,7 +296,7 @@ public class MongoDBResourceProvider imp
 
                     public Resource next() {
                         final DBObject obj = cur.next();
-                        final String objPath = obj.get(PROP_PATH).toString();
+                        final String objPath = obj.get(getPROP_PATH()).toString();
                         final int lastSlash = objPath.lastIndexOf('/');
                         final String name;
                         if (lastSlash == -1) {
@@ -332,7 +334,7 @@ public class MongoDBResourceProvider imp
     /**
      * Extract info about collection and path
      */
-    private String[] extractResourceInfo(final String path) {
+    protected String[] extractResourceInfo(final String path) {
         if ( path.startsWith(this.context.getRootWithSlash()) ) {
             if ( path.length() == this.context.getRootWithSlash().length() ) {
                 // special resource - show all collections
@@ -358,16 +360,18 @@ public class MongoDBResourceProvider imp
     /**
      * Check if a collection with a given name exists
      */
-    private boolean hasCollection(final String name) {
+    protected boolean hasCollection(final String name) {
+        logger.info("Mongo: Getting collection names");
         final Set<String> names = this.context.getDatabase().getCollectionNames();
         return names.contains(name) && !this.context.isFilterCollectionName(name);
     }
 
+    
     /**
      * Check if a collection with a given name exists and return it
      */
-    private DBCollection getCollection(final String name) {
-        if ( this.hasCollection(name) ) {
+    protected DBCollection getCollection(final String name) {
+        if ( this.hasCollection(name) ) {            
             return this.context.getDatabase().getCollection(name);
         }
         return null;
@@ -376,7 +380,7 @@ public class MongoDBResourceProvider imp
     /**
      * Get a resource
      */
-    private Resource getResource(final ResourceResolver resourceResolver, final String path,
final String[] info) {
+    protected Resource getResource(final ResourceResolver resourceResolver, final String
path, final String[] info) {
         if ( info.length == 0 ) {
             // special resource : all collections
             return new MongoDBCollectionResource(resourceResolver, path);
@@ -390,7 +394,7 @@ public class MongoDBResourceProvider imp
         logger.debug("Searching {} in {}", info[1], info[0]);
         final DBCollection col = this.getCollection(info[0]);
         if ( col != null ) {
-            final DBObject obj = col.findOne(QueryBuilder.start(PROP_PATH).is(info[1]).get());
+            final DBObject obj = col.findOne(QueryBuilder.start(getPROP_PATH()).is(info[1]).get());
             logger.debug("Found {}", obj);
             if ( obj != null ) {
                 return new MongoDBResource(resourceResolver,
@@ -414,4 +418,28 @@ public class MongoDBResourceProvider imp
         }
         return dbObj;
     }
+    
+    protected Set<String> getDeletedResources() {
+        return this.deletedResources;
+    }
+    
+    protected Map<String, MongoDBResource> getChangedResources() {
+        return this.changedResources;
+    }
+    
+    protected MongoDBContext getContext() {
+        return this.context;
+    }
+    
+    protected String getPROP_PATH() {
+        return PROP_PATH;
+    }
+
+    public Iterator<Resource> findResources(ResourceResolver resolver, String query,
String language) {
+        return null;
+    }
+
+    public Iterator<ValueMap> queryResources(ResourceResolver resolver, String query,
String language) {
+        return null;
+    }
 }

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java?rev=1512146&r1=1512145&r2=1512146&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/MongoDBResourceProviderFactory.java
Fri Aug  9 06:13:24 2013
@@ -35,7 +35,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.mongodb.DB;
+import com.mongodb.DBAddress;
 import com.mongodb.Mongo;
+import com.mongodb.MongoOptions;
 
 /**
  * The MongoDB resource provider factory allows to provided resources stored
@@ -66,10 +68,18 @@ public class MongoDBResourceProviderFact
 
     @Property(value=DEFAULT_DB)
     private static final String PROP_DB = "db";
-
+    
     @Property(unbounded=PropertyUnbounded.ARRAY, value="system.indexes")
     private static final String PROP_FILTER_COLLECTIONS = "filter.collections";
 
+    private static final int DEFAULT_NUMCONNECTIONS = 10;
+    @Property(intValue=DEFAULT_NUMCONNECTIONS)
+    private static final String PROP_NUM_CONNECTIONS = "numconnections";
+
+    private static final int DEFAULT_THREAD_MULTIPLIER= 5;
+    @Property(intValue=DEFAULT_THREAD_MULTIPLIER)
+    private static final String  PROP_THREAD_MULTIPLIER = "threadmultiplier";
+    
     /** Logger. */
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -96,8 +106,13 @@ public class MongoDBResourceProviderFact
         final String db = PropertiesUtil.toString(props.get(PROP_DB), DEFAULT_DB);
         logger.info("Starting MongoDB resource provider with host={}, port={}, db={}",
                 new Object[] {host, port, db});
+        final DBAddress address = new DBAddress(host, port, db);
+        final MongoOptions options = new MongoOptions();
+        
+        options.connectionsPerHost = PropertiesUtil.toInteger(props.get(PROP_NUM_CONNECTIONS),
DEFAULT_NUMCONNECTIONS);
+        options.threadsAllowedToBlockForConnectionMultiplier = PropertiesUtil.toInteger(props.get(PROP_THREAD_MULTIPLIER),
DEFAULT_THREAD_MULTIPLIER);
+        final Mongo m = new Mongo(address, options);
 
-        final Mongo m = new Mongo( host , port );
         final DB database = m.getDB( db );
         logger.info("Connected to database {}", database);
 
@@ -122,4 +137,8 @@ public class MongoDBResourceProviderFact
         // for now we allow anonymous access
         return new MongoDBResourceProvider(this.context);
     }
+    
+    protected MongoDBContext getContext() {
+        return this.context;
+    }
 }

Modified: sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java?rev=1512146&r1=1512145&r2=1512146&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/java/org/apache/sling/mongodb/impl/ReadableValueMap.java
Fri Aug  9 06:13:24 2013
@@ -39,6 +39,9 @@ public class ReadableValueMap implements
 
     protected void createValueMap(final DBObject dbObject) {
         final Map<String, Object> map = new HashMap<String, Object>();
+        if (dbObject == null) {
+            this.valueMap = Collections.<String, Object> emptyMap();
+        }
         for(final String key : dbObject.keySet()) {
             final String name = MongoDBResourceProvider.keyToPropName(key);
             if ( name != null ) {

Modified: sling/trunk/contrib/extensions/mongodb/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/mongodb/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1512146&r1=1512145&r2=1512146&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/mongodb/src/main/resources/OSGI-INF/metatype/metatype.properties
(original)
+++ sling/trunk/contrib/extensions/mongodb/src/main/resources/OSGI-INF/metatype/metatype.properties
Fri Aug  9 06:13:24 2013
@@ -43,4 +43,10 @@ db.name MongoDB Database
 db.description = The database to use.
 
 filter.collections.name = Filter Collections
-filter.collections.description = These collections are not available as resources.
\ No newline at end of file
+filter.collections.description = These collections are not available as resources.
+
+numconnections.name = Connections Per Host
+numconnections.description = MongoOptions.connectionsPerHost
+
+threadmultiplier.name = Thread Multiplier
+threadmultiplier.description = MongoOptions.threadsAllowedToBlockForConnectionMultiplier
\ No newline at end of file



Mime
View raw message