Search in sources :

Example 1 with StringBufferOutputStream

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IOException(java.io.IOException) Properties(java.util.Properties) INewJavaClassDataModelProperties(org.eclipse.jst.j2ee.internal.common.operations.INewJavaClassDataModelProperties) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) StringBufferOutputStream(com.liferay.ide.core.StringBufferOutputStream) HookDescriptorHelper(com.liferay.ide.hook.core.dd.HookDescriptorHelper) ArrayList(java.util.ArrayList) List(java.util.List) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with StringBufferOutputStream

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();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) StringBufferOutputStream(com.liferay.ide.core.StringBufferOutputStream)

Example 3 with StringBufferOutputStream

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]);
}
Also used : Java(org.apache.tools.ant.taskdefs.Java) PrintStream(java.io.PrintStream) Scanner(java.util.Scanner) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) Project(org.apache.tools.ant.Project) StringBufferOutputStream(com.liferay.ide.core.StringBufferOutputStream) DefaultLogger(org.apache.tools.ant.DefaultLogger)

Aggregations

StringBufferOutputStream (com.liferay.ide.core.StringBufferOutputStream)3 ArrayList (java.util.ArrayList)2 IPath (org.eclipse.core.runtime.IPath)2 HookDescriptorHelper (com.liferay.ide.hook.core.dd.HookDescriptorHelper)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 List (java.util.List)1 Properties (java.util.Properties)1 Scanner (java.util.Scanner)1 DefaultLogger (org.apache.tools.ant.DefaultLogger)1 Project (org.apache.tools.ant.Project)1 Java (org.apache.tools.ant.taskdefs.Java)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1