KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > InPlaceConfigurationUtil


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import org.apache.geronimo.kernel.config.ConfigurationData;
31
32 /**
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 class InPlaceConfigurationUtil {
36     private static final String JavaDoc IN_PLACE_LOCATION_FILE = "inPlaceLocation.config";
37     
38     InPlaceConfigurationUtil() {
39     }
40
41     public boolean isInPlaceConfiguration(File JavaDoc source) {
42         File JavaDoc inPlaceLocation = getInPlaceLocation(source);
43         return inPlaceLocation.exists();
44     }
45     
46     public void writeInPlaceLocation(ConfigurationData configurationData, File JavaDoc source) throws IOException JavaDoc {
47         if (null == configurationData.getInPlaceConfigurationDir()) {
48             return;
49         }
50
51         File JavaDoc inPlaceLocation = getInPlaceLocation(source);
52         Writer JavaDoc writer = null;
53         try {
54             OutputStream JavaDoc os = new FileOutputStream JavaDoc(inPlaceLocation);
55             writer = new PrintWriter JavaDoc(os);
56             File JavaDoc inPlaceConfigurationDir = configurationData.getInPlaceConfigurationDir();
57             String JavaDoc 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 JavaDoc ignored) {
66                     // ignored
67
}
68             }
69         }
70     }
71     
72     public File JavaDoc readInPlaceLocation(File JavaDoc source) throws IOException JavaDoc {
73         File JavaDoc inPlaceLocation = getInPlaceLocation(source);
74         
75         if (!inPlaceLocation.exists()) {
76             return null;
77         }
78         
79         BufferedReader JavaDoc reader = null;
80         try {
81             InputStream JavaDoc is = new FileInputStream JavaDoc(inPlaceLocation);
82             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
83             String JavaDoc path = reader.readLine();
84             return new File JavaDoc(path);
85         } finally {
86             if (null != reader) {
87                 try {
88                     reader.close();
89                 } catch (IOException JavaDoc e) {
90                 }
91             }
92         }
93     }
94
95     private File JavaDoc getInPlaceLocation(File JavaDoc source) {
96         File JavaDoc inPlaceLocation = new File JavaDoc(source, "META-INF");
97         inPlaceLocation.mkdirs();
98         return new File JavaDoc(inPlaceLocation, IN_PLACE_LOCATION_FILE);
99     }
100 }
101
Popular Tags