Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 296d451

Browse files
committed
Merge branch 'master' of https://github.com/strifel/chatoverflow
2 parents e9a4520 + 7b6b702 commit 296d451

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

src/main/scala/org/codeoverflow/chatoverflow/configuration/ConfigurationService.scala

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class ConfigurationService(val configFilePath: String) extends WithLogger {
2424
<connectorInstances></connectorInstances>
2525
</config>
2626

27+
// Note: Right now, its not supported to reload the framework / load the config more than once. So, caching is easy!
28+
private var cachedXML: Option[Node] = None
29+
2730
/**
2831
* Reads the config xml file and creates plugin instances, saved in the instance registry.
2932
*
@@ -73,6 +76,22 @@ class ConfigurationService(val configFilePath: String) extends WithLogger {
7376
}
7477
}
7578

79+
/**
80+
* Loads the config xml file and return its content. Supports caching of the xml file.
81+
*/
82+
private def loadXML(): Node = {
83+
if (cachedXML.isEmpty) {
84+
if (!new File(configFilePath).exists()) {
85+
logger debug s"Config file '$configFilePath' not found. Initialising with default values."
86+
saveXML(defaultContent)
87+
}
88+
89+
cachedXML = Some(xml.Utility.trim(xml.XML.loadFile(configFilePath)))
90+
logger info "Loaded config file."
91+
}
92+
cachedXML.get
93+
}
94+
7695
/**
7796
* Load all connector instances from the config xml and save them to the connector registry.
7897
*
@@ -103,20 +122,6 @@ class ConfigurationService(val configFilePath: String) extends WithLogger {
103122
}
104123
}
105124

106-
/**
107-
* Loads the config xml file and return its content.
108-
*/
109-
private def loadXML(): Node = {
110-
if (!new File(configFilePath).exists()) {
111-
logger debug s"Config file '$configFilePath' not found. Initialising with default values."
112-
saveXML(defaultContent)
113-
}
114-
115-
val xmlContent = xml.Utility.trim(xml.XML.loadFile(configFilePath))
116-
logger info "Loaded config file."
117-
xmlContent
118-
}
119-
120125
/**
121126
* Saves all connector and plugin instances.
122127
*
@@ -154,7 +159,7 @@ class ConfigurationService(val configFilePath: String) extends WithLogger {
154159
private def saveXML(xmlContent: Node): Unit = {
155160
// Create config folder, if not existent
156161
val dir = new File(configFilePath).getParentFile
157-
if(!dir.exists()) {
162+
if (!dir.exists()) {
158163
dir.mkdir()
159164
}
160165

@@ -195,7 +200,10 @@ class ConfigurationService(val configFilePath: String) extends WithLogger {
195200
val requirementMap = requirements.getRequirementMap
196201
val keys = requirementMap.keySet().toArray
197202

198-
for (key <- keys) yield {
203+
for {
204+
key <- keys
205+
if requirementMap.get(key).isSet
206+
} yield {
199207
<requirement>
200208
<uniqueRequirementId>
201209
{key}
@@ -204,7 +212,7 @@ class ConfigurationService(val configFilePath: String) extends WithLogger {
204212
{requirementMap.get(key).getTargetType.getName}
205213
</targetType>
206214
<content>
207-
{if (requirementMap.get(key).isSet) requirementMap.get(key).get().serialize()}
215+
{requirementMap.get(key).get().serialize()}
208216
</content>
209217
</requirement>
210218
}

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/plugin/PluginInstanceController.scala

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class PluginInstanceController(implicit val swagger: Swagger) extends JsonServle
150150
if (!ConfigurationService.fulfillRequirementByDeserializing(instanceName, requirementID, targetType,
151151
value, chatOverflow.pluginInstanceRegistry, chatOverflow.typeRegistry)) {
152152

153-
ResultMessage(success = false, "Unable to set requirement.")
153+
ResultMessage(success = false, "Unable to set the requirement.")
154154
} else {
155155
chatOverflow.save()
156156
ResultMessage(success = true)
@@ -161,6 +161,40 @@ class PluginInstanceController(implicit val swagger: Swagger) extends JsonServle
161161
}
162162
}
163163

164+
delete("/:instanceName/requirements/:requirementID", operation(deleteRequirement)) {
165+
authKeyRequired {
166+
val instanceName = params("instanceName")
167+
val requirementID = params("requirementID")
168+
169+
if (!chatOverflow.isLoaded) {
170+
ResultMessage(success = false, "Framework not loaded.")
171+
172+
} else {
173+
val pluginInstance = chatOverflow.pluginInstanceRegistry.getPluginInstance(instanceName)
174+
175+
if (pluginInstance.isEmpty) {
176+
ResultMessage(success = false, "Plugin instance not found.")
177+
178+
} else if (pluginInstance.get.isRunning) {
179+
ResultMessage(success = false, "Plugin is running.")
180+
181+
} else if (!pluginInstance.get.getRequirements.getRequirementById(requirementID).isPresent) {
182+
ResultMessage(success = false, "Requirement not found.")
183+
} else {
184+
185+
if (!pluginInstance.get.getRequirements.unsetRequirementById(requirementID)) {
186+
187+
ResultMessage(success = false, "Unable to remove the requirement. Already removed.")
188+
} else {
189+
chatOverflow.save()
190+
ResultMessage(success = true)
191+
}
192+
}
193+
}
194+
195+
}
196+
}
197+
164198
get("/:instanceName/log", operation(getLog)) {
165199
authKeyRequired {
166200
val instanceName = params("instanceName")

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/plugin/PluginInstanceControllerDefinition.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ trait PluginInstanceControllerDefinition extends SwaggerSupport with TagSupport
4444
parameter pathParam[String]("instanceName").description("The name of the plugin instance.")
4545
parameter pathParam[String]("requirementID").description("The unique id of the requirement.")
4646
parameter bodyParam[RequirementInfo]("body").description("Requires target type and serialized content."))
47+
val deleteRequirement: OperationBuilder =
48+
(apiOperation[ResultMessage]("deleteRequirement")
49+
summary "Removes a specific requirement."
50+
description "Removes a specific requirement by unseting its value."
51+
tags controllerTag
52+
parameter authHeader
53+
parameter pathParam[String]("instanceName").description("The name of the plugin instance.")
54+
parameter pathParam[String]("requirementID").description("The unique id of the requirement."))
4755
val getLog: OperationBuilder =
4856
(apiOperation[List[String]]("getLog")
4957
summary "Shows the log of a plugin instance."

0 commit comments

Comments
 (0)