001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.configuration;
018    
019    import java.io.BufferedReader;
020    import java.io.File;
021    import java.io.FileInputStream;
022    import java.io.FileOutputStream;
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.InputStreamReader;
026    import java.io.OutputStream;
027    import java.io.PrintWriter;
028    import java.io.Writer;
029    
030    import org.apache.geronimo.kernel.config.ConfigurationData;
031    
032    /**
033     * //todo make this package access again!
034     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
035     */
036    public class InPlaceConfigurationUtil {
037        private static final String IN_PLACE_LOCATION_FILE = "inPlaceLocation.config";
038    
039        public InPlaceConfigurationUtil() {
040        }
041    
042        public boolean isInPlaceConfiguration(File source) {
043            File inPlaceLocation = getInPlaceLocation(source);
044            return inPlaceLocation.exists();
045        }
046    
047        public void writeInPlaceLocation(ConfigurationData configurationData, File source) throws IOException {
048            if (null == configurationData.getInPlaceConfigurationDir()) {
049                return;
050            }
051    
052            File inPlaceLocation = getInPlaceLocation(source);
053            Writer writer = null;
054            try {
055                OutputStream os = new FileOutputStream(inPlaceLocation);
056                writer = new PrintWriter(os);
057                File inPlaceConfigurationDir = configurationData.getInPlaceConfigurationDir();
058                String absolutePath = inPlaceConfigurationDir.getAbsolutePath();
059                writer.write(absolutePath);
060                writer.close(); // also flushes the stream and shouldn't normally fail
061                writer = null;
062            } finally {
063                if (null != writer) {
064                    try {
065                        writer.close();
066                    } catch (IOException ignored) {
067                        // ignored
068                    }
069                }
070            }
071        }
072    
073        public File readInPlaceLocation(File source) throws IOException {
074            File inPlaceLocation = getInPlaceLocation(source);
075    
076            if (!inPlaceLocation.exists()) {
077                return null;
078            }
079    
080            BufferedReader reader = null;
081            try {
082                InputStream is = new FileInputStream(inPlaceLocation);
083                reader = new BufferedReader(new InputStreamReader(is));
084                String path = reader.readLine();
085                return new File(path);
086            } finally {
087                if (null != reader) {
088                    try {
089                        reader.close();
090                    } catch (IOException e) {
091                    }
092                }
093            }
094        }
095    
096        private File getInPlaceLocation(File source) {
097            File inPlaceLocation = new File(source, "META-INF");
098            inPlaceLocation.mkdirs();
099            return new File(inPlaceLocation, IN_PLACE_LOCATION_FILE);
100        }
101    }