001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)WsdlFactory.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.wsdl2.impl;
030:
031: import com.sun.jbi.wsdl2.WsdlException;
032:
033: import java.io.File;
034: import java.io.FileInputStream;
035: import java.io.IOException;
036:
037: import java.util.Properties;
038:
039: /**
040: * This class is the default WSDL factory. This supplies a factory for the
041: * default versions of the top-level WSDL component and ancillary classes
042: * for reading and writing WSDL.
043: * <p>
044: * This class is public, for the sake of the JBI framework. The framework
045: * makes available, through the environment context, the WsdlFactory. Other
046: * components, such as the WSDL registry, can then use it. Such client
047: * components only use the interfaces exposed in the base service, plus, of
048: * course, the ever-popular WsdlException.
049: *
050: * @author Sun Microsystems, Inc.
051: */
052: public class WsdlFactory implements com.sun.jbi.wsdl2.WsdlFactory {
053: /** The property name for the WSDL factory */
054: private static final String PROPERTY_NAME = Constants.ROOT_PUBLIC_NAME
055: + ".WsdlFactory";
056:
057: /** The file name for the WSDL2 properties */
058: private static final String PROPERTY_FILE_NAME = "jbi.wsdl2.properties";
059:
060: /** The default factory class for use by clients */
061: private static final String DEFAULT_FACTORY_IMPL_NAME = Constants.ROOT_PRIVATE_NAME
062: + ".WsdlFactory";
063:
064: /** The full path/name of the property file */
065: private static String sFullPropertyFileName = null;
066:
067: /**
068: * Create a new instance of a WSDL reader object. This can be used to
069: * create WSDL definitions components based on XML sources.
070: *
071: * @return A WSDL 2.0 reader object.
072: */
073: public com.sun.jbi.wsdl2.WsdlReader newWsdlReader() {
074: return new WsdlReader();
075: }
076:
077: /**
078: * Create a new instance of a WSDL writer.
079: *
080: * @return A new instance of a WSDL 2.0 writer.
081: */
082: public com.sun.jbi.wsdl2.WsdlWriter newWsdlWriter() {
083: return new WsdlWriter();
084: }
085:
086: /**
087: * Create a new instance of a WSDL Description component. This is for
088: * programmatic creation of service descriptions.
089: *
090: * @param tns Target name space for the new component.
091: * @return A new, empty WSDL Description component.
092: */
093: public com.sun.jbi.wsdl2.Description newDescription(String tns) {
094: com.sun.jbi.wsdl2.Description result = DescriptionImpl.Factory
095: .newInstance("");
096:
097: if (tns != null) {
098: result.setTargetNamespace(tns);
099: }
100:
101: return result;
102: }
103:
104: /**
105: * Create a new instance of a WSDL Description component. This is for
106: * programmatic creation of service descriptions.
107: *
108: * @deprecated - replaced by newDescription(String tns)
109: * @param tns Target name space for the new component.
110: * @return A new, empty WSDL Description component.
111: */
112: public com.sun.jbi.wsdl2.Definitions newDefinitions(String tns) {
113: return (com.sun.jbi.wsdl2.Definitions) newDescription(tns);
114: }
115:
116: /**
117: * Get a new instance of a WsdlFactory. This method
118: * follows (almost) the same basic sequence of steps that JAXP
119: * follows to determine the fully-qualified class name of the
120: * class which implements WsdlFactory. The steps (in order)
121: * are:
122: *<ol>
123: * <li>Check the com.sun.jbi.wsdl2.WsdlFactory system property.</li>
124: * <li>Check the lib/jbi.wsdl2.properties file in the JRE directory. The key
125: * will have the same name as the above system property.</li>
126: * <li>Use the default value.</li>
127: *</ol>
128: * Once an instance of a WsdlFactory is obtained, invoke
129: * newDescription(), newWsdlReader(), or newWsdlWriter(), to create
130: * the desired instances.
131: *
132: * @return An instance of a WsdlFactory.
133: * @exception WsdlException if the factory cannot be instantiated.
134: */
135: public static WsdlFactory newInstance() throws WsdlException {
136: String factoryImplName = findFactoryImplName();
137:
138: return newInstance(factoryImplName);
139: }
140:
141: /**
142: * Get a new instance of a WsdlFactory. This method returns an instance
143: * of the class factoryImplName. Once an instance of a WsdlFactory is
144: * obtained, invoke newWSDLReader() (and other factory methods in future)
145: * to create the desired instances.
146: *
147: * @param factoryImplName The fully-qualified class name of the
148: * class which provides a concrete implementation
149: * of the abstract class WsdlFactory.
150: * @return An instance of a WsdlFactory.
151: * @exception WsdlException if the factory cannot be instantiated.
152: */
153: public static WsdlFactory newInstance(String factoryImplName)
154: throws WsdlException {
155: if (factoryImplName != null) {
156: try {
157: Class cl = Class.forName(factoryImplName);
158:
159: return (WsdlFactory) cl.newInstance();
160: } catch (Exception ex) {
161: throw new WsdlException(
162: "Cannot instantiate WsdlFactory implementation\n"
163: + "\tclass name = {0}\n"
164: + "\terror = {2}: {1}\n",
165: new Object[] { factoryImplName,
166: ex.getMessage(),
167: ex.getClass().getName(), });
168: }
169: } else {
170: throw new WsdlException(
171: "Unable to find name of WsdlFactory implementation.");
172: }
173: }
174:
175: /**
176: * Find the class name of the WSDL factory implementation, using
177: * the search rules enumerated in newInstance().
178: *
179: * @return Fully qualified name of the WSDL factory to use.
180: * @see #newInstance()
181: */
182: private static String findFactoryImplName() {
183: String factoryImplName = null;
184:
185: // First, check the system property.
186: try {
187: factoryImplName = System.getProperty(PROPERTY_NAME);
188:
189: if (factoryImplName != null) {
190: return factoryImplName;
191: }
192: } catch (SecurityException e) {
193: // We aren't allowed to get system properties, so we fall
194: // back to plan 'B'.
195: ;
196: }
197:
198: // Second, check the properties file.
199: String propFileName = getFullPropertyFileName();
200:
201: if (propFileName != null) {
202: try {
203: Properties properties = new Properties();
204: File propFile = new File(propFileName);
205: FileInputStream fis = new FileInputStream(propFile);
206:
207: properties.load(fis);
208: fis.close();
209:
210: factoryImplName = properties.getProperty(PROPERTY_NAME);
211:
212: if (factoryImplName != null) {
213: return factoryImplName;
214: }
215: } catch (IOException e) {
216: // It appears that the properties file doesn't exist,
217: // or is malformed.
218: ;
219: }
220: }
221:
222: // Third, return the default.
223: return DEFAULT_FACTORY_IMPL_NAME;
224: }
225:
226: /**
227: * Get the full path / name of the property file.
228: *
229: * @return The full path/name of the property file; null if security
230: * settings disallow reading system properties.
231: */
232: private static String getFullPropertyFileName() {
233: if (sFullPropertyFileName == null) {
234: try {
235: String javaHome = System.getProperty("java.home");
236:
237: sFullPropertyFileName = javaHome + File.separator
238: + "lib" + File.separator + PROPERTY_FILE_NAME;
239: } catch (SecurityException e) {
240: // Security settings are such that we can't read System
241: // properties.
242: ;
243: }
244: }
245:
246: return sFullPropertyFileName;
247: }
248: }
|