STRATOS-561 Added CLI command and back-end implementation

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

Branch: refs/heads/master
Commit: bf70f20f9198b1f32b07eea85401e5e1c801df5a
Parents: 5e2fac6
Author: M. Isuru Tharanga Chrishantha Perera <[email protected]>
Authored: Thu Mar 27 17:58:28 2014 +0530
Committer: M. Isuru Tharanga Chrishantha Perera <[email protected]>
Committed: Thu Mar 27 17:58:28 2014 +0530

----------------------------------------------------------------------
 .../stratos/cli/RestCommandLineService.java     | 27 ++++++++++++++
 .../apache/stratos/cli/StratosApplication.java  |  5 ++-
 .../stratos/cli/commands/SyncCommand.java       |  8 ++--
 .../repository/RepositoryNotification.java      | 39 +++++++++++---------
 .../rest/endpoint/services/ServiceUtils.java    | 15 ++++++++
 .../rest/endpoint/services/StratosAdmin.java    | 11 ++++++
 6 files changed, 82 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/bf70f20f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
index 61c5314..b49f1c9 100644
--- 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
@@ -82,6 +82,7 @@ public class RestCommandLineService {
     private final String addTenantEndPoint = "/stratos/admin/tenant";
     private final String unsubscribeTenantEndPoint = 
"/stratos/admin/cartridge/unsubscribe";
     private final String cartridgeDeploymentEndPoint = 
"/stratos/admin/cartridge/definition";
+    private final String syncEndPoint = "/stratos/admin/sync";
     private final String partitionDeploymentEndPoint = 
"/stratos/admin/policy/deployment/partition";
     private final String autoscalingPolicyDeploymentEndPoint = 
"/stratos/admin/policy/autoscale";
     private final String deploymentPolicyDeploymentEndPoint = 
"/stratos/admin/policy/deployment";
@@ -1648,6 +1649,32 @@ public class RestCommandLineService {
             httpClient.getConnectionManager().shutdown();
         }
     }
