KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > CopySourceAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 org.apache.cocoon.acting;
17
18 import java.io.InputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.cocoon.components.source.impl.PartSource;
27 import org.apache.cocoon.environment.Redirector;
28 import org.apache.excalibur.source.ModifiableSource;
29 import org.apache.excalibur.source.Source;
30 import org.apache.excalibur.source.SourceResolver;
31 import org.apache.excalibur.source.TraversableSource;
32
33 /**
34  * The CopySourceAction copies the content of it's "src" attribute to its "dest" parameter.
35  * The destination must of course resolve to a <code>WriteableSource</code>
36  * <p>
37  * Example :
38  * <pre>
39  * &lt;map:act type="copy-source" SRC="cocoon://pipeline.xml"&gt;
40  * &lt;map:parameter name="dest" value="context://WEB-INF/data/file.xml"/&gt;
41  * .../...
42  * &lt;/map:act&gt;
43  *</pre>
44  *
45  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
46  * @version CVS $Id: CopySourceAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
47  */

48 public class CopySourceAction extends ServiceableAction implements ThreadSafe
49 {
50     
51     private SourceResolver resolver;
52
53     public void service(ServiceManager manager) throws ServiceException {
54         super.service(manager);
55         this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
56     }
57     
58     public Map JavaDoc act(Redirector redirector, org.apache.cocoon.environment.SourceResolver oldResolver, Map JavaDoc objectModel, String JavaDoc source, Parameters par)
59         throws Exception JavaDoc {
60         
61         // Get source and destination Sources
62
Source src = resolver.resolveURI(source);
63         Source dest = resolver.resolveURI(par.getParameter("dest"));
64         
65         // Check that dest is writeable
66
if (! (dest instanceof ModifiableSource)) {
67             throw new IllegalArgumentException JavaDoc("Non-writeable URI : " + dest.getURI());
68         }
69         
70         if (dest instanceof TraversableSource) {
71             TraversableSource trDest = (TraversableSource) dest;
72             if (trDest.isCollection()) {
73                 if (src instanceof TraversableSource) {
74                     dest = trDest.getChild(((TraversableSource)src).getName());
75                 } else if (src instanceof PartSource){
76                     // FIXME : quick hack to store "upload://xxx" sources into directories
77
// it would be better for the PartSource to be Traversable, or to
78
// create a new "NamedSource" interface
79
dest = trDest.getChild(((PartSource)src).getPart().getFileName());
80                 }
81             }
82         }
83         
84         ModifiableSource wdest = (ModifiableSource)dest;
85         
86         // Get streams
87
InputStream JavaDoc is = src.getInputStream();
88         OutputStream JavaDoc os = wdest.getOutputStream();
89         
90         // And transfer all content.
91
try {
92             byte[] buffer = new byte[1024];
93             int len;
94             while ((len = is.read(buffer, 0, buffer.length)) > 0) {
95                 os.write(buffer, 0, len);
96             }
97             os.close();
98         } catch(Exception JavaDoc e) {
99             if (wdest.canCancel(os)) {
100                 wdest.cancel(os);
101             }
102         } finally {
103             is.close();
104         }
105         // Success !
106
return EMPTY_MAP;
107     }
108 }
109
Popular Tags