1 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( "" ); } 50 51 52 protected abstract IConnection[] getConnections(); 53 54 55 protected abstract void executeAsyncJob( ExtendedProgressMonitor monitor ) throws Exception ; 56 57 58 protected String 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 IConnection[] connections = getConnections(); 72 for ( int i = 0; i < connections.length; i++ ) 73 { 74 IConnection connection = connections[i]; 75 if ( !connection.isOpened() ) 76 { 77 79 EventRegistry.suspendEventFireingInCurrentThread(); 80 try 81 { 82 connection.open( monitor ); 83 } 84 finally 85 { 86 EventRegistry.resumeEventFireingInCurrentThread(); 87 } 88 } 89 } 90 91 if ( !monitor.errorsReported() ) 93 { 94 try 95 { 96 executeAsyncJob( monitor ); 97 } 98 catch ( Exception e ) 99 { 100 monitor.reportError( e ); 101 } 102 finally 103 { 104 monitor.done(); 105 ipm.done(); 106 } 107 } 108 109 if ( monitor.isCanceled() ) 111 { 112 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 return externalResult; 123 } 124 else 125 { 126 return Status.OK_STATUS; 128 } 129 } 130 else 131 { 132 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 [] getLockedObjects(); 159 160 161 public boolean shouldSchedule() 162 { 163 164 Object [] myLockedObjects = getLockedObjects(); 165 String [] myLockedObjectsIdentifiers = getLockIdentifiers( myLockedObjects ); 166 167 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.getClass() == this.getClass() && job != this ) 176 { 177 178 AbstractEclipseJob otherJob = ( AbstractEclipseJob ) job; 179 Object [] otherLockedObjects = otherJob.getLockedObjects(); 180 String [] otherLockedObjectIdentifiers = getLockIdentifiers( otherLockedObjects ); 181 182 for ( int j = 0; j < otherLockedObjectIdentifiers.length; j++ ) 183 { 184 String other = otherLockedObjectIdentifiers[j]; 185 for ( int k = 0; k < myLockedObjectsIdentifiers.length; k++ ) 186 { 187 String 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 220 } 222 223 224 protected static String [] getLockIdentifiers( Object [] objects ) 225 { 226 String [] identifiers = new String [objects.length]; 227 for ( int i = 0; i < identifiers.length; i++ ) 228 { 229 Object 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 getLockIdentifier( IConnection connection ) 260 { 261 return connection.getHost() + ":" + connection.getPort(); 262 } 263 264 265 protected static String getLockIdentifier( IEntry entry ) 266 { 267 return getLockIdentifier( entry.getConnection() ) + "_" 268 + new StringBuffer ( entry.getDn().toString() ).reverse().toString(); 269 } 270 271 272 protected static String getLockIdentifier( IAttribute attribute ) 273 { 274 return getLockIdentifier( attribute.getEntry() ) + "_" + attribute.getDescription(); 275 } 276 277 278 protected static String getLockIdentifier( IValue value ) 279 { 280 return getLockIdentifier( value.getAttribute() ) + "_" + value.getStringValue(); 281 } 282 283 284 protected static String getLockIdentifier( ISearch search ) 285 { 286 return getLockIdentifier( search.getConnection() ) + "_" 287 + new StringBuffer ( search.getSearchBase().toString() ).reverse().toString(); 288 } 289 290 291 protected static String getLockIdentifier( Object object ) 292 { 293 return object.toString(); 294 } 295 296 } 297 | Popular Tags |