Source Code Cross Referenced for ConfigUtils.java in  » J2EE » openejb3 » org » apache » openejb » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » openejb3 » org.apache.openejb.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.