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: */package org.apache.openejb.util;
017:
018: import org.apache.openejb.loader.FileUtils;
019: import org.apache.openejb.loader.SystemInstance;
020:
021: import java.io.BufferedInputStream;
022: import java.io.BufferedOutputStream;
023: import java.io.File;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.net.URL;
028:
029: /**
030: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
031: */
032: public class ConfUtils {
033:
034: public static URL getConfResource(String name) {
035: URL resource = Thread.currentThread().getContextClassLoader()
036: .getResource(name);
037:
038: try {
039:
040: File loginConfig = ConfUtils.install(resource, name);
041:
042: if (loginConfig != null) {
043: resource = loginConfig.toURL();
044: }
045: } catch (IOException e) {
046: }
047:
048: return resource;
049: }
050:
051: public static File install(String source, String name)
052: throws IOException {
053: ClassLoader cl = Thread.currentThread().getContextClassLoader();
054: URL resource = cl.getResource(source);
055: return install(resource, name, false);
056: }
057:
058: public static File install(URL resource, String name)
059: throws IOException {
060: return install(resource, name, false);
061: }
062:
063: public static File install(URL resource, String name,
064: boolean overwrite) throws IOException {
065: if (resource == null)
066: return null;
067:
068: SystemInstance system = SystemInstance.get();
069: FileUtils base = system.getBase();
070: File conf = base.getDirectory("conf");
071:
072: if (!conf.exists())
073: return null;
074:
075: File file = new File(conf, name);
076:
077: if (file.exists() && !overwrite)
078: return file;
079:
080: InputStream in = resource.openStream();
081: in = new BufferedInputStream(in);
082:
083: FileOutputStream fout = new FileOutputStream(file);
084: BufferedOutputStream out = new BufferedOutputStream(fout);
085:
086: try {
087: int b = in.read();
088: while (b != -1) {
089: out.write(b);
090: b = in.read();
091: }
092: } finally {
093: try {
094: in.close();
095: } catch (IOException e) {
096: }
097: try {
098: out.close();
099: } catch (IOException e) {
100: }
101: }
102:
103: return file;
104: }
105: }
|