mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-17 05:52:26 +00:00
https://www.dev.java.net/svn/hudson/branches/managed-startup ........ r23666 | kohsuke | 2009-11-11 15:56:46 -0800 (Wed, 11 Nov 2009) | 1 line Reworked the initialization logic ........ r23667 | kohsuke | 2009-11-11 16:01:56 -0800 (Wed, 11 Nov 2009) | 1 line fixed a failing test ........ r23670 | kohsuke | 2009-11-11 16:42:32 -0800 (Wed, 11 Nov 2009) | 1 line use a non-snapshot ........ r23711 | kohsuke | 2009-11-13 14:27:28 -0800 (Fri, 13 Nov 2009) | 1 line Session -> Reactor ........ r23782 | kohsuke | 2009-11-17 10:35:14 -0800 (Tue, 17 Nov 2009) | 1 line Poorman's clone of JDK6 ServiceLoader. ........ r23783 | kohsuke | 2009-11-17 10:56:25 -0800 (Tue, 17 Nov 2009) | 1 line Integrated the fatal support so that all errors are sent to the listener. ........ r23784 | kohsuke | 2009-11-17 10:58:07 -0800 (Tue, 17 Nov 2009) | 1 line with the new reactor initialization, things are getting too verbose ........ r23785 | kohsuke | 2009-11-17 11:18:45 -0800 (Tue, 17 Nov 2009) | 1 line failing to load jobs are non-fatal, too ........ r23788 | kohsuke | 2009-11-17 13:18:59 -0800 (Tue, 17 Nov 2009) | 1 line logging the exception properly ........ r23809 | kohsuke | 2009-11-17 18:45:47 -0800 (Tue, 17 Nov 2009) | 1 line releasing 1.1 as milestone ........ r23810 | kohsuke | 2009-11-17 18:47:37 -0800 (Tue, 17 Nov 2009) | 2 lines - use a better idiom for building a sequence - use null display name to signify internal book-keeping tasks that's not worth displaying ........ r23817 | kohsuke | 2009-11-18 07:54:17 -0800 (Wed, 18 Nov 2009) | 1 line removed accidental JDK6 dependency. ........ r23859 | kohsuke | 2009-11-19 11:03:28 -0800 (Thu, 19 Nov 2009) | 1 line introducing the strategy pattern to hook into key decision making points during the start up ........ r23870 | kohsuke | 2009-11-19 14:33:07 -0800 (Thu, 19 Nov 2009) | 1 line bug fix. These steps add additional tasks to the reactor, so they need to also prevent future milestones from getting attained. ........ r23879 | kohsuke | 2009-11-19 16:54:59 -0800 (Thu, 19 Nov 2009) | 1 line moved the logic to the strategy. ........ r23899 | kohsuke | 2009-11-20 09:32:13 -0800 (Fri, 20 Nov 2009) | 1 line handle duplicate plugins better ........ r23900 | kohsuke | 2009-11-20 10:06:42 -0800 (Fri, 20 Nov 2009) | 1 line support pre-exploded plugin ........ r23901 | kohsuke | 2009-11-20 10:49:04 -0800 (Fri, 20 Nov 2009) | 1 line making it a subtype ........ r23902 | kohsuke | 2009-11-20 10:54:42 -0800 (Fri, 20 Nov 2009) | 1 line allow InitStrategy to skip some tasks ........ r23903 | kohsuke | 2009-11-20 11:03:38 -0800 (Fri, 20 Nov 2009) | 1 line doc improvement. ........ r23904 | kohsuke | 2009-11-20 11:03:49 -0800 (Fri, 20 Nov 2009) | 1 line no need to throw exceptions ........ r23906 | kohsuke | 2009-11-20 11:17:34 -0800 (Fri, 20 Nov 2009) | 1 line debug switch to disable the automatic launch of slaves ........ r23907 | kohsuke | 2009-11-20 11:25:15 -0800 (Fri, 20 Nov 2009) | 1 line added a magic object for cancelling all saves ........ r23909 | kohsuke | 2009-11-20 11:29:53 -0800 (Fri, 20 Nov 2009) | 1 line moving initialization up ........ r23913 | kohsuke | 2009-11-20 13:32:08 -0800 (Fri, 20 Nov 2009) | 1 line seeing heap space problem during tests ........ git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@23992 71c3de6d-444a-0410-be80-ed276b4c234a
175 lines
5.4 KiB
Java
175 lines
5.4 KiB
Java
/*
|
|
* The MIT License
|
|
*
|
|
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
package hudson;
|
|
|
|
import hudson.model.Saveable;
|
|
import hudson.model.Hudson;
|
|
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* Transaction-like object that can be used to make a bunch of changes to an object, and defer the
|
|
* {@link Saveable#save()} until the end.
|
|
*
|
|
* <p>
|
|
* The usage of {@link BulkChange} needs to follow a specific closure-like pattern, namely:
|
|
*
|
|
* <pre>
|
|
* BulkChange bc = new BulkChange(someObject);
|
|
* try {
|
|
* ... make changes to 'someObject'
|
|
* } finally {
|
|
* bc.commit();
|
|
* }
|
|
* </pre>
|
|
*
|
|
* <p>
|
|
* ... or if you'd like to avoid saving when something bad happens:
|
|
*
|
|
* <pre>
|
|
* BulkChange bc = new BulkChange(someObject);
|
|
* try {
|
|
* ... make changes to 'someObject'
|
|
* bc.commit();
|
|
* } finally {
|
|
* bc.abort();
|
|
* }
|
|
* </pre>
|
|
*
|
|
* <p>
|
|
* Use of this method is optional. If {@link BulkChange} is not used, individual mutator
|
|
* will perform the save operation, and things will just run somewhat slower.
|
|
*
|
|
*
|
|
* <h2>Cooperation from {@link Saveable}</h2>
|
|
* <p>
|
|
* For this class to work as intended, {@link Saveable} implementations need to co-operate.
|
|
* Namely,
|
|
*
|
|
* <ol>
|
|
* <li>
|
|
* Mutater methods should invoke {@code this.save()} so that if the method is called outside
|
|
* a {@link BulkChange}, the result will be saved immediately.
|
|
*
|
|
* <li>
|
|
* In the {@code save()} method implementation, use {@link #contains(Saveable)} and
|
|
* only perform the actual I/O operation when this method returns false.
|
|
* </ol>
|
|
*
|
|
* <p>
|
|
* See {@link Hudson#save()} as an example if you are not sure how to implement {@link Saveable}.
|
|
*
|
|
* @author Kohsuke Kawaguchi
|
|
* @since 1.249
|
|
*/
|
|
public class BulkChange {
|
|
private final Saveable saveable;
|
|
public final Exception allocator;
|
|
private final BulkChange parent;
|
|
|
|
private boolean completed;
|
|
|
|
public BulkChange(Saveable saveable) {
|
|
this.parent = current();
|
|
this.saveable = saveable;
|
|
// rememeber who allocated this object in case
|
|
// someone forgot to call save() at the end.
|
|
allocator = new Exception();
|
|
|
|
// in effect at construction
|
|
INSCOPE.set(this);
|
|
}
|
|
|
|
/**
|
|
* Saves the accumulated changes.
|
|
*/
|
|
public void commit() throws IOException {
|
|
if(completed) return;
|
|
completed = true;
|
|
|
|
// move this object out of the scope first before save, or otherwise the save() method will do nothing.
|
|
pop();
|
|
saveable.save();
|
|
}
|
|
|
|
/**
|
|
* Exits the scope of {@link BulkChange} without saving the changes.
|
|
*
|
|
* <p>
|
|
* This can be used when a bulk change fails in the middle.
|
|
* Note that unlike a real transaction, this will not roll back the state of the object.
|
|
*
|
|
* <p>
|
|
* The abort method can be called after the commit method, in which case this method does nothing.
|
|
* This is so that {@link BulkChange} can be used naturally in the try/finally block.
|
|
*/
|
|
public void abort() {
|
|
if(completed) return;
|
|
completed = true;
|
|
pop();
|
|
}
|
|
|
|
private void pop() {
|
|
if(current()!=this)
|
|
throw new AssertionError("Trying to save BulkChange that's not in scope");
|
|
INSCOPE.set(parent);
|
|
}
|
|
|
|
/**
|
|
* {@link BulkChange}s that are effective currently.
|
|
*/
|
|
private static final ThreadLocal<BulkChange> INSCOPE = new ThreadLocal<BulkChange>();
|
|
|
|
/**
|
|
* Gets the {@link BulkChange} instance currently in scope for the current thread.
|
|
*/
|
|
public static BulkChange current() {
|
|
return INSCOPE.get();
|
|
}
|
|
|
|
/**
|
|
* Checks if the given {@link Saveable} is currently in the bulk change.
|
|
*
|
|
* <p>
|
|
* The expected usage is from the {@link Saveable#save()} implementation to check
|
|
* if the actual persistence should happen now or not.
|
|
*/
|
|
public static boolean contains(Saveable s) {
|
|
for(BulkChange b=current(); b!=null; b=b.parent)
|
|
if(b.saveable==s || b.saveable==ALL)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Magic {@link Saveable} instance that can make {@link BulkChange} veto
|
|
* all the save operations by making the {@link #contains(Saveable)} method return
|
|
* true for everything.
|
|
*/
|
|
public static final Saveable ALL = new Saveable() {
|
|
public void save() {
|
|
}
|
|
};
|
|
}
|