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.sp;
21  
22  
23  import org.apache.directory.server.core.CoreSession;
24  import org.apache.directory.server.core.entry.ClonedServerEntry;
25  import org.apache.directory.server.core.entry.ServerEntry;
26  import org.apache.directory.server.core.entry.ServerStringValue;
27  import org.apache.directory.server.core.filtering.EntryFilteringCursor;
28  import org.apache.directory.shared.ldap.constants.SchemaConstants;
29  import org.apache.directory.shared.ldap.filter.EqualityNode;
30  import org.apache.directory.shared.ldap.filter.ExprNode;
31  import org.apache.directory.shared.ldap.filter.SearchScope;
32  import org.apache.directory.shared.ldap.message.AliasDerefMode;
33  import org.apache.directory.shared.ldap.name.LdapDN;
34  import org.apache.directory.shared.ldap.schema.AttributeType;
35  import org.apache.directory.shared.ldap.schema.AttributeTypeOptions;
36  
37  import javax.naming.NamingException;
38  import javax.naming.directory.SearchControls;
39  
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Set;
43  
44  
45  /**
46   * A Factory type class which holds a registry of supported {@link StoredProcEngineConfig}s. A container reference
47   * as the base for Stored Procedure storage on the DIT is also handled by this class.
48   * 
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   * @version $Rev$ $Date$
51   */
52  public class StoredProcExecutionManager
53  {
54      private final static Set<AttributeTypeOptions> EMPTY_ATTRIBS = Collections.emptySet();
55      
56      private final String storedProcContainer;
57  
58      private final List<StoredProcEngineConfig> storedProcEngineConfigs;
59  
60  
61      /**
62       * Creates a {@link StoredProcExecutionManager} instance.
63       * 
64       * @param storedProcContainer The base of the DIT subtree used for storing stored procedure units.
65       * @param storedProcEngineConfigs A list of {@link StoredProcEngineConfig}s to register different {@link StoredProcEngine}s with this manager.
66       */
67      public StoredProcExecutionManager( final String storedProcContainer, final List<StoredProcEngineConfig> storedProcEngineConfigs )
68      {
69          this.storedProcContainer = storedProcContainer;
70          this.storedProcEngineConfigs = storedProcEngineConfigs;
71      }
72      
73      /**
74       * Finds and returns a stored procedure unit entry whose identifier name
75       * is extracted from fullSPName.
76       * 
77       * @param rootDSE A handle on the root DSE to be used for searching the SP Unit over.
78       * @param fullSPName Full name of the Stored Procedure including the unit name.
79       * @return The entry associated with the SP Unit.
80       * @throws NamingException If the unit cannot be located or any other error occurs.
81       */
82      public ClonedServerEntry findStoredProcUnit( CoreSession session, String fullSPName ) throws Exception
83      {
84          SearchControls controls = new SearchControls();
85          controls.setReturningAttributes( SchemaConstants.ALL_USER_ATTRIBUTES_ARRAY );
86          controls.setSearchScope( SearchControls.SUBTREE_SCOPE );
87          String spUnitName = StoredProcUtils.extractStoredProcUnitName( fullSPName );
88          
89          AttributeType at = session.getDirectoryService()
90              .getRegistries().getAttributeTypeRegistry().lookup( "storedProcUnitName" );
91          ExprNode filter = new EqualityNode<String>( "storedProcUnitName", new ServerStringValue( at, spUnitName ) );
92          LdapDN dn = new LdapDN( storedProcContainer );
93          EntryFilteringCursor results = session.search( dn, SearchScope.SUBTREE, filter, 
94              AliasDerefMode.DEREF_ALWAYS, EMPTY_ATTRIBS );
95          if ( results.first() )
96          {
97              ClonedServerEntry entry = results.get();
98              results.close();
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 NamingException If no {@link StoredProcEngine} that can be associated with the language identifier in spUnitEntry can be found.
113      */
114     public StoredProcEngine getStoredProcEngineInstance( ClonedServerEntry spUnitEntry ) throws NamingException
115     {
116         String spLangId = ( String ) spUnitEntry.getOriginalEntry().get( "storedProcLangId" ).getString();
117 
118         for ( StoredProcEngineConfig engineConfig : storedProcEngineConfigs )
119         {
120             if ( engineConfig.getStoredProcLangId().equalsIgnoreCase( spLangId ) )
121             {
122                 Class<? extends StoredProcEngine> engineType = engineConfig.getStoredProcEngineType();
123                 StoredProcEngine engine;
124                 
125                 try
126                 {
127                     engine = engineType.newInstance();
128                 }
129                 catch ( InstantiationException e )
130                 {
131                     NamingException ne = new NamingException();
132                     ne.setRootCause( e );
133                     throw ne;
134                 }
135                 catch ( IllegalAccessException e )
136                 {
137                     NamingException ne = new NamingException();
138                     ne.setRootCause( e );
139                     throw ne;
140                 }
141                 
142                 engine.setSPUnitEntry( (ServerEntry)spUnitEntry.getOriginalEntry() );
143                 return engine;
144             }
145 
146         }
147 
148         throw new NamingException( "Stored Procedure Language, " + spLangId + " is not supported." );
149 
150     }
151 
152 }