KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.BufferedWriter JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.commons.codec.digest.DigestUtils;
32 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
33 import org.apache.directory.ldapstudio.browser.core.internal.model.AttributeComparator;
34 import org.apache.directory.ldapstudio.browser.core.internal.model.ConnectionException;
35 import org.apache.directory.ldapstudio.browser.core.internal.model.ReferralException;
36 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
37 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
38 import org.apache.directory.ldapstudio.browser.core.model.SearchParameter;
39 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEnumeration;
40 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
43 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDnLine;
44 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine;
45
46
47 public class ExportLdifJob extends AbstractEclipseJob
48 {
49
50     private String JavaDoc exportLdifFilename;
51
52     private IConnection connection;
53
54     private SearchParameter searchParameter;
55
56
57     public ExportLdifJob( String JavaDoc exportLdifFilename, IConnection connection, SearchParameter searchParameter )
58     {
59         this.exportLdifFilename = exportLdifFilename;
60         this.connection = connection;
61         this.searchParameter = searchParameter;
62
63         setName( BrowserCoreMessages.jobs__export_ldif_name );
64     }
65
66
67     protected IConnection[] getConnections()
68     {
69         return new IConnection[]
70             { connection };
71     }
72
73
74     protected Object JavaDoc[] getLockedObjects()
75     {
76         List JavaDoc l = new ArrayList JavaDoc();
77         l.add( connection.getUrl() + "_" + DigestUtils.shaHex( exportLdifFilename ) );
78         return l.toArray();
79     }
80
81
82     protected void executeAsyncJob( ExtendedProgressMonitor monitor )
83     {
84
85         monitor.beginTask( BrowserCoreMessages.jobs__export_ldif_task, 2 );
86         monitor.reportProgress( " " ); //$NON-NLS-1$
87
monitor.worked( 1 );
88
89         try
90         {
91             // open file
92
FileWriter JavaDoc fileWriter = new FileWriter JavaDoc( exportLdifFilename );
93             BufferedWriter JavaDoc bufferedWriter = new BufferedWriter JavaDoc( fileWriter );
94
95             // export
96
int count = 0;
97             export( connection, searchParameter, bufferedWriter, count, monitor );
98
99             // close file
100
bufferedWriter.close();
101             fileWriter.close();
102
103         }
104         catch ( Exception JavaDoc e )
105         {
106             monitor.reportError( e );
107         }
108
109     }
110
111
112     private static void export( IConnection connection, SearchParameter searchParameter, BufferedWriter JavaDoc bufferedWriter,
113         int count, ExtendedProgressMonitor monitor ) throws IOException JavaDoc, ConnectionException
114     {
115         try
116         {
117
118             AttributeComparator comparator = new AttributeComparator( connection );
119             LdifEnumeration enumeration = connection.exportLdif( searchParameter, monitor );
120             while ( !monitor.isCanceled() && enumeration.hasNext( monitor ) )
121             {
122                 LdifContainer container = enumeration.next( monitor );
123
124                 if ( container instanceof LdifContentRecord )
125                 {
126                     LdifContentRecord record = ( LdifContentRecord ) container;
127                     LdifDnLine dnLine = record.getDnLine();
128                     LdifAttrValLine[] attrValLines = record.getAttrVals();
129                     LdifSepLine sepLine = record.getSepLine();
130
131                     // sort and format
132
Arrays.sort( attrValLines, comparator );
133                     LdifContentRecord newRecord = new LdifContentRecord( dnLine );
134                     for ( int i = 0; i < attrValLines.length; i++ )
135                     {
136                         newRecord.addAttrVal( attrValLines[i] );
137                     }
138                     newRecord.finish( sepLine );
139                     String JavaDoc s = newRecord.toFormattedString();
140
141                     // String s = record.toFormattedString();
142
bufferedWriter.write( s );
143
144                     count++;
145                     monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__export_progress,
146                         new String JavaDoc[]
147                             { Integer.toString( count ) } ) );
148
149                 }
150
151             }
152         }
153         catch ( ConnectionException ce )
154         {
155
156             if ( ce.getLdapStatusCode() == 3 || ce.getLdapStatusCode() == 4 || ce.getLdapStatusCode() == 11 )
157             {
158                 // nothing
159
}
160             else if ( ce instanceof ReferralException )
161             {
162
163                 if ( searchParameter.getReferralsHandlingMethod() == IConnection.HANDLE_REFERRALS_FOLLOW )
164                 {
165
166                     ReferralException re = ( ReferralException ) ce;
167                     ISearch[] referralSearches = re.getReferralSearches();
168                     for ( int i = 0; i < referralSearches.length; i++ )
169                     {
170                         ISearch referralSearch = referralSearches[i];
171
172                         // open connection
173
if ( !referralSearch.getConnection().isOpened() )
174                         {
175                             referralSearch.getConnection().open( monitor );
176                         }
177
178                         // export recursive
179
export( referralSearch.getConnection(), referralSearch.getSearchParameter(), bufferedWriter,
180                             count, monitor );
181                     }
182                 }
183             }
184             else
185             {
186                 monitor.reportError( ce );
187             }
188         }
189
190     }
191
192
193     protected String JavaDoc getErrorMessage()
194     {
195         return BrowserCoreMessages.jobs__export_ldif_error;
196     }
197
198 }
199
Popular Tags