Search in sources :

Example 1 with Saveable

use of hudson.model.Saveable in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method notifyListeners.

void notifyListeners(List<FlowNode> nodes, boolean synchronous) {
    List<GraphListener> toRun = getListenersToRun();
    if (!toRun.isEmpty()) {
        Saveable s = Saveable.NOOP;
        try {
            Queue.Executable exec = owner.getExecutable();
            if (exec instanceof Saveable) {
                s = (Saveable) exec;
            }
        } catch (IOException x) {
            LOGGER.log(Level.WARNING, "failed to notify listeners of changes to " + nodes + " in " + this, x);
        }
        BulkChange bc = new BulkChange(s);
        try {
            for (FlowNode node : nodes) {
                for (GraphListener listener : toRun) {
                    if (listener instanceof GraphListener.Synchronous == synchronous) {
                        listener.onNewHead(node);
                    }
                }
            }
        } finally {
            if (synchronous) {
                // hack to skip save—we are holding a lock
                bc.abort();
            } else {
                try {
                    bc.commit();
                } catch (IOException x) {
                    LOGGER.log(Level.WARNING, null, x);
                }
            }
        }
    }
}
Also used : Saveable(hudson.model.Saveable) BulkChange(hudson.BulkChange) IOException(java.io.IOException) GraphListener(org.jenkinsci.plugins.workflow.flow.GraphListener) Queue(hudson.model.Queue) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 2 with Saveable

use of hudson.model.Saveable in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method saveOwner.

/**
 * Save the owner that holds this execution.
 */
void saveOwner() {
    try {
        if (this.owner.getExecutable() instanceof Saveable) {
            Saveable saveable = (Saveable) (this.owner.getExecutable());
            saveable.save();
        }
    } catch (IOException ex) {
        LOGGER.log(Level.WARNING, "Error persisting Run before shutdown", ex);
        persistedClean = false;
    }
}
Also used : Saveable(hudson.model.Saveable) IOException(java.io.IOException)

Example 3 with Saveable

use of hudson.model.Saveable in project hudson-2.x by hudson.

the class OldDataMonitor method doUpgrade.

/**
 * Save all or some of the files to persist data in the new forms.
 * Remove those items from the data map.
 */
public synchronized HttpResponse doUpgrade(StaplerRequest req, StaplerResponse rsp) throws IOException {
    String thruVerParam = req.getParameter("thruVer");
    VersionNumber thruVer = thruVerParam.equals("all") ? null : new VersionNumber(thruVerParam);
    updating = true;
    for (Iterator<Map.Entry<Saveable, VersionRange>> it = data.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<Saveable, VersionRange> entry = it.next();
        VersionNumber version = entry.getValue().max;
        if (version != null && (thruVer == null || !version.isNewerThan(thruVer))) {
            entry.getKey().save();
            it.remove();
        }
    }
    updating = false;
    return HttpResponses.forwardToPreviousPage();
}
Also used : Saveable(hudson.model.Saveable) HashMap(java.util.HashMap) Map(java.util.Map) VersionNumber(hudson.util.VersionNumber)

Example 4 with Saveable

use of hudson.model.Saveable in project hudson-2.x by hudson.

the class XStream2 method unmarshal.

@Override
public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder) {
    // init() is too early to do this
    // defensive because some use of XStream happens before plugins are initialized.
    Hudson h = Hudson.getInstance();
    if (h != null && h.pluginManager != null && h.pluginManager.uberClassLoader != null) {
        setClassLoader(h.pluginManager.uberClassLoader);
    }
    Object o = super.unmarshal(reader, root, dataHolder);
    if (oldData.get() != null) {
        oldData.remove();
        if (o instanceof Saveable)
            OldDataMonitor.report((Saveable) o, "1.106");
    }
    return o;
}
Also used : Saveable(hudson.model.Saveable) Hudson(hudson.model.Hudson)

Example 5 with Saveable

use of hudson.model.Saveable in project hudson-2.x by hudson.

the class RobustReflectionConverter method doUnmarshal.

