View Javadoc

1   /**
2    *
3    * Copyright 2006 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     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.geronimo.system.configuration;
18  
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.OutputStream;
27  import java.io.PrintWriter;
28  import java.io.Writer;
29  
30  import org.apache.geronimo.kernel.config.ConfigurationData;
31  
32  /**
33   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
34   */
35  class InPlaceConfigurationUtil {
36  	private static final String IN_PLACE_LOCATION_FILE = "inPlaceLocation.config";
37  	
38      InPlaceConfigurationUtil() {
39      }
40  
41      public boolean isInPlaceConfiguration(File source) {
42          File inPlaceLocation = getInPlaceLocation(source);
43          return inPlaceLocation.exists();
44      }
45      
46      public void writeInPlaceLocation(ConfigurationData configurationData, File source) throws IOException {
47      	if (null == configurationData.getInPlaceConfigurationDir()) {
48      		return;
49      	}
50  
51          File inPlaceLocation = getInPlaceLocation(source);
52          Writer writer = null;
53          try {
54          	OutputStream os = new FileOutputStream(inPlaceLocation);
55              writer = new PrintWriter(os);
56              File inPlaceConfigurationDir = configurationData.getInPlaceConfigurationDir();
57              String absolutePath = inPlaceConfigurationDir.getAbsolutePath();
58              writer.write(absolutePath);
59              writer.close(); // also flushes the stream and shouldn't normally fail
60              writer = null;
61          } finally {
62          	if (null != writer) {
63          		try {
64          			writer.close();
65  				} catch (IOException ignored) {
66                      // ignored
67  				}
68          	}
69          }
70      }
71      
72      public File readInPlaceLocation(File source) throws IOException {
73          File inPlaceLocation = getInPlaceLocation(source);
74          
75          if (!inPlaceLocation.exists()) {
76          	return null;
77          }
78          
79          BufferedReader reader = null;
80          try {
81          	InputStream is = new FileInputStream(inPlaceLocation);
82          	reader = new BufferedReader(new InputStreamReader(is));
83          	String path = reader.readLine();
84          	return new File(path);
85          } finally {
86          	if (null != reader) {
87          		try {
88          			reader.close();
89  				} catch (IOException e) {
90  				}
91          	}
92          }
93      }
94  
95      private File getInPlaceLocation(File source) {
96          File inPlaceLocation = new File(source, "META-INF");
97          inPlaceLocation.mkdirs();
98          return new File(inPlaceLocation, IN_PLACE_LOCATION_FILE);
99      }
100 }