KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > connectors > util > ConnectorDDTransformUtils


1
2 /*
3  * The contents of this file are subject to the terms
4  * of the Common Development and Distribution License
5  * (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the license at
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
10  * glassfish/bootstrap/legal/CDDLv1.0.txt.
11  * See the License for the specific language governing
12  * permissions and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL
15  * Header Notice in each file and include the License file
16  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
17  * If applicable, add the following below the CDDL Header,
18  * with the fields enclosed by brackets [] replaced by
19  * you own identifying information:
20  * "Portions Copyrighted [year] [name of copyright owner]"
21  *
22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23  */

24
25 package com.sun.enterprise.connectors.util;
26
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.deployment.archivist.*;
29 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
30 import com.sun.enterprise.deployment.deploy.shared.MemoryMappedArchive;
31 import com.sun.enterprise.config.serverbeans.ElementProperty;
32 import com.sun.enterprise.connectors.*;
33 import com.sun.logging.LogDomains;
34
35 import java.util.logging.*;
36 import java.util.*;
37 import java.io.FileInputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 import org.xml.sax.SAXParseException JavaDoc;
41
42
43 /** This is an util class pertaining to the connector deployment descriptor
44  * i.e ra.xml and sun-ra.xml. This consist of methods to obtain the deployment
45  * descriptor and perform various transformations on that to
46  * obtain/constructs classes/objects pertaining to the Connector modules
47  * like ConnectorDescriptorInfo.
48  * @author Srikanth P
49  */

