KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.PrintStream 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.dsmlv2.Dsmlv2ResponseParser;
34 import org.apache.directory.ldapstudio.dsmlv2.engine.Dsmlv2Engine;
35 import org.apache.directory.ldapstudio.dsmlv2.reponse.ErrorResponse;
36 import org.apache.directory.shared.ldap.codec.LdapResponse;
37 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
38
39
40 /**
41  * This class implements a Job for Importing a DSML File into a LDAP server
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class ImportDsmlJob extends AbstractEclipseJob
47 {
48     /** The connection to use */
49     private IConnection connection;
50
51     /** The DSML file to use */
52     private File JavaDoc dsmlFile;
53
54     /** The Save file to use */
55     private File JavaDoc responseFile;
56
57
58     /**
59      * Creates a new instance of ImportDsmlJob.
60      *
61      * @param connection
62      * The connection to use
63      * @param dsmlFile
64      * The DSML file to read from
65      * @param saveFile
66      * The Save file to use
67      * @param continueOnError
68      * The ContinueOnError flag
69      */

70     public ImportDsmlJob( IConnection connection, File JavaDoc dsmlFile, File JavaDoc saveFile )
71     {
72         this.connection = connection;
73         this.dsmlFile = dsmlFile;
74         this.responseFile = saveFile;
75
76         setName( BrowserCoreMessages.jobs__import_dsml_name );
77     }
78
79
80     /**
81      * Creates a new instance of ImportDsmlJob.
82      *
83      * @param connection
84      * The Connection to use
85      * @param dsmlFile
86      * The DSML file to read from
87      * @param continueOnError
88      * The ContinueOnError flag
89      */

90     public ImportDsmlJob( IConnection connection, File JavaDoc dsmlFile )
91     {
92         this( connection, dsmlFile, null );
93     }
94
95
96     /* (non-Javadoc)
97      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#getConnections()
98      */

99     protected IConnection[] getConnections()
100     {
101         return new IConnection[]
102             { connection };
103     }
104
105
106     /* (non-Javadoc)
107      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#getLockedObjects()
108      */

109     protected Object JavaDoc[] getLockedObjects()
110     {
111         List JavaDoc l = new ArrayList JavaDoc();
112         l.add( connection.getUrl() + "_" + DigestUtils.shaHex( dsmlFile.toString() ) );
113         return l.toArray();
114     }
115
116
117     /* (non-Javadoc)
118      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#executeAsyncJob(org.apache.directory.ldapstudio.browser.core.jobs.ExtendedProgressMonitor)
119      */

120     protected void executeAsyncJob( ExtendedProgressMonitor monitor )
121     {
122
123         monitor.beginTask( BrowserCoreMessages.jobs__import_dsml_task, 2 );
124         monitor.reportProgress( " " ); //$NON-NLS-1$
125
monitor.worked( 1 );
126         
127         Dsmlv2Engine engine = new Dsmlv2Engine( connection.getHost(), connection.getPort(), connection.getBindPrincipal(), connection.getBindPassword() );
128         try
129         {
130             // Executing the DSML request and getting the response
131
String JavaDoc response = engine.processDSMLFile( dsmlFile.getAbsolutePath() );
132             
133             // Saving Response if needed
134
if ( responseFile != null )
135             {
136                 FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc( responseFile );
137                 new PrintStream JavaDoc( fout ).println( response );
138                 fout.close();
139             }
140             
141             // Processing Reponse (Reading and displaying possible errors)
142
int errorCount = 0;
143             Dsmlv2ResponseParser responseParser = new Dsmlv2ResponseParser();
144             responseParser.setInput( response );
145             LdapResponse ldapResponse = responseParser.getNextResponse();
146             while ( ldapResponse != null )
147             {
148                 if ( ( ldapResponse instanceof ErrorResponse )
149                      || ( ldapResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS ) )
150                 {
151                     errorCount++;
152                 }
153                 ldapResponse = responseParser.getNextResponse();
154             }
155             
156             if ( errorCount > 0 )
157             {
158                 monitor.reportError( BrowserCoreMessages.bind(
159                         BrowserCoreMessages.dsml__n_errors_see_responsefile, new String JavaDoc[]
160                             { "" + errorCount } ) ); //$NON-NLS-1$
161
}
162         }
163         catch ( Exception JavaDoc e )
164         {
165             monitor.reportError( e );
166         }
167     }
168
169
170     /* (non-Javadoc)
171      * @see org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob#getErrorMessage()
172      */

173     protected String JavaDoc getErrorMessage()
174     {
175         return BrowserCoreMessages.jobs__import_dsml_error;
176     }
177
178 }
179
Popular Tags