Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2014 Les Hazlewood
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.leshazlewood.spin.cli
import com.leshazlewood.spin.lang.Classes
import com.leshazlewood.spin.lang.UnknownClassException
import java.nio.file.Files
/**
* Main class to run from the command line.
*
* @since 0.1.0
*/
class Main {
private ClassLoader classLoader = null
private File userDir = null
private File spinInstallDir = null
private File userSpinDir = null
private Map spinConfig = null
private File spinConfigFile = null
private String command = null
private String environmentName = null
private String serviceName = null
private List profiles = null
private Map pluginClasses = [:]
Class getPluginClass(Map service) {
final String type = service.type
Class clazz = pluginClasses.get(type)
if (clazz) return clazz
String candidate = type //assumes type is a fully-qualified class name of the plugin to use
if (!type.contains('.')) { //not a fully qualified class name, use default heuristic:
candidate = 'com.leshazlewood.spin.plugin.' + type.capitalize() + 'Plugin'
}
try {
clazz = Classes.forName(candidate)
} catch (UnknownClassException uce) {
String msg = "Unable to load plugin class $candidate for plugin type '$type' for service '${service.name}': ${uce.message}"
throw new IllegalArgumentException(msg)
}
//cache for later access:
pluginClasses.put(service.type as String, clazz)
return clazz
}
def getPlugin(Map service) {
/*String unqualifiedPluginFileName = service.type.capitalize() + 'Plugin.groovy'
return PLUGIN_SCRIPT_ENGINE.run(unqualifiedPluginFileName, new Binding())*/
Class pluginClass = getPluginClass(service)
return pluginClass.newInstance()
}
boolean isEnabled(Map service) {
if (service.containsKey('enabled') && !service.enabled) {
return false
}
if (service.containsKey('profiles')) {
if (!(service.profiles instanceof List)) {
if (!this.profiles.contains(service.profiles)) {
return false
}
} else {
boolean found = false
for (String profileName : service.profiles) {
if (this.profiles.contains(profileName)) {
found = true
break
}
}
if (!found) {
return false
}
}
}
return true
}
void status(Map services) {
int nameLength = 'NAME'.length()
int statusLength = 'STATUS'.length()
for (Map service : services.values()) {
String serviceName = service.name as String
nameLength = Math.max(nameLength, serviceName.length())
}
println String.format("%-${nameLength}s %-${statusLength}s", ["NAME", "STATUS"] as Object[])
for (Map service : services.values()) {
if (!isEnabled(service)) {
continue
}
def plugin = service.plugin
def request = createRequest(service, 'status')
def result = plugin.status(request) ?: [status: 'stopped']
String serviceName = service.name as String
result.serviceName = serviceName
statusLength = Math.max(statusLength, result.status.length())
//print as we get a result:
println String.format("%-${nameLength}s %-${statusLength}s", [result.serviceName, result.status] as Object[])
}
}
Map createRequest(Map service, String command) {
[
service: service,
config : service, //alias, need to remove
spin : spinConfig,
context: [
file : spinConfigFile,
command : command,
userDir : userDir,
spinHome : spinInstallDir,
userSpinHome: spinUserDir
]
]
}
void executeCommand(Collection