示例#1
0
  /** Create the data-service for select data by given key operation. */
  private void addSelectWithKeyOperation(
      DataService dataServiceObject,
      String schema,
      DatabaseMetaData metaData,
      String dbName,
      String tableName,
      String pKey)
      throws SQLException, DataServiceFault, NullPointerException {
    Map<String, WithParam> paramMap = new HashMap<String, WithParam>();
    List<String> paramList = new ArrayList<String>();

    ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null);
    String colomNames = "";
    int i = 0;
    while (columnNames.next()) {
      String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME);
      // get the colomn names for the query
      if (i == 0) {
        colomNames = " " + name;
      } else {
        colomNames = colomNames + ", " + name;
      }
      i++;
      if (pKey.equals(name)) {
        WithParam withParam =
            new WithParam(pKey, pKey, pKey, DBConstants.DataServiceGenerator.QUERY_PARAM);
        paramMap.put(pKey, withParam);
        paramList.add(pKey);
      }
    }
    Set<String> requiredRoles = new HashSet<String>();
    String queryId =
        DBConstants.DataServiceGenerator.SELECT_WITH_KEY
            + tableName
            + DBConstants.DataServiceGenerator._QUERY;
    String OpName =
        DBConstants.DataServiceGenerator.SELECT_WITH_KEY
            + tableName
            + DBConstants.DataServiceGenerator._OPERATION;
    CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles);
    List<CallQuery> callQueries = new ArrayList<CallQuery>();
    callQueries.add(callQuery);
    CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries);
    // batchRequest=false
    // parentOperation=null
    Operation operation =
        new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false);
    dataServiceObject.addOperation(operation);
    dataServiceObject.addQuery(
        this.getSelectWithKeyQuery(
            paramList,
            pKey,
            queryId,
            tableName,
            dataServiceObject,
            metaData,
            dbName,
            schema,
            colomNames));
  }
示例#2
0
  /** Select all operation. */
  private void addSelectAllOperation(
      DataService dataServiceObject,
      String schema,
      DatabaseMetaData metaData,
      String dbName,
      String tableName,
      String pKey)
      throws SQLException, DataServiceFault, NullPointerException {

    Map<String, WithParam> paramMap = new HashMap<String, WithParam>();
    List<String> paramList = new ArrayList<String>();
    /* get the primary key */
    // ResultSet resultSet = this.metaObject.getColumns(this.dbName, null,
    // this.tableName, null);
    ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null);
    String colomNames = "";
    int i = 0;
    while (columnNames.next()) {
      String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME);
      if (i == 0) {
        colomNames = " " + name;
      } else {
        colomNames = colomNames + ", " + name;
      }
      i++;
    }
    paramMap.clear();
    Set<String> requiredRoles = new HashSet<String>(); // empty set
    String queryId =
        new StringBuilder()
            .append(DBConstants.DataServiceGenerator.SELECT_ALL)
            .append(tableName)
            .append(DBConstants.DataServiceGenerator._QUERY)
            .toString();
    String OpName =
        new StringBuilder()
            .append(DBConstants.DataServiceGenerator.SELECT_ALL)
            .append(tableName)
            .append(DBConstants.DataServiceGenerator._OPERATION)
            .toString();
    CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles);
    List<CallQuery> callQueries = new ArrayList<CallQuery>();
    callQueries.add(callQuery);
    CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries);
    // batchRequest=false
    // parentOperation=null
    Operation operation =
        new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false);
    dataServiceObject.addOperation(operation);
    dataServiceObject.addQuery(
        this.getSelectAllQuery(
            paramList,
            queryId,
            tableName,
            dataServiceObject,
            metaData,
            dbName,
            schema,
            colomNames));
  }
