Ejemplo n.º 1
0
  public void start() throws Exception {
    log.info("Starting Gravity...");
    synchronized (this) {
      if (!started) {
        adapterFactory = new AdapterFactory(this);
        internalStart();
        serverChannel = new ServerChannel(this, ServerChannel.class.getName(), null, null);

        if (gravityConfig.isUseUdp()) {
          ServiceLoader<UdpReceiverFactory> loader = ServiceLoader.load(UdpReceiverFactory.class);
          Iterator<UdpReceiverFactory> factories = loader.iterator();
          if (factories.hasNext()) {
            udpReceiverFactory = factories.next();
            udpReceiverFactory.setPort(gravityConfig.getUdpPort());
            udpReceiverFactory.setNio(gravityConfig.isUdpNio());
            udpReceiverFactory.setConnected(gravityConfig.isUdpConnected());
            udpReceiverFactory.setSendBufferSize(gravityConfig.getUdpSendBufferSize());
            udpReceiverFactory.start();
          } else log.warn("UDP receiver factory not found");
        }

        started = true;
      }
    }
    log.info("Gravity successfully started.");
  }
Ejemplo n.º 2
0
  /*
   * (non-Javadoc)
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    if (log.isInfoEnabled())
      try {
        GraniteContext context =
            HttpGraniteContext.createThreadIntance(
                graniteConfig, servicesConfig, getServletContext(), request, response);
        if (context == null) throw new ServletException("GraniteContext not Initialized!!");

        // AMFContextImpl amf = (AMFContextImpl) context.getAMFContext();
        // Phase1 Deserializing AMF0 request
        if (log.isInfoEnabled())
          log.info(">>>>> Deserializing AMF0 request from..." + request.getRequestURI());
        AMF0Deserializer deserializer =
            new AMF0Deserializer(new DataInputStream(request.getInputStream()));
        AMF0Message amf0Request = deserializer.getAMFMessage();

        // Phase2 Processing AMF0 request
        if (log.isInfoEnabled()) log.info(">>>>> Processing AMF0 request: " + amf0Request);
        AMF0Message amf0Response = AMF0MessageProcessor.process(amf0Request);
        if (log.isInfoEnabled()) log.info("<<<<< Returning AMF0 response: " + amf0Response);

        // Phase3 Send back response to the client
        response.setContentType(AMF0Message.CONTENT_TYPE);
        AMF0Serializer serializer =
            new AMF0Serializer(new DataOutputStream(response.getOutputStream()));
        serializer.serializeMessage(amf0Response);
        if (log.isInfoEnabled()) log.info("...End of Processing AMF Request......");
      } catch (Exception e) {
        log.error(e, "Could not handle AMF request");
        throw new ServletException(e);
      }
  }
Ejemplo n.º 3
0
  public void stop(boolean now) throws Exception {
    log.info("Stopping Gravity (now=%s)...", now);
    synchronized (this) {
      if (adapterFactory != null) {
        try {
          adapterFactory.stopAll();
        } catch (Exception e) {
          log.error(e, "Error while stopping adapter factory");
        }
        adapterFactory = null;
      }

      if (serverChannel != null) {
        try {
          removeChannel(serverChannel.getId(), false);
        } catch (Exception e) {
          log.error(e, "Error while removing server channel: %s", serverChannel);
        }
        serverChannel = null;
      }

      if (channelsTimer != null) {
        try {
          channelsTimer.cancel();
        } catch (Exception e) {
          log.error(e, "Error while cancelling channels timer");
        }
        channelsTimer = null;
      }

      if (gravityPool != null) {
        try {
          if (now) gravityPool.shutdownNow();
          else gravityPool.shutdown();
        } catch (Exception e) {
          log.error(e, "Error while stopping thread pool");
        }
        gravityPool = null;
      }

      if (udpReceiverFactory != null) {
        try {
          udpReceiverFactory.stop();
        } catch (Exception e) {
          log.error(e, "Error while stopping udp receiver factory");
        }
        udpReceiverFactory = null;
      }

      started = false;
    }
    log.info("Gravity sucessfully stopped.");
  }
Ejemplo n.º 4
0
  private Message handleSecurityMessage(CommandMessage message) {
    GraniteConfig config = GraniteContext.getCurrentInstance().getGraniteConfig();

    Message response = null;

    if (!config.hasSecurityService())
      log.warn(
          "Ignored security operation (no security settings in granite-config.xml): %s", message);
    else if (!config.getSecurityService().acceptsContext())
      log.info(
          "Ignored security operation (security service does not handle this kind of granite context)",
          message);
    else {
      SecurityService securityService = config.getSecurityService();
      try {
        if (message.isLoginOperation())
          securityService.login(
              message.getBody(), (String) message.getHeader(Message.CREDENTIALS_CHARSET_HEADER));
        else securityService.logout();
      } catch (Exception e) {
        if (e instanceof SecurityServiceException)
          log.debug(e, "Could not process security operation: %s", message);
        else log.error(e, "Could not process security operation: %s", message);
        response = new ErrorMessage(message, e, true);
      }
    }

    if (response == null) {
      response = new AcknowledgeMessage(message, true);
      // For SDK 2.0.1_Hotfix2.
      if (message.isSecurityOperation()) response.setBody("success");
    }

    return response;
  }
  public void contextInitialized(ServletContextEvent event) {
    log.info("Loading JMF shared context");

    ServletContext servletContext = event.getServletContext();

    List<ExtendedObjectCodec> extendedObjectCodecs = loadExtendedObjectCodecs(servletContext);
    List<String> defaultStoredStrings = loadDefaultStoredStrings(servletContext);

    SharedContext sharedContext =
        new DefaultSharedContext(
            new DefaultCodecRegistry(extendedObjectCodecs), null, defaultStoredStrings);
    servletContext.setAttribute(SHARED_CONTEXT_KEY, sharedContext);

    SharedContext dumpSharedContext =
        new DefaultSharedContext(new DefaultCodecRegistry(), null, defaultStoredStrings);
    servletContext.setAttribute(DUMP_SHARED_CONTEXT_KEY, dumpSharedContext);

    log.info("JMF shared context loaded");
  }
  protected List<ExtendedObjectCodec> detectExtendedObjectCodecs() {
    log.info("Auto detecting extended object codec...");

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    for (String factoryName : CODEC_EXTENSION_FACTORY_NAMES) {
      try {
        CodecExtensionFactory factory =
            (CodecExtensionFactory) classLoader.loadClass(factoryName).newInstance();
        List<ExtendedObjectCodec> extendedObjectCodecs = factory.getCodecs();
        log.info("Using %s: %s", factoryName, extendedObjectCodecs);
        return extendedObjectCodecs;
      } catch (Throwable t) {
        log.debug(t, "Could not load factory: %s", factoryName);
      }
    }

    log.info("No extended object codec detected");
    return Collections.emptyList();
  }
Ejemplo n.º 7
0
  private void dumpBytes(String label, byte[] bytes) {
    StringBuilder hexSb = new StringBuilder();
    StringBuilder charSb = new StringBuilder();

    for (int i = 0; i < bytes.length; i++) {
      int b = bytes[i] & 0xFF;
      if (hexSb.length() > 0) {
        hexSb.append(' ');
        charSb.append(' ');
      }
      hexSb.append(HEXS.charAt(b >> 4)).append(HEXS.charAt(b & 0x0F));
      if (b >= 0x20 && b <= 0x7e) charSb.append(' ').append((char) b);
      else charSb.append("##");
    }

    log.info("[RAW %s] {\n%s\n%s\n}", label.toUpperCase(), hexSb.toString(), charSb.toString());

    if (dumpDir != null) {
      File file =
          new File(
              dumpDir.getPath()
                  + File.separator
                  + label
                  + "_"
                  + System.currentTimeMillis()
                  + ".amf");
      for (int i = 1; i < 100 && file.exists(); i++)
        file = new File(file.getAbsolutePath() + "." + i);

      OutputStream os = null;
      try {
        os = new FileOutputStream(file);
        os.write(bytes);
      } catch (Exception e) {
        log.error(e, "Could not write dump file: %s", file);
      } finally {
        if (os != null)
          try {
            os.close();
          } catch (Exception e) {
          }
      }
    }
  }
Ejemplo n.º 8
0
  protected void internalStart() {
    gravityPool = new GravityPool(gravityConfig);
    channelsTimer = new Timer();

    if (graniteConfig.isRegisterMBeans()) {
      try {
        ObjectName name =
            new ObjectName(
                "org.graniteds:type=Gravity,context=" + graniteConfig.getMBeanContextName());
        log.info("Registering MBean: %s", name);
        OpenMBean mBean = OpenMBean.createMBean(this);
        MBeanServerLocator.getInstance().register(mBean, name, true);
      } catch (Exception e) {
        log.error(
            e,
            "Could not register Gravity MBean for context: %s",
            graniteConfig.getMBeanContextName());
      }
    }
  }
  protected List<ExtendedObjectCodec> loadExtendedObjectCodecs(ServletContext servletContext) {
    String extendedObjectCodecsParam =
        servletContext.getInitParameter(EXTENDED_OBJECT_CODECS_PARAM);

    if (extendedObjectCodecsParam == null) return detectExtendedObjectCodecs();

    ClassLoader classLoader = getClass().getClassLoader();

    List<ExtendedObjectCodec> extendedObjectCodecs = new ArrayList<ExtendedObjectCodec>();
    for (String className : extendedObjectCodecsParam.trim().split("\\s*\\,\\s*")) {
      if (className.length() == 0) continue;

      log.info("Loading JMF extended object codec: %s", className);
      try {
        @SuppressWarnings("unchecked")
        Class<? extends ExtendedObjectCodec> cls =
            (Class<? extends ExtendedObjectCodec>) classLoader.loadClass(className);
        extendedObjectCodecs.add(cls.getDeclaredConstructor().newInstance());
      } catch (Throwable t) {
        log.warn(t, "Could not load JMF codec: %s", className);
      }
    }
    return extendedObjectCodecs;
  }