public Object doUnmarshal(final Object result, final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final SeenFields seenFields = new SeenFields();
    Iterator it = reader.getAttributeNames();
    // Remember outermost Saveable encountered, for reporting below
    if (result instanceof Saveable && context.get("Saveable") == null)
        context.put("Saveable", result);
    // Process attributes before recursing into child elements.
    while (it.hasNext()) {
        String attrAlias = (String) it.next();
        String attrName = mapper.attributeForAlias(attrAlias);
        Class classDefiningField = determineWhichClassDefinesField(reader);
        boolean fieldExistsInClass = fieldDefinedInClass(result, attrName);
        if (fieldExistsInClass) {
            Field field = reflectionProvider.getField(result.getClass(), attrName);
            SingleValueConverter converter = mapper.getConverterFromAttribute(field.getDeclaringClass(), attrName, field.getType());
            Class type = field.getType();
            if (converter == null) {
                converter = mapper.getConverterFromItemType(type);
            }
            if (converter != null) {
                Object value = converter.fromString(reader.getAttribute(attrAlias));
                if (type.isPrimitive()) {
                    type = Primitives.box(type);
                }
                if (value != null && !type.isAssignableFrom(value.getClass())) {
                    throw new ConversionException("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
                }
                reflectionProvider.writeField(result, attrName, value, classDefiningField);
                seenFields.add(classDefiningField, attrName);
            }
        }
    }
    Map implicitCollectionsForCurrentObject = null;
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        try {
            String fieldName = mapper.realMember(result.getClass(), reader.getNodeName());
            boolean implicitCollectionHasSameName = mapper.getImplicitCollectionDefForFieldName(result.getClass(), reader.getNodeName()) != null;
            Class classDefiningField = determineWhichClassDefinesField(reader);
            boolean fieldExistsInClass = !implicitCollectionHasSameName && fieldDefinedInClass(result, fieldName);
            Class type = determineType(reader, fieldExistsInClass, result, fieldName, classDefiningField);
            final Object value;
            if (fieldExistsInClass) {
                Field field = reflectionProvider.getField(result.getClass(), fieldName);
                value = unmarshalField(context, result, type, field);
                // TODO the reflection provider should have returned the proper field in first place ....
                Class definedType = reflectionProvider.getFieldType(result, fieldName, classDefiningField);
                if (!definedType.isPrimitive()) {
                    type = definedType;
                }
            } else {
                value = context.convertAnother(result, type);
            }
            if (value != null && !type.isAssignableFrom(value.getClass())) {
                LOGGER.warning("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
            // behave as if we didn't see this element
            } else {
                if (fieldExistsInClass) {
                    reflectionProvider.writeField(result, fieldName, value, classDefiningField);
                    seenFields.add(classDefiningField, fieldName);
                } else {
                    implicitCollectionsForCurrentObject = writeValueToImplicitCollection(context, value, implicitCollectionsForCurrentObject, result, fieldName);
                }
            }
        } catch (NonExistentFieldException e) {
            LOGGER.log(WARNING, "Skipping a non-existent field " + e.getFieldName());
            addErrorInContext(context, e);
        } catch (CannotResolveClassException e) {
            LOGGER.log(WARNING, "Skipping a non-existent type " + e.getMessage());
            addErrorInContext(context, e);
        } catch (LinkageError e) {
            LOGGER.log(WARNING, "Failed to resolve a type " + e.getMessage());
            addErrorInContext(context, e);
        }
        reader.moveUp();
    }
    // Report any class/field errors in Saveable objects
    if (context.get("ReadError") != null && context.get("Saveable") == result) {
        OldDataMonitor.report((Saveable) result, (ArrayList<Throwable>) context.get("ReadError"));
        context.put("ReadError", null);
    }
    return result;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Saveable(hudson.model.Saveable) NonExistentFieldException(com.thoughtworks.xstream.converters.reflection.NonExistentFieldException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) SingleValueConverter(com.thoughtworks.xstream.converters.SingleValueConverter) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Saveable (hudson.model.Saveable)5 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConversionException (com.thoughtworks.xstream.converters.ConversionException)1 SingleValueConverter (com.thoughtworks.xstream.converters.SingleValueConverter)1 NonExistentFieldException (com.thoughtworks.xstream.converters.reflection.NonExistentFieldException)1 CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)1 BulkChange (hudson.BulkChange)1 Hudson (hudson.model.Hudson)1 Queue (hudson.model.Queue)1 VersionNumber (hudson.util.VersionNumber)1 Field (java.lang.reflect.Field)1 Iterator (java.util.Iterator)1 GraphListener (org.jenkinsci.plugins.workflow.flow.GraphListener)1 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)1