You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2011/12/03 01:34:18 UTC

svn commit: r1209800 - /sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrItemAdapterFactory.java

Author: justin
Date: Sat Dec  3 00:34:17 2011
New Revision: 1209800

URL: http://svn.apache.org/viewvc?rev=1209800&view=rev
Log:
SLING-2315 - adding an AdapterFactory for Node and Property objects

Added:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrItemAdapterFactory.java

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrItemAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrItemAdapterFactory.java?rev=1209800&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrItemAdapterFactory.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrItemAdapterFactory.java Sat Dec  3 00:34:17 2011
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.jcr.resource.internal;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+
+import org.apache.sling.api.adapter.AdapterFactory;
+import org.apache.sling.api.resource.PersistableValueMap;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResource;
+import org.apache.sling.jcr.resource.internal.helper.jcr.JcrPropertyResource;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * AdapterFactory which adapts JCR Nodes and Properties into Resources.
+ */
+class JcrItemAdapterFactory implements AdapterFactory {
+
+    private final Logger logger = LoggerFactory.getLogger(JcrItemAdapterFactory.class);
+
+    private final JcrResourceResolverFactoryImpl resourceResolverFactory;
+
+    private ServiceRegistration serviceRegsitration;
+
+    public JcrItemAdapterFactory(BundleContext ctx, JcrResourceResolverFactoryImpl resourceResolverFactory) {
+        this.resourceResolverFactory = resourceResolverFactory;
+        Dictionary<Object, Object> properties = new Hashtable<Object, Object>();
+        properties.put(ADAPTABLE_CLASSES, new String[] { Node.class.getName(), Property.class.getName() });
+        properties.put(ADAPTER_CLASSES,
+                new String[] { Resource.class.getName(), Map.class.getName(), ValueMap.class.getName(),
+                        PersistableValueMap.class.getName() });
+        properties.put(Constants.SERVICE_DESCRIPTION, "JCR Item Adapter Factory");
+        properties.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        this.serviceRegsitration = ctx.registerService(AdapterFactory.class.getName(), this, properties);
+    }
+
+    public void dispose() {
+        if (this.serviceRegsitration != null) {
+            this.serviceRegsitration.unregister();
+            this.serviceRegsitration = null;
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
+        if (type == Resource.class) {
+            try {
+                if (adaptable instanceof Node) {
+                    Node node = (Node) adaptable;
+                    return (AdapterType) new JcrNodeResource(resourceResolverFactory.getResourceResolver(node
+                            .getSession()), node, resourceResolverFactory.getDynamicClassLoader());
+                } else if (adaptable instanceof Property) {
+                    Property property = (Property) adaptable;
+                    return (AdapterType) new JcrPropertyResource(resourceResolverFactory.getResourceResolver(property
+                            .getSession()), property);
+                }
+            } catch (RepositoryException e) {
+                logger.error("Unable to adapt JCR Item to a Resource", e);
+            }
+            return null;
+        } else {
+            return getAdapter(adaptable, Resource.class).adaptTo(type);
+        }
+    }
+
+}