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.api.sp;
21  
22  
23  import java.util.List;
24  
25  import javax.naming.directory.SearchControls;
26  
27  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
28  import org.apache.directory.api.ldap.model.cursor.Cursor;
29  import org.apache.directory.api.ldap.model.entry.Entry;
30  import org.apache.directory.api.ldap.model.entry.Value;
31  import org.apache.directory.api.ldap.model.exception.LdapException;
32  import org.apache.directory.api.ldap.model.filter.EqualityNode;
33  import org.apache.directory.api.ldap.model.filter.ExprNode;
34  import org.apache.directory.api.ldap.model.message.AliasDerefMode;
35  import org.apache.directory.api.ldap.model.message.SearchScope;
36  import org.apache.directory.api.ldap.model.name.Dn;
37  import org.apache.directory.api.ldap.model.schema.AttributeType;
38  import org.apache.directory.server.core.api.CoreSession;
39  import org.apache.directory.server.core.api.entry.ClonedServerEntry;
40  import org.apache.directory.server.i18n.I18n;
41  
42  
43  /**
44   * A Factory type class which holds a registry of supported {@link StoredProcEngineConfig}s. A container reference
45   * as the base for Stored Procedure storage on the DIT is also handled by this class.
46   * 
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class StoredProcExecutionManager
50  {
51      private final String storedProcContainer;
52  
53      private final List<StoredProcEngineConfig> storedProcEngineConfigs;
54  
55  
56      /**
57       * Creates a {@link StoredProcExecutionManager} instance.
58       * 
59       * @param storedProcContainer The base of the DIT subtree used for storing stored procedure units.
60       * @param storedProcEngineConfigs A list of {@link StoredProcEngineConfig}s to register different {@link StoredProcEngine}s with this manager.
61       */
62      public StoredProcExecutionManager( final String storedProcContainer,
63          final List<StoredProcEngineConfig> storedProcEngineConfigs )
64      {
65          this.storedProcContainer = storedProcContainer;
66          this.storedProcEngineConfigs = storedProcEngineConfigs;
67      }
68  
69  
70      /**
71       * Finds and returns a stored procedure unit entry whose identifier name
72       * is extracted from fullSPName.
73       * 
74       * @param session the session with a core directory service
75       * @param fullSPName Full name of the Stored Procedure including the unit name.
76       * @return The entry associated with the SP Unit.
77       * @throws Exception If the unit cannot be located or any other error occurs.
78       */
79      public Entry findStoredProcUnit( CoreSession session, String fullSPName ) throws Exception
80      {
81          SearchControls controls = new SearchControls();
82          controls.setReturningAttributes( SchemaConstants.ALL_USER_ATTRIBUTES_ARRAY );
83          controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
84          String spUnitName = StoredProcUtils.extractStoredProcUnitName( fullSPName );
85  
86          AttributeType storeProcUnitNamAT = session.getDirectoryService()
87              .getSchemaManager().lookupAttributeTypeRegistry( "storedProcUnitName" );
88          ExprNode filter = new EqualityNode<String>( storeProcUnitNamAT,
89              new Value( storeProcUnitNamAT, spUnitName ) );
90          Dn dn = session.getDirectoryService().getDnFactory().create( storedProcContainer );
91          Cursor<Entry> results = session.search( dn, SearchScope.SUBTREE, filter,
92              AliasDerefMode.DEREF_ALWAYS );
93          
94          if ( results.first() )
95          {
96              Entry entry = results.get();
97              results.close();
98  
99              return entry;
100         }
101 
102         return null;
103     }
104 
105 
106     /**
107      * Initializes and returns a {@link StoredProcEngine} instance which can operate on spUnitEntry
108      * considering its specific stored procedure language.
109      * 
110      * @param spUnitEntry The entry which a {@link StoredProcEngine} type will be mathched with respect to the language identifier.
111      * @return A {@link StoredProcEngine} associated with spUnitEntry.
112      * @throws org.apache.directory.api.ldap.model.exception.LdapException If no {@link StoredProcEngine} that can be associated with the language identifier in spUnitEntry can be found.
113      */
114     public StoredProcEngine getStoredProcEngineInstance( Entry spUnitEntry ) throws LdapException
115     {
116         String spLangId = ( ( ClonedServerEntry ) spUnitEntry ).getOriginalEntry().get( "storedProcLangId" )
117             .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 | IllegalAccessException e )
131                 {
132                     LdapException ne = new LdapException( e.getMessage(), e );
133                     ne.initCause( e );
134                     throw ne;
135                 }
136 
137                 engine.setSPUnitEntry( ( Entry ) ( ( ClonedServerEntry ) spUnitEntry ).getOriginalEntry() );
138                 return engine;
139             }
140 
141         }
142 
143         throw new LdapException( I18n.err( I18n.ERR_294, spLangId ) );
144 
145     }
146 
147 }