KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
25 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
26 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
27 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
28 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
29 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
30 import org.apache.directory.ldapstudio.browser.core.model.IValue;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.Platform;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.core.runtime.jobs.Job;
36
37
38 public abstract class AbstractEclipseJob extends Job
39 {
40
41     private IProgressMonitor externalProgressMonitor;
42
43     private IStatus externalResult;
44
45
46     protected AbstractEclipseJob()
47     {
48         super( "" ); //$NON-NLS-1$
49
}
50
51
52     protected abstract IConnection[] getConnections();
53
54
55     protected abstract void executeAsyncJob( ExtendedProgressMonitor monitor ) throws Exception JavaDoc;
56
57
58     protected String JavaDoc getErrorMessage()
59     {
60         return BrowserCoreMessages.jobs__error_occurred;
61     }
62
63
64     protected final IStatus run( IProgressMonitor ipm )
65     {
66
67         ExtendedProgressMonitor monitor = new ExtendedProgressMonitor( externalProgressMonitor == null ? ipm
68             : externalProgressMonitor );
69
70         // check if connection is opened
71
IConnection[] connections = getConnections();
72         for ( int i = 0; i < connections.length; i++ )
73         {
74             IConnection connection = connections[i];
75             if ( !connection.isOpened() )
76             {
77                 // monitor.reportError("Connection is closed");
78

79                 EventRegistry.suspendEventFireingInCurrentThread();
80                 try
81                 {
82                     connection.open( monitor );
83                 }
84                 finally
85                 {
86                     EventRegistry.resumeEventFireingInCurrentThread();
87                 }
88             }
89         }
90
91         // execute job
92
if ( !monitor.errorsReported() )
93         {
94             try
95             {
96                 executeAsyncJob( monitor );
97             }
98             catch ( Exception JavaDoc e )
99             {
100                 monitor.reportError( e );
101             }
102             finally
103             {
104                 monitor.done();
105                 ipm.done();
106             }
107         }
108
109         // error handling
110
if ( monitor.isCanceled() )
111         {
112             // System.out.println("Job: CANCEL+CANCEL");
113
externalResult = Status.CANCEL_STATUS;
114             return Status.CANCEL_STATUS;
115         }
116         else if ( monitor.errorsReported() )
117         {
118             externalResult = monitor.getErrorStatus( getErrorMessage() );
119             if ( externalProgressMonitor == null )
120             {
121                 // System.out.println("Job: ERROR+ERROR");
122
return externalResult;
123             }
124             else
125             {
126                 // System.out.println("Job: ERROR+OK");
127
return Status.OK_STATUS;
128             }
129         }
130         else
131         {
132             // System.out.println("Job: OK+OK");
133
externalResult = Status.OK_STATUS;
134             return Status.OK_STATUS;
135         }
136     }
137
138
139     public void setExternalProgressMonitor( IProgressMonitor externalProgressMonitor )
140     {
141         this.externalProgressMonitor = externalProgressMonitor;
142     }
143
144
145     public IStatus getExternalResult()
146     {
147         return this.externalResult;
148     }
149
150
151     public final void execute()
152     {
153         setUser( true );
154         schedule();
155     }
156
157
158     protected abstract Object JavaDoc[] getLockedObjects();
159
160
161     public boolean shouldSchedule()
162     {
163
164         Object JavaDoc[] myLockedObjects = getLockedObjects();
165         String JavaDoc[] myLockedObjectsIdentifiers = getLockIdentifiers( myLockedObjects );
166
167         // TODO: read, write
168

169         Job[] jobs = Platform.getJobManager().find( null );
170         for ( int i = 0; i < jobs.length; i++ )
171         {
172             Job job = jobs[i];
173
174             // if(job instanceof AbstractEclipseJob) {
175
if ( job.getClass() == this.getClass() && job != this )
176             {
177
178                 AbstractEclipseJob otherJob = ( AbstractEclipseJob ) job;
179                 Object JavaDoc[] otherLockedObjects = otherJob.getLockedObjects();
180                 String JavaDoc[] otherLockedObjectIdentifiers = getLockIdentifiers( otherLockedObjects );
181
182                 for ( int j = 0; j < otherLockedObjectIdentifiers.length; j++ )
183                 {
184                     String JavaDoc other = otherLockedObjectIdentifiers[j];
185                     for ( int k = 0; k < myLockedObjectsIdentifiers.length; k++ )
186                     {
187                         String JavaDoc my = myLockedObjectsIdentifiers[k];
188
189                         System.out.print( "other:" + other + ", my: " + my );
190
191                         if ( other.startsWith( my ) || my.startsWith( other ) )
192                         {
193                             System.out.println( ", shouldSchedule() = " + false );
194                             return false;
195                         }
196                         else
197                         {
198                             System.out.println();
199                         }
200
201                     }
202                 }
203
204             }
205         }
206         return super.shouldSchedule();
207
208         // // Doesn't work
209
// Job[] jobs = getJobManager().find(null);
210
// for (int i = 0; i < jobs.length; i++) {
211
// Job job = jobs[i];
212
// if(job instanceof AbstractEclipseJob) {
213
// System.out.println("shouldSchedule() = " + false);
214
// return false;
215
// }
216
// }
217
// System.out.println("shouldSchedule() = " + true);
218
// return true;
219

220         // return super.shouldSchedule();
221
}
222
223
224     protected static String JavaDoc[] getLockIdentifiers( Object JavaDoc[] objects )
225     {
226         String JavaDoc[] identifiers = new String JavaDoc[objects.length];
227         for ( int i = 0; i < identifiers.length; i++ )
228         {
229             Object JavaDoc o = objects[i];
230             if ( o instanceof IConnection )
231             {
232                 identifiers[i] = getLockIdentifier( ( IConnection ) o );
233             }
234             else if ( o instanceof IEntry )
235             {
236                 identifiers[i] = getLockIdentifier( ( IEntry ) o );
237             }
238             else if ( o instanceof IAttribute )
239             {
240                 identifiers[i] = getLockIdentifier( ( IAttribute ) o );
241             }
242             else if ( o instanceof IValue )
243             {
244                 identifiers[i] = getLockIdentifier( ( IValue ) o );
245             }
246             else if ( o instanceof ISearch )
247             {
248                 identifiers[i] = getLockIdentifier( ( ISearch ) o );
249             }
250             else
251             {
252                 identifiers[i] = getLockIdentifier( objects[i] );
253             }
254         }
255         return identifiers;
256     }
257
258
259     protected static String JavaDoc getLockIdentifier( IConnection connection )
260     {
261         return connection.getHost() + ":" + connection.getPort();
262     }
263
264
265     protected static String JavaDoc getLockIdentifier( IEntry entry )
266     {
267         return getLockIdentifier( entry.getConnection() ) + "_"
268             + new StringBuffer JavaDoc( entry.getDn().toString() ).reverse().toString();
269     }
270
271
272     protected static String JavaDoc getLockIdentifier( IAttribute attribute )
273     {
274         return getLockIdentifier( attribute.getEntry() ) + "_" + attribute.getDescription();
275     }
276
277
278     protected static String JavaDoc getLockIdentifier( IValue value )
279     {
280         return getLockIdentifier( value.getAttribute() ) + "_" + value.getStringValue();
281     }
282
283
284     protected static String JavaDoc getLockIdentifier( ISearch search )
285     {
286         return getLockIdentifier( search.getConnection() ) + "_"
287             + new StringBuffer JavaDoc( search.getSearchBase().toString() ).reverse().toString();
288     }
289
290
291     protected static String JavaDoc getLockIdentifier( Object JavaDoc object )
292     {
293         return object.toString();
294     }
295
296 }
297
Popular Tags