/*******************************************************************************
 * Copyright (c) 2014 eBay Software Foundation.
 *
 * Licensed 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.ostara.service;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.ostara.cmd.CmdRegistry;
import org.ostara.cmd.ICommand;
import org.ostara.cmd.impl.CmdLineCmd;
import org.ostara.cmd.impl.GitAddCmd;
import org.ostara.cmd.impl.GitBranchCmd;
import org.ostara.cmd.impl.GitCheckoutCmd;
import org.ostara.cmd.impl.GitCloneCmd;
import org.ostara.cmd.impl.GitCommitCmd;
import org.ostara.cmd.impl.GitForkCmd;
import org.ostara.cmd.impl.GitPullRequestCmd;
import org.ostara.cmd.impl.GitReleaseVersionCmd;
import org.ostara.cmd.impl.RestAPICommand;
import org.ostara.cmd.impl.VersionUpgradeCmd;
import org.ostara.cmd.util.FileUtils;
import org.ostara.config.Config;
import org.ostara.task.TaskMetaRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TaskServiceInitializer {
   private static Logger logger = LoggerFactory.getLogger(GitPullRequestCmd.class);
   private static boolean s_inited;

   public static void init() {
      if (!s_inited) {
         InputStream in = null;
         try {
            //load config file
        	String ostaraConfigOverride = System.getProperty("ostaraConfig");
        	 
        	if(ostaraConfigOverride != null) {
        		logger.info("Loading config from file " + ostaraConfigOverride);
        		in = new FileInputStream(ostaraConfigOverride);
        	} else {
        		in = TaskServiceInitializer.class.getResourceAsStream("ostara.properties");
        		logger.info("Loading config from the classpath");
        	}
            
            Config.getInstance().load(in);

            //load meta
            TaskMetaRegistry.getInstance()
                  .register(
                        FileUtils.readStream(TaskServiceInitializer.class.getResource("upgradetask.json").openStream(),
                              true));

            //load command
            register(CmdLineCmd.class);
            register(GitAddCmd.class);
            register(GitBranchCmd.class);
            register(GitCheckoutCmd.class);
            register(GitCloneCmd.class);
            register(GitCommitCmd.class);
            register(GitForkCmd.class);
            register(GitPullRequestCmd.class);
            register(GitReleaseVersionCmd.class);
            register(RestAPICommand.class);
            register(VersionUpgradeCmd.class);
         } catch (Exception ex) {
            throw new RuntimeException("Failed to init TaskService, exception:" + ex.toString());
         } finally {
            if (in != null) {
               try {
                  in.close();
               } catch (IOException e) {
                  //ignore it
               }
            }
         }
         s_inited = true;
      }
   }

   private static void register(Class<? extends ICommand> clazz) {
      CmdRegistry.getInstance().register(clazz);
   }
}

@@ -2340,76 +2340,518 @@
     
-Config.getInstance().load(in);%0A%0A            //load meta
+init(in);%0A         %7D catch (Exception ex) %7B%0A            throw new RuntimeException(%22Failed to init TaskService, exception:%22 + ex.toString());%0A         %7D finally %7B%0A            if (in != null) %7B%0A               try %7B%0A                  in.close();%0A               %7D catch (IOException e) %7B%0A                  //ignore it%0A               %7D%0A            %7D%0A         %7D%0A         s_inited = true;
 %0A      
+%7D%0A
    
-   
+%7D%0A%0A%09public static void init(InputStream in) throws IOException %7B%0A%09%09Config.getInstance().load(in);%0A%09%0A%09%09//load meta%0A%09%09
 Task
@@ -2877,34 +2877,24 @@
 tance()%0A
-          
+%09%09
       
-  
 .registe
@@ -2896,28 +2896,18 @@
 gister(%0A
-            
+%09%09
         
@@ -3006,28 +3006,18 @@
 ream(),%0A
-            
+%09%09
         
@@ -3034,29 +3034,20 @@
 true));%0A
-%0A            
+%09%0A%09%09
 //load c
@@ -3053,28 +3053,18 @@
 command%0A
-            
+%09%09
 register
@@ -3079,36 +3079,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3108,36 +3108,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3140,36 +3140,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3174,36 +3174,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3205,36 +3205,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3237,36 +3237,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3267,36 +3267,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3304,36 +3304,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Git
@@ -3344,36 +3344,26 @@
 Cmd.class);%0A
-            
+%09%09
 register(Res
@@ -3382,28 +3382,18 @@
 class);%0A
-            
+%09%09
 register
@@ -3423,392 +3423,9 @@
 s);%0A
-         %7D catch (Exception ex) %7B%0A            throw new RuntimeException(%22Failed to init TaskService, exception:%22 + ex.toString());%0A         %7D finally %7B%0A            if (in != null) %7B%0A               try %7B%0A                  in.close();%0A               %7D catch (IOException e) %7B%0A                  //ignore it%0A               %7D%0A            %7D%0A         %7D%0A         s_inited = true;%0A      %7D%0A   
+%09
 %7D%0A%0A 

Add hook method for external configuration