View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.trigger;
21  
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.naming.NamingException;
28  
29  import org.apache.directory.server.core.entry.ClonedServerEntry;
30  import org.apache.directory.server.core.entry.ServerEntry;
31  import org.apache.directory.server.core.interceptor.context.ModifyOperationContext;
32  import org.apache.directory.server.core.interceptor.context.OperationContext;
33  import org.apache.directory.server.core.partition.ByPassConstants;
34  import org.apache.directory.shared.ldap.entry.Modification;
35  import org.apache.directory.shared.ldap.name.LdapDN;
36  import org.apache.directory.shared.ldap.trigger.StoredProcedureParameter;
37  
38  
39  public class ModifyStoredProcedureParameterInjector extends AbstractStoredProcedureParameterInjector
40  {
41      private LdapDN modifiedEntryName;
42      private List<Modification> modifications;
43      private ServerEntry oldEntry;
44      
45      
46      public ModifyStoredProcedureParameterInjector( ModifyOperationContext opContext ) throws Exception
47      {
48          super( opContext );
49          modifiedEntryName = opContext.getDn();
50          modifications = opContext.getModItems();
51          this.oldEntry = getEntry( opContext );
52          Map<Class<?>, MicroInjector> injectors = super.getInjectors();
53          injectors.put( StoredProcedureParameter.Modify_OBJECT.class, $objectInjector );
54          injectors.put( StoredProcedureParameter.Modify_MODIFICATION.class, $modificationInjector );
55          injectors.put( StoredProcedureParameter.Modify_OLD_ENTRY.class, $oldEntryInjector );
56          injectors.put( StoredProcedureParameter.Modify_NEW_ENTRY.class, $newEntryInjector );
57      }
58      
59      
60      MicroInjector $objectInjector = new MicroInjector()
61      {
62          public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws NamingException
63          {
64              // Return a safe copy constructed with user provided name.
65              return new LdapDN( modifiedEntryName.getUpName() );
66          }
67      };
68      
69      
70      MicroInjector $modificationInjector = new MicroInjector()
71      {
72          public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws NamingException
73          {
74              List<Modification> newMods = new ArrayList<Modification>();
75              
76              for ( Modification mod:modifications )
77              {
78                  newMods.add( mod.clone() );
79              }
80              
81              return newMods;
82          }
83      };
84      
85      
86      MicroInjector $oldEntryInjector = new MicroInjector()
87      {
88          public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws NamingException
89          {
90              return oldEntry;
91          }
92      };
93      
94      
95      MicroInjector $newEntryInjector = new MicroInjector()
96      {
97          public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws Exception
98          {
99              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 }