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.sp;
021
022
023 import java.util.Collections;
024 import java.util.List;
025 import java.util.Set;
026
027 import javax.naming.directory.SearchControls;
028
029 import org.apache.directory.server.core.CoreSession;
030 import org.apache.directory.server.core.entry.ClonedServerEntry;
031 import org.apache.directory.server.core.filtering.EntryFilteringCursor;
032 import org.apache.directory.server.i18n.I18n;
033 import org.apache.directory.shared.ldap.constants.SchemaConstants;
034 import org.apache.directory.shared.ldap.entry.StringValue;
035 import org.apache.directory.shared.ldap.entry.ServerEntry;
036 import org.apache.directory.shared.ldap.exception.LdapException;
037 import org.apache.directory.shared.ldap.filter.EqualityNode;
038 import org.apache.directory.shared.ldap.filter.ExprNode;
039 import org.apache.directory.shared.ldap.filter.SearchScope;
040 import org.apache.directory.shared.ldap.message.AliasDerefMode;
041 import org.apache.directory.shared.ldap.name.DN;
042 import org.apache.directory.shared.ldap.schema.AttributeType;
043 import org.apache.directory.shared.ldap.schema.AttributeTypeOptions;
044
045
046 /**
047 * A Factory type class which holds a registry of supported {@link StoredProcEngineConfig}s. A container reference
048 * as the base for Stored Procedure storage on the DIT is also handled by this class.
049 *
050 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
051 * @version $Rev$ $Date$
052 */
053 public class StoredProcExecutionManager
054 {
055 private final static Set<AttributeTypeOptions> EMPTY_ATTRIBS = Collections.emptySet();
056
057 private final String storedProcContainer;
058
059 private final List<StoredProcEngineConfig> storedProcEngineConfigs;
060
061
062 /**
063 * Creates a {@link StoredProcExecutionManager} instance.
064 *
065 * @param storedProcContainer The base of the DIT subtree used for storing stored procedure units.
066 * @param storedProcEngineConfigs A list of {@link StoredProcEngineConfig}s to register different {@link StoredProcEngine}s with this manager.
067 */
068 public StoredProcExecutionManager( final String storedProcContainer, final List<StoredProcEngineConfig> storedProcEngineConfigs )
069 {
070 this.storedProcContainer = storedProcContainer;
071 this.storedProcEngineConfigs = storedProcEngineConfigs;
072 }
073
074 /**
075 * Finds and returns a stored procedure unit entry whose identifier name
076 * is extracted from fullSPName.
077 *
078 * @param rootDSE A handle on the root DSE to be used for searching the SP Unit over.
079 * @param fullSPName Full name of the Stored Procedure including the unit name.
080 * @return The entry associated with the SP Unit.
081 * @throws Exception If the unit cannot be located or any other error occurs.
082 */
083 public ClonedServerEntry findStoredProcUnit( CoreSession session, String fullSPName ) throws Exception
084 {
085 SearchControls controls = new SearchControls();
086 controls.setReturningAttributes( SchemaConstants.ALL_USER_ATTRIBUTES_ARRAY );
087 controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
088 String spUnitName = StoredProcUtils.extractStoredProcUnitName( fullSPName );
089
090 AttributeType at = session.getDirectoryService()
091 .getSchemaManager().lookupAttributeTypeRegistry( "storedProcUnitName" );
092 ExprNode filter = new EqualityNode<String>( "storedProcUnitName", new StringValue( at, spUnitName ) );
093 DN dn = new DN( storedProcContainer );
094 EntryFilteringCursor results = session.search( dn, SearchScope.SUBTREE, filter,
095 AliasDerefMode.DEREF_ALWAYS, EMPTY_ATTRIBS );
096 if ( results.first() )
097 {
098 ClonedServerEntry entry = results.get();
099 results.close();
100 return entry;
101 }
102
103 return null;
104 }
105
106
107 /**
108 * Initializes and returns a {@link StoredProcEngine} instance which can operate on spUnitEntry
109 * considering its specific stored procedure language.
110 *
111 * @param spUnitEntry The entry which a {@link StoredProcEngine} type will be mathched with respect to the language identifier.
112 * @return A {@link StoredProcEngine} associated with spUnitEntry.
113 * @throws LdapException If no {@link StoredProcEngine} that can be associated with the language identifier in spUnitEntry can be found.
114 */
115 public StoredProcEngine getStoredProcEngineInstance( ClonedServerEntry spUnitEntry ) throws LdapException
116 {
117 String spLangId = ( String ) spUnitEntry.getOriginalEntry().get( "storedProcLangId" ).getString();
118
119 for ( StoredProcEngineConfig engineConfig : storedProcEngineConfigs )
120 {
121 if ( engineConfig.getStoredProcLangId().equalsIgnoreCase( spLangId ) )
122 {
123 Class<? extends StoredProcEngine> engineType = engineConfig.getStoredProcEngineType();
124 StoredProcEngine engine;
125
126 try
127 {
128 engine = engineType.newInstance();
129 }
130 catch ( InstantiationException e )
131 {
132 LdapException ne = new LdapException( e.getMessage() );
133 ne.initCause( e );
134 throw ne;
135 }
136 catch ( IllegalAccessException e )
137 {
138 LdapException ne = new LdapException( e.getMessage() );
139 ne.initCause( e );
140 throw ne;
141 }
142
143 engine.setSPUnitEntry( (ServerEntry)spUnitEntry.getOriginalEntry() );
144 return engine;
145 }
146
147 }
148
149 throw new LdapException( I18n.err( I18n.ERR_294, spLangId ) );
150
151 }
152
153 }