Esempio n. 1
0
  @SuppressWarnings("unchecked")
  private boolean packTransactionalInstance(final Field afield, OutputObjectState os) {
    Object ptr = null;

    try {
      ptr = afield.get(_theObject);

      if (ptr == null) {
        os.packBoolean(false);
      } else {
        os.packBoolean(true);
        UidHelper.packInto(_container.getUidForHandle((T) ptr), os);
      }
    } catch (final ClassCastException ex) {
      System.err.println("Field " + ptr + " is not a transactional instance!");

      return false;
    } catch (final Exception ex) {
      ex.printStackTrace();

      return false;
    }

    return true;
  }
Esempio n. 2
0
  private boolean packObjectType(final Field afield, OutputObjectState os) {
    try {
      if (afield.getType().equals(Boolean.class))
        os.packBoolean(((Boolean) afield.get(_theObject)).booleanValue());
      else if (afield.getType().equals(Byte.class))
        os.packByte(((Byte) afield.get(_theObject)).byteValue());
      else if (afield.getType().equals(Short.class))
        os.packShort(((Short) afield.get(_theObject)).shortValue());
      else if (afield.getType().equals(Integer.class))
        os.packInt(((Integer) afield.get(_theObject)).intValue());
      else if (afield.getType().equals(Long.class))
        os.packLong(((Long) afield.get(_theObject)).longValue());
      else if (afield.getType().equals(Float.class))
        os.packFloat(((Float) afield.get(_theObject)).floatValue());
      else if (afield.getType().equals(Double.class))
        os.packDouble(((Double) afield.get(_theObject)).doubleValue());
      else if (afield.getType().equals(Character.class))
        os.packChar(((Character) afield.get(_theObject)).charValue());
      else if (afield.getType().equals(String.class))
        os.packString((String) afield.get(_theObject));
      else if (afield.getType().isAnnotationPresent(Transactional.class))
        return packTransactionalInstance(afield, os);
      else return false;
    } catch (final Exception ex) {
      ex.printStackTrace();

      return false;
    }

    return true;
  }
Esempio n. 3
0
  boolean removeState(Uid u, String tn, int s) throws ObjectStoreException {
    try {
      OutputObjectState removalState = new OutputObjectState(u, tn);

      removalState.packBytes(_removedState);

      if (!write_state(u, tn, removalState, s)) throw new ObjectStoreException();
    } catch (IOException ex) {
      throw new ObjectStoreException(ex.toString(), ex);
    }

    return true;
  }
Esempio n. 4
0
  private boolean packPrimitive(final Field afield, OutputObjectState os) {
    try {
      /*
       * TODO deal with arrays of primitive types.
       *
       * Workaround - provide saveState and restoreState annotations.
       */

      if (afield.getType().equals(Boolean.TYPE)) os.packBoolean(afield.getBoolean(_theObject));
      else if (afield.getType().equals(Byte.TYPE)) os.packByte(afield.getByte(_theObject));
      else if (afield.getType().equals(Short.TYPE)) os.packShort(afield.getShort(_theObject));
      else if (afield.getType().equals(Integer.TYPE)) os.packInt(afield.getInt(_theObject));
      else if (afield.getType().equals(Long.TYPE)) os.packLong(afield.getLong(_theObject));
      else if (afield.getType().equals(Float.TYPE)) os.packFloat(afield.getFloat(_theObject));
      else if (afield.getType().equals(Double.TYPE)) os.packDouble(afield.getDouble(_theObject));
      else if (afield.getType().equals(Character.TYPE)) os.packChar(afield.getChar(_theObject));
      else return false;
    } catch (final IOException ex) {
      return false;
    } catch (final Exception ex) {
      return false;
    }

    return true;
  }
