implementing an extension to execute a given script using Java's Process.exec 
command


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

Branch: refs/heads/master
Commit: cb2da8f1dabfcab70b207728a4295271fbc8279b
Parents: 32ed0f4
Author: Nirmal Fernando <[email protected]>
Authored: Mon Mar 10 15:04:40 2014 +0530
Committer: Nirmal Fernando <[email protected]>
Committed: Mon Mar 10 15:04:40 2014 +0530

----------------------------------------------------------------------
 .../executor/impl/ScriptExtensionExecutor.java  | 74 ++++++++++++++++++++
 1 file changed, 74 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/cb2da8f1/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/executor/impl/ScriptExtensionExecutor.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/executor/impl/ScriptExtensionExecutor.java
 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/executor/impl/ScriptExtensionExecutor.java
new file mode 100644
index 0000000..58ee43a
--- /dev/null
+++ 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/executor/impl/ScriptExtensionExecutor.java
@@ -0,0 +1,74 @@
+/*
+ * 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.stratos.cartridge.agent.executor.impl;
+
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cartridge.agent.executor.ExtensionExecutor;
+import org.apache.stratos.cartridge.agent.util.ExtensionUtils;
+import org.apache.stratos.common.util.CommandUtils;
+
+/**
+ * This extension can be used to run script files in a given order.
+ * 
+ */
+public class ScriptExtensionExecutor extends ExtensionExecutor {
+       
+       private static final Log log = 
LogFactory.getLog(ScriptExtensionExecutor.class);
+
+       public ScriptExtensionExecutor() {
+               super(ScriptExtensionExecutor.class.getName());
+       }
+       
+       public ScriptExtensionExecutor(List<String> fileNames) {
+               super.setFileNamesToBeExecuted(fileNames);
+       }
+
+       @Override
+       public void execute() {
+               try {
+            if(log.isDebugEnabled()) {
+                log.debug("Executing Extension: "+super.getId());
+            }
+            
+            // run script files one by one
+                       for (String script : super.getFileNamesToBeExecuted()) {
+                               try {
+                                       log.info("Executing script file: " + 
script);
+                                       String command = 
ExtensionUtils.prepareCommand(script);
+                                       CommandUtils.executeCommand(command);
+                               } catch (Exception e) {
+                                       log.error("Could not execute script: " 
+ script, e);
+                               }
+                       }
+        }
+        catch (Exception e) {
+            log.error("Could not execute extension: "+super.getId() , e);
+        }
+       }
+
+       @Override
+       public void cleanUp() {
+       }
+       
+       
+}

Reply via email to