use of com.liferay.ide.core.StringBufferOutputStream in project liferay-ide by liferay.
the class AddHookOperation method createPortalProperties.
protected IStatus createPortalProperties(IDataModel dm) {
IProject project = getTargetProject();
String portalPropertiesFile = dm.getStringProperty(PORTAL_PROPERTIES_FILE);
// check to see if we have an existing file to read in
IPath portalPropertiesPath = new Path(portalPropertiesFile);
if (!portalPropertiesFile.startsWith("/")) {
IFolder sourceFolder = CoreUtil.getSourceFolders(JavaCore.create(project)).get(0);
portalPropertiesPath = sourceFolder.getFullPath().append(portalPropertiesFile);
}
IPath propertiesFilesPath = portalPropertiesPath.makeRelativeTo(project.getFullPath());
IFile propertiesFile = project.getFile(propertiesFilesPath);
Properties properties = new Properties();
if (FileUtil.exists(propertiesFile)) {
try {
properties.load(propertiesFile.getContents());
} catch (Exception e) {
return HookCore.createErrorStatus(e);
}
}
List<String[]> actionItems = (List<String[]>) dm.getProperty(PORTAL_PROPERTIES_ACTION_ITEMS);
if (actionItems != null) {
for (String[] actionItem : actionItems) {
properties.put(actionItem[0], actionItem[1]);
}
}
List<String[]> overrideItems = (List<String[]>) dm.getProperty(PORTAL_PROPERTIES_OVERRIDE_ITEMS);
if (overrideItems != null) {
for (String[] overrideItem : overrideItems) {
properties.put(overrideItem[0], overrideItem[1]);
}
}
StringBufferOutputStream buffer = new StringBufferOutputStream();
try {
properties.store(buffer, StringPool.EMPTY);
} catch (IOException ioe) {
return HookCore.createErrorStatus(ioe);
}
try {
ByteArrayInputStream bis = new ByteArrayInputStream(buffer.toString().getBytes("UTF-8"));
if (propertiesFile.exists()) {
propertiesFile.setContents(bis, IResource.FORCE, null);
} else {
CoreUtil.prepareFolder((IFolder) propertiesFile.getParent());
propertiesFile.create(bis, true, null);
}
} catch (Exception e) {
return HookCore.createErrorStatus(e);
}
HookDescriptorHelper hookDescHelper = new HookDescriptorHelper(getTargetProject());
String propertiesClasspath = null;
IPackageFragmentRoot packRoot = (IPackageFragmentRoot) model.getProperty(INewJavaClassDataModelProperties.JAVA_PACKAGE_FRAGMENT_ROOT);
if ((packRoot != null) && packRoot.getPath().isPrefixOf(propertiesFile.getFullPath())) {
IPath fullPath = propertiesFile.getFullPath();
propertiesClasspath = fullPath.makeRelativeTo(packRoot.getPath()).toPortableString();
}
IStatus status = hookDescHelper.setPortalProperties(model, propertiesClasspath);
return status;
}
use of com.liferay.ide.core.StringBufferOutputStream in project liferay-ide by liferay.
the class FileUtil method readContents.
public static String readContents(InputStream contents) throws IOException {
byte[] buffer = new byte[4096];
BufferedInputStream bin = new BufferedInputStream(contents);
StringBufferOutputStream out = new StringBufferOutputStream();
int bytesRead = 0;
/*
* Keep reading from the file while there is any content when the end of the stream has been reached,
* -1 is returned
*/
while ((bytesRead = bin.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
// bytesTotal += bytesRead;
}
if (bin != null) {
bin.close();
}
if (out != null) {
out.flush();
out.close();
}
return out.toString();
}
use of com.liferay.ide.core.StringBufferOutputStream in project liferay-ide by liferay.
the class BladeCLI method execute.
public static String[] execute(String args) throws BladeCLIException {
IPath bladeCLIPath = getBladeCLIPath();
if (FileUtil.notExists(bladeCLIPath)) {
throw new BladeCLIException("Could not get blade cli jar.");
}
Project project = new Project();
Java javaTask = new Java();
javaTask.setProject(project);
javaTask.setFork(true);
javaTask.setFailonerror(true);
javaTask.setJar(bladeCLIPath.toFile());
javaTask.setArgs(args);
DefaultLogger logger = new DefaultLogger();
project.addBuildListener(logger);
StringBufferOutputStream out = new StringBufferOutputStream();
logger.setOutputPrintStream(new PrintStream(out));
logger.setMessageOutputLevel(Project.MSG_INFO);
int returnCode = javaTask.executeJava();
List<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(out.toString());
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine().replaceAll(".*\\[null\\] ", ""));
}
scanner.close();
boolean hasErrors = false;
StringBuilder errors = new StringBuilder();
for (String line : lines) {
if (line.startsWith("Error")) {
hasErrors = true;
} else if (hasErrors) {
errors.append(line);
}
}
if ((returnCode != 0) || hasErrors) {
throw new BladeCLIException(errors.toString());
}
return lines.toArray(new String[0]);
}
Aggregations