You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by sc...@apache.org on 2017/03/07 22:05:36 UTC

[36/50] [abbrv] airavata git commit: Add ProfileService Server and Client

Add ProfileService Server and Client


Project: http://git-wip-us.apache.org/repos/asf/airavata/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/cbe1c280
Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/cbe1c280
Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/cbe1c280

Branch: refs/heads/develop
Commit: cbe1c280ca50c75af7bfe0146a15472aa073eba4
Parents: fcc06e0
Author: Gourav Shenoy <sh...@gmail.com>
Authored: Sun Mar 5 18:16:01 2017 -0500
Committer: Gourav Shenoy <sh...@gmail.com>
Committed: Sun Mar 5 18:16:01 2017 -0500

----------------------------------------------------------------------
 .../profile-service-server/pom.xml              |  30 ++++
 .../handlers/UserProfileServiceHandler.java     | 141 ++++++++++++++++
 .../profile/server/ProfileServiceServer.java    | 168 +++++++++++++++++++
 .../client/ProfileServiceClientFactory.java     |  46 +++++
 .../user/cpi/profile_user_cpiConstants.java     |   2 +
 .../main/resources/airavata-server.properties   |   2 +-
 .../profile-user/profile-user-cpi.thrift        |   1 +
 7 files changed, 389 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/cbe1c280/airavata-services/profile-service/profile-service-server/pom.xml
