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      public static final String TRIGGER_EXECUTION_SPECIFIC_AREA_VALUE = SchemaConstants.TRIGGER_EXECUTION_SPECIFIC_AREA;
45      public static final String TRIGGER_EXECUTION_SUBENTRY_OC = "triggerExecutionSubentry";
46      public static final String ENTRY_TRIGGER_SPECIFICATION_ATTR = "entryTriggerSpecification";
47      public static final String PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR = "prescriptiveTriggerSpecification";
48  
49  
50      /**
51       * Private constructor.
52       */
53      private TriggerUtils()
54      {
55      }
56  
57  
58      public static void defineTriggerExecutionSpecificPoint( LdapContext apCtx ) throws NamingException
59      {
60          Attributes ap = apCtx.getAttributes( "", new String[]
61              { SchemaConstants.ADMINISTRATIVE_ROLE_AT } );
62          Attribute administrativeRole = ap.get( SchemaConstants.ADMINISTRATIVE_ROLE_AT );
63          if ( administrativeRole == null
64              || !AttributeUtils.containsValueCaseIgnore( administrativeRole, TRIGGER_EXECUTION_SPECIFIC_AREA_VALUE ) )
65          {
66              Attributes changes = new BasicAttributes( SchemaConstants.ADMINISTRATIVE_ROLE_AT,
67                  TRIGGER_EXECUTION_SPECIFIC_AREA_VALUE, true );
68              apCtx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
69          }
70      }
71  
72  
73      public static void createTriggerExecutionSubentry(
74          LdapContext apCtx,
75          String subentryCN,
76          String subtreeSpec,
77          String prescriptiveTriggerSpec ) throws NamingException
78      {
79          Attributes subentry = new BasicAttributes( SchemaConstants.CN_AT, subentryCN, true );
80          Attribute objectClass = new BasicAttribute( SchemaConstants.OBJECT_CLASS_AT );
81          subentry.put( objectClass );
82          objectClass.add( SchemaConstants.TOP_OC );
83          objectClass.add( SchemaConstants.SUBENTRY_OC );
84          objectClass.add( TRIGGER_EXECUTION_SUBENTRY_OC );
85          subentry.put( SchemaConstants.SUBTREE_SPECIFICATION_AT, subtreeSpec );
86          subentry.put( PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR, prescriptiveTriggerSpec );
87          apCtx.createSubcontext( "cn=" + subentryCN, subentry );
88      }
89  
90  
91      public static void loadPrescriptiveTriggerSpecification(
92          LdapContext apCtx,
93          String subentryCN,
94          String triggerSpec ) throws NamingException
95      {
96          Attributes changes = new BasicAttributes( PRESCRIPTIVE_TRIGGER_SPECIFICATION_ATTR, triggerSpec, true );
97          apCtx.modifyAttributes( "cn=" + subentryCN, DirContext.ADD_ATTRIBUTE, changes );
98      }
99  
100 
101     public static void loadEntryTriggerSpecification(
102         LdapContext ctx,
103         String triggerSpec ) throws NamingException
104     {
105         Attributes changes = new BasicAttributes( ENTRY_TRIGGER_SPECIFICATION_ATTR, triggerSpec, true );
106         ctx.modifyAttributes( "", DirContext.ADD_ATTRIBUTE, changes );
107     }
108 }