示例#3
0
  private void makeServices(
      String dbName,
      String[] tableNames,
      DatabaseMetaData metaData,
      List<DataService> dataServiceList,
      String schema,
      String datasourceId,
      String serviceNamespace)
      throws SQLException, DataServiceFault {

    for (String tableName : tableNames) {
      String serviceName = tableName + DBConstants.DataServiceGenerator.SERVICE_NAME_SUFFIX;
      DataService dataService =
          new DataService(
              serviceName,
              DBConstants.DataServiceGenerator.MUTLIPLE_SERVICE_DESCRIPTION,
              null,
              null,
              DBConstants.DataServiceGenerator.ACTIVE,
              false,
              false,
              false,
              null);
      if (DBUtils.isEmptyString(serviceNamespace)) {
        dataService.setServiceNamespace(DBConstants.WSO2_DS_NAMESPACE);
      } else {
        dataService.setServiceNamespace(serviceNamespace);
      }
      this.setConfig(dataService, datasourceId);
      String tablePrimaryKey = this.getPrimaryKey(metaData, dbName, schema, tableName);
      this.addOperations(dataService, schema, metaData, dbName, tableName, tablePrimaryKey);
      dataServiceList.add(dataService);
    }
  }
示例#4
0
 @Test
 public void testUpdateProduct_updatesTheProduct() throws Exception {
   Product p = service.getAllProducts().iterator().next();
   p.setProductName("My Test Name");
   service.updateProduct(p);
   Product p2 = service.getAllProducts().iterator().next();
   assertEquals("My Test Name", p2.getProductName());
 }
示例#5
0
  /**
   * createUser Creates a new user in the database.
   *
   * @param newUser The user instance to be inserted.
   * @return Returns the user inserted from the database, or null if error.
   */
  @Override
  public User createUser(User newUser) {
    // format the string
    String query = "INSERT INTO Users(Email, FacebookID, Username, Fullname, RoleID, Status)";
    query += " VALUES ('%1$s', '%2$s', '%3$s', '%4$s', %5$d, %6$d)";

    query =
        String.format(
            query,
            newUser.getEmail(),
            newUser.getFacebookID(),
            newUser.getUsername(),
            newUser.getFullname(),
            newUser.getRoleID(),
            (newUser.getStatus() ? 0 : 1));

    // if everything worked, inserted id will have the identity key
    // or primary key
    int insertedId = DataService.executeCreate(query);
    if (insertedId > 0) {
      return getUserById(insertedId);
    }

    return null;
  }
示例#6
0
  /** Update Operation. */
  private void addUpdateOperation(
      DataService dataServiceObject,
      String schema,
      DatabaseMetaData metaData,
      String dbName,
      String tableName,
      String pKey)
      throws SQLException, DataServiceFault {
    Map<String, WithParam> paramMap = new HashMap<String, WithParam>();
    List<String> paramList = new ArrayList<String>();

    ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null);
    while (columnNames.next()) {
      String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME);
      if (!name.equals(pKey)) {
        WithParam withParam1 =
            new WithParam(name, name, name, DBConstants.DataServiceGenerator.QUERY_PARAM);
        paramMap.put(name, withParam1);
        paramList.add(name); // add to this @param into @param List
      }
    }
    WithParam withParam2 =
        new WithParam(pKey, pKey, pKey, DBConstants.DataServiceGenerator.QUERY_PARAM);
    paramMap.put(pKey, withParam2);
    paramList.add(pKey);
    Set<String> requiredRoles = new HashSet<String>(); // empty set
    String queryId =
        DBConstants.DataServiceGenerator.UPDATE_
            + tableName
            + DBConstants.DataServiceGenerator._QUERY;
    String OpName =
        DBConstants.DataServiceGenerator.UPDATE_
            + tableName
            + DBConstants.DataServiceGenerator._OPERATION;
    CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles);
    List<CallQuery> callQueries = new ArrayList<CallQuery>();
    callQueries.add(callQuery);
    CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries);
    // batchRequest=false
    // parentOperation=null
    Operation operation =
        new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false);
    dataServiceObject.addOperation(operation);
    dataServiceObject.addQuery(
        this.getUpdateQuery(
            paramList, pKey, queryId, tableName, dataServiceObject, metaData, dbName, schema));
  }
