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.security.Principal;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.naming.Name;
31  import javax.naming.NamingException;
32  
33  import org.apache.directory.server.core.interceptor.context.OperationContext;
34  import org.apache.directory.server.core.partition.ByPassConstants;
35  import org.apache.directory.shared.ldap.name.LdapDN;
36  import org.apache.directory.shared.ldap.trigger.StoredProcedureParameter;
37  import org.apache.directory.shared.ldap.trigger.StoredProcedureParameter.Generic_LDAP_CONTEXT;
38  
39  
40  public abstract class AbstractStoredProcedureParameterInjector implements StoredProcedureParameterInjector
41  {
42      private OperationContext opContext;
43      private Map<Class<?>, MicroInjector> injectors;
44      
45      
46      public AbstractStoredProcedureParameterInjector( OperationContext opContext )
47      {
48          this.opContext = opContext;
49          injectors = new HashMap<Class<?>, MicroInjector>();
50          injectors.put( StoredProcedureParameter.Generic_OPERATION_PRINCIPAL.class, $operationPrincipalInjector );
51          injectors.put( StoredProcedureParameter.Generic_LDAP_CONTEXT.class, $ldapContextInjector );
52      }
53      
54      
55      protected Name getOperationPrincipal() throws NamingException
56      {
57          Principal principal = opContext.getSession().getEffectivePrincipal();
58          Name userName = new LdapDN( principal.getName() );
59          return userName;
60      }
61      
62      
63      protected Map<Class<?>, MicroInjector> getInjectors()
64      {
65          return injectors;
66      }
67      
68      
69      public OperationContext getOperationContext()
70      {
71          return opContext;
72      }
73      
74      
75      public void setOperationContext( OperationContext invocation )
76      {
77          this.opContext = invocation;
78      }
79      
80      
81      public final List<Object> getArgumentsToInject( OperationContext opContext, 
82          List<StoredProcedureParameter> parameterList ) throws Exception
83      {
84          List<Object> arguments = new ArrayList<Object>();
85          
86          Iterator<StoredProcedureParameter> it = parameterList.iterator();
87          
88          while ( it.hasNext() )
89          {
90              StoredProcedureParameter spParameter = it.next();
91              MicroInjector injector = injectors.get( spParameter.getClass() );
92              arguments.add( injector.inject( opContext, spParameter ) );
93          }
94          
95          return arguments;
96      }
97      
98      
99      MicroInjector $operationPrincipalInjector = new MicroInjector()
100     {
101         public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws Exception
102         {
103             return getOperationPrincipal();
104         }
105     };
106     
107     
108     MicroInjector $ldapContextInjector = new MicroInjector()
109     {
110         public Object inject(  OperationContext opContext, StoredProcedureParameter param ) throws Exception
111         {
112             Generic_LDAP_CONTEXT ldapCtxParam = ( Generic_LDAP_CONTEXT ) param;
113             LdapDN ldapCtxName = ldapCtxParam.getCtxName();
114             return opContext.lookup( ldapCtxName, ByPassConstants.LOOKUP_BYPASS );
115         }
116     };
117 }