Subversion Repositories XServices

Rev

Rev 46 | Blame | Last modification | View Log | Download | RSS feed

/*
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
 *
 *   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 net.brutex.xservices.ws.impl;

import java.io.File;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import net.brutex.xservices.types.ArchiveResource;
import net.brutex.xservices.types.FileResource;
import net.brutex.xservices.types.FileSetResource;
import net.brutex.xservices.types.ResourceInterface;
import net.brutex.xservices.types.ReturnCode;
import net.brutex.xservices.util.BrutexNamespaces;
import net.brutex.xservices.util.RunTask;
import net.brutex.xservices.ws.FileService;
import net.brutex.xservices.ws.XServicesFault;

import org.apache.tools.ant.taskdefs.Basename;
import org.apache.tools.ant.taskdefs.Chmod;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.taskdefs.Echo;
import org.apache.tools.ant.taskdefs.LoadResource;
import org.apache.tools.ant.taskdefs.optional.unix.Chgrp;
import org.apache.tools.ant.taskdefs.optional.unix.Chown;
import org.apache.tools.ant.types.FileSet;

/**
 *
 * @author Brian Rosenberger, bru@brutex.de
 */
@WebService(
                targetNamespace = BrutexNamespaces.WS_XSERVICES, 
                endpointInterface ="net.brutex.xservices.ws.FileService", 
                serviceName = "FileService"
                        )
public class FileServiceImpl implements FileService {

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#basename(java.lang.String, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "basename")
    public ReturnCode basename(@WebParam(name = "file") String filename,
            @WebParam(name = "suffix") String suffix) {
        return basename(new File(filename), suffix);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#copy(net.brutex.xservices.types.FileSetResource, java.lang.String, boolean, boolean, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "copy")
    public ReturnCode copy(@WebParam(name = "fileset") FileSetResource src,
            @WebParam(name = "todir") String todir,
            @WebParam(name = "preservelastmodified") boolean plm,
            @WebParam(name = "overwrite") boolean overwrite,
            @WebParam(name = "encoding") String encoding)
            throws XServicesFault {
        return copy(src, new File(todir), plm, overwrite, encoding);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#loadRes(net.brutex.xservices.types.FileResource, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "loadResource")
    public ReturnCode loadRes(@WebParam(name = "resource") FileResource res,
            @WebParam(name = "encoding") String encoding) {
        if (encoding == null || encoding.equals("")) {
            encoding = System.getProperty("file.encoding");
        }
        return loadResource(res, encoding);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#loadResFromArchive(net.brutex.xservices.types.ArchiveResource, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "loadResourceFromArchive")
    public ReturnCode loadResFromArchive(@WebParam(name = "archiveresource") ArchiveResource res,
            @WebParam(name = "encoding") String encoding) {
        if (encoding == null || encoding.equals("")) {
            encoding = System.getProperty("file.encoding");
        }
        return loadResource(res, encoding);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#echo2file(java.lang.String, java.lang.String, java.lang.String, boolean)
         */
    @Override
        @WebMethod(operationName = "echoToFile")
    public ReturnCode echo2file(@WebParam(name = "message") String message,
            @WebParam(name = "file") String file, @WebParam(name = "encoding") String encoding,
            @WebParam(name = "append") boolean append) {
        return echo(message, new File(file), encoding, append);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#changeOwner(net.brutex.xservices.types.FileSetResource, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "changeOwner")
    public ReturnCode changeOwner(@WebParam(name = "fileset") FileSetResource res,
            @WebParam(name = "owner") String owner) {
        return chown(res, owner);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#changeGroup(net.brutex.xservices.types.FileSetResource, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "changeGroup")
    public ReturnCode changeGroup(@WebParam(name = "fileset") FileSetResource res,
            @WebParam(name = "group") String group) {
        return chgrp(res, group);
    }

    /* (non-Javadoc)
         * @see net.brutex.xservices.ws.impl.FileService#changeMode(net.brutex.xservices.types.FileSetResource, java.lang.String)
         */
    @Override
        @WebMethod(operationName = "changeMode")
    public ReturnCode changeMode(@WebParam(name="fileset") FileSetResource res,
            @WebParam(name="permissions") String perm) {
        return chmod(res, perm);
    }

    @WebMethod(exclude = true)
    private ReturnCode basename(File file,
            String suffix) {
        Basename basename = new Basename();
        RunTask runner = new RunTask(basename);
        basename.setFile(file);
        if (suffix != null && !suffix.equals("")) {
            basename.setSuffix(suffix);
        }
        basename.setProperty("basename.value");
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode loadResource(ResourceInterface src, String encoding) {
        LoadResource lr = new LoadResource();
        lr.setTaskName("LoadResource");
        RunTask runner = new RunTask(lr);
        lr.addConfigured(src.getAntResource(lr.getProject()));
        lr.setEncoding(encoding);
        System.out.println("Using encoding: " + encoding);
        lr.setProperty("LoadResource.out");
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode echo(String msg, File file, String encoding, boolean append) {
        Echo echo = new Echo();
        echo.setTaskName("toFile");
        RunTask runTask = new RunTask(echo);
        echo.addText(msg);
        echo.setEncoding(encoding);
        echo.setFile(file);
        echo.setAppend(append);
        return runTask.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode copy(FileSetResource src, File dst, boolean preservelastmodified,
            boolean overwrite, String encoding) {
        Copy copy = new Copy();
        copy.setTaskName("Copy");
        RunTask runner = new RunTask(copy);
        FileSet set = src.getAntFileSet(copy.getProject());
        copy.add(set);

        if (dst.isDirectory()) {
            copy.setTodir(dst);
        }
        if (dst.isFile()) {
            copy.setTofile(dst);
        }
        copy.setOverwrite(overwrite);
        copy.setPreserveLastModified(preservelastmodified);
        if (encoding != null && !encoding.equals("")) {
            copy.setOutputEncoding(encoding);
        } else {
            copy.setOutputEncoding(System.getProperty("file.encoding"));
        }

        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode chown(FileSetResource src, String owner) {
        Chown chown = new Chown();
        chown.setTaskName("Chown");
        RunTask runner = new RunTask(chown);
        chown.setOwner(owner);
        FileSet set = src.getAntFileSet(chown.getProject());
        chown.add(set);
        chown.setMaxParallel(300);
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode chgrp(FileSetResource src, String group) {
        Chgrp chgrp = new Chgrp();
        chgrp.setTaskName("Chgrp");
        RunTask runner = new RunTask(chgrp);
        chgrp.setGroup(group);
        FileSet set = src.getAntFileSet(chgrp.getProject());
        chgrp.add(set);
        chgrp.setMaxParallel(300);
        return runner.postTask();
    }

    @WebMethod(exclude = true)
    private ReturnCode chmod(FileSetResource src, String perm) {
        Chmod chmod = new Chmod();
        chmod.setTaskName("Chmod");
        RunTask runner = new RunTask(chmod);
        FileSet set = src.getAntFileSet(chmod.getProject());
        chmod.add(set);
        chmod.setMaxParallel(300);
        chmod.setPerm(perm);
        chmod.setVerbose(true);
        return runner.postTask();
    }

}