View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.jbi.component;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  import it.imolinfo.jbi4corba.jbi.Messages;
13  import it.imolinfo.jbi4corba.jbi.component.runtime.ComponentRuntime;
14  
15  
16  import it.imolinfo.jbi4corba.jbi.endpoint.Jbi4CorbaEndpoint;
17  
18  
19  import it.imolinfo.jbi4corba.jbi.endpoint.Jbi4CorbaSFServiceEndpoint;
20  
21  
22  import it.imolinfo.jbi4corba.utils.HelperEPRUtils;
23  import java.io.StringWriter;
24  
25  import javax.jbi.component.ComponentLifeCycle;
26  import javax.jbi.component.ServiceUnitManager;
27  import javax.jbi.servicedesc.ServiceEndpoint;
28  
29  
30  import javax.xml.transform.OutputKeys;
31  import javax.xml.transform.Transformer;
32  import javax.xml.transform.TransformerException;
33  import javax.xml.transform.TransformerFactory;
34  import javax.xml.transform.dom.DOMSource;
35  import javax.xml.transform.stream.StreamResult;
36  
37  
38  
39  import org.w3c.dom.Node;
40  import org.w3c.dom.NodeList;
41  
42  
43  
44  /**
45   * Jbi4EJB Binding Component. 
46   * @see Jbi4CorbaLifeCycle for more details of the generated code.
47   *
48   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
49   */
50  public class Jbi4CorbaRuntime extends ComponentRuntime {
51      	
52      /** The Logger. */
53      private static final Logger LOG = LoggerFactory
54              .getLogger(Jbi4CorbaRuntime.class);    
55      /** **/  
56      private static final Messages MESSAGES = 
57      	Messages.getMessages(Jbi4CorbaRuntime .class);
58      
59      /**
60       * constructor.
61       */
62      public Jbi4CorbaRuntime() {
63          super();
64      }
65      
66      /**
67       * creates the Component specific implementation of the ComponentLifeCycle.
68       * 
69       * @return the <code>Jbi4EjbLifeCycle</code> instance
70       */
71      protected ComponentLifeCycle createComponentLifeCycle() {        
72          return new Jbi4CorbaLifeCycle(this);
73      }
74      
75      /**
76       * creates the Component specific implementation of the ServiceUnitManager.
77       * 
78       * @return the <code>Jbi4EjbSUManager</code> instance
79       */
80      protected ServiceUnitManager createServiceUnitManager() {
81          return new Jbi4CorbaSUManager(this);
82      }
83      
84      
85      /**
86       * Resolve Ednpoint for Dynamic PartnerLink
87       * @param  epr Document fragment that rappresent the EndpointReference
88       * @return the <code>ServiceEndpoint</code> instance
89       * 
90       */
91      @Override
92        public javax.jbi.servicedesc.ServiceEndpoint resolveEndpointReference(
93          org.w3c.dom.DocumentFragment epr) {
94      
95      	ServiceEndpoint sendpoint=null;
96          ServiceEndpoint resolvedEP=null;
97          String ior = null;
98          NodeList children = epr.getChildNodes();
99          try {
100 
101         	if (children != null && children.getLength() > 1) {
102         	
103         		ior = HelperEPRUtils.getDynamicIORFromEPR(epr);
104         		sendpoint = HelperEPRUtils.getEndpointInfo(epr);
105 
106            
107         	} else if (children.getLength() == 1) {   // "wrapper" element found
108            
109         	
110         		Node child = children.item(0);
111             
112         		ior = HelperEPRUtils.getDynamicIORFromEPR(child);
113         		sendpoint = HelperEPRUtils.getEndpointInfo(child);
114 
115         	
116         	} else {
117            
118                     throw new Exception(MESSAGES.getString("CRB000231_Invalid_EPR_Format"));
119         	}
120                 
121                 
122                 //Find the Service Endpoint on BUS
123                 if (ior != null && sendpoint != null) {
124                
125                     
126                     Jbi4CorbaSUManager manager = (Jbi4CorbaSUManager) this.getServiceUnitManager();
127                     if (manager != null) {
128                         Jbi4CorbaEndpoint endpoint = manager.getDeployedEndpoint(sendpoint); 
129             
130                         if (endpoint != null) {
131                         //ToDo i18N
132                         LOG.debug("RESOLVED EPR ==>"+endpoint.getEndpointName());
133                         resolvedEP=new Jbi4CorbaSFServiceEndpoint(endpoint,epr,ior);
134                
135                         }
136         
137                     }
138                     
139                 } else {
140                 
141                     String msg=MESSAGES.getString("CRB000232_Can't_find_Activate_Endpoint");
142                     LOG.info(msg);    
143                     
144                 } 
145 
146         } catch (Exception e) {
147         	
148         	LOG.error(MESSAGES.getString("CRB000232_Invalid_EPR_Format"));
149         }
150        
151        
152         
153         return resolvedEP;
154     }
155     
156     
157     
158     
159     /**
160      * Retrieves a DOM representation containing metadata which describes the
161      * service provided by this component, through the given endpoint.
162      *
163      * @param endpoint the service endpoint.
164      * @return the description for the specified service endpoint.
165      * @see javax.jbi.Component#getServiceDescription(javax.jbi.servicedesc.ServiceEndpoint)
166      */
167     
168     @Override
169     public org.w3c.dom.Document getServiceDescription(ServiceEndpoint ref) {
170         LOG.debug("Called getServiceDescription for endpoint: " + ref);
171         Jbi4CorbaSUManager manager = (Jbi4CorbaSUManager) this.getServiceUnitManager();
172         if (manager != null) {
173             Jbi4CorbaEndpoint endpoint = manager.getDeployedEndpoint(ref); 
174             if (endpoint != null) {
175                 if (LOG.isDebugEnabled()) {                    
176                     String documentString = null;
177                     try {
178                         // Set up the output transformer to convert the Document to String
179                         TransformerFactory transfac = TransformerFactory.newInstance();
180                         Transformer trans = transfac.newTransformer();
181                         trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
182                         trans.setOutputProperty(OutputKeys.INDENT, "yes");
183                         StringWriter sw = new StringWriter();
184                         StreamResult result = new StreamResult(sw);
185                         DOMSource source = new DOMSource(endpoint.getServiceDescription());
186                         trans.transform(source, result);
187                         documentString = sw.toString();
188                         
189                     } catch (TransformerException e) {
190                         // Should be a warn, but can be raised only if the debug level is activated.
191                         LOG.debug("Error in transforming service description to String for logging pourpose");                        
192                     }
193                     LOG.debug("Endpoint found, returning:" + documentString);                                       
194                 }
195                 return endpoint.getServiceDescription();
196             } else {
197                 LOG.debug("No endpoint found, returning null");
198             }
199         } 
200         // Should never happen.
201         return null;            
202     }
203     
204 }