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.security.Principal;
024 import java.util.ArrayList;
025 import java.util.HashMap;
026 import java.util.Iterator;
027 import java.util.List;
028 import java.util.Map;
029
030 import org.apache.directory.server.core.interceptor.context.OperationContext;
031 import org.apache.directory.server.core.partition.ByPassConstants;
032 import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
033 import org.apache.directory.shared.ldap.name.DN;
034 import org.apache.directory.shared.ldap.trigger.StoredProcedureParameter;
035 import org.apache.directory.shared.ldap.trigger.StoredProcedureParameter.Generic_LDAP_CONTEXT;
036
037
038 public abstract class AbstractStoredProcedureParameterInjector implements StoredProcedureParameterInjector
039 {
040 private OperationContext opContext;
041 private Map<Class<?>, MicroInjector> injectors;
042
043
044 public AbstractStoredProcedureParameterInjector( OperationContext opContext )
045 {
046 this.opContext = opContext;
047 injectors = new HashMap<Class<?>, MicroInjector>();
048 injectors.put( StoredProcedureParameter.Generic_OPERATION_PRINCIPAL.class, $operationPrincipalInjector );
049 injectors.put( StoredProcedureParameter.Generic_LDAP_CONTEXT.class, $ldapContextInjector );
050 }
051
052
053 protected DN getOperationPrincipal() throws LdapInvalidDnException
054 {
055 Principal principal = opContext.getSession().getEffectivePrincipal();
056 DN userName = new DN( principal.getName() );
057 return userName;
058 }
059
060
061 protected Map<Class<?>, MicroInjector> getInjectors()
062 {
063 return injectors;
064 }
065
066
067 public OperationContext getOperationContext()
068 {
069 return opContext;
070 }
071
072
073 public void setOperationContext( OperationContext invocation )
074 {
075 this.opContext = invocation;
076 }
077
078
079 public final List<Object> getArgumentsToInject( OperationContext opContext,
080 List<StoredProcedureParameter> parameterList ) throws Exception
081 {
082 List<Object> arguments = new ArrayList<Object>();
083
084 Iterator<StoredProcedureParameter> it = parameterList.iterator();
085
086 while ( it.hasNext() )
087 {
088 StoredProcedureParameter spParameter = it.next();
089 MicroInjector injector = injectors.get( spParameter.getClass() );
090 arguments.add( injector.inject( opContext, spParameter ) );
091 }
092
093 return arguments;
094 }
095
096
097 MicroInjector $operationPrincipalInjector = new MicroInjector()
098 {
099 public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws Exception
100 {
101 return getOperationPrincipal();
102 }
103 };
104
105
106 MicroInjector $ldapContextInjector = new MicroInjector()
107 {
108 public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws Exception
109 {
110 Generic_LDAP_CONTEXT ldapCtxParam = ( Generic_LDAP_CONTEXT ) param;
111 DN ldapCtxName = ldapCtxParam.getCtxName();
112 return opContext.lookup( ldapCtxName, ByPassConstants.LOOKUP_BYPASS );
113 }
114 };
115 }