Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 /*
  * Copyright (C) 1998, 1999, 2000, 2001,
  *
  * Arjuna Solutions Limited,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.
  *
  * $Id: ShadowingStore.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.internal.arjuna.objectstore;
 
 import java.io.File;
 
A shadowing file store implementation. Each version of the object's state is maintained in a separate file. So, the original is stored in one file, and the shadow (the updated state) is stored in another. When the transaction commits, the shadow is made the original. If the transaction rolls back then the shadow is simply removed from the object store.

Author(s):
Mark Little (mark@arjuna.com)
Version:
$Id: ShadowingStore.java 2342 2006-03-30 13:06:17Z $
Since:
1.0
 
 
 public class ShadowingStore extends FileSystemStore
 {
    

Returns:
current state of object. Assumes that genPathName allocates enough extra space to allow extra chars to be added. State search is ordered OS_SHADOW, OS_UNCOMMITTED_HIDDEN, OS_ORIGINAL, OS_COMMITTED_HIDDEN.
 
 
     public int currentState (Uid objUidString tName)
             throws ObjectStoreException
     {
         int theState = .;
 
         String path = genPathName(objUidtName.);
 
         if (exists(path))
         {
             theState = .;
         }
         else
         {
             path = path + ;
 
             if (exists(path))
             {
                 theState = .;
             }
             else
             {
                 path = genPathName(objUidtName.);
 
                 if (exists(path))
                 {
                     theState = .;
                 }
                else
                {
                    path = path + ;
                    if (exists(path))
                    {
                        theState = .;
                    }
                }
            }
        }
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.currentState("+objUid+", "+tName+") - returning "+
                    StateStatus.stateStatusString(theState));
        }
        return theState;
    }

    
Commit a previous write_state operation which was made with the SHADOW StateType argument. This is achieved by renaming the shadow and removing the hidden version.
    public boolean commit_state (Uid objUidString tName)
            throws ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.commit_state(" + objUid + ", " + tName + ")");
        }
        boolean result = false;
        if (tName != null)
        {
            String shadow = null;
            String filename = null;
            int state = currentState(objUidtName);
            if ((state == .)
                    || (state == .))
            {
                shadow = genPathName(objUidtName.);
                filename = genPathName(objUidtName.);
                if (state == .)
                {
                    /* maintain hidden status on rename */
                    shadow = shadow + ;
                    filename = filename + ;
                }
                File shadowState = new File(shadow);
                File originalState = new File(filename);
                /*
                 * We need to do this because rename will not overwrite an
                 * existing file in Windows, as it will in Unix. It is safe to
                 * do so since we have written the shadow.
                 */
                result = renameFromTo(shadowStateoriginalState);
                if (!result) {
                    ..warn_objectstore_ShadowingStore_2(shadowfilename);
                }
                else
                {
                    super.addToCache(filename);
                    super.removeFromCache(shadow);
                }
                shadowState = null;
                originalState = null;
            }
            else
                result = true;
        }
        else
            throw new ObjectStoreException(
                    "ShadowStore::commit_state - "
                            + ..get_objectstore_notypenameuid()
                            + objUid);
        return result;
    }

    
