Esempio n. 1
0
 public static long inputStream2DataOutput(
     final InputStream is, final DataOutput output, final BytesPool pool) {
   long totalBytes = 0;
   final Ref<byte[]> bytes = pool.retainObject();
   try {
     while (is.available() > 0) {
       final int actualSize = is.read(bytes.object());
       if (actualSize == -1) {
         break;
       }
       output.write(bytes.object(), 0, actualSize);
       totalBytes += actualSize;
       if (LOG.isTraceEnabled()) {
         LOG.trace("read bytebuf's content to bytesList, size {}", actualSize);
       }
     }
   } catch (Throwable e) {
     LOG.warn(
         "exception when inputStream -> OutputStream, detail: {}",
         ExceptionUtils.exception2detail(e));
   } finally {
     bytes.release();
   }
   return totalBytes;
 }
Esempio n. 2
0
  public void start() {
    this._zkCache
        .getListenable()
        .addListener(
            new TreeCacheListener() {

              @Override
              public void childEvent(CuratorFramework client, TreeCacheEvent event)
                  throws Exception {
                switch (event.getType()) {
                  case NODE_ADDED:
                    nodeAdded(event);
                    break;
                  case NODE_REMOVED:
                    nodeRemoved(event);
                    break;
                  case NODE_UPDATED:
                    nodeUpdated(event);
                  default:
                    if (LOG.isDebugEnabled()) {
                      LOG.debug("unhandle event ({}), just ignore.", event);
                    }
                    break;
                }
              }
            });
    try {
      this._zkCache.start();
    } catch (Exception e) {
      LOG.error(
          "exception when TreeCache({})'s start, detail:{}",
          this._zkCache,
          ExceptionUtils.exception2detail(e));
    }
  }
Esempio n. 3
0
  public static Blob file2Blob(final File file, final BytesPool pool) {
    InputStream is = null;
    PooledBytesOutputStream os = null;

    try {
      is = new FileInputStream(file);
      os = new PooledBytesOutputStream(pool);
      inputStream2OutputStream(is, os);
      return os.drainToBlob();
    } catch (Exception e) {
      LOG.warn(
          "exception when file2Blob for file {}, detail: {}",
          file,
          ExceptionUtils.exception2detail(e));
    } finally {
      if (null != is) {
        try {
          is.close();
        } catch (IOException e) {
        }
      }
      if (null != os) {
        try {
          os.close();
        } catch (IOException e) {
        }
      }
    }
    return null;
  }
 public Object getMBean(final String suffix) {
   try {
     return this._objectNames.get(ObjectName.getInstance(createObjectName(suffix)));
   } catch (Exception e) {
     LOG.error(
         "exception when getMBean for suffix {}, detail: {}",
         suffix,
         ExceptionUtils.exception2detail(e));
     return null;
   }
 }
Esempio n. 5
0
 private void nodeAdded(final TreeCacheEvent event) throws Exception {
   if (LOG.isDebugEnabled()) {
     LOG.debug("handle event ({}), try to add or update operator", event);
   }
   try {
     this._operator.doAdd(this._root, event);
   } catch (Exception e) {
     LOG.warn(
         "exception when doAdd for event({}), detail:{}",
         event,
         ExceptionUtils.exception2detail(e));
   }
 }
  public boolean isRegistered(final String suffix) {
    try {
      final ObjectName objectName = ObjectName.getInstance(createObjectName(suffix));

      return (this._objectNames.containsKey(objectName)
          || this._mbeanServer.isRegistered(objectName));
    } catch (Exception e) {
      LOG.error(
          "exception when test suffix({}) isRegistered, detail:{}",
          suffix,
          ExceptionUtils.exception2detail(e));
    }
    return false;
  }
  public boolean registerMBean(final String suffix, final Object mbean) {

    if (null == mbean) {
      LOG.error("registerMBean with suffix({}) failed, mbean is null", suffix);
      return false;
    }

    this._rlock.lock();

    try {
      final ObjectName objectName = ObjectName.getInstance(createObjectName(suffix));
      if (null != this._objectNames.putIfAbsent(objectName, mbean)) {
        //  该ObjectName 已经或正在注册 mbean
        LOG.error(
            "registerMBean {}/{} failed, ObjectName({}) already used",
            new Object[] {suffix, mbean, objectName});
        return false;
      }

      try {
        this._mbeanServer.registerMBean(mbean, objectName);
      } catch (Exception e) {
        this._objectNames.remove(objectName);
        throw e;
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("register mbean {} with objectname {} succeed.", mbean, objectName);
      }

      return true;

    } catch (Exception e) {
      LOG.error(
          "exception when registerMBean {}/{}, detail: {}",
          new Object[] {suffix, mbean, ExceptionUtils.exception2detail(e)});
      return false;
    } finally {
      this._rlock.unlock();
    }
  }
  public void unregisterMBean(final String suffix) {
    this._rlock.lock();

    try {
      final ObjectName objectName = ObjectName.getInstance(createObjectName(suffix));
      if (this._objectNames.containsKey(objectName)) {
        this._mbeanServer.unregisterMBean(objectName);
        this._objectNames.remove(objectName);
        if (LOG.isDebugEnabled()) {
          LOG.debug("unregister mbean by objectname {} succeed.", objectName);
        }
      } else {
        LOG.error(
            "unregister mbean failed, bcs objectname({}) not exist or not register via this MBeanRegisterSupport.",
            objectName);
      }
    } catch (Exception e) {
      LOG.error("exception when unregisterMBean, detail: {}", ExceptionUtils.exception2detail(e));
    } finally {
      this._rlock.unlock();
    }
  }
  public boolean replaceRegisteredMBean(
      final String suffix, final Object oldMBean, final Object newMBean) {
    this._rlock.lock();

    try {
      final ObjectName objectName = ObjectName.getInstance(createObjectName(suffix));
      if (this._objectNames.containsKey(objectName)) {
        if (this._objectNames.replace(objectName, oldMBean, newMBean)) {
          try {
            this._mbeanServer.unregisterMBean(objectName);
            this._mbeanServer.registerMBean(newMBean, objectName);
            if (LOG.isDebugEnabled()) {
              LOG.debug("replaceRegisteredMBean objectname {} succeed.", objectName);
            }
            return true;
          } catch (Exception e) {
            this._objectNames.remove(objectName);
            throw e;
          }
        } else {
          LOG.error(
              "replaceRegisteredMBean mbean failed, bcs objectname({}) 's oldMBean not match.",
              objectName);
        }
      } else {
        LOG.error(
            "replaceRegisteredMBean mbean failed, bcs objectname({}) not exist or not register via this MBeanRegisterSupport.",
            objectName);
      }
      return false;
    } catch (Exception e) {
      LOG.error(
          "exception when replaceRegisteredMBean, detail: {}", ExceptionUtils.exception2detail(e));
      return false;
    } finally {
      this._rlock.unlock();
    }
  }
  public void unregisterAllMBeans() {
    this._wlock.lock();

    try {
      for (Map.Entry<ObjectName, Object> entry : this._objectNames.entrySet()) {
        try {
          this._mbeanServer.unregisterMBean(entry.getKey());
          if (LOG.isDebugEnabled()) {
            LOG.debug(
                "unregisterAllMBeans: unregister mbean with objectname {} succeed.",
                entry.getKey());
          }
        } catch (Exception e) {
          LOG.error(
              "exception when (unregisterAllMBean|destroy)'s unregisterMBean({}), detail: {}",
              entry.getKey(),
              ExceptionUtils.exception2detail(e));
        }
      }
      _objectNames.clear();
    } finally {
      this._wlock.unlock();
    }
  }