Subversion Repositories XServices

Rev

Details | 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
 */
46 brianR 16
package net.brutex.xservices.ws.impl;
6 brianR 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;
46 brianR 27
import net.brutex.xservices.util.BrutexNamespaces;
6 brianR 28
import net.brutex.xservices.util.RunTask;
46 brianR 29
import net.brutex.xservices.ws.FileService;
30
import net.brutex.xservices.ws.XServicesFault;
31
 
6 brianR 32
import org.apache.tools.ant.taskdefs.Basename;
14 brianR 33
import org.apache.tools.ant.taskdefs.Chmod;
6 brianR 34
import org.apache.tools.ant.taskdefs.Copy;
35
import org.apache.tools.ant.taskdefs.Echo;
36
import org.apache.tools.ant.taskdefs.LoadResource;
12 brianR 37
import org.apache.tools.ant.taskdefs.optional.unix.Chgrp;
38
import org.apache.tools.ant.taskdefs.optional.unix.Chown;
6 brianR 39
import org.apache.tools.ant.types.FileSet;
40
 
41
/**
42
 *
43
 * @author Brian Rosenberger, bru@brutex.de
44
 */
46 brianR 45
@WebService(
46
		targetNamespace = BrutexNamespaces.WS_XSERVICES,
47
		endpointInterface ="net.brutex.xservices.ws.FileService",
48
		name = "FileService"
49
			)
