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  
21  package org.apache.directory.api.ldap.trigger;
22  
23  
24  import javax.naming.NamingException;
25  import javax.naming.directory.Attribute;
26  import javax.naming.directory.Attributes;
27  import javax.naming.directory.BasicAttribute;
28  import javax.naming.directory.BasicAttributes;
29  import javax.naming.directory.DirContext;
30  import javax.naming.ldap.LdapContext;
31  
32  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
33  import org.apache.directory.api.ldap.model.entry.AttributeUtils;
34  
35  
36  /**
37   * A utility class for working with Triggers Execution Administrative Points
38   * Trigger Execution Subentries and Trigger Specifications.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public final class TriggerUtils
43  {
44      /**
45       * Private constructor.
46       */
47      private TriggerUtils()
48      {
49      }
50  
51  
52      /**
53       * Defines the Administration point and administrative role for the TriggerExecution specific point
54       * @param apCtx The administrative point context
55       * @throws NamingException If the operation failed
56       */
57      public static void defineTriggerExecutionSpecificPoint( LdapContext apCtx ) throws NamingException
58      {
59          Attributes ap = apCtx.getAttributes( "", new String[] { SchemaConstants.ADMINISTRATIVE_ROLE_AT } );
60          Attribute administrativeRole = ap.get( SchemaConstants.ADMINISTRATIVE_ROLE_AT );
61          
62          if ( ( administrativeRole == null )
63              || !AttributeUtils.containsValueCaseIgnore( administrativeRole, SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA ) )
64          {
65              Attributes changes = new BasicAttributes( SchemaConstants.ADMINISTRATIVE_ROLE_AT,
66                  SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA, true );
67              apCtx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
68          }
69      }
70  
71  
72      /**
73       * Create the Trigger execution subentry
74       * 
75       * @param apCtx The administration point context
76       * @param subentryCN The CN used by the suentry
77       * @param subtreeSpec The subtree specification
78       * @param prescriptiveTriggerSpec The prescriptive trigger specification
79       * @throws NamingException If the operation failed
80       */
81      public static void createTriggerExecutionSubentry(
82          LdapContext apCtx,
83          String subentryCN,
84          String subtreeSpec,
85          String prescriptiveTriggerSpec ) throws NamingException
86      {
87          Attributes subentry = new BasicAttributes( SchemaConstants.CN_AT, subentryCN, true );
88          Attribute objectClass = new BasicAttribute( SchemaConstants.OBJECT_CLASS_AT );
89          subentry.put( objectClass );
90          objectClass.add( SchemaConstants.TOP_OC );
91          objectClass.add( SchemaConstants.SUBENTRY_OC );
92          objectClass.add( SchemaConstants.TRIGGER_EXECUTION_SUBENTRY_OC );
93          subentry.put( SchemaConstants.SUBTREE_SPECIFICATION_AT, subtreeSpec );
94          subentry.put( SchemaConstants.PRESCRIPTIVE_TRIGGER_SPECIFICATION_AT, prescriptiveTriggerSpec );
95          apCtx.createSubcontext( "cn=" + subentryCN, subentry );
96      }
97  
98  
99      /**
100      * Load an prescriptive trigger specification
101      * 
102      * @param apCtx The administrative point context
103      * @param subentryCN The subentry CN
104      * @param triggerSpec The trigger specification
105      * @throws NamingException If the operation failed
106      */
107     public static void loadPrescriptiveTriggerSpecification(
108         LdapContext apCtx,
109         String subentryCN,
110         String triggerSpec ) throws NamingException
111     {
112         Attributes changes = new BasicAttributes( SchemaConstants.PRESCRIPTIVE_TRIGGER_SPECIFICATION_AT, triggerSpec, true );
113         apCtx.modifyAttributes( "cn=" + subentryCN, DirContext.ADD_ATTRIBUTE, changes );
114     }
115 
116 
117     /**
118      * Load the trigger specification entry
119      * 
120      * @param ctx The context
121      * @param triggerSpec The trigger specification
122      * @throws NamingException If the operation failed
123      */
124     public static void loadEntryTriggerSpecification(
125         LdapContext ctx,
126         String triggerSpec ) throws NamingException
127     {
128         Attributes changes = new BasicAttributes( SchemaConstants.ENTRY_TRIGGER_SPECIFICATION_AT, triggerSpec, true );
129         ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
130     }
131 }