/*
 * 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.jackrabbit.core.session;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.jackrabbit.core.ItemImpl;
import org.apache.jackrabbit.core.id.NodeId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Operation to persist transient changes in a session.
 */
public class SessionSaveOperation implements SessionWriteOperation<Object> {

    private static final Logger LOG = LoggerFactory.getLogger(SessionSaveOperation.class);
    private static final boolean LOG_WITH_STACKTRACE = Boolean.getBoolean("org.jackrabbit.logWithStackTrace");

    /**
     * Persists transient changes by delegating to the save() method of the
     * root node (or the parent of transient changes if access to the root
     * node is not available to this session).
     */
    public Object perform(SessionContext context) throws RepositoryException {
        NodeId id;
        // JCR-2425: check whether session is allowed to read root node
        if (context.getSessionImpl().hasPermission("/", Session.ACTION_READ)) {
            id = context.getRootNodeId();
        } else {
            id = context.getItemStateManager().getIdOfRootTransientNodeState();
        }
        if (LOG.isDebugEnabled()) {
            NodeId transientRoot = context.getItemStateManager().getIdOfRootTransientNodeState();
            ItemImpl item = context.getItemManager().getItem(transientRoot);
            if (LOG_WITH_STACKTRACE) {
                LOG.debug("Saving changes under " + item.getPath(), new Exception());
            } else {
                LOG.debug("Saving changes under " + item.getPath());
            }
        }
        context.getItemManager().getItem(id).save();
        return this;
    }

    //--------------------------------------------------------------< Object >

    /**
     * Returns a string representation of this operation.
     */
    public String toString() {
        return "session.save()";
    }

}
@@ -2043,24 +2043,71 @@
 nabled()) %7B%0A
+            String path;%0A            try %7B%0A    
             
@@ -2204,16 +2204,20 @@
         
+    
 ItemImpl
@@ -2273,16 +2273,188 @@
 tRoot);%0A
+                path = item.getPath();%0A            %7D catch (Exception e) %7B%0A                LOG.warn(%22Could not get the path%22, e);%0A                path = %22?%22;%0A            %7D%0A
         
@@ -2536,30 +2536,20 @@
 der %22 + 
-item.getP
+p
 ath
-()
 , new Ex
@@ -2633,30 +2633,20 @@
 der %22 + 
-item.getP
+p
 ath
-()
 );%0A     

Improve logging of Session.save() to trace back root cause of externally modified nodes - workaround for the NullPointerException