01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.acting;
18:
19: import java.util.Map;
20:
21: import org.apache.avalon.framework.parameters.Parameters;
22: import org.apache.avalon.framework.service.ServiceException;
23: import org.apache.avalon.framework.service.ServiceManager;
24: import org.apache.avalon.framework.thread.ThreadSafe;
25: import org.apache.cocoon.components.source.impl.PartSource;
26: import org.apache.cocoon.environment.Redirector;
27: import org.apache.excalibur.source.ModifiableSource;
28: import org.apache.excalibur.source.Source;
29: import org.apache.excalibur.source.SourceResolver;
30: import org.apache.excalibur.source.SourceUtil;
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: * <map:act type="copy-source" src="cocoon://pipeline.xml">
40: * <map:parameter name="dest" value="context://WEB-INF/data/file.xml"/>
41: * .../...
42: * </map:act>
43: *</pre>
44: *
45: * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
46: * @version $Id: CopySourceAction.java 433543 2006-08-22 06:22:54Z crossley $
47: */
48: public class CopySourceAction extends ServiceableAction implements
49: ThreadSafe {
50:
51: private SourceResolver resolver;
52:
53: public void service(ServiceManager manager) throws ServiceException {
54: super .service(manager);
55: this .resolver = (SourceResolver) manager
56: .lookup(SourceResolver.ROLE);
57: }
58:
59: public Map act(Redirector redirector,
60: org.apache.cocoon.environment.SourceResolver oldResolver,
61: Map objectModel, String source, Parameters par)
62: throws Exception {
63:
64: // Get source and destination Sources
65: Source src = resolver.resolveURI(source);
66: Source dest = resolver.resolveURI(par.getParameter("dest"));
67:
68: // Check that dest is writeable
69: if (!(dest instanceof ModifiableSource)) {
70: throw new IllegalArgumentException("Non-writeable URI : "
71: + dest.getURI());
72: }
73:
74: if (dest instanceof TraversableSource) {
75: TraversableSource trDest = (TraversableSource) dest;
76: if (trDest.isCollection()) {
77: if (src instanceof TraversableSource) {
78: dest = trDest.getChild(((TraversableSource) src)
79: .getName());
80: } else if (src instanceof PartSource) {
81: // FIXME : quick hack to store "upload://xxx" sources into directories
82: // it would be better for the PartSource to be Traversable, or to
83: // create a new "NamedSource" interface
84: dest = trDest.getChild(((PartSource) src).getPart()
85: .getFileName());
86: }
87: }
88: }
89: // And transfer all content.
90: try {
91: SourceUtil.copy(src, dest);
92: } finally {
93: resolver.release(src);
94: resolver.release(dest);
95: }
96: // Success !
97: return EMPTY_MAP;
98: }
99: }
|