Esempio n. 1
0
  @Override
  public RubySymbol execute(VirtualFrame frame) {
    notDesignedForCompilation();

    final Object receiverObject = receiver.execute(frame);

    final RubyMethod methodObject = (RubyMethod) method.execute(frame);

    RubyModule module;

    if (receiverObject instanceof RubyModule) {
      module = (RubyModule) receiverObject;
    } else {
      module = ((RubyBasicObject) receiverObject).getSingletonClass(this);
    }

    final RubyMethod methodWithDeclaringModule = methodObject.withDeclaringModule(module);

    if (moduleFunctionFlag(frame)) {
      module.addMethod(this, methodWithDeclaringModule.withVisibility(Visibility.PRIVATE));
      module
          .getSingletonClass(this)
          .addMethod(this, methodWithDeclaringModule.withVisibility(Visibility.PUBLIC));
    } else {
      module.addMethod(this, methodWithDeclaringModule);
    }

    return getContext().newSymbol(method.getName());
  }
Esempio n. 2
0
  public static void initMethods(RubyModule klass) {
    klass
        .getSingletonClass()
        .defineMethod(
            "dosync",
            new RubyNoOrOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                try {
                  getInstance().addQueueCommand(new SyncCommand(SyncThread.scSyncAll, true));

                } catch (Exception e) {
                  LOG.ERROR("dosync failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
                return getInstance().getRetValue();
              }

              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                try {
                  String str = arg.asString();
                  boolean show = arg.equals(RubyConstant.QTRUE) || "true".equalsIgnoreCase(str);
                  getInstance().addQueueCommand(new SyncCommand(SyncThread.scSyncAll, show));
                } catch (Exception e) {
                  LOG.ERROR("dosync failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
                return getInstance().getRetValue();
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "dosync_source",
            new RubyOneOrTwoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
                try {
                  int nSrcID = 0;
                  String strName = "";
                  if (arg instanceof RubyFixnum) nSrcID = arg.toInt();
                  else strName = arg.toStr();

                  getInstance()
                      .addQueueCommand(
                          new SyncCommand(SyncThread.scSyncOne, strName, nSrcID, true));
                } catch (Exception e) {
                  LOG.ERROR("dosync_source failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
                return getInstance().getRetValue();
              }

              protected RubyValue run(
                  RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
                try {
                  String str = arg1.asString();
                  boolean show = arg1.equals(RubyConstant.QTRUE) || "true".equalsIgnoreCase(str);

                  int nSrcID = 0;
                  String strName = "";
                  if (arg0 instanceof RubyFixnum) nSrcID = arg0.toInt();
                  else strName = arg0.toStr();

                  getInstance()
                      .addQueueCommand(
                          new SyncCommand(SyncThread.scSyncOne, strName, nSrcID, show));
                } catch (Exception e) {
                  LOG.ERROR("dosync_source failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
                return getInstance().getRetValue();
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "dosearch",
            new RubyVarArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
                if (args.size() != 7)
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in SyncEngine.dosearch_source: wrong number of arguments ( "
                          + args.size()
                          + " for "
                          + 7
                          + " )");

                try {
                  Vector arSources = RhoRuby.makeVectorStringFromArray(args.get(0));

                  String from = args.get(1).toStr();
                  String params = args.get(2).toStr();

                  String str = args.get(3).asString();
                  int nProgressStep = args.get(4).toInt();
                  String callback = args.get(5) != RubyConstant.QNIL ? args.get(5).toStr() : "";
                  String callback_params =
                      args.get(6) != RubyConstant.QNIL ? args.get(6).toStr() : "";

                  boolean bSearchSyncChanges =
                      args.get(3).equals(RubyConstant.QTRUE) || "true".equalsIgnoreCase(str);
                  stopSync();

                  if (callback != null && callback.length() > 0)
                    getSyncEngine().getNotify().setSearchNotification(callback, callback_params);

                  getInstance()
                      .addQueueCommand(
                          new SyncSearchCommand(
                              from, params, arSources, bSearchSyncChanges, nProgressStep));
                } catch (Exception e) {
                  LOG.ERROR("SyncEngine.login", e);
                  RhoRuby.raise_RhoError(RhoAppAdapter.ERR_RUNTIME);
                }

                return getInstance().getRetValue();
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "stop_sync",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                try {
                  stopSync();
                } catch (Exception e) {
                  LOG.ERROR("stop_sync failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "login",
            new RubyVarArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
                if (args.size() != 3)
                  throw new RubyException(
                      RubyRuntime.ArgumentErrorClass,
                      "in SyncEngine.login: wrong number of arguments ( "
                          + args.size()
                          + " for "
                          + 3
                          + " )");

                try {
                  String name = args.get(0).toStr();
                  String password = args.get(1).toStr();
                  String callback = args.get(2).toStr();

                  stopSync();

                  getInstance()
                      .addQueueCommand(
                          new SyncLoginCommand(
                              name,
                              password,
                              callback,
                              new SyncNotify.SyncNotification(callback, "", false)));
                } catch (Exception e) {
                  LOG.ERROR("SyncEngine.login", e);
                  RhoRuby.raise_RhoError(RhoAppAdapter.ERR_RUNTIME);
                }

                return getInstance().getRetValue();
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "logged_in",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                DBAdapter db = DBAdapter.getUserDB();

                try {
                  return getSyncEngine().isLoggedIn()
                      ? ObjectFactory.createInteger(1)
                      : ObjectFactory.createInteger(0);
                } catch (Exception e) {
                  LOG.ERROR("logged_in failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "logout",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                DBAdapter db = DBAdapter.getUserDB();

                try {
                  stopSync();
                  getSyncEngine().stopSyncByUser();
                  getSyncEngine().logout();
                } catch (Exception e) {
                  LOG.ERROR("logout failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_notification",
            new RubyVarArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {

                try {
                  int source_id = args.get(0).toInt();
                  String url = args.get(1).toStr();
                  String params = args.get(2).toStr();
                  getSyncEngine()
                      .getNotify()
                      .setSyncNotification(
                          source_id,
                          new SyncNotify.SyncNotification(
                              url, params != null ? params : "", source_id != -1));
                } catch (Exception e) {
                  LOG.ERROR("set_notification failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
                return RubyConstant.QNIL;
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "clear_notification",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  int source_id = arg1.toInt();
                  getSyncEngine().getNotify().clearSyncNotification(source_id);
                } catch (Exception e) {
                  LOG.ERROR("clear_notification failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_pollinterval",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  int nOldInterval = getInstance().getPollInterval();
                  int nInterval = arg1.toInt();
                  getInstance().setPollInterval(nInterval);

                  return ObjectFactory.createInteger(nOldInterval);
                } catch (Exception e) {
                  LOG.ERROR("set_pollinterval failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "get_pollinterval",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                try {
                  int nOldInterval = getInstance().getPollInterval();
                  return ObjectFactory.createInteger(nOldInterval);
                } catch (Exception e) {
                  LOG.ERROR("set_pollinterval failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_syncserver",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  String syncserver = arg1.toStr();

                  stopSync();
                  getSyncEngine().setSyncServer(syncserver);

                  if (syncserver != null && syncserver.length() > 0) {
                    SyncThread.getInstance().start(SyncThread.epLow);
                    if (ClientRegister.getInstance() != null)
                      ClientRegister.getInstance().startUp();
                  } else {
                    // DO NOT STOP thread. because they cannot be restarted
                    // SyncThread.getInstance().stop(SYNC_WAIT_BEFOREKILL_SECONDS);
                    // if ( ClientRegister.getInstance() != null )
                    //	ClientRegister.getInstance().stop(SYNC_WAIT_BEFOREKILL_SECONDS);
                  }

                } catch (Exception e) {
                  LOG.ERROR("set_syncserver failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "get_src_attrs",
            new RubyTwoArgMethod() {
              protected RubyValue run(
                  RubyValue receiver, RubyValue arg0, RubyValue arg1, RubyBlock block) {
                try {
                  // String strPartition = arg0.toStr();
                  // int nSrcID = arg1.toInt();
                  // return DBAdapter.getDB(strPartition).getAttrMgr().getAttrsBySrc(nSrcID);
                  return RubyConstant.QNIL;
                } catch (Exception e) {
                  LOG.ERROR("get_src_attrs failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "is_blob_attr",
            new RubyVarArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
                try {
                  String strPartition = args.get(0).toStr();
                  Integer nSrcID = new Integer(args.get(1).toInt());
                  String strAttrName = args.get(2).toStr();
                  boolean bExists =
                      DBAdapter.getDB(strPartition).getAttrMgr().isBlobAttr(nSrcID, strAttrName);
                  return ObjectFactory.createBoolean(bExists);
                } catch (Exception e) {
                  LOG.ERROR("get_src_attrs failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_objectnotify_url",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  String url = arg1.toStr();
                  SyncNotify.setObjectNotifyUrl(url);
                } catch (Exception e) {
                  LOG.ERROR("set_objectnotify_url failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "add_objectnotify",
            new RubyTwoArgMethod() {
              protected RubyValue run(
                  RubyValue receiver, RubyValue arg1, RubyValue arg2, RubyBlock block) {
                try {
                  Integer nSrcID = new Integer(arg1.toInt());
                  String strObject = arg2.toStr();

                  getSyncEngine().getNotify().addObjectNotify(nSrcID, strObject);
                } catch (Exception e) {
                  LOG.ERROR("add_objectnotify failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "clean_objectnotify",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                try {
                  getSyncEngine().getNotify().cleanObjectNotifications();
                } catch (Exception e) {
                  LOG.ERROR("clean_objectnotify failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "get_lastsync_objectcount",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  Integer nSrcID = new Integer(arg1.toInt());
                  int nCount = getSyncEngine().getNotify().getLastSyncObjectCount(nSrcID);

                  return ObjectFactory.createInteger(nCount);
                } catch (Exception e) {
                  LOG.ERROR("get_lastsync_objectcount failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "get_pagesize",
            new RubyNoArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyBlock block) {
                try {
                  return ObjectFactory.createInteger(getSyncEngine().getSyncPageSize());
                } catch (Exception e) {
                  LOG.ERROR("get_pagesize failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_pagesize",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  getSyncEngine().setSyncPageSize(arg1.toInt());
                } catch (Exception e) {
                  LOG.ERROR("set_pagesize failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_threaded_mode",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  boolean bThreadMode = arg1 == RubyConstant.QTRUE;
                  getInstance().setNonThreadedMode(!bThreadMode);
                  getSyncEngine().setNonThreadedMode(!bThreadMode);
                } catch (Exception e) {
                  LOG.ERROR("set_threaded_mode failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "enable_status_popup",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  boolean bEnable = arg1 == RubyConstant.QTRUE;
                  getSyncEngine().getNotify().enableStatusPopup(bEnable);
                } catch (Exception e) {
                  LOG.ERROR("enable_status_popup failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });

    klass
        .getSingletonClass()
        .defineMethod(
            "set_source_property",
            new RubyVarArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyArray args, RubyBlock block) {
                try {
                  Integer nSrcID = new Integer(args.get(0).toInt());
                  String strPropName = args.get(1).toStr();
                  String strPropValue = args.get(2).toStr();

                  SyncEngine.getSourceOptions().setProperty(nSrcID, strPropName, strPropValue);

                  return RubyConstant.QNIL;
                } catch (Exception e) {
                  LOG.ERROR("set_source_property failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }
              }
            });
    klass
        .getSingletonClass()
        .defineMethod(
            "set_ssl_verify_peer",
            new RubyOneArgMethod() {
              protected RubyValue run(RubyValue receiver, RubyValue arg1, RubyBlock block) {
                try {
                  boolean bVerify = arg1 == RubyConstant.QTRUE;
                  getSyncEngine().getNet().sslVerifyPeer(bVerify);
                } catch (Exception e) {
                  LOG.ERROR("set_ssl_verify_peer failed", e);
                  throw (e instanceof RubyException
                      ? (RubyException) e
                      : new RubyException(e.getMessage()));
                }

                return RubyConstant.QNIL;
              }
            });
  }