50
public class FileServiceImpl implements FileService {
6 brianR 51
 
46 brianR 52
    /* (non-Javadoc)
53
	 * @see net.brutex.xservices.ws.impl.FileService#basename(java.lang.String, java.lang.String)
54
	 */
55
    @Override
56
	@WebMethod(operationName = "basename")
12 brianR 57
    public ReturnCode basename(@WebParam(name = "file") String filename,
6 brianR 58
            @WebParam(name = "suffix") String suffix) {
59
        return basename(new File(filename), suffix);
60
    }
61
 
46 brianR 62
    /* (non-Javadoc)
63
	 * @see net.brutex.xservices.ws.impl.FileService#copy(net.brutex.xservices.types.FileSetResource, java.lang.String, boolean, boolean, java.lang.String)
64
	 */
65
    @Override
66
	@WebMethod(operationName = "copy")
12 brianR 67
    public ReturnCode copy(@WebParam(name = "fileset") FileSetResource src,
68
            @WebParam(name = "todir") String todir,
69
            @WebParam(name = "preservelastmodified") boolean plm,
70
            @WebParam(name = "overwrite") boolean overwrite,
71
            @WebParam(name = "encoding") String encoding)
72
            throws XServicesFault {
73
        return copy(src, new File(todir), plm, overwrite, encoding);
6 brianR 74
    }
75
 
46 brianR 76
    /* (non-Javadoc)
77
	 * @see net.brutex.xservices.ws.impl.FileService#loadRes(net.brutex.xservices.types.FileResource, java.lang.String)
78
	 */
79
    @Override
80
	@WebMethod(operationName = "loadResource")
12 brianR 81
    public ReturnCode loadRes(@WebParam(name = "resource") FileResource res,
6 brianR 82
            @WebParam(name = "encoding") String encoding) {
83
        if (encoding == null || encoding.equals("")) {
84
            encoding = System.getProperty("file.encoding");
85
        }
86
        return loadResource(res, encoding);
87
    }
88
 
46 brianR 89
    /* (non-Javadoc)
90
	 * @see net.brutex.xservices.ws.impl.FileService#loadResFromArchive(net.brutex.xservices.types.ArchiveResource, java.lang.String)
91
	 */
92
    @Override
93
	@WebMethod(operationName = "loadResourceFromArchive")
12 brianR 94
    public ReturnCode loadResFromArchive(@WebParam(name = "archiveresource") ArchiveResource res,
6 brianR 95
            @WebParam(name = "encoding") String encoding) {
96
        if (encoding == null || encoding.equals("")) {
97
            encoding = System.getProperty("file.encoding");
98
        }
99
        return loadResource(res, encoding);
100
    }
101
 
46 brianR 102
    /* (non-Javadoc)
103
	 * @see net.brutex.xservices.ws.impl.FileService#echo2file(java.lang.String, java.lang.String, java.lang.String, boolean)
104
	 */
105
    @Override
106
	@WebMethod(operationName = "echoToFile")
12 brianR 107
    public ReturnCode echo2file(@WebParam(name = "message") String message,
108
            @WebParam(name = "file") String file, @WebParam(name = "encoding") String encoding,
109
            @WebParam(name = "append") boolean append) {
6 brianR 110
        return echo(message, new File(file), encoding, append);
111
    }
112
 
46 brianR 113
    /* (non-Javadoc)
114
	 * @see net.brutex.xservices.ws.impl.FileService#changeOwner(net.brutex.xservices.types.FileSetResource, java.lang.String)
115
	 */
116
    @Override
117
	@WebMethod(operationName = "changeOwner")
12 brianR 118
    public ReturnCode changeOwner(@WebParam(name = "fileset") FileSetResource res,
119
            @WebParam(name = "owner") String owner) {
120
        return chown(res, owner);
121
    }
122
 
46 brianR 123
    /* (non-Javadoc)
124
	 * @see net.brutex.xservices.ws.impl.FileService#changeGroup(net.brutex.xservices.types.FileSetResource, java.lang.String)
125
	 */
126
    @Override
127
	@WebMethod(operationName = "changeGroup")
12 brianR 128
    public ReturnCode changeGroup(@WebParam(name = "fileset") FileSetResource res,
129
            @WebParam(name = "group") String group) {
130
        return chgrp(res, group);
131
    }
132
 
46 brianR 133
    /* (non-Javadoc)
134
	 * @see net.brutex.xservices.ws.impl.FileService#changeMode(net.brutex.xservices.types.FileSetResource, java.lang.String)
135
	 */
136
    @Override
137
	@WebMethod(operationName = "changeMode")
14 brianR 138
    public ReturnCode changeMode(@WebParam(name="fileset") FileSetResource res,
139
            @WebParam(name="permissions") String perm) {
140
        return chmod(res, perm);
141
    }
142
 
6 brianR 143
    @WebMethod(exclude = true)
12 brianR 144
    private ReturnCode basename(File file,
6 brianR 145
            String suffix) {
146
        Basename basename = new Basename();
147
        RunTask runner = new RunTask(basename);
148
        basename.setFile(file);
149
        if (suffix != null && !suffix.equals("")) {
150
            basename.setSuffix(suffix);
151
        }
152
        basename.setProperty("basename.value");
12 brianR 153
        return runner.postTask();
6 brianR 154
    }
12 brianR 155
 
156
    @WebMethod(exclude = true)
157
    private ReturnCode loadResource(ResourceInterface src, String encoding) {
6 brianR 158
        LoadResource lr = new LoadResource();
159
        lr.setTaskName("LoadResource");
160
        RunTask runner = new RunTask(lr);
161
        lr.addConfigured(src.getAntResource(lr.getProject()));
162
        lr.setEncoding(encoding);
163
        System.out.println("Using encoding: " + encoding);
164
        lr.setProperty("LoadResource.out");
12 brianR 165
        return runner.postTask();
6 brianR 166
    }
167
 
168
    @WebMethod(exclude = true)
12 brianR 169
    private ReturnCode echo(String msg, File file, String encoding, boolean append) {
6 brianR 170
        Echo echo = new Echo();
171
        echo.setTaskName("toFile");
172
        RunTask runTask = new RunTask(echo);
173
        echo.addText(msg);
174
        echo.setEncoding(encoding);
175
        echo.setFile(file);
176
        echo.setAppend(append);
12 brianR 177
        return runTask.postTask();
6 brianR 178
    }
179
 
12 brianR 180
    @WebMethod(exclude = true)
181
    private ReturnCode copy(FileSetResource src, File dst, boolean preservelastmodified,
6 brianR 182
            boolean overwrite, String encoding) {
183
        Copy copy = new Copy();
184
        copy.setTaskName("Copy");
185
        RunTask runner = new RunTask(copy);
186
        FileSet set = src.getAntFileSet(copy.getProject());
187
        copy.add(set);
188
 
12 brianR 189
        if (dst.isDirectory()) {
190
            copy.setTodir(dst);
191
        }
192
        if (dst.isFile()) {
193
            copy.setTofile(dst);
194
        }
6 brianR 195
        copy.setOverwrite(overwrite);
196
        copy.setPreserveLastModified(preservelastmodified);
12 brianR 197
        if (encoding != null && !encoding.equals("")) {
6 brianR 198
            copy.setOutputEncoding(encoding);
199
        } else {
200
            copy.setOutputEncoding(System.getProperty("file.encoding"));
201
        }
202
 
12 brianR 203
        return runner.postTask();
6 brianR 204
    }
12 brianR 205
 
206
    @WebMethod(exclude = true)
207
    private ReturnCode chown(FileSetResource src, String owner) {
208
        Chown chown = new Chown();
209
        chown.setTaskName("Chown");
210
        RunTask runner = new RunTask(chown);
211
        chown.setOwner(owner);
212
        FileSet set = src.getAntFileSet(chown.getProject());
213
        chown.add(set);
14 brianR 214
        chown.setMaxParallel(300);
12 brianR 215
        return runner.postTask();
216
    }
217
 
218
    @WebMethod(exclude = true)
219
    private ReturnCode chgrp(FileSetResource src, String group) {
220
        Chgrp chgrp = new Chgrp();
221
        chgrp.setTaskName("Chgrp");
222
        RunTask runner = new RunTask(chgrp);
223
        chgrp.setGroup(group);
224
        FileSet set = src.getAntFileSet(chgrp.getProject());
225
        chgrp.add(set);
14 brianR 226
        chgrp.setMaxParallel(300);
12 brianR 227
        return runner.postTask();
228
    }
14 brianR 229
 
230
    @WebMethod(exclude = true)
231
    private ReturnCode chmod(FileSetResource src, String perm) {
232
        Chmod chmod = new Chmod();
233
        chmod.setTaskName("Chmod");
234
        RunTask runner = new RunTask(chmod);
235
        FileSet set = src.getAntFileSet(chmod.getProject());
236
        chmod.add(set);
237
        chmod.setMaxParallel(300);
238
        chmod.setPerm(perm);
239
        chmod.setVerbose(true);
240
        return runner.postTask();
241
    }
242
 
6 brianR 243
}