Author: cziegeler
Date: Fri Dec 28 03:51:49 2007
New Revision: 607215

URL: http://svn.apache.org/viewvc?rev=607215&view=rev
Log:
Use reflection to check for method of jackrabbit node type manager as the 
instanceof is failing in some cases.
This is a workaround until the real problem has been found/solved.

Modified:
    
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/NodeTypeLoader.java
    
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/internal/loader/Loader.java

Modified: 
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/NodeTypeLoader.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/NodeTypeLoader.java?rev=607215&r1=607214&r2=607215&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/NodeTypeLoader.java
 (original)
+++ 
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/NodeTypeLoader.java
 Fri Dec 28 03:51:49 2007
@@ -20,6 +20,8 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.net.URL;
 
 import javax.jcr.RepositoryException;
@@ -109,35 +111,75 @@
     public static boolean registerNodeType(Session session, InputStream source)
             throws IOException, RepositoryException {
 
+        // this is a temporary workaround as the instanceof test seems to fail
+        // in some cases (FIXME)
         Workspace workspace = session.getWorkspace();
         NodeTypeManager ntm = workspace.getNodeTypeManager();
-        if (ntm instanceof JackrabbitNodeTypeManager) {
-            log.debug("Using Jackrabbit to import node types");
-            JackrabbitNodeTypeManager jntm = (JackrabbitNodeTypeManager) ntm;
+        try {
+            final Method m = ntm.getClass().getMethod("registerNodeTypes", new 
Class[] {InputStream.class, String.class});
+            log.debug("Using Jackrabbit via reflection to import node types");
             try {
-                jntm.registerNodeTypes(source,
-                    JackrabbitNodeTypeManager.TEXT_X_JCR_CND);
+                m.invoke(ntm, new Object[] {source, 
JackrabbitNodeTypeManager.TEXT_X_JCR_CND});
                 return true;
-            } catch (RepositoryException re) {
-                Throwable t = re.getCause();
-                if (t != null
-                    && t.getClass().getName().endsWith(
-                        ".InvalidNodeTypeDefException")) {
+            } catch (InvocationTargetException e) {
+                final Throwable targetE = e.getTargetException();
+                if ( targetE instanceof RepositoryException ) {
+                    Throwable t = ((RepositoryException)targetE).getCause();
+                    if (t != null
+                        && t.getClass().getName().endsWith(
+                            ".InvalidNodeTypeDefException")) {
+                        // hacky wacky: interpret message to check whether it 
is for
+                        // duplicate node type -> very bad, that this is the 
only
+                        // way to check !!!
+                        if (t.getCause().getMessage().indexOf("already 
exists") >= 0) {
+                            // alright, node types are already registered, 
ignore
+                            // this
+                            log.debug("Node types already registered...");
+                            return true;
+                        }
+                    }
+
+                    // get here to rethrow the RepositoryException
+                    throw (RepositoryException)targetE;
+                }
+                // we ignore it
+            } catch (IllegalArgumentException e) {
+                // we ignore it
+            } catch (IllegalAccessException e) {
+                // we ignore it
+            }
+        } catch (SecurityException e) {
+            // we ignore it
+        } catch (NoSuchMethodException e) {
+            // we ignore it
+        }
+//        if (ntm instanceof JackrabbitNodeTypeManager) {
+//            log.debug("Using Jackrabbit to import node types");
+//            JackrabbitNodeTypeManager jntm = (JackrabbitNodeTypeManager) ntm;
+//            try {
+//                jntm.registerNodeTypes(source,
+//                    JackrabbitNodeTypeManager.TEXT_X_JCR_CND);
+//                return true;
+//            } catch (RepositoryException re) {
+//                Throwable t = re.getCause();
+//                if (t != null
+//                    && t.getClass().getName().endsWith(
+//                        ".InvalidNodeTypeDefException")) {
                     // hacky wacky: interpret message to check whether it is 
for
                     // duplicate node type -> very bad, that this is the only
                     // way to check !!!
-                    if (re.getCause().getMessage().indexOf("already exists") 
>= 0) {
+//                    if (re.getCause().getMessage().indexOf("already exists") 
>= 0) {
                         // alright, node types are already registered, ignore
                         // this
-                        log.debug("Node types already registered...");
-                        return true;
-                    }
-                }
+//                        log.debug("Node types already registered...");
+//                        return true;
+//                    }
+//                }
 
                 // get here to rethrow the RepositoryException
-                throw re;
-            }
-        }
+//                throw re;
+//            }
+//        }
 
         log.warn("Repository is not Jackrabbit based, cannot import node 
types");
         return false;

Modified: 
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/internal/loader/Loader.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/internal/loader/Loader.java?rev=607215&r1=607214&r2=607215&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/internal/loader/Loader.java
 (original)
+++ 
incubator/sling/trunk/jcr/api/src/main/java/org/apache/sling/jcr/api/internal/loader/Loader.java
 Fri Dec 28 03:51:49 2007
@@ -203,14 +203,16 @@
                     // if we are retrying we already logged this message once, 
so we won't log it again
                     if ( !isRetry ) {
                         log.warn("Cannot read node types {} from bundle {}: 
{}",
-                            new Object[]{ nodeTypeFile, 
bundle.getSymbolicName()}, ioe);
+                            new Object[]{ nodeTypeFile, 
bundle.getSymbolicName(), ioe });
+                        log.warn("Stacktrace ", ioe);
                     }
                 } catch (Exception e) {
                     success = false;
                     // if we are retrying we already logged this message once, 
so we won't log it again
                     if ( !isRetry ) {
                         log.error("Error loading node types {} from bundle {}: 
{}",
-                            new Object[]{ nodeTypeFile, 
bundle.getSymbolicName()}, e);
+                            new Object[]{ nodeTypeFile, 
bundle.getSymbolicName(), e });
+                        log.error("Stacktrace ", e);
                     }
                 } finally {
                     if (ins != null) {


Reply via email to