1 20 21 package org.apache.directory.ldapstudio.browser.core.jobs; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 27 28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 29 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 30 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 31 import org.apache.directory.ldapstudio.browser.core.events.ChildrenInitializedEvent; 32 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 33 import org.apache.directory.ldapstudio.browser.core.internal.model.AliasBaseEntry; 34 import org.apache.directory.ldapstudio.browser.core.internal.model.ReferralBaseEntry; 35 import org.apache.directory.ldapstudio.browser.core.internal.model.Search; 36 import org.apache.directory.ldapstudio.browser.core.model.Control; 37 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 38 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 39 import org.apache.directory.ldapstudio.browser.core.model.IRootDSE; 40 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 41 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 42 43 44 public class InitializeChildrenJob extends AbstractAsyncBulkJob 45 { 46 47 private IEntry[] entries; 48 49 50 public InitializeChildrenJob( IEntry[] entries ) 51 { 52 this.entries = entries; 53 setName( BrowserCoreMessages.jobs__init_entries_title_subonly ); 54 } 55 56 57 protected IConnection[] getConnections() 58 { 59 IConnection[] connections = new IConnection[entries.length]; 60 for ( int i = 0; i < connections.length; i++ ) 61 { 62 connections[i] = entries[i].getConnection(); 63 } 64 return connections; 65 } 66 67 68 protected Object [] getLockedObjects() 69 { 70 List l = new ArrayList (); 71 l.addAll( Arrays.asList( entries ) ); 72 return l.toArray(); 73 } 74 75 76 protected String getErrorMessage() 77 { 78 return entries.length == 1 ? BrowserCoreMessages.jobs__init_entries_error_1 79 : BrowserCoreMessages.jobs__init_entries_error_n; 80 } 81 82 83 protected void executeBulkJob( ExtendedProgressMonitor monitor ) 84 { 85 monitor.beginTask( " ", entries.length + 2 ); monitor.reportProgress( " " ); 88 for ( int pi = 0; pi < entries.length && !monitor.isCanceled(); pi++ ) 89 { 90 91 monitor.setTaskName( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_task, new String [] 92 { this.entries[pi].getDn().toString() } ) ); 93 monitor.worked( 1 ); 94 95 if ( entries[pi].getConnection() != null && entries[pi].getConnection().isOpened() 96 && entries[pi].isDirectoryEntry() ) 97 { 98 initializeChildren( entries[pi], monitor ); 99 } 100 } 101 } 102 103 104 protected void runNotification() 105 { 106 for ( int pi = 0; pi < entries.length; pi++ ) 107 { 108 IEntry parent = entries[pi]; 109 if ( parent.getConnection() != null && entries[pi].getConnection().isOpened() && parent.isDirectoryEntry() ) 110 { 111 EventRegistry.fireEntryUpdated( new ChildrenInitializedEvent( parent ), this ); 112 } 113 } 114 } 115 116 117 public static void initializeChildren( IEntry parent, ExtendedProgressMonitor monitor ) 118 { 119 120 monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_progress_sub, 121 new String [] 122 { parent.getDn().toString() } ) ); 123 124 IEntry[] oldChildren = parent.getChildren(); 126 for ( int i = 0; oldChildren != null && i < oldChildren.length; i++ ) 127 { 128 if ( oldChildren[i] != null ) 129 { 130 parent.deleteChild( oldChildren[i] ); 131 } 132 } 133 parent.setChildrenInitialized( false ); 134 135 if ( parent instanceof IRootDSE ) 136 { 137 parent.getConnection().fetchRootDSE( monitor ); 141 parent.setChildrenInitialized( true ); 142 return; 143 } 144 else 145 { 146 int scope = ISearch.SCOPE_ONELEVEL; 148 int derefAliasMethod = parent.getConnection().getAliasesDereferencingMethod(); 149 int handleReferralsMethod = parent.getConnection().getReferralsHandlingMethod(); 150 if ( BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean( 151 BrowserCoreConstants.PREFERENCE_SHOW_ALIAS_AND_REFERRAL_OBJECTS ) ) 152 { 153 scope = ( parent.isAlias() || parent.isReferral() ) ? ISearch.SCOPE_OBJECT : ISearch.SCOPE_ONELEVEL; 154 derefAliasMethod = parent.isAlias() ? IConnection.DEREFERENCE_ALIASES_FINDING 155 : IConnection.DEREFERENCE_ALIASES_NEVER; 156 handleReferralsMethod = parent.isReferral() ? IConnection.HANDLE_REFERRALS_FOLLOW 157 : IConnection.HANDLE_REFERRALS_IGNORE; 158 } 159 160 ISearch search = new Search( null, parent.getConnection(), parent.getDn(), parent.getChildrenFilter(), 162 ISearch.NO_ATTRIBUTES, scope, parent.getConnection().getCountLimit(), 163 parent.getConnection().getTimeLimit(), derefAliasMethod, handleReferralsMethod, BrowserCorePlugin 164 .getDefault().getPluginPreferences().getBoolean( BrowserCoreConstants.PREFERENCE_CHECK_FOR_CHILDREN ), 165 BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean( 166 BrowserCoreConstants.PREFERENCE_SHOW_ALIAS_AND_REFERRAL_OBJECTS ), null ); 167 parent.getConnection().search( search, monitor ); 168 ISearchResult[] srs = search.getSearchResults(); 169 monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_progress_subcount, 170 new String [] 171 { srs == null ? Integer.toString( 0 ) : Integer.toString( srs.length ), parent.getDn().toString() } ) ); 172 173 if ( srs != null && srs.length > 0 ) 175 { 176 177 181 IEntry[] connChildren = parent.getChildren(); 182 for ( int i = 0; connChildren != null && i < connChildren.length; i++ ) 183 { 184 if ( connChildren[i] != null ) 185 { 186 parent.deleteChild( connChildren[i] ); 187 } 188 } 189 parent.setChildrenInitialized( false ); 190 191 for ( int i = 0; srs != null && i < srs.length; i++ ) 192 { 193 if ( parent.isReferral() ) 194 { 195 ReferralBaseEntry referralBaseEntry = new ReferralBaseEntry( srs[i].getEntry().getConnection(), 196 srs[i].getEntry().getDn() ); 197 parent.addChild( referralBaseEntry ); 198 } 201 else if ( parent.isAlias() ) 202 { 203 AliasBaseEntry aliasBaseEntry = new AliasBaseEntry( srs[i].getEntry().getConnection(), srs[i] 204 .getEntry().getDn() ); 205 parent.addChild( aliasBaseEntry ); 206 } 209 else 210 { 211 parent.addChild( srs[i].getEntry() ); 212 } 213 } 214 } 215 else 216 { 217 parent.setHasChildrenHint( false ); 218 } 219 220 ISearch subSearch = new Search( null, parent.getConnection(), parent.getDn(), parent.getChildrenFilter(), 222 ISearch.NO_ATTRIBUTES, scope, parent.getConnection().getCountLimit(), 223 parent.getConnection().getTimeLimit(), derefAliasMethod, handleReferralsMethod, BrowserCorePlugin 224 .getDefault().getPluginPreferences().getBoolean( BrowserCoreConstants.PREFERENCE_CHECK_FOR_CHILDREN ), 225 BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean( 226 BrowserCoreConstants.PREFERENCE_SHOW_ALIAS_AND_REFERRAL_OBJECTS ), new Control[] 227 { Control.SUBENTRIES_CONTROL } ); 228 if ( BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean( 229 BrowserCoreConstants.PREFERENCE_FETCH_SUBENTRIES ) ) 230 { 231 parent.getConnection().search( subSearch, monitor ); 232 ISearchResult[] subSrs = subSearch.getSearchResults(); 233 monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_progress_subcount, 234 new String [] 235 { subSrs == null ? Integer.toString( 0 ) : Integer.toString( subSrs.length ), 236 parent.getDn().toString() } ) ); 237 if ( subSrs != null && subSrs.length > 0 ) 239 { 240 241 for ( int i = 0; subSrs != null && i < subSrs.length; i++ ) 242 { 243 parent.addChild( subSrs[i].getEntry() ); 244 } 245 } 246 } 247 248 parent.setHasMoreChildren( search.isCountLimitExceeded() || subSearch.isCountLimitExceeded() 250 || monitor.isCanceled() ); 251 } 252 253 parent.setChildrenInitialized( true ); 255 256 } 257 258 } 259 | Popular Tags |