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.partition;
21  
22  
23  import org.apache.directory.server.core.DirectoryService;
24  import org.apache.directory.server.core.entry.ClonedServerEntry;
25  import org.apache.directory.server.core.interceptor.context.EntryOperationContext;
26  import org.apache.directory.server.core.interceptor.context.LookupOperationContext;
27  
28  import javax.naming.NameNotFoundException;
29  
30  
31  /**
32   * A {@link Partition} that helps users to implement their own partition.
33   * Most methods are implemented by default.  Please look at the description of
34   * each methods for the detail of implementations.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $Rev: 663827 $, $Date: 2008-06-06 07:38:03 +0200 (Fr, 06 Jun 2008) $
38   */
39  public abstract class AbstractPartition implements Partition
40  {
41      /** {@link DirectoryService} specified at {@link #init(DirectoryService)}. */
42      protected DirectoryService directoryService;
43      /** <tt>true</tt> if and only if this partition is initialized. */
44      protected boolean initialized;
45  
46  
47      protected AbstractPartition()
48      {
49      }
50  
51  
52      /**
53       * Sets up (<tt>directoryService</tt> and calls {@link #doInit()} where you have to put your
54       * initialization code in.  {@link #isInitialized()} will return <tt>true</tt> if
55       * {@link #doInit()} returns without any errors.  {@link #destroy()} is called automatically
56       * as a clean-up process if {@link #doInit()} throws an exception.
57       */
58      public final void init( DirectoryService directoryService ) throws Exception
59      {
60          if ( initialized )
61          {
62              // Already initialized.
63              return;
64          }
65  
66          this.directoryService = directoryService;
67          try
68          {
69              doInit();
70              initialized = true;
71          }
72          finally
73          {
74              if ( !initialized )
75              {
76                  destroy();
77              }
78          }
79      }
80      
81  
82      /**
83       * Override this method to put your initialization code.
84       */
85      protected void doInit()
86      {
87      }
88  
89  
90      /**
91       * Calls {@link #doDestroy()} where you have to put your destroy code in,
92       * and clears default properties.  Once this method is invoked, {@link #isInitialized()}
93       * will return <tt>false</tt>.
94       */
95      public final void destroy()
96      {
97          try
98          {
99              doDestroy();
100         }
101         finally
102         {
103             initialized = false;
104             directoryService = null;
105         }
106     }
107 
108 
109     /**
110      * Override this method to put your initialization code.
111      */
112     protected void doDestroy()
113     {
114     }
115 
116 
117     /**
118      * Returns <tt>true</tt> if this context partition is initialized successfully.
119      */
120     public final boolean isInitialized()
121     {
122         return initialized;
123     }
124 
125 
126     /**
127      * Returns {@link DirectoryService} that is provided from
128      * {@link #init(DirectoryService)}.
129      * @return return the directory service core
130      */
131     public final DirectoryService getDirectoryService()
132     {
133         return directoryService;
134     }
135 
136 
137     /**
138      * This method does nothing by default.
139      */
140     public void sync() throws Exception
141     {
142     }
143 
144 
145     /**
146      * This method calls {@link Partition#lookup(LookupOperationContext)} and return <tt>true</tt>
147      * if it returns an entry by default.  Please override this method if
148      * there is more effective way for your implementation.
149      */
150     public boolean hasEntry( EntryOperationContext entryContext ) throws Exception
151     {
152         try
153         {
154             return entryContext.lookup( entryContext.getDn(), ByPassConstants.LOOKUP_BYPASS ) != null; 
155         }
156         catch ( NameNotFoundException e )
157         {
158             return false;
159         }
160     }
161 
162 
163     /**
164      * This method calls {@link Partition#lookup(LookupOperationContext)}
165      * with null <tt>attributeIds</tt> by default.  Please override
166      * this method if there is more effective way for your implementation.
167      */
168     public ClonedServerEntry lookup( LookupOperationContext lookupContext ) throws Exception
169     {
170         return null;
171     }
172 }