Hide/reveal an object regardless of state. Hidden objects cannot be read but they can be written (Crash recovery needs this).
    public boolean hide_state (Uid objUidString tName)
            throws ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.hide_state(" + objUid + ", " + tName + ")");
        }
        boolean hiddenOk = true;
        int state = currentState(objUidtName);
        String path1 = null;
        String path2 = null;
        switch (state)
        {
            case .:
            case .:
                break;
            case .:
            {
                path1 = genPathName(objUidtName.);
                path2 = new String(path1) + ;
                File newState = new File(path1);
                File oldState = new File(path2);
                if (renameFromTo(newStateoldState))
                {
                    super.removeFromCache(path1);
                    super.addToCache(path2);
                }
                else {
                    ..warn_objectstore_ShadowingStore_3(newState.getName(), oldState.getName());
                }
                newState = null;
                oldState = null;
                break;
            }
            case .:
            {
                path1 = genPathName(objUidtName.);
                path2 = new String(path1) + ;
                File newState = new File(path1);
                File oldState = new File(path2);
                if (renameFromTo(newStateoldState))
                {
                    super.removeFromCache(path1);
                    super.addToCache(path2);
                }
                else {
                    ..warn_objectstore_ShadowingStore_3(newState.getName(), oldState.getName());
                }
                newState = null;
                oldState = null;
                break;
            }
            default:
                hiddenOk = false;
        }
        return hiddenOk;
    }
    public boolean reveal_state (Uid objUidString tName)
            throws ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.reveal_state(" + objUid + ", " + tName + ")");
        }
        boolean revealedOk = true;
        int state = currentState(objUidtName);
        String path1 = null;
        String path2 = null;
        switch (state)
        {
            case .:
            {
                path1 = genPathName(objUidtName.);
                path2 = new String(path1) + ;
                File newState = new File(path2);
                File oldState = new File(path1);
                if (renameFromTo(newStateoldState))
                {
                    super.removeFromCache(path2);
                    super.addToCache(path1);
                }
                else {
                    ..warn_objectstore_ShadowingStore_4(newState.getName(), oldState.getName());
                }
                newState = null;
                oldState = null;
                break;
            }
            case .:
            {
                path1 = genPathName(objUidtName.);
                path2 = new String(path1) + ;
                File newState = new File(path2);
                File oldState = new File(path1);
                if (renameFromTo(newStateoldState))
                {
                    super.removeFromCache(path2);
                    super.addToCache(path1);
                }
                else {
                    ..warn_objectstore_ShadowingStore_4(newState.getName(), oldState.getName());
                }
                newState = null;
                oldState = null;
                break;
            }
            case .:
            case .:
                break;
            default:
                revealedOk = false;
        }
        return revealedOk;
    }

    

Returns:
the file name for the state of the object identified by the Uid and TypeName. If the StateType argument is OS_SHADOW then the Uid part of the name includes # characters. Builds on lower level genPathName which allocates enough slop to accomodate the extra chars.
    protected String genPathName (Uid objUidString tNameint ft)
            throws ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.genPathName(" + objUid + ", " + tName + ", " + StateType.stateTypeString(ft) + ")");
        }
        String fname = super.genPathName(objUidtNameft);
        if (ft == .)
            fname = fname + ;
        return fname;
    }
    protected String revealedId (String name)
    {
        int index = name.indexOf();
        if (index == -1)
            index = name.indexOf();
        if (index != -1)
            return name.substring(0, index);
        else
            return name;
    }
    protected InputObjectState read_state (Uid objUidString tNameint ft)
            throws ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.read_state(" + objUid + ", " + tName + ", " + StateType.stateTypeString(ft) + ")");
        }
        InputObjectState new_image = null;
        if (tName != null)
        {
            int state = currentState(objUidtName);
            if ((state == .)
                    || (state == .))
            {
                /*
                 * Is the current state the same as that requested?
                 */
                if (((state == .) && (ft != .))
                        || ((state == .) && (ft != .)))
                {
                    return null;
                }
                String fname = genPathName(objUidtNameft);
                File fd = openAndLock(fname.false);
                if (fd != null)
                {
                    int imageSize = (intfd.length();
                    byte[] buffer = new byte[imageSize];
                    FileInputStream ifile = null;
                    try
                    {
                        ifile = new FileInputStream(fd);
                    }
                    catch (FileNotFoundException e)
                    {
                        closeAndUnlock(fdifilenull);
                        ..info("ObjectStore record was deleted during restoration, users should not deleted records manually: " + fd.getAbsolutePath(), e);
                        return null;
                    }
                    /* now try to read the actual image out of the store */
                    try
                    {
                        if ((buffer != null)
                                && (ifile.read(buffer, 0, imageSize) == imageSize))
                        {
                            new_image = new InputObjectState(objUidtName,
                                    buffer);
                        }
                        else {
                            ..warn_objectstore_ShadowingStore_7();
                        }
                    }
                    catch (IOException e)
                    {
                        closeAndUnlock(fdifilenull);
                        throw new ObjectStoreException(
                                "ShadowingStore::read_state failed: " + ee);
                    }
                    if (!closeAndUnlock(fdifilenull)) {
                        ..warn_objectstore_ShadowingStore_8(fname);
                    }
                }
            }
        }
        else
            throw new ObjectStoreException(
                    "ShadowStore::read_state - "
                            + ..get_objectstore_notypenameuid()
                            + objUid);
        return new_image;
    }
    protected boolean remove_state (Uid objUidString nameint ft)
            throws ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.remove_state(" + objUid + ", " + name + ", " + StateType.stateTypeString(ft) + ")");
        }
        boolean removeOk = true;
        if (name != null)
        {
            int state = currentState(objUidname);
            if ((state == .)
                    || (state == .))
            {
                String fname = genPathName(objUidnameft);
                File fd = openAndLock(fname.false);
                if (fd != null)
                {
                    if (!fd.canWrite())
                    {
                        removeOk = false;
                        if (ft == .) {
                            ..warn_objectstore_ShadowingStore_9(objUidname);
                            if (!fd.exists()) {
                                ..warn_objectstore_ShadowingStore_10(objUidname);
                            }
                        }
                    }
                    else
                    {
                        if (!fd.delete())
                        {
                            removeOk = false;
                            if (ft == .) {
                                ..warn_objectstore_ShadowingStore_11(fname);
                            }
                        }
                    }
                    closeAndUnlock(fdnullnull);
                }
                else {
                    ..warn_objectstore_ShadowingStore_12(objUid);
                    removeOk = false;
                }
                if (removeOk)
                    super.removeFromCache(fname);
            }
            else
            {
                removeOk = false;
                if (state == .)
                    ..info_objectstore_ShadowingStore_14(objUidname);
                else
                    ..info_objectstore_ShadowingStore_15(objUidname);
            }
        }
        else {
            removeOk = false;
        }
        return removeOk;
    }

    
