Example #1
0
 /** Intercepts all messages sent from the client */
 @Override
 public String handle(ClientImpl client, Transport transport, Message message)
     throws IOException {
   if (!message.getChannel().startsWith("/meta")) {
     // These are published messages
     Persistable target =
         (Persistable) Identification.idForString((String) message.getChannel()).getTarget();
     Function messageFunc = (Function) target.get("message");
     messageFunc.call(
         PersevereContextFactory.getContext(),
         GlobalData.getGlobalScope(),
         target,
         new Object[] {message.getData()});
     // don't actually publish it, let Persevere handle it
     return null;
   }
   if (message.getChannel().startsWith("/meta/subscribe")) {
     // A subscription request, we just need to make sure the global listener
     //	object is subscribed to the given resource
     String id = (String) message.get("subscription");
     while (id.endsWith("*")) {
       id = id.substring(0, id.length() - 1);
     }
     Object target = Identification.idForString(id).getTarget();
     if (target instanceof ObservablePersistable) ((ObservablePersistable) target).subscribe();
     PersistableObject.addListener(jettyBayeuxRestListener);
   }
   return super.handle(client, transport, message);
 }
  /* ------------------------------------------------------------ */
  private void doInvoke(Method method, Client fromClient, Client toClient, Message msg) {
    String channel = (String) msg.get(Bayeux.CHANNEL_FIELD);
    Object data = msg.get(Bayeux.DATA_FIELD);
    String id = msg.getId();

    if (method != null) {
      try {
        Class<?>[] args = method.getParameterTypes();
        Object arg = Message.class.isAssignableFrom(args[1]) ? msg : data;

        Object reply = null;
        switch (method.getParameterTypes().length) {
          case 2:
            reply = method.invoke(this, fromClient, arg);
            break;
          case 3:
            reply = method.invoke(this, fromClient, arg, id);
            break;
          case 4:
            reply = method.invoke(this, fromClient, channel, arg, id);
            break;
        }

        if (reply != null) send(fromClient, channel, reply, id);
      } catch (Exception e) {
        System.err.println(method);
        exception(fromClient, toClient, msg, e);
      } catch (Error e) {
        System.err.println(method);
        exception(fromClient, toClient, msg, e);
      }
    }
  }
 public void deliver(Client fromClient, Client toClient, Message msg) {
   if (!_seeOwn && fromClient == getClient()) return;
   String channel = (String) msg.get(Bayeux.CHANNEL_FIELD);
   Method method = _methods.get(channel);
   invoke(method, fromClient, toClient, msg);
 }