Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

injecting PL environment variable into workspace settings file #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public abstract class Strings {
* templates for the update of an workspace.
*/
public static final String FOLDER_UPDATE = "update";


/**
* The {@link java.io.File#getName() name} of the {@link java.io.File#isDirectory() folder} with the configuration
* templates for the update of an workspace.
*/
public static final String FOLDER_PL = "productionLine";
/**
* The {@link java.io.File#getName() name} of the {@link java.io.File#isDirectory() folder} with the plugin
* configuration in the eclipse workspace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
*
*/
public class Configurator {


/**
* Handler for .pref files.
*/
private final TextHandler textHandler;
/**
* Handler for .pref files.
*/
Expand All @@ -50,6 +55,11 @@ public class Configurator {
* Relative path to eclipse workspace update templates.
*/
private final String pluginsUpdateDirectoryPath;

/**
* Relative path to eclipse workspace update templates.
*/
private final String pluginsPLDirectoryPath;

/**
* Creates a new {@link Configurator} with the given paths.
Expand All @@ -64,10 +74,23 @@ public Configurator(String workspacePath, String replacementPatternsPath, String
Resolver resolver = createResolver(replacementPatternsPath, Strings.REPLACEMENT_REG_EX);
this.prefHandler = new PrefHandler(resolver);
this.xmlHandler = new XmlHandler(resolver);
this.textHandler = new TextHandler(resolver);
this.pluginsSetupDirectoryPath = eclipseTemplatesPath + Strings.FILE_SEPARATOR + Strings.FOLDER_SETUP;
this.pluginsUpdateDirectoryPath = eclipseTemplatesPath + Strings.FILE_SEPARATOR + Strings.FOLDER_UPDATE;
this.pluginsPLDirectoryPath = eclipseTemplatesPath + Strings.FILE_SEPARATOR + Strings.FOLDER_PL;
}

/**
* inject Production Line contents (url and user name) in settings file
*/
public void injectPLContent() {

for (EclipseWorkspaceFile file : collectProductionLineWorkspaceFiles()) {
System.out.println(file.plFile.getPath());
mergePLFiles(file);
}
}

/**
* Creates/updates the workspace. See {@link #mergeFiles(EclipseWorkspaceFile)} for further details.
*/
Expand All @@ -90,6 +113,32 @@ public void saveChangesInWorkspace(boolean saveNewProperties) {
}
}

/**
* Update the workspaceFile from the Production Line settings file.
* with the updateFile.
*
* @param file is the {@link EclipseWorkspaceFile}.
*/
protected void mergePLFiles(EclipseWorkspaceFile file) {

if (file.relativePath.endsWith(".prefs")) {
this.prefHandler.updatePL(file.workspaceFile, file.plFile);
} else {
File source;
if (file.plFile.exists()) {
source = file.plFile;
} else {
return;
}
if ((file.relativePath.endsWith(".xml")) || (file.relativePath.endsWith(".xmi"))
|| (file.relativePath.endsWith(".launch"))) {
this.xmlHandler.update(source, file.workspaceFile);
} else if (file.relativePath.endsWith(".txt")) {
this.textHandler.update(source,file.workspaceFile);
}
}
}

/**
* Creates or updates the workspaceFile with setupFile or updateFile. If the workspaceFile does not exist, the
* workspaceFile will be the setupFile merged with the updateFile. If the workspaceFile does exist, it will be merged
Expand Down Expand Up @@ -274,14 +323,18 @@ public static void main(String[] args) {
}
workspacePath = workspacePath + Strings.FILE_SEPARATOR + workspacePathSuffix;
Log.LOGGER.info("No .metadata folder found. Relocated to " + workspacePathSuffix);

}

Configurator configurator = new Configurator(workspacePath, replacementPatternsPath, eclipseTemplatesPath);

if (args[0].equals("-u")) {
Log.LOGGER.info("Updating workspace");
configurator.updateWorkspace();
Log.LOGGER.info("Completed");
} else if (args[0].equals("-pl")) {
Log.LOGGER.info("Updating Production Line workspace settings");
configurator.injectPLContent();
Log.LOGGER.info("Completed");
} else if (args[0].equals("-c")) {
Log.LOGGER.info("Merging workspace changes back into templates (excluding new properties)");
configurator.saveChangesInWorkspace(false);
Expand All @@ -307,6 +360,18 @@ private static void logCall(String[] args) {
Log.LOGGER.info(buffer.toString());
}

/**
* @return a {@link Collection} with all available {@link EclipseWorkspaceFile}.
*/
private Collection<EclipseWorkspaceFile> collectProductionLineWorkspaceFiles() {

Map<String, EclipseWorkspaceFile> fileMap = new HashMap<String, EclipseWorkspaceFile>();
collectWorkspaceFiles(new File(this.pluginsPLDirectoryPath), fileMap);
Collection<EclipseWorkspaceFile> values = fileMap.values();
Log.LOGGER.info("Collected " + values.size() + " configuration files.");
return values;
}

/**
* @return a {@link Collection} with all available {@link EclipseWorkspaceFile}.
*/
Expand Down Expand Up @@ -395,6 +460,9 @@ public class EclipseWorkspaceFile {

/** The file template for update. */
private final File updateFile;

/** The file template for update. */
private final File plFile;

/**
* Creates a new instance.
Expand All @@ -412,7 +480,8 @@ public EclipseWorkspaceFile(String relativePath) {
this.workspaceFile = new File(Configurator.this.workspacePath + Strings.FILE_SEPARATOR + relativePath);
this.setupFile = new File(Configurator.this.pluginsSetupDirectoryPath + Strings.FILE_SEPARATOR + relativePath);
this.updateFile = new File(Configurator.this.pluginsUpdateDirectoryPath + Strings.FILE_SEPARATOR + relativePath);
assert (this.setupFile.exists() || this.updateFile.exists());
this.plFile = new File(Configurator.this.pluginsPLDirectoryPath + Strings.FILE_SEPARATOR + relativePath);
assert (this.setupFile.exists() || this.updateFile.exists() || this.plFile.exists());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ public PrefHandler(Resolver resolver) {
this.resolver = resolver;
}

/**
* Creates or updates the workspacePrefFile with setupPrefFile or updatePrefFile. If the workspacePrefFile does not
* exist, the workspacePrefFile will be the setupPrefFile merged with the updatePrefFile. If the workspacePrefFile
* does exist, it will be merged with the updatePrefFile.
*
* @param workspacePrefFile - the prefFile to be updated.
* @param setupPrefFile - prefFile needed for creation.
* @param updatePrefFile - prefFile need for creation and update.
*/
public void updatePL(File workspacePrefFile, File plFile) {

SortedProperties properties = new SortedProperties();
mergeProperties(properties, plFile);
resolveVariables(properties);
writeProperties(properties, workspacePrefFile);
}

/**
* Creates or updates the workspacePrefFile with setupPrefFile or updatePrefFile. If the workspacePrefFile does not
* exist, the workspacePrefFile will be the setupPrefFile merged with the updatePrefFile. If the workspacePrefFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class Resolver {
* RegEx to look for to find variables.
*/
private String replacementRegEx;



/**
* Creates a new {@link Resolver} with the given <code>replacementPatterns</code> and <code>regEx</code>.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package io.oasp.ide.eclipse.configurator.core;

import io.oasp.ide.eclipse.configurator.logging.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.logging.Level;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;


/**
* Class to resolve variables within text files.
* @author abennani
*
*/
public class TextHandler {

/**
* {@link Resolver} to resolve variables within text files.
*/
private Resolver resolver;

/**
* Creates a new {@link XmlHandler} that uses the given {@link Resolver}
* to resolve variables within text files..
* @param resolver - resolver for variables.
*/
public TextHandler(Resolver resolver) {

this.resolver = resolver;
}

/**
* Writes the resolved content of the textfile to the destination file.
* @param textFile - textFile to be solved and written to the destination.
* @param destination - destination file for the resolved content of the textFile.
*/
public void update(File textFile, File destination) {

FileInputStream fstream = null;
DataInputStream in = null;
BufferedReader br = null;
try {

fstream = new FileInputStream(textFile);
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));

resolveVariables(br,destination);

} catch (IOException e) {
Log.LOGGER
.log(Level.ALL, "An io error occurred during read of file: " + textFile.getAbsolutePath(), e.getStackTrace());
} finally {
try{
if( br != null ){
br.close(); // Will close bw and fw too
}
else if( in != null ){
in.close(); // Will close fw too
}
else if( fstream != null ){
fstream.close();
}
}
catch( IOException e ){
// Closing the file writers failed for some obscure reason
}

}
}

/**
* Calls {@link #resolveElement(Element)} for every element in the Buffer and write
* it to the destination file.
* @param textFile - textFile to be solved and written to the destination.
* @param destination - destination file for the resolved content of the textFile.
*/
private void resolveVariables(BufferedReader buffer, File destination) {

String strLine;
PrintWriter out = null;
BufferedWriter bw = null;
FileWriter fw = null;

try{
while ((strLine = buffer.readLine()) != null) {
String resolvedLine = resolver.resolveVariables(strLine);
try {
fw = new FileWriter(destination);
bw = new BufferedWriter(fw);
out = new PrintWriter(bw);
out.println(resolvedLine);
}catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}catch (IOException e) {
System.err.println(e);
}finally{
try{
if(out != null){
out.close();
}
if(bw != null){
bw.close();
}
if(fw != null){
fw.close();
}
}catch( IOException e ){
// Closing the file writers failed for some obscure reason
}
}
}




}