001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.server.config.repository;
014
015import java.io.File;
016import java.nio.file.Files;
017import java.util.HashMap;
018import java.util.Map;
019
020import org.apache.juneau.config.Config;
021
022@SuppressWarnings("javadoc")
023public class GetConfiguration implements Command, GetValue<Map<String, ConfigItem>> {
024
025   private static final String APPLICATION = "APPLICATION";
026   private static final String PROJECT = "PROJECT";
027   private static final String EXT = ".cfg";
028   private static final String BAR = "/";
029
030   private Map<String, ConfigItem> configs = new HashMap<>();
031
032   private String project;
033   private String branch;
034
035   public GetConfiguration(String project, String branch) {
036      this.branch = branch;
037      this.project = project;
038   }
039
040   @Override
041   public void execute() throws Exception {
042
043      Config config = Config.create().name("juneau-server-config.cfg").build();
044
045      String pathStr = config.getString("GitServer/pathLocal");
046
047      String git = config.get("GitServer/gitRemote");
048
049      GitControl gitControl = new GitControl(pathStr, git);
050
051      File path = new File(pathStr);
052
053      if (path.isDirectory()) {
054         gitControl.pullFromRepo();
055      } else {
056         gitControl.cloneRepo();
057      }
058
059      gitControl.branch(branch);
060      gitControl.pullFromRepo();
061
062      String fileDefaultStr = APPLICATION.toLowerCase().concat(EXT);
063      String fileProjectStr = this.project.concat(EXT);
064
065      File fileDefault = new File(pathStr.concat(BAR).concat(fileDefaultStr));
066      if (fileDefault.exists()) {
067         String lines = new String(Files.readAllBytes(fileDefault.toPath()));
068         configs.put(APPLICATION, new ConfigItem(lines));
069      }
070
071      File fileProject = new File(pathStr.concat(BAR).concat(fileProjectStr));
072      if (fileProject.exists()) {
073         String linesProject = new String(Files.readAllBytes(fileProject.toPath()));
074         configs.put(PROJECT, new ConfigItem(linesProject));
075      }
076   }
077
078   @Override
079   public Map<String, ConfigItem> get() {
080      return configs;
081   }
082
083}