alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.util;

import java.util.HashMap;
import java.util.HashSet;
import org.netbeans.api.mdr.events.TransactionEvent;
import org.netbeans.mdr.NBMDRepositoryImpl;
import org.netbeans.mdr.storagemodel.MdrStorage;
import org.netbeans.mdr.persistence.StorageException;

/** This class implements mutex that controls storage transactions as well as 
 * MDR events firing.
 *
 * @author  Martin Matula
 * @version 0.1
 */
public abstract class TransactionMutex {

    // storage
    private final MdrStorage storage;
    
    // events notifier
    private final EventNotifier notifier;

    // the repository (used as event source for transaction events)
    private final NBMDRepositoryImpl repository;
    
    protected TransactionMutex(Object storage, Object notifier, Object repository) {
        this.storage = (MdrStorage) storage;
        this.notifier = (EventNotifier) notifier;
        this.repository = (NBMDRepositoryImpl) repository;
    }
    
    public abstract boolean willFail();
    
    public abstract boolean pendingChanges();
 
    public abstract void enter(boolean writeAccess);
    public abstract boolean leave(boolean fail);
    
    public final void leave() {
        leave(false);
    }
    
    protected final void start() {
        TransactionEvent event = new TransactionEvent(
            repository,
            TransactionEvent.EVENT_TRANSACTION_START
        );
        notifier.REPOSITORY.firePlannedChange(storage, event);
    }
    
    protected final void end(boolean fail) {
        try {
            TransactionEvent event = new TransactionEvent(
                repository,
                TransactionEvent.EVENT_TRANSACTION_END
            );
            notifier.REPOSITORY.firePlannedChange(storage, event);
            if (fail) {
                notifier.fireCancelled();
                storage.rollback();
//                            Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException("rollback"));
            } else {
                notifier.fireChanged();
                storage.commit();
                //Logger.getDefault().log("commited");
//                            Logger.getDefault().notify(Logger.INFORMATIONAL, new DebugException("commit"));
            }
        } catch (StorageException e) {
            try { 
                Logger.getDefault().notify(Logger.INFORMATIONAL, e);
                storage.rollback();
            } catch (StorageException fatal) {
                throw new DebugException("Fatal I/O error: " + fatal.toString());
            }
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.