50
51 public class ConnectorDDTransformUtils {
52
53     static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
54
55     /** Constructs ConnectorDescriptorInfo object from the
56      * ConnectionDefDescriptor object (deployment descriptor of connector
57      * module)
58      * @param connectionDefDescriptor ConnectionDefDescriptor object which
59                represents the ra.xml and sun-ra.xml
60      * @return Transformed ConnectorDescriptorInfo object
61      */

62
63     public static ConnectorDescriptorInfo getConnectorDescriptorInfo(
64                           ConnectionDefDescriptor connectionDefDescriptor)
65     {
66
67         ConnectorDescriptorInfo connectorDescInfo =
68                             new ConnectorDescriptorInfo();
69         connectorDescInfo.setConnectionDefinitionName(
70                           connectionDefDescriptor.getConnectionFactoryIntf());
71         connectorDescInfo.setManagedConnectionFactoryClass(
72                   connectionDefDescriptor.getManagedConnectionFactoryImpl());
73         connectorDescInfo.setConnectionFactoryClass(
74                        connectionDefDescriptor.getConnectionFactoryImpl());
75         connectorDescInfo.setConnectionFactoryInterface(
76                        connectionDefDescriptor.getConnectionFactoryIntf());
77         connectorDescInfo.setConnectionInterface(
78                        connectionDefDescriptor.getConnectionIntf());
79         connectorDescInfo.setConnectionClass(
80                        connectionDefDescriptor.getConnectionImpl());
81         connectorDescInfo.setMCFConfigProperties(
82                        connectionDefDescriptor.getConfigProperties());
83         return connectorDescInfo;
84     }
85
86     /**
87      * merges the properties mentioned in first parameter with the Set of
88      * properties mentioned in second parameter.
89      * Values of first parameter takes precedence over second.
90      * First parameter represents properties present in domain.xml
91      * Second parameter contains values mentioned in deployment descriptors.
92      * @param props Array of properties that needs to be merged with
93      * properties mentioned in deployement descriptor. These values
94      * takes precedence over values present in deployment descriptors.
95      * @param Properties present in deployment descriptor.
96      * @return Set of merged properties.
97      */

98
99     public static Set mergeProps(ElementProperty[] props, Set defaultMCFProps){
100         HashSet mergedSet = new HashSet();
101
102         if(defaultMCFProps != null) {
103             Object JavaDoc[] defaultProps = defaultMCFProps.toArray();
104
105             for (int i = 0; i < defaultProps.length; i++) {
106                 mergedSet.add(defaultProps[i]);
107             }
108         }
109
110         for (int i =0; props!= null && i< props.length; i++) {
111              EnvironmentProperty ep = new EnvironmentProperty(
112                                 props[i].getName(),props[i].getValue(),null);
113              if (defaultMCFProps.contains(ep)) {
114                //get the environment property in the mergedset
115
Iterator iter = defaultMCFProps.iterator();
116                 while(iter.hasNext()){
117                        EnvironmentProperty envProp =
118                                (EnvironmentProperty)iter.next();
119                        //and if they are equal, set ep's type to envProp's type
120

121                        //This set is important because envProp has the ra.xml
122
//specified property-Type. When the ra-bean-class does
123
//not have any getter method for a property, the property
124
//Type specified in ra.xml should be used.
125
if (envProp.equals(ep)) {
126                           if (envProp.getType() != null) {
127                                ep.setType(envProp.getType());
128                           }
129                        }
130                 }
131
132                 _logger.log(Level.FINER,
133                      "After merging props with defaultMCFProps: envPropName: "
134                      + ep.getName() + " envPropValue : " + ep.getValue());
135                 mergedSet.remove(ep);
136              }
137              mergedSet.add(ep);
138         }
139
140         return mergedSet;
141     }
142
143     /**
144      * Get the ConnectorDescriptor object which represents the ra.xml and
145      * sun-ra.xml from an exploded rar module.
146      * @param moduleDir Directory where rar is exploded.
147      * @return ConnectorDescriptor object which
148      * represents the ra.xml and sun-ra.xml
149      * @throws ConnectorRuntimeException if ra.xml could not be located or
150      * invalid. For 1.0 type rar if sun-ra.xml is not present or
151      * invalid this exception is thrown. For 1.5 type rar sun-ra.xml
152      * should not be present.
153      */

154
155     public static ConnectorDescriptor getConnectorDescriptor(String JavaDoc moduleDir)
156                                throws ConnectorRuntimeException
157     {
158
159         try {
160
161             FileArchive fileArchive = new FileArchive();
162             fileArchive.open(moduleDir); // directory where rar is exploded
163
ConnectorArchivist connectorArchivist = new ConnectorArchivist();
164             ConnectorDescriptor connectorDescriptor =
165                    (ConnectorDescriptor) connectorArchivist.open(fileArchive);
166             return connectorDescriptor;
167         } catch(IOException JavaDoc ex) {
168             ConnectorRuntimeException cre = new ConnectorRuntimeException(
169                         "Failed to read the connector deployment descriptors");
170             cre.initCause(ex);
171             _logger.log(Level.SEVERE,
172                     "rardeployment.connector_descriptor_read_error",moduleDir);
173             _logger.log(Level.SEVERE,"",cre);
174             throw cre;
175         } catch(SAXParseException JavaDoc ex) {
176             ConnectorRuntimeException cre = new ConnectorRuntimeException(
177                      "Failed to parse the connector deployment descriptors");
178             cre.initCause(ex);
179             _logger.log(Level.SEVERE,
180                    "rardeployment.connector_descriptor_parse_error",moduleDir);
181             _logger.log(Level.SEVERE,"",cre);
182             throw cre;
183         }
184     }
185
186     /**
187      * Obtain all the ConnectionDefDescriptor(abstracts the
188      * <connector-definition> element in ra.xml) objects that are
189      * present in given ra.xml file.
190      * @param connectorDesc ConnectorDescriptor object which
191      * represents the ra.xml and sun-ra.xml
192      * @return Array of ConnectionDefDescriptor objects which represent the
193      * <connection-definition> element.
194      */

195
196
197     public static ConnectionDefDescriptor[] getConnectionDefs(
198                         ConnectorDescriptor connectorDesc)
199     {
200         ConnectionDefDescriptor[] connectionDefDescs = null;
201         OutboundResourceAdapter ora =
202                       connectorDesc.getOutboundResourceAdapter();
203         if (ora != null) {
204             Set connectionDefs = ora.getConnectionDefs();
205             int size = connectionDefs.size();
206             if(size == 0) {
207                 return null;
208             }
209             Iterator iter = connectionDefs.iterator();
210             connectionDefDescs = new ConnectionDefDescriptor[size];
211             for (int i=0; i<size; ++i) {
212                connectionDefDescs[i] =
213                        (ConnectionDefDescriptor)iter.next();
214             }
215         }
216         return connectionDefDescs;
217     }
218
219     /**
220      * Returns all the message listeners present in the connectorDescriptor
221      * which abstracts the ra.xml
222      * @param desc connectorDescriptor which abstracts the ra.xml
223      * @return Array of MessageListener objects
224      */

225
226      public MessageListener[] getMessageListeners(ConnectorDescriptor desc) {
227
228          InboundResourceAdapter inboundRA = null;
229          Set messageListenerSet = null;
230          if(desc != null &&
231                  (inboundRA = desc.getInboundResourceAdapter()) != null) {
232              messageListenerSet = inboundRA.getMessageListeners();
233          }
234
235          if(messageListenerSet == null) {
236              return null;
237          }
238          int size = messageListenerSet.size();
239          MessageListener[] messageListeners =
240                   new MessageListener[size];
241          Iterator iter = messageListenerSet.iterator();
242          for(int i=0;i<size;++i){
243              messageListeners[i] = (MessageListener)iter.next();
244          }
245          return messageListeners;
246      }
247
248      public static String JavaDoc getResourceAdapterClassName
249                              (String JavaDoc rarLocation) {
250          //Use the deployment APIs to get the name of the resourceadapter
251
//class through the connector descriptor
252
try {
253              FileInputStream JavaDoc fis = new FileInputStream JavaDoc(rarLocation);
254              MemoryMappedArchive mma = new MemoryMappedArchive(fis);
255              ConnectorArchivist ca = new ConnectorArchivist();
256              ConnectorDescriptor cd = (ConnectorDescriptor)ca.open(mma);
257              return cd.getResourceAdapterClass();
258          } catch (IOException JavaDoc e) {
259              _logger.info(e.getMessage());
260              _logger.log(Level.FINE, "Error while trying to read connector" +
261                     "descriptor to get resource-adapter properties", e);
262          } catch (SAXParseException JavaDoc e) {
263              _logger.info(e.getMessage());
264              _logger.log(Level.FINE, "Error while trying to read connector" +
265                     "descriptor to get resource-adapter properties", e);
266          }
267          return null;
268      }
269 }
270
271
Popular Tags