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.config;
017:
018: import org.apache.openejb.OpenEJBException;
019: import org.apache.openejb.config.sys.Deployments;
020: import org.apache.openejb.config.sys.Openejb;
021: import org.apache.openejb.config.sys.JaxbOpenejb;
022: import org.apache.openejb.loader.SystemInstance;
023: import org.apache.openejb.util.LogCategory;
024: import org.apache.openejb.util.Logger;
025: import org.apache.openejb.util.Messages;
026: import org.apache.xbean.finder.ResourceFinder;
027:
028: import java.io.File;
029: import java.io.FileNotFoundException;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034: import java.net.URL;
035: import java.util.Properties;
036:
037: public class ConfigUtils {
038:
039: public static Messages messages = new Messages(
040: "org.apache.openejb.util.resources");
041: public static Logger logger = Logger.getInstance(
042: LogCategory.OPENEJB, "org.apache.openejb.util.resources");
043:
044: public static String searchForConfiguration()
045: throws OpenEJBException {
046: return searchForConfiguration(SystemInstance.get().getProperty(
047: "openejb.configuration"));
048: }
049:
050: public static String searchForConfiguration(String path)
051: throws OpenEJBException {
052: return ConfigUtils.searchForConfiguration(path, SystemInstance
053: .get().getProperties());
054: }
055:
056: public static String searchForConfiguration(String path,
057: Properties props) throws OpenEJBException {
058: File file = null;
059: if (path != null) {
060: /*
061: * [1] Try finding the file relative to the current working
062: * directory
063: */
064: file = new File(path);
065: if (file != null && file.exists() && file.isFile()) {
066: return file.getAbsolutePath();
067: }
068:
069: /*
070: * [2] Try finding the file relative to the openejb.base directory
071: */
072: try {
073: file = SystemInstance.get().getBase().getFile(path);
074: if (file != null && file.exists() && file.isFile()) {
075: return file.getAbsolutePath();
076: }
077: } catch (FileNotFoundException ignored) {
078: } catch (IOException ignored) {
079: }
080:
081: /*
082: * [3] Try finding the file relative to the openejb.home directory
083: */
084: try {
085: file = SystemInstance.get().getHome().getFile(path);
086: if (file != null && file.exists() && file.isFile()) {
087: return file.getAbsolutePath();
088: }
089: } catch (FileNotFoundException ignored) {
090: } catch (IOException ignored) {
091: }
092:
093: logger.warning("Cannot find the configuration file ["
094: + path + "], Trying conf/openejb.xml instead.");
095: }
096:
097: try {
098: /*
099: * [4] Try finding the standard openejb.xml file relative to the
100: * openejb.base directory
101: */
102: try {
103: file = SystemInstance.get().getBase().getFile(
104: "conf/openejb.xml");
105: if (file != null && file.exists() && file.isFile()) {
106: return file.getAbsolutePath();
107: }
108: } catch (java.io.FileNotFoundException e) {
109: }
110:
111: /*
112: * [5] Try finding the standard openejb.conf file relative to the
113: */
114: try {
115: file = SystemInstance.get().getBase().getFile(
116: "conf/openejb.conf");
117: if (file != null && file.exists() && file.isFile()) {
118: return file.getAbsolutePath();
119: }
120: } catch (java.io.FileNotFoundException e) {
121: }
122:
123: /* [6] No config found! Create a config for them
124: * using the default.openejb.conf file from
125: * the openejb-x.x.x.jar
126: */
127:
128: File confDir = SystemInstance.get().getBase().getDirectory(
129: "conf", false);
130:
131: // TODO: DMB: This logic needs to be revisited
132: if (confDir.exists()) {
133: File config = new File(confDir, "openejb.xml");
134: logger
135: .info("Cannot find the configuration file [conf/openejb.xml]. Creating one at "
136: + config.getAbsolutePath());
137: file = createConfig(config);
138: } else {
139: logger
140: .info("Cannot find the configuration file [conf/openejb.xml]. Will attempt to create one for the beans deployed.");
141: // logger.warning("Cannot find the configuration file [conf/openejb.xml]. Using the default configuration.");
142: // ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
143: // URL resource = classLoader.getResource("default.openejb.conf");
144: // return resource.toExternalForm();
145: }
146:
147: } catch (java.io.IOException e) {
148: e.printStackTrace();
149: throw new OpenEJBException(
150: "Could not locate config file: ", e);
151: }
152:
153: /*TODO:2: Check these too.
154: * OPENJB_HOME/lib/openejb-x.x.x.jar
155: * OPENJB_HOME/dist/openejb-x.x.x.jar
156: */
157: return (file == null) ? null : file.getAbsolutePath();
158: }
159:
160: public static File createConfig(File config)
161: throws java.io.IOException {
162: InputStream in = null;
163: OutputStream out = null;
164: try {
165: ResourceFinder finder = new ResourceFinder("");
166: URL defaultConfig = finder.find("default.openejb.conf");
167: in = defaultConfig.openStream();
168: out = new FileOutputStream(config);
169:
170: int b;
171: while ((b = in.read()) != -1) {
172: out.write(b);
173: }
174: } finally {
175: if (in != null) {
176: in.close();
177: }
178: if (out != null) {
179: out.close();
180: }
181:
182: }
183: return config;
184: }
185:
186: public static boolean addDeploymentEntryToConfig(
187: String jarLocation, Openejb config) {
188: File jar = new File(jarLocation);
189:
190: /* Check to see if the entry is already listed */
191: for (Deployments d : config.getDeployments()) {
192:
193: if (d.getJar() != null) {
194: try {
195: File target = SystemInstance.get().getBase()
196: .getFile(d.getJar(), false);
197:
198: /*
199: * If the jar entry is already there, no need
200: * to add it to the config or go any futher.
201: */
202: if (jar.equals(target))
203: return false;
204: } catch (java.io.IOException e) {
205: /* No handling needed. If there is a problem
206: * resolving a config file path, it is better to
207: * just add this jars path explicitly.
208: */
209: }
210: } else if (d.getDir() != null) {
211: try {
212: File target = SystemInstance.get().getBase()
213: .getFile(d.getDir(), false);
214: File jarDir = jar.getAbsoluteFile().getParentFile();
215:
216: /*
217: * If a dir entry is already there, the jar
218: * will be loaded automatically. No need
219: * to add it explicitly to the config or go
220: * any futher.
221: */
222: if (jarDir != null && jarDir.equals(target))
223: return false;
224: } catch (java.io.IOException e) {
225: /* No handling needed. If there is a problem
226: * resolving a config file path, it is better to
227: * just add this jars path explicitly.
228: */
229: }
230: }
231: }
232:
233: /* Create a new Deployments entry */
234: Deployments dep = JaxbOpenejb.createDeployments();
235: dep.setJar(jarLocation);
236: config.getDeployments().add(dep);
237: return true;
238: }
239: }
|