001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.trigger;
021    
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.apache.directory.server.core.entry.ClonedServerEntry;
028    import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
029    import org.apache.directory.server.core.interceptor.context.OperationContext;
030    import org.apache.directory.server.core.partition.ByPassConstants;
031    import org.apache.directory.shared.ldap.entry.Modification;
032    import org.apache.directory.shared.ldap.entry.ServerEntry;
033    import org.apache.directory.shared.ldap.exception.LdapException;
034    import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
035    import org.apache.directory.shared.ldap.name.DN;
036    import org.apache.directory.shared.ldap.trigger.StoredProcedureParameter;
037    
038    
039    public class ModifyStoredProcedureParameterInjector extends AbstractStoredProcedureParameterInjector
040    {
041        private DN modifiedEntryName;
042        private List<Modification> modifications;
043        private ServerEntry oldEntry;
044        
045        
046        public ModifyStoredProcedureParameterInjector( ModifyOperationContext opContext ) throws Exception
047        {
048            super( opContext );
049            modifiedEntryName = opContext.getDn();
050            modifications = opContext.getModItems();
051            this.oldEntry = getEntry( opContext );
052            Map<Class<?>, MicroInjector> injectors = super.getInjectors();
053            injectors.put( StoredProcedureParameter.Modify_OBJECT.class, $objectInjector );
054            injectors.put( StoredProcedureParameter.Modify_MODIFICATION.class, $modificationInjector );
055            injectors.put( StoredProcedureParameter.Modify_OLD_ENTRY.class, $oldEntryInjector );
056            injectors.put( StoredProcedureParameter.Modify_NEW_ENTRY.class, $newEntryInjector );
057        }
058        
059        
060        MicroInjector $objectInjector = new MicroInjector()
061        {
062            public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapInvalidDnException
063            {
064                // Return a safe copy constructed with user provided name.
065                return new DN( modifiedEntryName.getName() );
066            }
067        };
068        
069        
070        MicroInjector $modificationInjector = new MicroInjector()
071        {
072            public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapException
073            {
074                List<Modification> newMods = new ArrayList<Modification>();
075                
076                for ( Modification mod:modifications )
077                {
078                    newMods.add( mod.clone() );
079                }
080                
081                return newMods;
082            }
083        };
084        
085        
086        MicroInjector $oldEntryInjector = new MicroInjector()
087        {
088            public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapException
089            {
090                return oldEntry;
091            }
092        };
093        
094        
095        MicroInjector $newEntryInjector = new MicroInjector()
096        {
097            public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws Exception
098            {
099                return getEntry( opContext );
100            }
101        };
102        
103        
104        private ClonedServerEntry getEntry( OperationContext opContext ) throws Exception
105        {
106            /**
107             * Using LOOKUP_EXCLUDING_OPR_ATTRS_BYPASS here to exclude operational attributes
108             * especially subentry related ones like "triggerExecutionSubentries".
109             */
110            return opContext.lookup( modifiedEntryName, ByPassConstants.LOOKUP_EXCLUDING_OPR_ATTRS_BYPASS );
111        }
112    }