Esempio n. 5
0
  /**
   * Does nothing except indicate that this thread is finished with the log on behalf of this
   * transaction.
   */
  protected boolean remove_state(Uid u, String tn, int s) throws ObjectStoreException {
    // maybe write a removal entry into the log.

    try {
      /*
       * If we don't add a removal entry then recovery has to work a
       * little harder to figure things out. But it has to cater for the
       * situation where a removal record write fails anyway, so this
       * shouldn't be a big deal. On the up side it improves performance
       * by 30% for this implementation, which is a 40% improvement over
       * the basic file-based log!
       */

      /*
       * If we write a removal record as a separate entity to the original
       * data item then we cannot ensure that they will go into the same
       * log with a pre-set size for the log. Therefore, we have two
       * options:
       *
       * (i) find the old entry in the log and mark it as deleted.
       * (ii) increase the size of the log to accommodate the removal entry.
       *
       * We currently go for option (ii) as this is the quickest.
       */

      if (_synchronousRemoval) {
        OutputObjectState removalState = new OutputObjectState(u, tn);

        removalState.packBytes(_removedState);

        if (!write_state(u, tn, removalState, s)) throw new ObjectStoreException();
      } else _purger.addRemovedState(u, tn, s);
    } catch (IOException ex) {
      throw new ObjectStoreException(ex.toString(), ex);
    } catch (final Throwable ex) {
      ex.printStackTrace();

      throw new ObjectStoreException(ex.toString(), ex);
    } finally {
      removeFromLog(u);
    }

    return true;
  }
