Esempio n. 1
0
  public String executeService(String name, String request) throws Exception {
    String result = null;
    Action action = null;

    System.out.println("executando servico " + name);

    ServiceDefinition pd = getService(name);
    String actionName = pd.getClasses().get(0).getName();
    ;

    ServiceContext ctx = new ServiceContext(repository);

    // carrega a classe e cria uma nova instancia
    Object obj = null;
    try {
      obj = ctx.getClassLoader().loadClass(actionName).newInstance();

    } catch (InstantiationException iEx) {
      throw new Exception(actionName, iEx);

    } catch (IllegalAccessException iaEx) {
      throw new Exception(actionName, iaEx);

    } catch (ClassNotFoundException cnfEx) {
      throw new Exception(actionName, cnfEx);

    } catch (NoClassDefFoundError ncdfErr) {
      throw new Exception(actionName, ncdfErr);

    } catch (Exception ncdfErr) {
      throw new Exception(actionName, ncdfErr);
    }

    action = (Action) obj;

    Object response = action.execute(request);

    if (pd.getResponse() != null && pd.getResponse().equals("json")) {

      // JSONArray jsonArray = JSONArray.fromObject( response );
      // result = jsonArray.toString();

      StringWriter out = new StringWriter();
      JSONValue.writeJSONString(response, out);
      result = out.toString();

    } else {
      result = String.valueOf(response);
    }

    return result;
  }