001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.core.partition; 021 022 023 import javax.naming.InvalidNameException; 024 import javax.naming.NameNotFoundException; 025 026 import org.apache.directory.server.core.entry.ClonedServerEntry; 027 import org.apache.directory.server.core.interceptor.context.EntryOperationContext; 028 import org.apache.directory.server.core.interceptor.context.LookupOperationContext; 029 030 031 /** 032 * A {@link Partition} that helps users to implement their own partition. 033 * Most methods are implemented by default. Please look at the description of 034 * each methods for the detail of implementations. 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 * @version $Rev: 925506 $, $Date: 2010-03-20 01:50:21 +0100 (Sat, 20 Mar 2010) $ 038 */ 039 public abstract class AbstractPartition implements Partition 040 { 041 /** <tt>true</tt> if and only if this partition is initialized. */ 042 protected boolean initialized; 043 044 protected AbstractPartition() 045 { 046 } 047 048 049 /** 050 * Sets up (<tt>directoryService</tt> and calls {@link #doInit()} where you have to put your 051 * initialization code in. {@link #isInitialized()} will return <tt>true</tt> if 052 * {@link #doInit()} returns without any errors. {@link #destroy()} is called automatically 053 * as a clean-up process if {@link #doInit()} throws an exception. 054 */ 055 public final void initialize( ) throws Exception 056 { 057 if ( initialized ) 058 { 059 // Already initialized. 060 return; 061 } 062 063 try 064 { 065 doInit(); 066 initialized = true; 067 } 068 catch ( Exception e ) 069 { 070 e.printStackTrace(); 071 } 072 finally 073 { 074 if ( !initialized ) 075 { 076 destroy(); 077 } 078 } 079 } 080 081 082 /** 083 * Override this method to put your initialization code. 084 * @throws Exception 085 */ 086 protected abstract void doInit() throws InvalidNameException, Exception; 087 088 089 /** 090 * Calls {@link #doDestroy()} where you have to put your destroy code in, 091 * and clears default properties. Once this method is invoked, {@link #isInitialized()} 092 * will return <tt>false</tt>. 093 */ 094 public final void destroy() throws Exception 095 { 096 try 097 { 098 doDestroy(); 099 } 100 finally 101 { 102 initialized = false; 103 } 104 } 105 106 107 /** 108 * Override this method to put your initialization code. 109 */ 110 protected abstract void doDestroy() throws Exception; 111 112 113 /** 114 * Returns <tt>true</tt> if this context partition is initialized successfully. 115 */ 116 public boolean isInitialized() 117 { 118 return initialized; 119 } 120 121 122 /** 123 * This method does nothing by default. 124 */ 125 public abstract void sync() throws Exception; 126 127 128 /** 129 * This method calls {@link Partition#lookup(LookupOperationContext)} and return <tt>true</tt> 130 * if it returns an entry by default. Please override this method if 131 * there is more effective way for your implementation. 132 */ 133 public boolean hasEntry( EntryOperationContext entryContext ) throws Exception 134 { 135 try 136 { 137 return entryContext.lookup( entryContext.getDn(), ByPassConstants.LOOKUP_BYPASS ) != null; 138 } 139 catch ( NameNotFoundException e ) 140 { 141 return false; 142 } 143 } 144 145 146 /** 147 * This method calls {@link Partition#lookup(LookupOperationContext)} 148 * with null <tt>attributeIds</tt> by default. Please override 149 * this method if there is more effective way for your implementation. 150 */ 151 public abstract ClonedServerEntry lookup( LookupOperationContext lookupContext ) throws Exception; 152 }