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: * @(#)TransformationEngineContext.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.xslt;
030:
031: import java.util.logging.Logger;
032:
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Node;
035:
036: import java.util.Hashtable;
037:
038: import javax.jbi.servicedesc.ServiceEndpoint;
039:
040: import javax.xml.parsers.DocumentBuilderFactory;
041:
042: import javax.jbi.component.ComponentContext;
043: import javax.jbi.messaging.DeliveryChannel;
044: import javax.jbi.JBIException;
045:
046: import javax.wsdl.Definition;
047:
048: /**
049: * Context object to store Transformation engine specific informaiton.
050: *
051: * @author Sun Microsystems, Inc.
052: */
053: public final class TransformationEngineContext {
054: /**
055: * This object
056: */
057: private static TransformationEngineContext sTEContext;
058:
059: /**
060: * ComponentContext object
061: */
062: private ComponentContext mContext;
063:
064: /**
065: *
066: */
067: private Logger mLogger;
068:
069: /**
070: *
071: */
072: private Hashtable mDescriptorCache;
073:
074: /**
075: * Hashtable holding WSDL Definitions for deployment
076: */
077: private Hashtable mDefCache;
078:
079: /**
080: * Creates a new Transformation engine context object.
081: */
082: private TransformationEngineContext() {
083: mDescriptorCache = new Hashtable();
084: mDefCache = new Hashtable();
085: }
086:
087: /**
088: * Gets the delivery channel.
089: *
090: * @return delivery channel
091: */
092: public DeliveryChannel getChannel() {
093: DeliveryChannel chnl = null;
094:
095: if (mContext != null) {
096: try {
097: chnl = mContext.getDeliveryChannel();
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: }
102:
103: return chnl;
104: }
105:
106: /**
107: * Sets the Component context.
108: *
109: * @param ctx component context.
110: */
111: public void setContext(ComponentContext ctx) {
112: mContext = ctx;
113: }
114:
115: /**
116: * DOCUMENT ME!
117: *
118: * @return Transformation engine context
119: */
120: public ComponentContext getContext() {
121: return mContext;
122: }
123:
124: /**
125: * Used to grab a reference of this object.
126: *
127: * @return an initialized TransformationEngineContext reference
128: */
129: public static synchronized TransformationEngineContext getInstance() {
130: if (sTEContext == null) {
131: synchronized (TransformationEngineContext.class) {
132: if (sTEContext == null) {
133: sTEContext = new TransformationEngineContext();
134: }
135: }
136: }
137:
138: return sTEContext;
139: }
140:
141: /**
142: * This method returns a Logger object obtained from the ComponentFramework
143: * Currenly the suffix passed is ignored and default component name is used
144: * used to create a logger.
145: *
146: * @param suffix Suffix to be used for this logger.
147: *
148: * @return Logger Logger object.
149: */
150: public Logger getLogger(String suffix) {
151: try {
152: //Passing empty string for suffix will use component name as suffix.
153: mLogger = mContext.getLogger("", null);
154: } catch (JBIException e) {
155: e.printStackTrace();
156: }
157: return mLogger;
158: }
159:
160: /**
161: * Adds an endpoint document.
162: *
163: * @param epname endpoint name.
164: * @param df WSDL document.
165: */
166: public void addEndpointDoc(String epname, Document df) {
167: if ((epname != null) && (df != null)) {
168: try {
169: mDescriptorCache.put(epname, df);
170: } catch (Exception e) {
171: e.printStackTrace();
172: }
173: }
174: }
175:
176: /**
177: * Adds an endpoint document.
178: *
179: * @param epname endpoint name.
180: * @param filename wsdl file.
181: */
182: public void addEndpointDoc(String key, String filename) {
183: if ((key != null) && (filename != null)) {
184: try {
185: org.w3c.dom.Document d = DocumentBuilderFactory
186: .newInstance().newDocumentBuilder().parse(
187: filename);
188: mDescriptorCache.put(key, d);
189: } catch (Exception e) {
190: e.printStackTrace();
191: }
192: }
193: }
194:
195: /**
196: *
197: */
198: public void clearCache() {
199: mDescriptorCache = new Hashtable();
200: }
201:
202: /**
203: *
204: *
205: * @param ef ServiceEndpoint object
206: *
207: * @return Document Document Fragment describing endpoint.
208: */
209: public Document getServiceDescription(ServiceEndpoint ef) {
210: try {
211: Document df = (Document) mDescriptorCache.get(ef
212: .getServiceName().toString()
213: + ef.getEndpointName());
214: /*org.w3c.dom.Document d =
215: DocumentBuilderFactory.newInstance().newDocumentBuilder()
216: .newDocument();
217: Node n = d.importNode(df, true);
218: d.appendChild(n);
219: */
220:
221: return df;
222: } catch (Exception e) {
223: return null;
224: }
225: }
226:
227: public Definition getEndpointDef(ServiceEndpoint epRef) {
228: try {
229: Definition epDef = (Definition) mDefCache.get(epRef
230: .getServiceName().toString()
231: + epRef.getEndpointName());
232:
233: return epDef;
234: } catch (Exception e) {
235: e.printStackTrace();
236: return null;
237: }
238: }
239:
240: public void addEndpointDef(String key, Definition epDef) {
241: if ((key != null) && (epDef != null)) {
242: try {
243: mDefCache.put(key, epDef);
244: } catch (Exception e) {
245: e.printStackTrace();
246: }
247: }
248: }
249: }
|