----------------------------------------------------------------------
diff --git a/airavata-services/profile-service/profile-service-server/pom.xml b/airavata-services/profile-service/profile-service-server/pom.xml
new file mode 100644
index 0000000..1bf16b8
--- /dev/null
+++ b/airavata-services/profile-service/profile-service-server/pom.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>org.apache.airavata</groupId>
+        <artifactId>profile-service</artifactId>
+        <version>0.17-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>profile-service-server</artifactId>
+    <name>Airavata Profile Service Server</name>
+    <description>Module to expose a profile service as a thrift service</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.airavata</groupId>
+            <artifactId>profile-service-stubs</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.airavata</groupId>
+            <artifactId>profile-user-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+    
+</project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/cbe1c280/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java
----------------------------------------------------------------------
diff --git a/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java b/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java
new file mode 100644
index 0000000..5ddfc19
--- /dev/null
+++ b/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java
@@ -0,0 +1,141 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+package org.apache.airavata.service.profile.handlers;
+
+import org.apache.airavata.model.user.UserProfile;
+import org.apache.airavata.service.profile.user.core.entities.UserProfileEntity;
+import org.apache.airavata.service.profile.user.core.repositories.UserProfileRepository;
+import org.apache.airavata.service.profile.user.cpi.UserProfileService;
+import org.apache.airavata.service.profile.user.cpi.exception.UserProfileServiceException;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+public class UserProfileServiceHandler implements UserProfileService.Iface {
+
+    private final static Logger logger = LoggerFactory.getLogger(UserProfileServiceHandler.class);
+
+    private UserProfileRepository userProfileRepository;
+
+    public UserProfileServiceHandler() {
+
+        userProfileRepository = new UserProfileRepository(UserProfile.class, UserProfileEntity.class);
+    }
+
+    public String addUserProfile(UserProfile userProfile) throws UserProfileServiceException {
+        try{
+            userProfileRepository.create(userProfile);
+            if (null != userProfile)
+                return userProfile.getUserId();
+            return null;
+        } catch (Exception e){
+            logger.error("Error while creating user profile", e);
+            UserProfileServiceException exception = new UserProfileServiceException();
+            exception.setMessage("Error while creating user profile. More info : " + e.getMessage());
+            throw exception;
+        }
+    }
+
+    public boolean updateUserProfile(UserProfile userProfile) throws UserProfileServiceException, TException {
+        try {
+            if(userProfileRepository.update(userProfile) != null)
+                return true;
+            return false;
+        } catch (Exception e) {
+            logger.error("Error while Updating user profile", e);
+            UserProfileServiceException exception = new UserProfileServiceException();
+            exception.setMessage("Error while Updating user profile. More info : " + e.getMessage());
+            throw exception;
+        }
+    }
+
+    public UserProfile getUserProfileById(String userId, String gatewayId) throws UserProfileServiceException {
+        try{
+            UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId);
+            if(userProfile != null)
+                return userProfile;
+            return null;
+        } catch (Exception e) {
+            logger.error("Error retrieving user profile by ID", e);
+            UserProfileServiceException exception = new UserProfileServiceException();
+            exception.setMessage("Error retrieving user profile by ID. More info : " + e.getMessage());
+            throw exception;
+        }
+    }
+
+    // FIXME: shouldn't deleteUserProfile require the gatewayId as well?
+    public boolean deleteUserProfile(String userId) throws UserProfileServiceException {
+        try{
+            boolean deleteResult = userProfileRepository.delete(userId);
+            return deleteResult;
+        } catch (Exception e) {
+            logger.error("Error while deleting user profile", e);
+            UserProfileServiceException exception = new UserProfileServiceException();
+            exception.setMessage("Error while deleting user profile. More info : " + e.getMessage());
+            throw exception;
+        }
+    }
+
+    public List<UserProfile> getAllUserProfilesInGateway(String gatewayId, int offset, int limit) throws UserProfileServiceException {
+        try{
+            List<UserProfile> usersInGateway = userProfileRepository.getAllUserProfilesInGateway(gatewayId, offset, limit);
+            if(usersInGateway != null)
+                return usersInGateway;
+            return null;
+        } catch (Exception e) {
+            logger.error("Error while retrieving user profile List", e);
+            UserProfileServiceException exception = new UserProfileServiceException();
+            exception.setMessage("Error while retrieving user profile List. More info : " + e.getMessage());
+            throw exception;
+        }
+    }
+
+
+    public UserProfile getUserProfileByName(String userName, String gatewayId) throws UserProfileServiceException {
+        try{
+            UserProfile userProfile = userProfileRepository.getUserProfileByNameAndGateWay(userName, gatewayId);
+            if(userProfile != null)
+                return userProfile;
+            return null;
+        } catch (Exception e) {
+            logger.error("Error while retrieving user profile", e);
+            UserProfileServiceException exception = new UserProfileServiceException();
+            exception.setMessage("Error while retrieving user profile. More info : " + e.getMessage());
+            throw exception;
+        }
+    }
+
+    public boolean doesUserExist(String userName, String gatewayId) throws UserProfileServiceException, TException {
+        try{
+            UserProfile userProfile = userProfileRepository.getUserProfileByNameAndGateWay(userName, gatewayId);
+            if (null != userProfile)
+                return true;
+            return false;
+        } catch (Exception e) {
+            logger.error("Error while finding user profile", e);
+            UserProfileServiceException exception = new UserProfileServiceException();
+            exception.setMessage("Error while finding user profile. More info : " + e.getMessage());
+            throw exception;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/airavata/blob/cbe1c280/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/server/ProfileServiceServer.java
----------------------------------------------------------------------
diff --git a/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/server/ProfileServiceServer.java b/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/server/ProfileServiceServer.java
new file mode 100644
index 0000000..459fd36
--- /dev/null
+++ b/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/server/ProfileServiceServer.java
@@ -0,0 +1,168 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+package org.apache.airavata.service.profile.server;
+
+import org.apache.airavata.common.utils.IServer;
+import org.apache.airavata.common.utils.ServerSettings;
+import org.apache.airavata.service.profile.handlers.UserProfileServiceHandler;
+import org.apache.airavata.service.profile.user.cpi.UserProfileService;
+import org.apache.airavata.service.profile.user.cpi.profile_user_cpiConstants;
+import org.apache.thrift.TMultiplexedProcessor;
+import org.apache.thrift.server.TServer;
+import org.apache.thrift.server.TThreadPoolServer;
+import org.apache.thrift.transport.TServerSocket;
+import org.apache.thrift.transport.TServerTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.InetSocketAddress;
+import java.util.Date;
+
+public class ProfileServiceServer implements IServer {
+
+    private final static Logger logger = LoggerFactory.getLogger(ProfileServiceServer.class);
+
+    private static final String SERVER_NAME = "Profile Service Server";
+    private static final String SERVER_VERSION = "1.0";
+
+    private ServerStatus status;
+    private TServer server;
+
+    public ProfileServiceServer() {
+        setStatus(ServerStatus.STOPPED);
+    }
+
+    public void updateTime() {
+
+    }
+
+    public Date getTime() {
+        return null;
+    }
+
+    public String getName() {
+        return SERVER_NAME;
+    }
+
+    public String getVersion() {
+        return SERVER_VERSION;
+    }
+
+    public void start() throws Exception {
+
+        try {
+            setStatus(ServerStatus.STARTING);
+            final int serverPort = Integer.parseInt(ServerSettings.getProfileServiceServerPort());
+            final String serverHost = ServerSettings.getProfileServiceServerHost();
+
+            // create multiple processors for each profile-service
+            UserProfileService.Processor userProfileProcessor = new UserProfileService.Processor(new UserProfileServiceHandler());
+
+            // create a multiplexed processor
+            TMultiplexedProcessor profileServiceProcessor = new TMultiplexedProcessor();
+            profileServiceProcessor.registerProcessor(profile_user_cpiConstants.USER_PROFILE_CPI_NAME, userProfileProcessor);
+
+            TServerTransport serverTransport;
+
+            if (serverHost == null) {
+                serverTransport = new TServerSocket(serverPort);
+            } else {
+                InetSocketAddress inetSocketAddress = new InetSocketAddress(serverHost, serverPort);
+                serverTransport = new TServerSocket(inetSocketAddress);
+            }
+            TThreadPoolServer.Args options = new TThreadPoolServer.Args(serverTransport);
+            options.minWorkerThreads = 30;
+            server = new TThreadPoolServer(options.processor(profileServiceProcessor));
+
+            new Thread() {
+                public void run() {
+                    server.serve();
+                    setStatus(ServerStatus.STOPPED);
+                    logger.info("Profile Service Server Stopped.");
+                }
+            }.start();
+            new Thread() {
+                public void run() {
+                    while (!server.isServing()) {
+                        try {
+                            Thread.sleep(500);
+                        } catch (InterruptedException e) {
+                            break;
+                        }
+                    }
+                    if (server.isServing()) {
+                        setStatus(ServerStatus.STARTED);
+                        logger.info("Starting Profile Service Server on Port " + serverPort);
+                        logger.info("Listening to Profile Service Server clients ....");
+                    }
+                }
+            }.start();
+        } catch (TTransportException e) {
+            setStatus(ServerStatus.FAILED);
+            throw new Exception("Error while starting the Profile Service Server", e);
+        }
+    }
+
+    public void stop() throws Exception {
+
+        if (server != null && server.isServing()) {
+            setStatus(ServerStatus.STOPING);
+            server.stop();
+        }
+    }
+
+    public void restart() throws Exception {
+
+        stop();
+        start();
+    }
+
+    public void configure() throws Exception {
+
+    }
+
+    public ServerStatus getStatus() throws Exception {
+        return status;
+    }
+
+    private void setStatus(ServerStatus stat) {
+        status = stat;
+        status.updateTime();
+    }
+
+    public TServer getServer() {
+        return server;
+    }
+
+    public void setServer(TServer server) {
+        this.server = server;
+    }
+
+    public static void main(String[] args) {
+        try {
+            new ProfileServiceServer().start();
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/airavata/blob/cbe1c280/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java
----------------------------------------------------------------------
diff --git a/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java b/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java
new file mode 100644
index 0000000..2c14813
--- /dev/null
+++ b/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java
@@ -0,0 +1,46 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.airavata.service.profile.client;
+
+import org.apache.airavata.service.profile.user.cpi.UserProfileService;
+import org.apache.airavata.service.profile.user.cpi.exception.UserProfileServiceException;
+import org.apache.airavata.service.profile.user.cpi.profile_user_cpiConstants;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TMultiplexedProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TSocket;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+
+
+public class ProfileServiceClientFactory {
+    public static UserProfileService.Client createUserProfileServiceClient(String serverHost, int serverPort)  throws UserProfileServiceException {
+        try {
+            TTransport transport = new TSocket(serverHost, serverPort);
+            transport.open();
+            TProtocol protocol = new TBinaryProtocol(transport);
+            TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, profile_user_cpiConstants.USER_PROFILE_CPI_NAME);
+            return new UserProfileService.Client(multiplexedProtocol);
+        } catch (TTransportException e) {
+            throw new UserProfileServiceException();
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/cbe1c280/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/user/cpi/profile_user_cpiConstants.java
----------------------------------------------------------------------
diff --git a/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/user/cpi/profile_user_cpiConstants.java b/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/user/cpi/profile_user_cpiConstants.java
index 9d583ee..4cc4a89 100644
--- a/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/user/cpi/profile_user_cpiConstants.java
+++ b/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/user/cpi/profile_user_cpiConstants.java
@@ -54,4 +54,6 @@ public class profile_user_cpiConstants {
 
   public static final String USER_PROFILE_CPI_VERSION = "0.17";
 
+  public static final String USER_PROFILE_CPI_NAME = "UserProfileService";
+
 }

http://git-wip-us.apache.org/repos/asf/airavata/blob/cbe1c280/modules/configuration/server/src/main/resources/airavata-server.properties
----------------------------------------------------------------------
diff --git a/modules/configuration/server/src/main/resources/airavata-server.properties b/modules/configuration/server/src/main/resources/airavata-server.properties
index 55f6086..07a373a 100644
--- a/modules/configuration/server/src/main/resources/airavata-server.properties
+++ b/modules/configuration/server/src/main/resources/airavata-server.properties
@@ -349,5 +349,5 @@ user.profile.catalog.validationQuery=SELECT 1
 # Profile Service Configuration
 ###########################################################################
 profile.service.server.host=localhost
-profile.service.server.port=8961
+profile.service.server.port=8962
 profile_service=org.apache.airavata.service.profile.server.ProfileServiceServer
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/cbe1c280/thrift-interface-descriptions/service-cpis/profile-service/profile-user/profile-user-cpi.thrift
----------------------------------------------------------------------
diff --git a/thrift-interface-descriptions/service-cpis/profile-service/profile-user/profile-user-cpi.thrift b/thrift-interface-descriptions/service-cpis/profile-service/profile-user/profile-user-cpi.thrift
index ebf21f7..e575a4b 100644
--- a/thrift-interface-descriptions/service-cpis/profile-service/profile-user/profile-user-cpi.thrift
+++ b/thrift-interface-descriptions/service-cpis/profile-service/profile-user/profile-user-cpi.thrift
@@ -30,6 +30,7 @@ include "profile_user_cpi_errors.thrift"
 namespace java org.apache.airavata.service.profile.user.cpi
 
 const string USER_PROFILE_CPI_VERSION = "0.17"
+const string USER_PROFILE_CPI_NAME = "UserProfileService"
 
 service UserProfileService {