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.pluto.util.assemble.file;
18:
19: import java.io.File;
20: import java.io.FileInputStream;
21: import java.io.FileOutputStream;
22: import java.io.IOException;
23: import java.io.InputStream;
24:
25: import org.apache.commons.io.FileUtils;
26: import org.apache.pluto.util.UtilityException;
27: import org.apache.pluto.util.assemble.AssemblerConfig;
28: import org.apache.pluto.util.assemble.WebXmlRewritingAssembler;
29:
30: /**
31: *
32: * @version 1.0
33: * @since Nov 8, 2004
34: */
35: public class FileAssembler extends WebXmlRewritingAssembler {
36: // Constructor -------------------------------------------------------------
37:
38: /**
39: * Default no-arg constructor.
40: */
41: public FileAssembler() {
42: // Do nothing.
43: }
44:
45: // Assembler Impl ----------------------------------------------------------
46:
47: public void assemble(AssemblerConfig config)
48: throws UtilityException {
49: try {
50: final File webappDescriptor = config.getWebappDescriptor();
51: InputStream webXmlIn = new FileInputStream(webappDescriptor);
52:
53: final File portletDescriptor = config
54: .getPortletDescriptor();
55: InputStream portletXmlIn = new FileInputStream(
56: portletDescriptor);
57:
58: final File destinationDescriptor = config.getDestination();
59: if (webappDescriptor.equals(destinationDescriptor)) {
60: final File tempXml = File.createTempFile(
61: webappDescriptor.getName() + ".", ".tmp");
62: final FileOutputStream webXmlOut = new FileOutputStream(
63: tempXml);
64:
65: this .updateWebappDescriptor(webXmlIn, portletXmlIn,
66: webXmlOut, config.getDispatchServletClass());
67:
68: //Move the temp file to the destination location
69: destinationDescriptor.delete();
70: // renameTo() is impl-specific
71: boolean success = tempXml
72: .renameTo(destinationDescriptor);
73: if (!success) {
74: FileUtils.copyFile(tempXml, destinationDescriptor);
75: }
76: } else {
77: destinationDescriptor.getParentFile().mkdirs();
78: final FileOutputStream webXmlOut = new FileOutputStream(
79: destinationDescriptor);
80: this .updateWebappDescriptor(webXmlIn, portletXmlIn,
81: webXmlOut, config.getDispatchServletClass());
82: }
83: } catch (IOException ex) {
84: throw new UtilityException(ex.getMessage(), ex, null);
85: }
86: }
87: }
|