write_state saves the ObjectState in a file named by the type and Uid of the ObjectState. If the second argument is SHADOW, then the file name is different so that a subsequent commit_state invocation will rename the file.
    protected boolean write_state (Uid objUidString tName,
                                   OutputObjectState stateint ftthrows ObjectStoreException
    {
        if (..isTraceEnabled()) {
            ..trace("ShadowingStore.write_state(" + objUid + ", " + tName + ", " + StateType.stateTypeString(ft) + ")");
        }
        if (tName != null)
        {
            String fname = genPathName(objUidtNameft);
            File fd = openAndLock(fname.true);
            int imageSize = (intstate.length();
            if (fd == null) {
                ..warn_objectstore_ShadowingStore_18(fname);
                return false;
            }
            FileOutputStream ofile = null;
            if (imageSize > 0)
            {
                try
                {
                    ofile = new FileOutputStream(fd);
                    ofile.write(state.buffer(), 0, imageSize);
                    if (synchronousWrites())
                    {
                        // must flush any in-memory buffering prior to sync
                        ofile.flush();
                        FileDescriptor fileDesc = ofile.getFD(); // assume it's
                        // valid!
                        fileDesc.sync();
                    }
                }
                catch (SyncFailedException e)
                {
                    closeAndUnlock(fdnullofile);
                    throw new ObjectStoreException(
                            "ShadowingStore::write_state() - write failed to sync for "
                                    + fnamee);
                }
                catch (FileNotFoundException e)
                {
                    closeAndUnlock(fdnullofile);
                    e.printStackTrace();
                    throw new ObjectStoreException(
                            "ShadowingStore::write_state() - write failed to locate file "
                                    + fname + ": " + ee);
                }
                catch (IOException e)
                {
                    closeAndUnlock(fdnullofile);
                    e.printStackTrace();
                    throw new ObjectStoreException(
                            "ShadowingStore::write_state() - write failed for "
                                    + fname + ": " + ee);
                }
            }
            if (!closeAndUnlock(fdnullofile)) {
                ..warn_objectstore_ShadowingStore_19(fname);
            }
            super.addToCache(fname);
            return true;
        }
        else
            throw new ObjectStoreException(
                    "ShadowStore::write_state - "
                            + ..get_objectstore_notypenameuid()
                            + objUid);
    }
    public ShadowingStore(ObjectStoreEnvironmentBean objectStoreEnvironmentBeanthrows ObjectStoreException
    {
        super(objectStoreEnvironmentBean);
    }
    public static final char HIDDINGCHAR = '#';
    public static final char SHADOWCHAR = '!';
New to GrepCode? Check out our FAQ X