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