KickJava   Java API By Example, From Geeks To Geeks.

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


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.Reader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.commons.codec.digest.DigestUtils;
31 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
32 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
33 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEnumeration;
34 import org.apache.directory.ldapstudio.browser.core.model.ldif.parser.LdifParser;
35
36
37 public class ExecuteLdifJob extends AbstractEclipseJob
38 {
39
40     private IConnection connection;
41
42     private String JavaDoc ldif;
43
44     private boolean continueOnError;
45
46
47     public ExecuteLdifJob( IConnection connection, String JavaDoc ldif, boolean continueOnError )
48     {
49         this.connection = connection;
50         this.ldif = ldif;
51         this.continueOnError = continueOnError;
52
53         setName( BrowserCoreMessages.jobs__execute_ldif_name );
54     }
55
56
57     protected IConnection[] getConnections()
58     {
59         return new IConnection[]
60             { connection };
61     }
62
63
64     protected Object JavaDoc[] getLockedObjects()
65     {
66         List JavaDoc l = new ArrayList JavaDoc();
67         l.add( connection.getUrl() + "_" + DigestUtils.shaHex( ldif ) );
68         return l.toArray();
69     }
70
71
72     protected void executeAsyncJob( ExtendedProgressMonitor monitor )
73     {
74
75         monitor.beginTask( BrowserCoreMessages.jobs__execute_ldif_task, 2 );
76         monitor.reportProgress( " " ); //$NON-NLS-1$
77
monitor.worked( 1 );
78
79         try
80         {
81             Reader JavaDoc ldifReader = new StringReader JavaDoc( this.ldif );
82             LdifParser parser = new LdifParser();
83             LdifEnumeration enumeration = parser.parse( ldifReader );
84
85             Writer JavaDoc logWriter = new Writer JavaDoc()
86             {
87                 public void close()
88                 {
89                 }
90
91
92                 public void flush()
93                 {
94                 }
95
96
97                 public void write( char[] cbuf, int off, int len )
98                 {
99                 }
100             };
101
102             connection.importLdif( enumeration, logWriter, continueOnError, monitor );
103
104             logWriter.close();
105             ldifReader.close();
106         }
107         catch ( Exception JavaDoc e )
108         {
109             monitor.reportError( e );
110         }
111     }
112
113
114     protected String JavaDoc getErrorMessage()
115     {
116         return BrowserCoreMessages.jobs__execute_ldif_error;
117     }
118
119 }
120
Popular Tags