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.File; 020import java.nio.file.Files; 021import java.util.HashMap; 022import java.util.Map; 023 024import org.apache.juneau.config.Config; 025 026@SuppressWarnings("javadoc") 027public class GetConfiguration implements Command, GetValue<Map<String, ConfigItem>> { 028 029 private static final String APPLICATION = "APPLICATION"; 030 private static final String PROJECT = "PROJECT"; 031 private static final String EXT = ".cfg"; 032 private static final String BAR = "/"; 033 034 private Map<String, ConfigItem> configs = new HashMap<>(); 035 036 private String project; 037 private String branch; 038 039 public GetConfiguration(String project, String branch) { 040 this.branch = branch; 041 this.project = project; 042 } 043 044 @Override 045 public void execute() throws Exception { 046 047 Config config = Config.create().name("juneau-server-config.cfg").build(); 048 049 String pathStr = config.get("GitServer/pathLocal").orElse(null); 050 051 String git = config.get("GitServer/gitRemote").orElse(null); 052 053 GitControl gitControl = new GitControl(pathStr, git); 054 055 File path = new File(pathStr); 056 057 if (path.isDirectory()) { 058 gitControl.pullFromRepo(); 059 } else { 060 gitControl.cloneRepo(); 061 } 062 063 gitControl.branch(branch); 064 gitControl.pullFromRepo(); 065 066 String fileDefaultStr = APPLICATION.toLowerCase().concat(EXT); 067 String fileProjectStr = this.project.concat(EXT); 068 069 File fileDefault = new File(pathStr.concat(BAR).concat(fileDefaultStr)); 070 if (fileDefault.exists()) { 071 String lines = new String(Files.readAllBytes(fileDefault.toPath())); 072 configs.put(APPLICATION, new ConfigItem(lines)); 073 } 074 075 File fileProject = new File(pathStr.concat(BAR).concat(fileProjectStr)); 076 if (fileProject.exists()) { 077 String linesProject = new String(Files.readAllBytes(fileProject.toPath())); 078 configs.put(PROJECT, new ConfigItem(linesProject)); 079 } 080 } 081 082 @Override 083 public Map<String, ConfigItem> get() { 084 return configs; 085 } 086 087}