-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dhfprod 1718 process manager (#1790)
* Add a method to return all of the ProcessTypes * Add interface for ProcessManager * Update Process interface * Add ProcessManager class for handling Processes * Add tests for ProcessManager class * Update HubProject to have a single getProcessDir method * Update Process model class and test docs to better conform to the model
- Loading branch information
1 parent
58a83a0
commit b7ceb9a
Showing
14 changed files
with
517 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
marklogic-data-hub/src/main/java/com/marklogic/hub/ProcessManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2012-2019 MarkLogic Corporation | ||
* | ||
* Licensed 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.marklogic.hub; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.marklogic.hub.processes.Process; | ||
|
||
import java.util.ArrayList; | ||
|
||
public interface ProcessManager { | ||
|
||
/** | ||
* String value for the mapping file extension | ||
*/ | ||
String PROCESSES_FILE_EXTENSION = ".processes.json"; | ||
|
||
/** | ||
* Saves a Process to disk | ||
* | ||
* @param process - the Process object to be saved | ||
*/ | ||
void saveProcess(Process process); | ||
|
||
/** | ||
* Deletes a Process from disk | ||
* | ||
* @param process - the Process object to be deleted | ||
*/ | ||
void deleteProcess(Process process); | ||
|
||
/** | ||
* Returns a list of all Processes currently defined | ||
* | ||
* @return - an ArrayList of all Process objects from disk | ||
*/ | ||
ArrayList<Process> getProcesses(); | ||
|
||
/** | ||
* Returns a single Process given a name and a type | ||
* | ||
* @param name - name of the Process | ||
* @param type - type of the Process | ||
* @return the Process object | ||
*/ | ||
Process getProcess(String name, Process.ProcessType type); | ||
|
||
/** | ||
* Returns a list of Processes that have the given type | ||
* | ||
* @param type - type of the Process | ||
* @return an ArrayList of Process objects of a given type | ||
*/ | ||
ArrayList<Process> getProcessesByType(Process.ProcessType type); | ||
|
||
/** | ||
* Returns a list of Process names that have the given type | ||
* | ||
* @param type - type of the Process | ||
* @return an ArrayList of Process names of a given type | ||
*/ | ||
ArrayList<String> getProcessNamesByType(Process.ProcessType type); | ||
|
||
/** | ||
* Creates a Process from a given JsonNode | ||
* | ||
* @param json - a JsonNode | ||
* @return a Process object | ||
*/ | ||
Process createProcessFromJSON(JsonNode json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
marklogic-data-hub/src/main/java/com/marklogic/hub/impl/ProcessManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright 2012-2019 MarkLogic Corporation | ||
* | ||
* Licensed 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.marklogic.hub.impl; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.marklogic.hub.HubConfig; | ||
import com.marklogic.hub.ProcessManager; | ||
import com.marklogic.hub.error.DataHubProjectException; | ||
import com.marklogic.hub.processes.Process; | ||
import com.marklogic.hub.util.FileUtil; | ||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
|
||
@Component | ||
public class ProcessManagerImpl implements ProcessManager { | ||
|
||
@Autowired | ||
private HubConfig hubConfig; | ||
|
||
@Override | ||
public void saveProcess(Process process) { | ||
try { | ||
String processString = process.serialize(); | ||
Path dir = resolvePath(hubConfig.getProcessDir(process.getType()), process.getName()); | ||
if (!dir.toFile().exists()) { | ||
dir.toFile().mkdirs(); | ||
} | ||
String processFileName = process.getName() + PROCESSES_FILE_EXTENSION; | ||
File file = Paths.get(dir.toString(), processFileName).toFile(); | ||
//create the object mapper to pretty print to disk | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.enable(SerializationFeature.INDENT_OUTPUT); | ||
Object json = objectMapper.readValue(processString, Object.class); | ||
FileOutputStream fileOutputStream = new FileOutputStream(file); | ||
fileOutputStream.write(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json).getBytes()); | ||
fileOutputStream.flush(); | ||
fileOutputStream.close(); | ||
} | ||
catch (JsonProcessingException e) { | ||
throw new DataHubProjectException("Could not serialize Process for project."); | ||
} | ||
catch (IOException e) { | ||
throw new DataHubProjectException("Could not write Process to disk for project."); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteProcess(Process process) { | ||
Path dir = resolvePath(hubConfig.getProcessDir(process.getType()), process.getName()); | ||
if (dir.toFile().exists()) { | ||
try { | ||
FileUtils.deleteDirectory(dir.toFile()); | ||
} | ||
catch (IOException e) { | ||
throw new DataHubProjectException("Could not delete Process for project."); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ArrayList<Process> getProcesses() { | ||
ArrayList<Process> processList = new ArrayList<>(); | ||
for (Process.ProcessType processType : Process.ProcessType.getProcessTypes()) { | ||
for (String name : getProcessNamesByType(processType)) { | ||
processList.add(getProcess(name, processType)); | ||
} | ||
} | ||
return processList; | ||
} | ||
|
||
@Override | ||
public Process getProcess(String name, Process.ProcessType type) { | ||
Path processPath = resolvePath(hubConfig.getProcessDir(type), name); | ||
|
||
try { | ||
String targetFileName = name + PROCESSES_FILE_EXTENSION; | ||
FileInputStream fileInputStream = new FileInputStream(processPath.resolve(targetFileName).toFile()); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode node = objectMapper.readTree(fileInputStream); | ||
Process newProcess = createProcessFromJSON(node); | ||
if (newProcess != null && newProcess.getName().length() > 0) { | ||
return newProcess; | ||
} | ||
} | ||
catch (IOException e) { | ||
throw new DataHubProjectException("Could not read mapping on disk."); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public ArrayList<Process> getProcessesByType(Process.ProcessType type) { | ||
ArrayList<Process> processList = new ArrayList<>(); | ||
for (String name : getProcessNamesByType(type)) { | ||
processList.add(getProcess(name, type)); | ||
} | ||
return processList; | ||
} | ||
|
||
@Override | ||
public ArrayList<String> getProcessNamesByType(Process.ProcessType type) { | ||
return (ArrayList<String>) FileUtil.listDirectFolders(hubConfig.getProcessDir(type)); | ||
} | ||
|
||
@Override | ||
public Process createProcessFromJSON(JsonNode json) { | ||
Process process = Process.create("default", Process.ProcessType.INGEST); | ||
process.deserialize(json); | ||
return process; | ||
} | ||
|
||
private Path resolvePath(Path path, String more) { | ||
return path.resolve(more); | ||
} | ||
} |
Oops, something went wrong.