KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.io.Writer JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.apache.commons.codec.digest.DigestUtils;
36 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
37 import org.apache.directory.ldapstudio.browser.core.events.BulkModificationEvent;
38 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
39 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
40 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEnumeration;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.parser.LdifParser;
43
44
45 public class ImportLdifJob extends AbstractAsyncBulkJob
46 {
47
48     private IConnection connection;
49
50     private File JavaDoc ldifFile;
51
52     private File JavaDoc logFile;
53
54     private boolean continueOnError;
55
56
57     public ImportLdifJob( IConnection connection, File JavaDoc ldifFile, File JavaDoc logFile, boolean continueOnError )
58     {
59         this.connection = connection;
60         this.ldifFile = ldifFile;
61         this.logFile = logFile;
62         this.continueOnError = continueOnError;
63
64         setName( BrowserCoreMessages.jobs__import_ldif_name );
65     }
66
67
68     public ImportLdifJob( IConnection connection, File JavaDoc ldifFile, boolean continueOnError )
69     {
70         this( connection, ldifFile, null, continueOnError );
71     }
72
73
74     protected IConnection[] getConnections()
75     {
76         return new IConnection[]
77             { connection };
78     }
79
80
81     protected Object JavaDoc[] getLockedObjects()
82     {
83         List JavaDoc l = new ArrayList JavaDoc();
84         l.add( connection.getUrl() + "_" + DigestUtils.shaHex( ldifFile.toString() ) );
85         return l.toArray();
86     }
87
88
89     protected void executeBulkJob( ExtendedProgressMonitor monitor ) throws ModelModificationException
90     {
91
92         monitor.beginTask( BrowserCoreMessages.jobs__import_ldif_task, 2 );
93         monitor.reportProgress( " " ); //$NON-NLS-1$
94
monitor.worked( 1 );
95
96         try
97         {
98             Reader JavaDoc ldifReader = new BufferedReader JavaDoc( new FileReader JavaDoc( this.ldifFile ) );
99             LdifParser parser = new LdifParser();
100             LdifEnumeration enumeration = parser.parse( ldifReader );
101
102             Writer JavaDoc logWriter;
103             if ( this.logFile != null )
104             {
105                 logWriter = new BufferedWriter JavaDoc( new FileWriter JavaDoc( this.logFile ) );
106             }
107             else
108             {
109                 logWriter = new Writer JavaDoc()
110                 {
111                     public void close() throws IOException JavaDoc
112                     {
113                     }
114
115
116                     public void flush() throws IOException JavaDoc
117                     {
118                     }
119
120
121                     public void write( char[] cbuf, int off, int len ) throws IOException JavaDoc
122                     {
123                     }
124                 };
125             }
126
127             connection.importLdif( enumeration, logWriter, continueOnError, monitor );
128
129             logWriter.close();
130             ldifReader.close();
131         }
132         catch ( Exception JavaDoc e )
133         {
134             monitor.reportError( e );
135         }
136     }
137
138
139     protected String JavaDoc getErrorMessage()
140     {
141         return BrowserCoreMessages.jobs__import_ldif_error;
142     }
143
144
145     protected void runNotification()
146     {
147         EventRegistry.fireEntryUpdated( new BulkModificationEvent( connection ), this );
148     }
149
150 }
151
Popular Tags