示例#7
0
  /** Insert Operation. */
  private void addInsertOperation(
      DataService dataServiceObject,
      String schema,
      DatabaseMetaData metaData,
      String dbName,
      String tableName)
      throws DataServiceFault, SQLException {

    /* do insertion operation */
    Map<String, WithParam> paramMap = new HashMap<String, WithParam>();
    List<String> paramList = new ArrayList<String>();
    ResultSet columnNames = getColumnNames(metaData, schema, dbName, tableName, null);

    while (columnNames.next()) {
      if (this.isAutoIncrementField(columnNames)) {
        continue;
      }
      String name = columnNames.getString(DBConstants.DataServiceGenerator.COLUMN_NAME);
      WithParam withParam =
          new WithParam(name, name, name, DBConstants.DataServiceGenerator.QUERY_PARAM);
      paramMap.put(name, withParam);
      paramList.add(name);
    }
    Set<String> requiredRoles = new HashSet<String>();
    String queryId =
        DBConstants.DataServiceGenerator.INSERT_
            + tableName
            + DBConstants.DataServiceGenerator._QUERY;
    String OpName =
        DBConstants.DataServiceGenerator.INSERT_
            + tableName
            + DBConstants.DataServiceGenerator._OPERATION;
    CallQuery callQuery = new CallQuery(dataServiceObject, queryId, paramMap, requiredRoles);
    List<CallQuery> callQueries = new ArrayList<CallQuery>();
    callQueries.add(callQuery);
    CallQueryGroup callQueryGroup = new CallQueryGroup(callQueries);
    // batchRequest=false
    // parentOperation=null
    Operation op =
        new Operation(dataServiceObject, OpName, null, callQueryGroup, false, null, false, false);
    dataServiceObject.addOperation(op);
    dataServiceObject.addQuery(
        this.getInsertQuery(
            paramList, queryId, tableName, dataServiceObject, metaData, dbName, schema));
  }
示例#8
0
 private void setConfig(DataService dataServiceObject, String carbonSourceId)
     throws DataServiceFault {
   Map<String, String> properties = new HashMap<String, String>();
   properties.put(DBConstants.CarbonDatasource.NAME, carbonSourceId);
   Config config =
       new SQLCarbonDataSourceConfig(
           dataServiceObject, DBConstants.DataServiceGenerator.CONFIG_ID, properties);
   dataServiceObject.addConfig(config);
 }
示例#9
0
 private DataService generateService(
     String datasourceId,
     String dbName,
     String[] schemas,
     String[] tableNames,
     DatabaseMetaData metaData,
     String serviceNamespace,
     String serviceName)
     throws DataServiceFault, SQLException {
   // String serviceName = dbName + DBConstants.DataServiceGenerator.SERVICE_NAME_SUFFIX;
   DataService dataService =
       new DataService(
           serviceName,
           DBConstants.DataServiceGenerator.SINGLE_SERVICE_DESCRIPTION,
           null,
           null,
           DBConstants.DataServiceGenerator.ACTIVE,
           false,
           false,
           false,
           null);
   this.setConfig(dataService, datasourceId);
   if (DBUtils.isEmptyString(serviceNamespace)) {
     dataService.setServiceNamespace(DBConstants.WSO2_DS_NAMESPACE);
   } else {
     dataService.setServiceNamespace(serviceNamespace);
   }
   if (schemas.length != 0) {
     for (String schema : schemas) {
       makeService(dbName, tableNames, metaData, dataService, schema);
     }
   } else {
     String schema = null;
     makeService(dbName, tableNames, metaData, dataService, schema);
   }
   return dataService;
 }
