All Downloads are FREE. Search and download functionalities are using the official Maven repository.

groovity.portal.lib.config.grvt Maven / Gradle / Ivy

There is a newer version: 2.1.0-beta.1
Show newest version
/*******************************************************************************
 * © 2018 Disney | ABC Television Group
 *
 * Licensed under the Apache License, Version 2.0 (the "Apache License")
 * with the following modification; you may not use this file except in
 * compliance with the Apache License and the following modification to it:
 * Section 6. Trademarks. is deleted and replaced with:
 *
 * 6. Trademarks. This License does not grant permission to use the trade
 *     names, trademarks, service marks, or product names of the Licensor
 *     and its affiliates, except as required to comply with Section 4(c) of
 *     the License and to reproduce the content of the NOTICE file.
 *
 * You may obtain a copy of the Apache License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Apache License with the above modification is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the Apache License for the specific
 * language governing permissions and limitations under the Apache License.
 *******************************************************************************/
import static java.lang.reflect.Modifier.isStatic;

@Field Map scriptConfiguration;
@Field Map pathConfiguration;
@Field Map> pathProperties;
@Field Map pathTree;

def load(){
	scriptConfiguration = getClass().getClassLoader().scriptClasses.findAll{
		it.declaredFields.find{ it.name=='conf' && isStatic(it.modifiers) }
	}.collectEntries{
		[it,it.conf]
	};
	pathConfiguration = scriptConfiguration.collectEntries{ key, value ->
		[ key.classLoader.sourcePath - '.grvt', value]
	}
	//log.info("path config is ${pathConfiguration}");
	pathProperties = [:];
	def pathAdder = { path, props -> 
		def pl = pathProperties[path];
		if(pl==null){
			pl = new LinkedHashSet();
			pathProperties[path] = pl;
		}
		pl.addAll(props);
	}
	scriptConfiguration.each { entry ->
		String sourcePath = entry.key.classLoader.sourcePath - '.grvt';
		//log.info("loading property paths for ${sourcePath}");
		def props = entry.key.classLoader.configurationKeys;
		pathAdder(sourcePath,props)
		int ls = sourcePath.lastIndexOf("/");
		while(ls >= 0){
			pathAdder(sourcePath.substring(0,ls+1),props)
			//log.info("added parent path ${sourcePath.substring(0,ls+1)}");
			ls = sourcePath.lastIndexOf("/",ls-1);
		}
	}
	//log.info("All property paths are ${pathProperties}");
	pathRecursion = { dir, pathMap ->
		int depth = dir.count("/");
		int nextDepth = depth+1;
		def allPaths = pathProperties.keySet();
		def childFiles = allPaths.findAll{ !it.endsWith("/") && it.count("/")==depth && it.startsWith(dir) }
		def childDirs = allPaths.findAll{ it.endsWith("/") && it.count("/")==nextDepth && it.startsWith(dir) }
		childDirs.each{ childDir->
			def childMap = [:];
			pathRecursion(childDir,childMap);
			pathMap[childDir - dir] = childMap;
		}
		childFiles.each{
			pathMap[it - dir] = it;
		}
	}
	pathTree = [:];
	pathRecursion("/",pathTree);
	//log.info("Path tree is ${pathTree}");
}

@Function(info="Return a complete map of script classes to their active configuration")
public Map getScriptConfiguration(){
	scriptConfiguration
}

@Function(info="Return active configuration for the given path")
public Map getConfiguration(String path){
	return pathConfiguration.get(path);
}

@Function(info="return a map of paths to a list of possible configuration property names")
public Map> getPathProperties(){
	pathProperties;
}

@Function(info="return a recursive map of the available configuration namespace")
public Map getPathTree(){
	pathTree;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy