Subversion Repositories XServices

Rev

Rev 12 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 brianR 1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.xservices.ws;
17
 
18
import java.io.File;
19
import javax.jws.WebMethod;
20
import javax.jws.WebParam;
21
import javax.jws.WebService;
22
import net.brutex.xservices.types.ArchiveResource;
23
import net.brutex.xservices.types.FileResource;
24
import net.brutex.xservices.types.FileSetResource;
25
import net.brutex.xservices.types.ResourceInterface;
12 brianR 26
import net.brutex.xservices.types.ReturnCode;
6 brianR 27
import net.brutex.xservices.util.RunTask;
28
import org.apache.tools.ant.taskdefs.Basename;
14 brianR 29
import org.apache.tools.ant.taskdefs.Chmod;
6 brianR 30
import org.apache.tools.ant.taskdefs.Copy;
31
import org.apache.tools.ant.taskdefs.Echo;
32
import org.apache.tools.ant.taskdefs.LoadResource;
12 brianR 33
import org.apache.tools.ant.taskdefs.optional.unix.Chgrp;
34
import org.apache.tools.ant.taskdefs.optional.unix.Chown;
6 brianR 35
import org.apache.tools.ant.types.FileSet;
36
 
37
/**
38
 *
39
 * @author Brian Rosenberger, bru@brutex.de
40
 */
12 brianR 41
@WebService(targetNamespace = "http://ws.xservices.brutex.net", name = "FileService")
6 brianR 42
public class FileService {
43
 
44
    @WebMethod(operationName = "basename")
12 brianR 45
    public ReturnCode basename(@WebParam(name = "file") String filename,
6 brianR 46
            @WebParam(name = "suffix") String suffix) {
47
        return basename(new File(filename), suffix);
48
    }
49
 
50
    @WebMethod(operationName = "copy")
12 brianR 51
    public ReturnCode copy(@WebParam(name = "fileset") FileSetResource src,
52
            @WebParam(name = "todir") String todir,
53
            @WebParam(name = "preservelastmodified") boolean plm,
54
            @WebParam(name = "overwrite") boolean overwrite,
55
            @WebParam(name = "encoding") String encoding)
56
            throws XServicesFault {
57
        return copy(src, new File(todir), plm, overwrite, encoding);
6 brianR 58
    }
59
 
60
    @WebMethod(operationName = "loadResource")
12 brianR 61
    public ReturnCode loadRes(@WebParam(name = "resource") FileResource res,
6 brianR 62
            @WebParam(name = "encoding") String encoding) {
63
        if (encoding == null || encoding.equals("")) {
64
            encoding = System.getProperty("file.encoding");
65
        }
66
        return loadResource(res, encoding);
67
    }
68
 
69
    @WebMethod(operationName = "loadResourceFromArchive")
12 brianR 70
    public ReturnCode loadResFromArchive(@WebParam(name = "archiveresource") ArchiveResource res,
6 brianR 71
            @WebParam(name = "encoding") String encoding) {
72
        if (encoding == null || encoding.equals("")) {
73
            encoding = System.getProperty("file.encoding");
74
        }
75
        return loadResource(res, encoding);
76
    }
77
 
78
    @WebMethod(operationName = "echoToFile")
12 brianR 79
    public ReturnCode echo2file(@WebParam(name = "message") String message,
80
            @WebParam(name = "file") String file, @WebParam(name = "encoding") String encoding,
81
            @WebParam(name = "append") boolean append) {
6 brianR 82
        return echo(message, new File(file), encoding, append);
83
    }
84
 
12 brianR 85
    @WebMethod(operationName = "changeOwner")
86
    public ReturnCode changeOwner(@WebParam(name = "fileset") FileSetResource res,
87
            @WebParam(name = "owner") String owner) {
88
        return chown(res, owner);
89
    }
90
 
91
    @WebMethod(operationName = "changeGroup")
92
    public ReturnCode changeGroup(@WebParam(name = "fileset") FileSetResource res,
93
            @WebParam(name = "group") String group) {
94
        return chgrp(res, group);
95
    }
96
 
14 brianR 97
    @WebMethod(operationName = "changeMode")
98
    public ReturnCode changeMode(@WebParam(name="fileset") FileSetResource res,
99
            @WebParam(name="permissions") String perm) {
100
        return chmod(res, perm);
101
    }
102
 
6 brianR 103
    @WebMethod(exclude = true)
12 brianR 104
    private ReturnCode basename(File file,
6 brianR 105
            String suffix) {
106
        Basename basename = new Basename();
107
        RunTask runner = new RunTask(basename);
108
        basename.setFile(file);
109
        if (suffix != null && !suffix.equals("")) {
110
            basename.setSuffix(suffix);
111
        }
112
        basename.setProperty("basename.value");
12 brianR 113
        return runner.postTask();
6 brianR 114
    }
12 brianR 115
 
116
    @WebMethod(exclude = true)
117
    private ReturnCode loadResource(ResourceInterface src, String encoding) {
6 brianR 118
        LoadResource lr = new LoadResource();
119
        lr.setTaskName("LoadResource");
120
        RunTask runner = new RunTask(lr);
121
        lr.addConfigured(src.getAntResource(lr.getProject()));
122
        lr.setEncoding(encoding);
123
        System.out.println("Using encoding: " + encoding);
124
        lr.setProperty("LoadResource.out");
12 brianR 125
        return runner.postTask();
6 brianR 126
    }
127
 
128
    @WebMethod(exclude = true)
12 brianR 129
    private ReturnCode echo(String msg, File file, String encoding, boolean append) {
6 brianR 130
        Echo echo = new Echo();
131
        echo.setTaskName("toFile");
132
        RunTask runTask = new RunTask(echo);
133
        echo.addText(msg);
134
        echo.setEncoding(encoding);
135
        echo.setFile(file);
136
        echo.setAppend(append);
12 brianR 137
        return runTask.postTask();
6 brianR 138
    }
139
 
12 brianR 140
    @WebMethod(exclude = true)
141
    private ReturnCode copy(FileSetResource src, File dst, boolean preservelastmodified,
6 brianR 142
            boolean overwrite, String encoding) {
143
        Copy copy = new Copy();
144
        copy.setTaskName("Copy");
145
        RunTask runner = new RunTask(copy);
146
        FileSet set = src.getAntFileSet(copy.getProject());
147
        copy.add(set);
148
 
12 brianR 149
        if (dst.isDirectory()) {
150
            copy.setTodir(dst);
151
        }
152
        if (dst.isFile()) {
153
            copy.setTofile(dst);
154
        }
6 brianR 155
        copy.setOverwrite(overwrite);
156
        copy.setPreserveLastModified(preservelastmodified);
12 brianR 157
        if (encoding != null && !encoding.equals("")) {
6 brianR 158
            copy.setOutputEncoding(encoding);
159
        } else {
160
            copy.setOutputEncoding(System.getProperty("file.encoding"));
161
        }
162
 
12 brianR 163
        return runner.postTask();
6 brianR 164
    }
12 brianR 165
 
166
    @WebMethod(exclude = true)
167
    private ReturnCode chown(FileSetResource src, String owner) {
168
        Chown chown = new Chown();
169
        chown.setTaskName("Chown");
170
        RunTask runner = new RunTask(chown);
171
        chown.setOwner(owner);
172
        FileSet set = src.getAntFileSet(chown.getProject());
173
        chown.add(set);
14 brianR 174
        chown.setMaxParallel(300);
12 brianR 175
        return runner.postTask();
176
    }
177
 
178
    @WebMethod(exclude = true)
179
    private ReturnCode chgrp(FileSetResource src, String group) {
180
        Chgrp chgrp = new Chgrp();
181
        chgrp.setTaskName("Chgrp");
182
        RunTask runner = new RunTask(chgrp);
183
        chgrp.setGroup(group);
184
        FileSet set = src.getAntFileSet(chgrp.getProject());
185
        chgrp.add(set);
14 brianR 186
        chgrp.setMaxParallel(300);
12 brianR 187
        return runner.postTask();
188
    }
14 brianR 189
 
190
    @WebMethod(exclude = true)
191
    private ReturnCode chmod(FileSetResource src, String perm) {
192
        Chmod chmod = new Chmod();
193
        chmod.setTaskName("Chmod");
194
        RunTask runner = new RunTask(chmod);
195
        FileSet set = src.getAntFileSet(chmod.getProject());
196
        chmod.add(set);
197
        chmod.setMaxParallel(300);
198
        chmod.setPerm(perm);
199
        chmod.setVerbose(true);
200
        return runner.postTask();
201
    }
202
 
6 brianR 203
}