package tool.repository;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import tool.ToolPlugin;
import tool.ToolProjectSupport;
public class CreateRepository implements IRunnableWithProgress {
private static final String CONSOLE_NAME = "RPCreate";
private IProject project;
private String reposName;
protected MessageConsole msgConsole;
protected MessageConsoleStream msgStream;
private Process rpCreateProcess;
public CreateRepository(IProject project, String reposName){
super();
this.project = project;
this.reposName = reposName;
this.msgConsole = ToolPlugin.findConsole(CONSOLE_NAME);
if (this.msgConsole != null)
msgStream = msgConsole.newMessageStream();
}
protected CreateRepository(String reposName) { //Testing only
super();
this.reposName = reposName;
}
@Override
public void run(IProgressMonitor monitor) {
Process Proc;
SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 9);
try {
Assert.isNotNull(this.project);
IPath reposPath = ToolProjectSupport.getLocalReposFolders(project).getLocation().append(this.reposName);
File reposFile = reposPath.toFile();
subMonitor.beginTask("Creating local repository", 9);
subMonitor.subTask(this.reposName);
rpcreate(reposPath).waitFor();
subMonitor.worked(5);
// logger
subMonitor.subTask("Setting default log flags");
project.setPersistentProperty(ToolProjectSupport.loggerQualifiedName,
ToolProjectSupport.DEFAULT_LOG_FLAGS);
subMonitor.worked(1);
// repos
subMonitor.subTask("Adding repository to project");
project.setPersistentProperty(ToolProjectSupport.reposQualifiedName,
"bt:" + reposPath.toFile().getAbsolutePath());
subMonitor.worked(1);
// workspace
subMonitor.subTask("Setting workspace: " + this.reposName);
project.setPersistentProperty(ToolProjectSupport.workspaceQualifiedName,
this.reposName);
// workspacePassword
project.setPersistentProperty(ToolProjectSupport.worspacePasswordQualifiedName,
this.reposName);
subMonitor.worked(2);
} catch (InterruptedException e) {
reportError(e);
} catch (CoreException e) {
reportError(e);
} finally {
subMonitor.done();
}
}
public Process rpcreate(IPath reposPath){
writeToConsole("-- rpcreate at: " + reposPath);
ToolPlugin.log(IStatus.INFO, "-- rpcreate at: " + reposPath);
String osName = System.getProperty("os.name");
if (!osName.startsWith("Windows")){ // Only run it on windows
writeToConsole("-- Can only run rpcreate on Windows --");
ToolPlugin.log(IStatus.INFO, "-- Can only run rpcreate on Windows --");
return null;
}
File forteRoot = new File(ToolProjectSupport.getForteRoot());
ToolPlugin.log(IStatus.INFO, "-- Interface FORTE_ROOT = " + forteRoot);
writeToConsole("-- Interface FORTE_ROOT = " + forteRoot);
ToolPlugin.log(IStatus.INFO, "-- local repository = " + reposPath.toOSString());
writeToConsole("-- local repository = " + reposPath.toOSString());
try {
OutputContainer container = new OutputContainer(this.msgStream);
this.rpcreate(forteRoot, reposPath.toFile(), container);
ToolPlugin.log(IStatus.INFO, "-- rpcreate process started --");
writeToConsole("-- rpcreate process started --");
// Start readers to consume output. It looks very innocuous with the output from the process
// being mapped to p.inputStream, but this is correct.
} catch (IOException e) {
reportError(e);
} catch (InterruptedException e) {
reportError(e);
}
return this.rpCreateProcess;
}
public Process rpcreate(File forteRoot, File reposPath, OutputContainer container) throws IOException, InterruptedException{
ProcessBuilder pb = new ProcessBuilder(forteRoot + "/install/bin/rpcreate",
"-fr",
"bt:" + reposPath.getAbsolutePath(),
"-r");
Map<String, String> env = pb.environment();
env.put("FORTE_ROOT", forteRoot.getAbsolutePath());
env.put("FORTE_LOGGER_SETUP", ToolProjectSupport.DEFAULT_LOG_FLAGS);
this.rpCreateProcess = pb.start();
Thread.sleep(250);
//System.out.println("started rpcreate with " + pb.command());
//System.out.println("started rpcreate with " + pb.environment());
new Thread(new InputStreamHandler(this.rpCreateProcess.getInputStream(), container, false), "OutputFileHandler").start();
new Thread(new InputStreamHandler(this.rpCreateProcess.getErrorStream(), container, true), "ErrorFileHandler").start();
return this.rpCreateProcess;
}
protected void writeToConsole(String message){
if (this.msgStream != null)
this.msgStream.println(message);
}
protected void reportError(Throwable e){
if (ToolPlugin.getDefault() != null)
ToolPlugin.showError("Cannot create repository " + this.reposName, e);
else
e.printStackTrace();
}
}