org.jlot.client.executor.PullCommandExecutor Maven / Gradle / Ivy
package org.jlot.client.executor;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.inject.Inject;
import org.jlot.client.configuration.ProjectConfiguration;
import org.jlot.client.executor.spi.AbstractCommandExecutor;
import org.jlot.client.remote.PullRestCommand;
import org.jlot.client.remote.rest.RestException;
import org.jlot.core.form.PullForm;
import org.jlot.core.utils.SortedProperties;
import org.jlot.core.utils.VersionResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class PullCommandExecutor extends AbstractCommandExecutor
{
private final static Logger log = LoggerFactory.getLogger(PullCommandExecutor.class);
@Inject
private PullRestCommand pullRestCommand;
@Inject
private ProjectConfiguration projectConfiguration;
@Inject
private VersionResolver versionResolver;
@Override
public boolean executeInternal ( Properties properties ) throws RestException
{
PullForm pullForm = new PullForm();
pullForm.setProjectName(projectConfiguration.getProjectName());
pullForm.setVersionName(projectConfiguration.getVersionName());
pullForm.setJlotClientVersion(versionResolver.getJlotVersionName());
File file = pullRestCommand.execute(pullForm);
log.info("processing zip file");
processPullFile(file, projectConfiguration.getEncoding());
return true;
}
private void processPullFile ( File file, String encoding )
{
ZipFile zipFile;
try
{
zipFile = new ZipFile(file);
Enumeration extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements())
{
ZipEntry zipEntry = entries.nextElement();
log.info("processing {}", zipEntry.getName());
processResource(zipEntry.getName(), encoding, zipFile.getInputStream(zipEntry));
}
zipFile.close();
}
catch (Exception e)
{
new IllegalStateException("ZipFile could not be processed properly", e);
}
}
private void processResource ( String name, String encoding, InputStream inputStream ) throws IOException
{
String baseDir = projectConfiguration.getBasedir();
String filename = baseDir + name;
Reader reader = new InputStreamReader(inputStream, encoding);
SortedProperties properties = new SortedProperties();
properties.load(reader);
FileOutputStream fileOutputStream = new FileOutputStream(filename);
Writer writer = new OutputStreamWriter(fileOutputStream, encoding);
properties.store(writer, encoding);
}
}