Esempio n. 6
0
  private boolean packPrimitiveArray(final Field afield, OutputObjectState os) {
    boolean success = true;

    try {
      Class<?> c = afield.getType();
      final int size = Array.getLength(afield.get(_theObject));

      if (c.equals(int[].class)) {
        final int[] objs = (int[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packInt(objs[i]);
      } else if (c.equals(boolean[].class)) {
        final boolean[] objs = (boolean[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packBoolean(objs[i]);
      } else if (c.equals(byte[].class)) {
        final byte[] objs = (byte[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packByte(objs[i]);
      } else if (c.equals(short[].class)) {
        final short[] objs = (short[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packShort(objs[i]);
      } else if (c.equals(long[].class)) {
        final long[] objs = (long[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packLong(objs[i]);
      } else if (c.equals(float[].class)) {
        final float[] objs = (float[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packFloat(objs[i]);
      } else if (c.equals(double[].class)) {
        final double[] objs = (double[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packDouble(objs[i]);
      } else if (c.equals(char[].class)) {
        final char[] objs = (char[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) os.packChar(objs[i]);
      } else success = false;
    } catch (final Throwable ex) {
      ex.printStackTrace();

      success = false;
    }

    return success;
  }
Esempio n. 7
0
  public boolean save_state(OutputObjectState objectState, int objectType) {
    super.save_state(objectState, objectType);
    try {
      for (int x = 0; x < _width; x++) {
        for (int y = 0; y < _height; y++) {
          objectState.packInt(_values[x][y]);
        }
      }

      return true;
    } catch (Exception exception) {
      System.err.println("AITMatrixImpl01.save_state: " + exception);
      return false;
    }
  }
Esempio n. 8
0
  /**
   * 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.
   *
   * <p>We need to make sure that each entry is written to the next empty location in the log even
   * if there's already an entry for this tx.
   */
  protected boolean write_state(Uid objUid, String tName, OutputObjectState state, int ft)
      throws ObjectStoreException {
    if (tsLogger.logger.isTraceEnabled()) {
      tsLogger.logger.trace(
          "ShadowingStore.write_state("
              + objUid
              + ", "
              + tName
              + ", "
              + StateType.stateTypeString(ft)
              + ")");
    }

    String fname = null;
    File fd = null;

    if (tName != null) {
      int imageSize = (int) state.length();
      byte[] uidString = objUid.stringForm().getBytes();
      int buffSize =
          _redzone.length
              + uidString.length
              + imageSize
              + 8; // don't put in endOfLog since we keep overwriting that.
      RandomAccessFile ofile = null;
      java.nio.channels.FileLock lock = null;

      if (imageSize > 0) {
        TransactionData theLogEntry =
            getLogName(objUid, tName, buffSize); // always adds entry to log
        LogInstance theLog = theLogEntry.container;

        if (theLog == null) throw new ObjectStoreException();

        fname = genPathName(theLog.getName(), tName, ft);
        fd = openAndLock(fname, FileLock.F_WRLCK, true);

        if (fd == null) {
          tsLogger.i18NLogger.warn_objectstore_ShadowingStore_18(fname);

          return false;
        }

        boolean setLength = !fd.exists();

        try {
          ofile = new RandomAccessFile(fd, FILE_MODE);

          if (setLength) {
            ofile.setLength(_maxFileSize);
          } else {
            // may have to resize file if we keep updating this transaction info

            if (theLog.remaining() < buffSize) {
              long size = ofile.length() + buffSize - theLog.remaining();

              ofile.setLength(size);

              theLog.resize(size);
            }
          }

          java.nio.ByteBuffer buff = java.nio.ByteBuffer.allocate(buffSize);

          buff.put(_redzone);
          buff.putInt(uidString.length);
          buff.put(uidString);
          buff.putInt(imageSize);
          buff.put(state.buffer());

          synchronized (_lock) {
            ofile.seek(theLogEntry.offset);

            ofile.write(buff.array());
          }
        } catch (SyncFailedException e) {
          unlockAndClose(fd, ofile);

          throw new ObjectStoreException(
              "ShadowingStore::write_state() - write failed to sync for " + fname, e);
        } catch (FileNotFoundException e) {
          unlockAndClose(fd, ofile);

          e.printStackTrace();

          throw new ObjectStoreException(
              "ShadowingStore::write_state() - write failed to locate file " + fname + ": " + e, e);
        } catch (IOException e) {
          unlockAndClose(fd, ofile);

          e.printStackTrace();

          throw new ObjectStoreException(
              "ShadowingStore::write_state() - write failed for " + fname + ": " + e, e);
        } finally {
          try {
            if (lock != null) lock.release();
          } catch (IOException ex) {
            ex.printStackTrace();
          }
        }
      }

      if (!unlockAndClose(fd, ofile)) {
        tsLogger.i18NLogger.warn_objectstore_ShadowingStore_19(fname);
      }

      super.addToCache(fname);

      return true;
    } else
      throw new ObjectStoreException(
          "ShadowStore::write_state - "
              + tsLogger.i18NLogger.get_objectstore_notypenameuid()
              + objUid);
  }
Esempio n. 9
0
  /**
   * This is a recovery-only method and should not be called during normal execution. As such we
   * need to load in all of the logs we can find that aren't already loaded (or activated).
   */
  public boolean allObjUids(String tName, InputObjectState state, int match)
      throws ObjectStoreException {
    /*
     * match will always be OS_COMMITTED since that's all we ever write for
     * the logs.
     */

    // in case of asynchronous removals trigger the purger now.

    _purger.trigger();

    /*
     * Get a list of logs. Load them in to memory if we aren't already
     * working on them/it. But we can prune the entry once we're
     * finished or the memory footprint will grow. We should do this
     * for all frozen entries eventually too.
     */

    InputObjectState logs = new InputObjectState();
    OutputObjectState objUids = new OutputObjectState();

    /*
     * We never call this method except during recovery. As such we shouldn't
     * need to worry about optimizations such as checking whether or not the
     * log is in current working memory.
     */

    if (!super.allObjUids(tName, logs, match)) return false;
    else {
      /*
       * Now we have all of the log names let's attach to each one
       * and locate the committed instances (not deleted.)
       */

      Uid logName = new Uid(Uid.nullUid());

      try {
        do {
          logName = UidHelper.unpackFrom(logs);

          if (logName.notEquals(Uid.nullUid())) {
            /*
             * Could check to see if log is in current working memory.
             */

            /*
             * TODO
             *
             * First purge the log if we can, but we need to know that
             * we're not playing with an instance that is being manipulated
             * from another VM instance.
             */

            ArrayList<InputObjectState> txs = scanLog(logName, tName);

            if (txs.size() > 0) {
              for (int i = 0; i < txs.size(); i++) {
                UidHelper.packInto(txs.get(i).stateUid(), objUids);
              }
            }
          }
        } while (logName.notEquals(Uid.nullUid()));

        // remember null terminator

        UidHelper.packInto(Uid.nullUid(), objUids);

        state.setBuffer(objUids.buffer());
      } catch (final IOException ex) {
        ex.printStackTrace();

        return false;
      }

      return true;
    }
  }
 /** save the endpoint reference to the coordinator for this participant */
 protected void saveEndpointReference(OutputObjectState oos) throws IOException {
   // the toString method will do what we need
   oos.packString(endpoint.toString());
 }
Esempio n. 11
0
  private boolean packObjectArray(final Field afield, OutputObjectState os) {
    boolean success = true;

    try {
      Class<?> c = afield.getType();
      final int size = Array.getLength(afield.get(_theObject));

      if (c.equals(Integer[].class)) {
        final Integer[] objs = (Integer[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packInt(objs[i]);
          }
        }
      } else if (c.equals(Boolean[].class)) {
        final Boolean[] objs = (Boolean[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packBoolean(objs[i]);
          }
        }
      } else if (c.equals(Byte[].class)) {
        final Byte[] objs = (Byte[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packByte(objs[i]);
          }
        }
      } else if (c.equals(Short[].class)) {
        final Short[] objs = (Short[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packShort(objs[i]);
          }
        }
      } else if (c.equals(Long[].class)) {
        final Long[] objs = (Long[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packLong(objs[i]);
          }
        }
      } else if (c.equals(Float[].class)) {
        final Float[] objs = (Float[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packFloat(objs[i]);
          }
        }
      } else if (c.equals(Double[].class)) {
        final Double[] objs = (Double[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packDouble(objs[i]);
          }
        }
      } else if (c.equals(Character[].class)) {
        final Character[] objs = (Character[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packChar(objs[i]);
          }
        }
      } else if (c.equals(String[].class)) {
        final String[] objs = (String[]) afield.get(_theObject);

        for (int i = 0; (i < size) && success; i++) {
          if (objs[i] == null) os.packBoolean(false);
          else {
            os.packBoolean(true);
            os.packString(objs[i]);
          }
        }
      } else {
        System.err.println("Array type " + c + " not supported!");

        success = false;
      }
    } catch (final Throwable ex) {
      ex.printStackTrace();

      success = false;
    }

    return success;
  }
Esempio n. 12
0
  public boolean save_state(OutputObjectState os, int t) {
    boolean res = false;

    try {
      os.packInt(_heuristic);
      os.packBoolean(_committed);

      /*
       * Since we don't know what type of Xid we are using, leave it up to
       * XID to pack.
       */

      XidImple.pack(os, _tranID);

      /*
       * If no recovery object set then rely upon object serialisation!
       */

      if (_recoveryObject == null) {
        os.packInt(RecoverableXAConnection.OBJECT_RECOVERY);

        os.packString(_productName);
        os.packString(_productVersion);
        os.packString(_jndiName);

        if (_theXAResource instanceof Serializable) {
          try {
            ByteArrayOutputStream s = new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(s);

            o.writeObject(_theXAResource);
            o.close();

            os.packBoolean(true);
            String name = _theXAResource.getClass().getName();
            os.packString(name);

            os.packBytes(s.toByteArray());
          } catch (NotSerializableException ex) {
            jtaLogger.i18NLogger.warn_resources_arjunacore_savestate();

            return false;
          }
        } else {
          // have to rely upon XAResource.recover!

          os.packBoolean(false);
        }
      } else {
        os.packInt(RecoverableXAConnection.AUTO_RECOVERY);
        os.packString(_recoveryObject.getClass().getName());

        _recoveryObject.packInto(os);
      }

      res = true;
    } catch (Exception e) {
      jtaLogger.i18NLogger.warn_resources_arjunacore_savestateerror(
          _theXAResource.toString(), XAHelper.xidToString(_tranID), e);

      res = false;
    }

    if (res) res = super.save_state(os, t);

    return res;
  }