コード例 #1
0
/** @author hp */
public class CompositeClosureEx implements ClosureEx {

  private static final Logger logger = LoggerFactory.getLogger(CompositeClosureEx.class);
  private ClosureEx[] closures;

  /** @return the closures */
  public List<ClosureEx> getClosures() {
    return Arrays.asList(closures);
  }

  /** @param closures the closures to set */
  public void setClosures(List<ClosureEx> closures) {
    this.closures = closures.toArray(new ClosureEx[0]);
  }

  /* (non-Javadoc)
   * @see com.skymobi.util.ClosureEx#execute(java.lang.Object[])
   */
  @Override
  public void execute(final Object... args) {
    for (ClosureEx closure : this.closures) {
      try {
        closure.execute(args);
      } catch (Exception e) {
        logger.error("execute:", e);
      }
    }
  }

  /* (non-Javadoc)
   * @see com.skymobi.util.ClosureEx#setCanceled(boolean)
   */
  @Override
  public void setCanceled(boolean canceled) {
    for (ClosureEx closure : this.closures) {
      try {
        closure.setCanceled(canceled);
      } catch (Exception e) {
        logger.error("setCanceled:", e);
      }
    }
  }
}
コード例 #2
0
/** @author isdom */
public class ShortCodec extends AbstractPrimitiveCodec implements ByteFieldCodec {

  private static final Logger logger = LoggerFactory.getLogger(ShortCodec.class);

  public DecResult decode(DecContext ctx) {
    byte[] bytes = ctx.getDecBytes();
    int byteLength = ctx.getByteSize();
    NumberCodec numberCodec = ctx.getNumberCodec();

    if (byteLength > bytes.length) {
      String errmsg =
          "ShortCodec: not enough bytes for decode, need ["
              + byteLength
              + "], actually ["
              + bytes.length
              + "].";
      if (null != ctx.getField()) {
        errmsg += "/ cause field is [" + ctx.getField() + "]";
      }
      logger.error(errmsg);
      throw new RuntimeException(errmsg);
      // return makeDecResult( retShort, bytes );
    }
    return new DecResult(
        numberCodec.bytes2Short(bytes, byteLength),
        ArrayUtils.subarray(bytes, byteLength, bytes.length));
  }

  public byte[] encode(EncContext ctx) {
    short enc = ((Short) ctx.getEncObject()).shortValue();
    int byteLength = ctx.getByteSize();
    NumberCodec numberCodec = ctx.getNumberCodec();

    return numberCodec.short2Bytes(enc, byteLength);
  }

  public Class<?>[] getFieldType() {
    return new Class<?>[] {short.class, Short.class};
  }
}
コード例 #3
0
/** @author hp */
public class FieldUtils {

  private static final Logger logger = LoggerFactory.getLogger(FieldUtils.class);

  public static Class<?> getComponentClass(Field field) {
    if (null == field) {
      String errmsg = "FieldUtils: field is null, can't get compoment class.";
      logger.error(errmsg);
      throw new RuntimeException(errmsg);
    }
    Type type = field.getGenericType();

    if (null == type || !(type instanceof ParameterizedType)) {
      String errmsg =
          "FieldUtils: getGenericType invalid, can't get compoment class."
              + "/ cause field is ["
              + field
              + "]";
      logger.error(errmsg);
      throw new RuntimeException(errmsg);
    }
    ParameterizedType parameterizedType = (ParameterizedType) type;
    Class<?> clazz = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    return clazz;
  }

  public static Field[] getAllFieldsOfClass(Class<?> cls) {
    Field[] fields = new Field[0];

    Class<?> itr = cls;
    while ((null != itr) && !itr.equals(Object.class)) {
      fields = (Field[]) ArrayUtils.addAll(itr.getDeclaredFields(), fields);
      itr = itr.getSuperclass();
    }

    return fields;
  }