+    
+       public void sync(String alias) throws CommandException {
+               DefaultHttpClient httpClient = new DefaultHttpClient();
+               try {
+                       HttpResponse response = restClient.doPost(httpClient, 
restClient.getBaseURL() + syncEndPoint, alias);
+
+                       String responseCode = "" + 
response.getStatusLine().getStatusCode();
+
+                       if (responseCode.equals(CliConstants.RESPONSE_OK)) {
+                               System.out.format("Synchronizing repository for 
alias: %s%n", alias);
+                               return;
+                       } else {
+                               GsonBuilder gsonBuilder = new GsonBuilder();
+                               Gson gson = gsonBuilder.create();
+                               String resultString = 
getHttpResponseString(response);
+                               ExceptionMapper exception = 
gson.fromJson(resultString, ExceptionMapper.class);
+                               System.out.println(exception);
+                               return;
+                       }
+
+               } catch (Exception e) {
+                       handleException("Exception when synchronizing 
repository for alias: " + alias, e);
+               } finally {
+                       httpClient.getConnectionManager().shutdown();
+               }
+       }
 
     // This class convert JSON string to deploymentpolicylist object
     private class DeploymentPolicyList {

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/bf70f20f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
index 9d435d7..8a888ee 100644
--- 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
@@ -64,6 +64,7 @@ import org.apache.stratos.cli.commands.PartitionCommand;
 import org.apache.stratos.cli.commands.PartitionDeploymentCommand;
 import org.apache.stratos.cli.commands.SubscribeCommand;
 import org.apache.stratos.cli.commands.SubscribedCartridgeInfoCommand;
+import org.apache.stratos.cli.commands.SyncCommand;
 import org.apache.stratos.cli.commands.UndeployCartridgeDefinitionCommand;
 import org.apache.stratos.cli.commands.UndeployServiceDefinitionCommand;
 import org.apache.stratos.cli.commands.UnsubscribeCommand;
@@ -206,8 +207,8 @@ public class StratosApplication extends 
CommandLineApplication<StratosCommandCon
                //command = new RemoveDomainMappingCommand();
                //commands.put(command.getName(), command);
                
-               //command = new SyncCommand();
-               //commands.put(command.getName(), command);
+               command = new SyncCommand();
+               commands.put(command.getName(), command);
                
                //command = new PoliciesCommand();
                //commands.put(command.getName(), command);

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/bf70f20f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/SyncCommand.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/SyncCommand.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/SyncCommand.java
index 7c32a27..f866813 100644
--- 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/SyncCommand.java
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/SyncCommand.java
@@ -19,13 +19,13 @@
 package org.apache.stratos.cli.commands;
 
 import org.apache.commons.cli.Options;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.stratos.cli.Command;
-import org.apache.stratos.cli.CommandLineService;
+import org.apache.stratos.cli.RestCommandLineService;
 import org.apache.stratos.cli.StratosCommandContext;
 import org.apache.stratos.cli.exception.CommandException;
 import org.apache.stratos.cli.utils.CliConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class SyncCommand implements Command<StratosCommandContext> {
 
@@ -60,7 +60,7 @@ public class SyncCommand implements 
Command<StratosCommandContext> {
                                logger.debug("Synchronizing repository for 
alias {}", alias);
                        }
 
-                       CommandLineService.getInstance().sync(alias);
+                       RestCommandLineService.getInstance().sync(alias);
                        return CliConstants.SUCCESSFUL_CODE;
                } else {
                        context.getStratosApplication().printUsage(getName());

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/bf70f20f/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/repository/RepositoryNotification.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/repository/RepositoryNotification.java
 
b/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/repository/RepositoryNotification.java
index 6b543d2..a814055 100644
--- 
a/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/repository/RepositoryNotification.java
+++ 
b/components/org.apache.stratos.manager/src/main/java/org/apache/stratos/manager/repository/RepositoryNotification.java
@@ -48,24 +48,29 @@ public class RepositoryNotification {
             }
 
             for (CartridgeSubscription cartridgeSubscription : 
cartridgeSubscriptions) {
-
-                if (cartridgeSubscription.getRepository() != null) {
-                    InstanceNotificationPublisher publisher = new 
InstanceNotificationPublisher();
-                    
publisher.sendArtifactUpdateEvent(cartridgeSubscription.getRepository(), 
String.valueOf(cartridgeSubscription.getCluster().getId()),
-                            
String.valueOf(cartridgeSubscription.getSubscriber().getTenantId()));
-
-                    if (log.isDebugEnabled()) {
-                        log.debug("Git pull request from " + 
cartridgeSubscription.getRepository() + "repository, for the tenant " +
-                                
String.valueOf(cartridgeSubscription.getSubscriber().getTenantId()));
-                    }
-
-                } else {
-                    if(log.isDebugEnabled()) {
-                        log.debug("No repository found for subscription with 
alias: " + cartridgeSubscription.getAlias() + ", type: " + 
cartridgeSubscription.getType()+
-                                ". Not sending the Artifact Updated event");
-                    }
-                }
+               updateRepository(cartridgeSubscription);
             }
         }
     }
+    
+       public void updateRepository(CartridgeSubscription 
cartridgeSubscription) {
+               if (cartridgeSubscription.getRepository() != null) {
+                       InstanceNotificationPublisher publisher = new 
InstanceNotificationPublisher();
+                       
publisher.sendArtifactUpdateEvent(cartridgeSubscription.getRepository(),
+                                       
String.valueOf(cartridgeSubscription.getCluster().getId()),
+                                       
String.valueOf(cartridgeSubscription.getSubscriber().getTenantId()));
+
+                       if (log.isDebugEnabled()) {
+                               log.debug("Git pull request from " + 
cartridgeSubscription.getRepository()
+                                               + "repository, for the tenant "
+                                               + 
String.valueOf(cartridgeSubscription.getSubscriber().getTenantId()));
+                       }
+
+               } else {
+                       if (log.isDebugEnabled()) {
+                               log.debug("No repository found for subscription 
with alias: " + cartridgeSubscription.getAlias()
+                                               + ", type: " + 
cartridgeSubscription.getType() + ". Not sending the Artifact Updated event");
+                       }
+               }
+       }
 }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/bf70f20f/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/ServiceUtils.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/ServiceUtils.java
 
b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/ServiceUtils.java
index ce3bcf1..54eb15b 100644
--- 
a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/ServiceUtils.java
+++ 
b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/ServiceUtils.java
@@ -954,6 +954,10 @@ public class ServiceUtils {
         }
         return true;
     }
+    
+    public static CartridgeSubscription getCartridgeSubscription(String alias, 
ConfigurationContext configurationContext) {
+       return 
cartridgeSubsciptionManager.getCartridgeSubscription(ApplicationManagementUtil.getTenantId(configurationContext),
 alias);
+    }
 
     static SubscriptionInfo subscribeToCartridge (CartridgeInfoBean 
cartridgeInfoBean, ConfigurationContext configurationContext, String 
tenantUsername,
                                                   String tenantDomain) throws 
RestAPIException {
@@ -1164,5 +1168,16 @@ public class ServiceUtils {
             throw new RestAPIException(msg, e);
         }
     }
+    
+    static void sendRepositoryNotification(CartridgeSubscription 
cartridgeSubscription) throws RestAPIException {
+        try {
+            RepositoryNotification repoNotification = new 
RepositoryNotification();
+            repoNotification.updateRepository(cartridgeSubscription);
+        } catch (Exception e) {
+            String msg = "Failed to get git repository notifications. Cause : 
" + e.getMessage();
+            log.error(msg, e);
+            throw new RestAPIException(msg, e);
+        }
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/bf70f20f/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/StratosAdmin.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/StratosAdmin.java
 
b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/StratosAdmin.java
index 8198848..5f411dc 100644
--- 
a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/StratosAdmin.java
+++ 
b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/services/StratosAdmin.java
@@ -26,6 +26,7 @@ import org.apache.stratos.common.util.ClaimsMgtUtil;
 import org.apache.stratos.common.util.CommonUtil;
 import org.apache.stratos.manager.dto.Cartridge;
 import org.apache.stratos.manager.dto.SubscriptionInfo;
+import org.apache.stratos.manager.subscription.CartridgeSubscription;
 import org.apache.stratos.rest.endpoint.ServiceHolder;
 import org.apache.stratos.rest.endpoint.Utils;
 import org.apache.stratos.rest.endpoint.annotation.AuthorizationAction;
@@ -61,6 +62,7 @@ import javax.ws.rs.*;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
@@ -961,6 +963,15 @@ public class StratosAdmin extends AbstractAdmin {
 
         ServiceUtils.getGitRepositoryNotification(payload);
     }
+    
+       @POST
+       @Path("/sync")
+       @Consumes("application/json")
+       @AuthorizationAction("/permission/protected/manage/monitor/tenants")
+       public void synchronizeRepository(String alias) throws RestAPIException 
{
+               CartridgeSubscription cartridgeSubscription = 
ServiceUtils.getCartridgeSubscription(alias, getConfigContext());
+               ServiceUtils.sendRepositoryNotification(cartridgeSubscription);
+       }
 
     private List<TenantInfoBean> getAllTenants() throws RestAPIException {
         TenantManager tenantManager = ServiceHolder.getTenantManager();

Reply via email to