Author: fmeschbe
Date: Mon Jan 7 07:59:28 2008
New Revision: 609651
URL: http://svn.apache.org/viewvc?rev=609651&view=rev
Log:
Complete the implementation of the OCM Adapter Factory
Modified:
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/ObjectContentManagerFactory.java
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/OcmAdapterFactory.java
Modified:
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/ObjectContentManagerFactory.java
URL:
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/ObjectContentManagerFactory.java?rev=609651&r1=609650&r2=609651&view=diff
==============================================================================
---
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/ObjectContentManagerFactory.java
(original)
+++
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/ObjectContentManagerFactory.java
Mon Jan 7 07:59:28 2008
@@ -339,7 +339,7 @@
String[] mappedClasses = (mapper != null)
? mapper.getMappedClasses()
: new String[0];
- adapterFactory = new OcmAdapterFactory(
+ adapterFactory = new OcmAdapterFactory(this,
componentContext.getBundleContext(), mappedClasses);
}
Modified:
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/OcmAdapterFactory.java
URL:
http://svn.apache.org/viewvc/incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/OcmAdapterFactory.java?rev=609651&r1=609650&r2=609651&view=diff
==============================================================================
---
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/OcmAdapterFactory.java
(original)
+++
incubator/sling/whiteboard/fmeschbe/resource/jcr/ocm/src/main/java/org/apache/sling/jcr/ocm/impl/OcmAdapterFactory.java
Mon Jan 7 07:59:28 2008
@@ -21,7 +21,16 @@
import java.util.Dictionary;
import java.util.Hashtable;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.jackrabbit.ocm.exception.JcrMappingException;
+import org.apache.jackrabbit.ocm.exception.NestableRuntimeException;
+import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException;
+import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
import org.apache.sling.api.resource.Resource;
+import org.apache.sling.jcr.ocm.DefaultMappedObject;
import org.apache.sling.osgi.commons.AdapterFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@@ -29,12 +38,19 @@
class OcmAdapterFactory implements AdapterFactory {
+ private static final String CLASS_OBJECT = Object.class.getName();
+
+ private final ObjectContentManagerFactory factory;
+
private Dictionary<String, Object> registrationProperties;
private ServiceRegistration registration;
- OcmAdapterFactory(BundleContext bundleContext, String[] mappedClasses) {
+ OcmAdapterFactory(ObjectContentManagerFactory factory, BundleContext
bundleContext, String[] mappedClasses) {
+ this.factory = factory;
+ mappedClasses = ensureClassObject(mappedClasses);
+
registrationProperties = new Hashtable<String, Object>();
registrationProperties.put(ADAPTABLE_CLASSES,
Resource.class.getName());
registrationProperties.put(ADAPTER_CLASSES, mappedClasses);
@@ -46,6 +62,7 @@
void updateAdapterClasses(String[] mappedClasses) {
// set the new set of mapped classes
+ mappedClasses = ensureClassObject(mappedClasses);
registrationProperties.put(ADAPTER_CLASSES, mappedClasses);
// update the registration to have the factory registry updated
@@ -59,10 +76,56 @@
}
}
+ @SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable,
Class<AdapterType> type) {
- // TODO Auto-generated method stub
+
+ // must work, we only support Resource
+ Resource res = (Resource) adaptable;
+
+ // the resource must be node based, otherwise return null
+ Node node = res.adaptTo(Node.class);
+ if (node == null) {
+ return null;
+ }
+
+ try {
+ Session session = node.getSession();
+ ObjectContentManager ocm =
factory.getObjectContentManager(session);
+
+ // default mapping for Object.class
+ if (type.getName().equals(CLASS_OBJECT)) {
+ // unchecked cast
+ try {
+ return (AdapterType) ocm.getObject(res.getURI());
+ } catch (JcrMappingException jme) {
+ // no default mapping, try DefaultMappedObject
+ type = (Class<AdapterType>) DefaultMappedObject.class;
+ }
+ }
+
+ // unchecked cast
+ return (AdapterType) ocm.getObject(type, res.getURI());
+ } catch (RepositoryException re) {
+ // TODO: should log
+ } catch (NestableRuntimeException nre) {
+ // TODO: should log (OCM mapping failed)
+ }
+
+ // fall back to no mapping
return null;
}
+ private String[] ensureClassObject(String[] mappedClasses) {
+ for (int i=0; i < mappedClasses.length; i++) {
+ if (CLASS_OBJECT.equals(mappedClasses[i])) {
+ return mappedClasses;
+ }
+ }
+
+ String[] extended = new String[mappedClasses.length+1];
+ System.arraycopy(mappedClasses, 0, extended, 0, mappedClasses.length);
+ extended[extended.length-1] = CLASS_OBJECT;
+ return extended;
+ }
}