Package org.cafesip.jiplet.utils

Source Code of org.cafesip.jiplet.utils.FileUtils

/*
* Created on Jan 2, 2005
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cafesip.jiplet.utils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.jar.JarFile;

import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Delete;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.types.FileSet;

/**
* @author amit
*
*/
public class FileUtils
{
    private Project project;

    private StringBufferOutputStream out;

    private StringBufferOutputStream err;

    public static final String SRR_EXTENSION = ".srr";

    public static final String SPR_EXTENSION = ".spr";

    /**
     * 
     */
    public FileUtils()
    {
        super();

        project = new Project();
        DefaultLogger listener = new DefaultLogger();
        listener.setEmacsMode(true);
        listener.setMessageOutputLevel(Project.MSG_INFO);

        out = new StringBufferOutputStream();
        listener.setOutputPrintStream(new PrintStream(out));

        err = new StringBufferOutputStream();
        listener.setErrorPrintStream(new PrintStream(err));

        project.addBuildListener(listener);

        project.init();
    }

    public boolean extract(String src, String dest)
    {
        try
        {
            err.clear();
            out.clear();

            Expand expand = new Expand();
            expand.setProject(project);
            expand.setSrc(new File(src));
            expand.setDest(new File(dest));
            expand.execute();

            if (err.toString().length() > 0)
            {
                return false;
            }

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public boolean deleteDir(String dir)
    {
        try
        {
            err.clear();
            out.clear();

            Delete del = new Delete();
            del.setProject(project);
            del.setDir(new File(dir));
            del.execute();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public boolean copy(String file, String toDir)
    {
        try
        {
            err.clear();
            out.clear();

            Copy copy = new Copy();
            copy.setProject(project);

            File f = new File(file);
            if (f.isFile() == true)
            {
                copy.setTodir(new File(toDir));
                copy.setFile(f);
            }
            else
            {
                File dir = new File(toDir, f.getName());
                dir.mkdir();

                copy.setTodir(dir);
                FileSet fs = new FileSet();
                fs.setDir(f);
                copy.addFileset(fs);
            }
            copy.execute();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public boolean copyFile(String file, String toFile)
    {
        try
        {
            err.clear();
            out.clear();

            Copy copy = new Copy();
            copy.setProject(project);

            File f = new File(file);
            copy.setTofile(new File(toFile));
            copy.setFile(f);
            copy.execute();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public boolean extractSpr(String sprFile, String toDir, String dir)
    {
        File spr = new File(sprFile);
        String name = parseContextNameFromSpr(spr);
        if (dir != null)
        {
            name = dir;
        }

        if (toDir == null)
        {
            toDir = spr.getParent();
        }

        File new_dir = new File(toDir, name);
        new_dir.mkdir();

        return extract(sprFile, new_dir.getAbsolutePath());
    }

    public static String parseContextNameFromSpr(File spr)
    {
        String fname = spr.getName();
        if (fname.endsWith(".spr") == true)
        {
            return fname.substring(0, fname.length() - ".spr".length());
        }

        return fname;
    }

    public static String parseRealmDirNameFromSrr(File srr)
    {
        String fname = srr.getName();
        if (fname.endsWith(".srr") == true)
        {
            return fname.substring(0, fname.length() - ".srr".length());
        }

        return fname;
    }

    public static boolean validDeployableSrr(String path)
    {
        File file = new File(path);
        if (!file.exists())
        {
            return false;
        }

        if (file.isDirectory())
        {
            return false;
        }

        if (!path.endsWith(FileUtils.SRR_EXTENSION))
        {
            return false;
        }

        try
        {
            JarFile jar = new JarFile(file);

            if (jar.getJarEntry("META-INF/realm.xml") == null)
            {
                return false;
            }

            return true;
        }
        catch (IOException e)
        {
            // probably because this is not a proper jar file
            return false;
        }
    }

    public static boolean validDeployableRealmDir(String path)
    {
        File file = new File(path);
        if (!file.exists())
        {
            return false;
        }

        if (!file.isDirectory())
        {
            return false;
        }

        File jipxml = new File(file, "META-INF/realm.xml");
        if (!jipxml.exists())
        {
            return false;
        }

        return true;
    }

    public static boolean validDeployableSpr(String path)
    {
        File file = new File(path);
        if (!file.exists())
        {
            return false;
        }

        if (file.isDirectory())
        {
            return false;
        }

        if (!path.endsWith(FileUtils.SPR_EXTENSION))
        {
            return false;
        }

        try
        {
            JarFile jar = new JarFile(file);

            if (jar.getJarEntry("JIP-INF/jip.xml") == null)
            {
                return false;
            }

            return true;
        }
        catch (IOException e)
        {
            // probably because this is not a proper jar file
            return false;
        }
    }

    public static boolean validDeployableJipletDir(String path)
    {
        File file = new File(path);
        if (!file.exists())
        {
            return false;
        }

        if (!file.isDirectory())
        {
            return false;
        }

        File jipxml = new File(file, "JIP-INF/jip.xml");
        if (!jipxml.exists())
        {
            return false;
        }

        return true;
    }

    public void cleanupDir(String dir)
    {
        File tmp = new File(dir);
        if (tmp.exists() == false)
        {
            return;
        }

        File[] files = tmp.listFiles(new FilenameFilter()
        {
            public boolean accept(File dir, String name)
            {
                if ((name.equals(".") == true) || (name.equals("..") == true))
                {
                    return false;
                }
                return true;
            }
        });

        FileUtils ant = new FileUtils();
        for (int i = 0; i < files.length; i++)
        {
            if (files[i].isDirectory() == true)
            {
                ant.deleteDir(files[i].getAbsolutePath());
            }
            else
            {
                files[i].delete();
            }
        }
    }

    /**
     * @return Returns the err.
     */
    public StringBufferOutputStream getErr()
    {
        return err;
    }

    /**
     * @param err
     *            The err to set.
     */
    public void setErr(StringBufferOutputStream err)
    {
        this.err = err;
    }

    /**
     * @return Returns the out.
     */
    public StringBufferOutputStream getOut()
    {
        return out;
    }

    /**
     * @param out
     *            The out to set.
     */
    public void setOut(StringBufferOutputStream out)
    {
        this.out = out;
    }

    /**
     * @return Returns the project.
     */
    public Project getProject()
    {
        return project;
    }

    /**
     * @param project
     *            The project to set.
     */
    public void setProject(Project project)
    {
        this.project = project;
    }
}
TOP

Related Classes of org.cafesip.jiplet.utils.FileUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.