  public static Field[] getAnnotationFieldsOf(
      Class<?> cls, Class<? extends Annotation> annotationClass) {
    Field[] fields = new Field[0];

    Class<?> itr = cls;
    while ((null != itr) && !itr.equals(Object.class)) {
      fields = (Field[]) ArrayUtils.addAll(itr.getDeclaredFields(), fields);
      itr = itr.getSuperclass();
    }

    int idx = 0;
    for (Field field : fields) {
      if (null != field.getAnnotation(annotationClass)) {
        idx++;
      }
    }

    Field[] ret = new Field[idx];
    idx = 0;
    for (Field field : fields) {
      field.setAccessible(true);
      if (null != field.getAnnotation(annotationClass)) {
        ret[idx++] = field;
      }
    }

    return ret;
  }
}
コード例 #4
0
/** @author isdom */
public class LenListCodec implements ByteFieldCodec {

  private static final Logger logger = LoggerFactory.getLogger(LenListCodec.class);

  //	private	DecContextFactory	decContextFactory;
  //	private	EncContextFactory	encContextFactory;

  public DecContextFactory getDecContextFactory() {
    return null; // decContextFactory;
  }

  public void setDecContextFactory(DecContextFactory decContextFactory) {
    //		this.decContextFactory = decContextFactory;
  }

  public EncContextFactory getEncContextFactory() {
    return null; // encContextFactory;
  }

  public void setEncContextFactory(EncContextFactory encContextFactory) {
    //		this.encContextFactory = encContextFactory;
  }

  public Class<?> getCompomentClass(Field field) {
    if (null == field) {
      String errmsg = "LenListCodec: field is null, can't get compoment class.";
      logger.error(errmsg);
      throw new RuntimeException(errmsg);
    }
    Type type = field.getGenericType();

    if (null == type || !(type instanceof ParameterizedType)) {
      String errmsg =
          "LenListCodec: getGenericType invalid, can't get compoment class."
              + "/ cause field is ["
              + field
              + "]";
      logger.error(errmsg);
      throw new RuntimeException(errmsg);
    }
    ParameterizedType parameterizedType = (ParameterizedType) type;
    Class<?> clazz = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    return clazz;
  }

  public DecResult decode(DecContext ctx) {
    DecResult ret =
        ctx.getCodecOf(short.class)
            .decode(
                ctx.getDecContextFactory()
                    .createDecContext(ctx.getDecBytes(), short.class, ctx.getDecOwner(), null));
    short listLength = (Short) ret.getValue();
    byte[] bytes = ret.getRemainBytes();
    Class<?> compomentClass = getCompomentClass(ctx.getField());

    ArrayList<Object> list = null;
    if (listLength > 0) {
      //            if (logger.isDebugEnabled()) {
      //                logger.debug( "bytes2Array: decode Array length [" +  arrayLength +"]" );
      //            }
      list = new ArrayList<Object>(listLength);
      ByteFieldCodec anyCodec = ctx.getCodecOf(FieldCodecCategory.ANY);

      for (int idx = 0; idx < listLength; idx++) {
        ret =
            anyCodec.decode(
                ctx.getDecContextFactory()
                    .createDecContext(bytes, compomentClass, ctx.getDecOwner(), null));
        list.add(ret.getValue());
        bytes = ret.getRemainBytes();
      }
    }

    return new DecResult(list, bytes);
  }

  @SuppressWarnings("unchecked")
  public byte[] encode(EncContext ctx) {
    ArrayList<Object> list = (ArrayList<Object>) ctx.getEncObject();
    int listLength = (null != list ? list.size() : 0);
    Class<?> compomentClass = getCompomentClass(ctx.getField());

    byte[] bytes =
        ctx.getCodecOf(short.class)
            .encode(
                ctx.getEncContextFactory().createEncContext((short) listLength, short.class, null));

    if (listLength > 0) {

      ByteFieldCodec anyCodec = ctx.getCodecOf(FieldCodecCategory.ANY);

      for (int idx = 0; idx < listLength; idx++) {
        bytes =
            ArrayUtils.addAll(
                bytes,
                anyCodec.encode(
                    ctx.getEncContextFactory()
                        .createEncContext(list.get(idx), compomentClass, null)));
      }
    }
    return bytes;
  }

  public FieldCodecCategory getCategory() {
    return null;
  }

  public Class<?>[] getFieldType() {
    return new Class<?>[] {ArrayList.class};
  }
}