KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > jobs > InitializeChildrenJob


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.ldapstudio.browser.core.jobs;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
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 JavaDoc[] getLockedObjects()
69     {
70         List JavaDoc l = new ArrayList JavaDoc();
71         l.addAll( Arrays.asList( entries ) );
72         return l.toArray();
73     }
74
75
76     protected String JavaDoc 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 ); //$NON-NLS-1$
86
monitor.reportProgress( " " ); //$NON-NLS-1$
87

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 JavaDoc[]
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 JavaDoc[]
122                 { parent.getDn().toString() } ) );
123
124         // clear old children
125
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             // special handling for Root DSE
138
//parent.setChildrenInitialized( true );
139
//return;
140
parent.getConnection().fetchRootDSE( monitor );
141             parent.setChildrenInitialized( true );
142             return;
143         }
144         else
145         {
146             // determine alias and referral handling
147
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             // get children,
161
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 JavaDoc[]
171                     { srs == null ? Integer.toString( 0 ) : Integer.toString( srs.length ), parent.getDn().toString() } ) );
172
173             // fill children in search result
174
if ( srs != null && srs.length > 0 )
175             {
176     
177                 /*
178                  * clearing old children before filling new subenties is
179                  * necessary to handle aliases and referrals.
180                  */

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                         // System.out.println("Ref: " +
199
// referralBaseEntry.getUrl());
200
}
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                         // System.out.println("Ali: " +
207
// aliasBaseEntry.getUrl());
208
}
209                     else
210                     {
211                         parent.addChild( srs[i].getEntry() );
212                     }
213                 }
214             }
215             else
216             {
217                 parent.setHasChildrenHint( false );
218             }
219     
220             // get subentries
221
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 JavaDoc[]
235                         { subSrs == null ? Integer.toString( 0 ) : Integer.toString( subSrs.length ),
236                             parent.getDn().toString() } ) );
237                 // fill children in search result
238
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             // check exceeded limits / canceled
249
parent.setHasMoreChildren( search.isCountLimitExceeded() || subSearch.isCountLimitExceeded()
250                 || monitor.isCanceled() );
251         }
252
253         // set initialized state
254
parent.setChildrenInitialized( true );
255
256     }
257
258 }
259
Popular Tags