示例#10
0
  public User getUserByFacebookId(String facebookId) {
    String query = "SELECT * FROM Users WHERE FacebookID = " + facebookId;
    ResultSet rs = DataService.getData(query);

    try {
      if (rs.next()) {
        return convertResultSetToUser(rs);
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return null;
  }
示例#11
0
  /**
   * getAll Obtains all of the Users from the database.
   *
   * @return A Generic list of users from the database.
   */
  @Override
  public List<User> getAll() {

    ResultSet rs = DataService.getData("SELECT * FROM Users");

    List<User> users = new ArrayList<User>();

    try {
      while (rs.next()) {
        users.add(convertResultSetToUser(rs));
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return users;
  }
示例#12
0
  /**
   * updateUser Updates a user in the database.
   *
   * @param newUser The user instance to be updated.
   * @return Returns true or false if the user was updated or not.
   */
  @Override
  public boolean updateUser(User newUser) {
    // format the string
    String query =
        "UPDATE Users SET Email = '%1$s', FacebookID = '%2$s', Username = '******', RoleID = %4$d, Status = %5$d, Fullname = '%7$s' WHERE UserID = %6$d";

    query =
        String.format(
            query,
            newUser.getEmail(),
            newUser.getFacebookID(),
            newUser.getUsername(),
            newUser.getRoleID(),
            (newUser.getStatus() ? 0 : 1),
            newUser.getUserID(),
            newUser.getFullname());

    // if everything worked, inserted id will have the identity key
    // or primary key
    return DataService.executeUpdate(query);
  }
示例#13
0
  @Test(groups = "example.ipc.server")
  public void startServer() {
    PeerInfo serverInfo = new PeerInfo(socketAddress);

    RpcServerCallExecutor executor = new ThreadPoolCallExecutor(10, 10);

    DuplexTcpServerBootstrap bootstrap =
        new DuplexTcpServerBootstrap(
            serverInfo,
            new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()),
            executor);

    bootstrap
        .getRpcServiceRegistry()
        .registerService(
            DataService.newReflectiveService(
                new DataService.Interface() {
                  public void getData(
                      final RpcController controller,
                      final GetRequest request,
                      final RpcCallback<GetResponse> done) {
                    System.out.println(request);
                    done.run(
                        GetResponse.newBuilder()
                            .addData(
                                KeyValue.newBuilder()
                                    .setRow(ByteString.copyFromUtf8("row"))
                                    .setQualifier(ByteString.copyFromUtf8("qualifier"))
                                    .setTimestamp(System.currentTimeMillis())
                                    .setValue(ByteString.copyFromUtf8("value")))
                            .build());
                  }
                }));

    System.out.println("Starting server on " + socketAddress);
    bootstrap.bind();
  }
示例#14
0
 @Test
 public void testDataServiceCanFetchCategories() throws Exception {
   assertFalse(service.getAllCategories().isEmpty());
 }
示例#15
0
 @Test
 public void testDataServiceCanFetchProducts() throws Exception {
   assertFalse(service.getAllProducts().isEmpty());
 }
示例#16
0
  /** 服务开始启动, 0. 初始化Redis数据库连接。 1. 加载所有blot,初始化状态数据。 2. 加载spout 开始处理数据 */
  public void start() {
    int coreWrokerSize = Settings.getInt(Settings.CORE_WORKER_SIZE, 10);
    int queueSize = Settings.getInt(Settings.WRITE_LOG_QUEUE_SIZE, 1024);

    taskQueue = new LinkedBlockingDeque<Runnable>(queueSize);

    log.debug(
        "start message process thread pool, core size:"
            + coreWrokerSize
            + ", queue size:"
            + queueSize);
    workPool =
        new ThreadPoolExecutor(coreWrokerSize, coreWrokerSize * 2, 10, TimeUnit.SECONDS, taskQueue);

    String appKey = Settings.getString(Settings.TAODIAN_APPID, null); // System.getProperty("");
    String appSecret = Settings.getString(Settings.TAODIAN_APPSECRET, null);
    String appRoute = Settings.getString(Settings.TAODIAN_APPROUTE, "http://api.zaol.cn/api/route");
    boolean inSAE = Settings.getString("in_sae", "n").equals("y");

    if (appKey != null && appSecret != null) {
      api = new TaodianApi(appKey, appSecret, appRoute, inSAE ? "simple" : "apache");
    } else {
      log.info("The taodian.api_id and taodian.api_secret Java properties are required.");
      System.exit(255);
    }

    ds = new DataService();
    if (!ds.start(workPool, api)) {
      log.error("Data service start failed");
      System.exit(255);
    }

    topology = new DefaultSimpleTopology(workPool);
    TopologyBuilder builder = null; // new SimpleTopologyBuilder();
    String topolgyName = Settings.getString(Settings.TOPOLOGY, "default_topology.cfg");

    File f = new File(topolgyName);
    InputStream ins = null;
    if (f.isFile()) {
      log.info("Load topology from file, " + f.getAbsolutePath());
      try {
        ins = new FileInputStream(f);
      } catch (FileNotFoundException e) {
        log.error(e, e);
      }
    } else {
      ins = this.getClass().getClassLoader().getResourceAsStream(topolgyName);
    }
    if (ins != null) {
      builder = new SimpleTopologyBuilder(ins);
    }

    builder.buildToplogy(topology, new ObjectFactory());

    if (fileSpout != null) {
      topology.setSpout(Settings.INPUT_SPOUT, fileSpout);
    } else {
      String clickGate = Settings.getString(Settings.CLICK_GATE_LOG_URL, "");
      HTTPURLSpout spout = new HTTPURLSpout();
      if (spout.connect(clickGate)) {
        topology.setSpout(Settings.INPUT_SPOUT, spout);
      }
    }

    topology.start();
  }
示例#17
0
 /**
  * deleteUser Deletes a user from the database.
  *
  * @param userId The id of the user to be deleted in the database.
  * @return Returns true if the user was deleted and false if the user was not deleted.
  */
 @Override
 public boolean deleteUser(int userId) {
   String query = "DELETE FROM Users WHERE UserID = " + userId;
   return DataService.executeDelete(query);
 }
示例#18
0
  @Test(dependsOnGroups = "example.ipc.server")
  public void clientTest() throws IOException, ServiceException, InterruptedException {
    PeerInfo client = new PeerInfo(socketAddress.getHostName(), 1234);

    ThreadPoolCallExecutor executor = new ThreadPoolCallExecutor(3, 10);

    DuplexTcpClientBootstrap bootstrap =
        new DuplexTcpClientBootstrap(
            client,
            new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool()),
            executor);

    bootstrap.setOption("connectTimeoutMillis", 10000);
    bootstrap.setOption("connectResponseTimeoutMillis", 10000);
    bootstrap.setOption("receiveBufferSize", 1048576);
    bootstrap.setOption("tcpNoDelay", false);

    RpcClientChannel channel = bootstrap.peerWith(socketAddress);

    // blocking calll
    DataService.BlockingInterface dataService = DataService.newBlockingStub(channel);
    RpcController controller = channel.newRpcController();

    // make request
    GetRequest request = GetRequest.newBuilder().setRow(ByteString.copyFromUtf8("row1")).build();
    final Stopwatch stopwatch = new Stopwatch().start();
    GetResponse response = dataService.getData(controller, request);
    stopwatch.stop();
    System.out.println(response.getDataList());
    System.out.printf("Request took %s milliseconds\n", stopwatch.elapsedMillis());

    // do it again since the socket is open
    stopwatch.reset().start();
    response = dataService.getData(controller, request);
    stopwatch.stop();
    System.out.println(response.getDataList());
    System.out.printf("Request took %s milliseconds\n", stopwatch.elapsedMillis());

    // non-blocking
    DataService.Stub stub = DataService.newStub(channel);
    final Object lock = new Object();
    stopwatch.reset().start();
    stub.getData(
        controller,
        request,
        new RpcCallback<GetResponse>() {
          public void run(final GetResponse parameter) {
            System.out.println("Non-Blocking Callback");
            System.out.println(parameter.getDataList());

            stopwatch.stop();
            System.out.printf("Request took %s milliseconds\n", stopwatch.elapsedMillis());
            synchronized (lock) {
              lock.notify();
            }
          }
        });
    synchronized (lock) {
      lock.wait();
    }
  }