KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package com.sun.enterprise.connectors.util;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39
40 import com.sun.enterprise.connectors.ConnectorRuntimeException;
41 import com.sun.enterprise.deployment.ConnectorDescriptor;
42 import com.sun.enterprise.deployment.EjbMessageBeanDescriptor;
43 import com.sun.enterprise.deployment.EnvironmentProperty;
44 import com.sun.logging.LogDomains;
45 import com.sun.enterprise.util.i18n.StringManager;
46
47 /**
48  * This is a utility class to obtain the properties of a
49  * RA JavaBean housed in a RAR module/deployment dir, without exploding the RAR
50  * contents. This method would be used by the admin-gui to configure
51  * RA properties during RA deployment to a cluster.
52  *
53  * @author Sivakumar Thyagarajan
54  */

55 public class RARUtils {
56     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
57     private static StringManager localStrings =
58         StringManager.getManager( RARUtils.class );
59
60     /**
61      * Finds the properties of a RA JavaBean bundled in a RAR
62      * without exploding the RAR
63      *
64      * @param pathToDeployableUnit a physical,accessible location of the connector module.
65      * [either a RAR for RAR-based deployments or a directory for Directory based deployments]
66      * @return A Map that is of <String RAJavaBeanPropertyName, String defaultPropertyValue>
67      * An empty map is returned in the case of a 1.0 RAR
68      */

69     public static Map JavaDoc getRABeanProperties (String JavaDoc pathToDeployableUnit) throws ConnectorRuntimeException{
70         File JavaDoc f = new File JavaDoc(pathToDeployableUnit);
71         if (!f.exists()){
72             String JavaDoc i18nMsg = localStrings.getString(
73                 "rar_archive_not_found", pathToDeployableUnit);
74             throw new ConnectorRuntimeException( i18nMsg );
75         }
76         if(f.isDirectory()) {
77             return getRABeanPropertiesForDirectoryBasedDeployment(pathToDeployableUnit);
78         } else {
79             return getRABeanPropertiesForRARBasedDeployment(pathToDeployableUnit);
80         }
81     }
82     
83     private static Map JavaDoc getRABeanPropertiesForRARBasedDeployment(String JavaDoc rarLocation){
84         ConnectorRARClassLoader jarCL =
85                             (new ConnectorRARClassLoader(rarLocation, RARUtils.class.getClassLoader()));
86         String JavaDoc raClassName = ConnectorDDTransformUtils.
87                                     getResourceAdapterClassName(rarLocation);
88         _logger.finer("RA class : " + raClassName);
89         Map JavaDoc hMap = new HashMap JavaDoc();
90         try {
91            hMap = extractRABeanProps(raClassName, jarCL);
92         } catch (ClassNotFoundException JavaDoc e) {
93             _logger.info(e.getMessage());
94             _logger.log(Level.FINE, "Error while trying to find class "
95                             + raClassName + "in RAR at " + rarLocation, e);
96         }
97         return hMap;
98     }
99     
100     private static Map JavaDoc getRABeanPropertiesForDirectoryBasedDeployment(
101                     String JavaDoc directoryLocation) {
102         Map JavaDoc hMap = new HashMap JavaDoc();
103         //Use the deployment APIs to get the name of the resourceadapter
104
//class through the connector descriptor
105
try {
106             ConnectorDescriptor cd = ConnectorDDTransformUtils.
107                                 getConnectorDescriptor(directoryLocation);
108             String JavaDoc raClassName = cd.getResourceAdapterClass();
109             
110             File JavaDoc f = new File JavaDoc(directoryLocation);
111             URLClassLoader JavaDoc ucl = new URLClassLoader JavaDoc(new URL JavaDoc[]{f.toURI().toURL()},
112                                                                                                 RARUtils.class.getClassLoader());
113             hMap = extractRABeanProps(raClassName, ucl);
114         } catch (IOException JavaDoc e) {
115             _logger.info(e.getMessage());
116             _logger.log(Level.FINE, "IO Error while trying to read connector" +
117                    "descriptor to get resource-adapter properties", e);
118         } catch (ClassNotFoundException JavaDoc e) {
119             _logger.info(e.getMessage());
120             _logger.log(Level.FINE, "Unable to find class while trying to read connector" +
121                    "descriptor to get resource-adapter properties", e);
122         } catch (ConnectorRuntimeException e) {
123             _logger.info(e.getMessage());
124             _logger.log(Level.FINE, "Error while trying to read connector" +
125                    "descriptor to get resource-adapter properties", e);
126         } catch (Exception JavaDoc e) {
127             _logger.info(e.getMessage());
128             _logger.log(Level.FINE, "Error while trying to read connector" +
129                    "descriptor to get resource-adapter properties", e);
130         }
131         return hMap;
132     }
133     
134     /**
135      * Extracts RA Bean properties via reflection.
136      *
137      * @param raClassName The RA Bean class name.
138      * @param ucl the classloader to use to find the class.
139      */

140     private static Map JavaDoc extractRABeanProps(String JavaDoc raClassName, ClassLoader JavaDoc classLoader)
141                                     throws ClassNotFoundException JavaDoc {
142         Map JavaDoc hMap = new HashMap JavaDoc();
143         //Only if RA is a 1.5 RAR, we need to get RA JavaBean properties, else
144
//return an empty map.
145
if(raClassName.trim().length() != 0) {
146             Class JavaDoc c = classLoader.loadClass(raClassName);
147             if(_logger.isLoggable(Level.FINER)) printClassDetails(c);
148             hMap = getJavaBeanProperties(c);
149         }
150         return hMap;
151     }
152
153     public static void main(String JavaDoc[] args) {
154         if(!(args.length >= 1)){
155             System.out.println("<Usage> java RARUtils directory-path ");
156             return;
157         }
158         
159         Map JavaDoc hMap = RARUtils.getRABeanPropertiesForDirectoryBasedDeployment(args[0]);
160         System.out.println("RA JavaBean Properties");
161         System.out.println(hMap);
162     }
163
164     private static Map JavaDoc getJavaBeanProperties(Class JavaDoc c) {
165         Method JavaDoc[] m = c.getMethods();
166         Map JavaDoc hMap = new HashMap JavaDoc();
167         for (int i = 0; i < m.length; i++) {
168             _logger.finer(m[i].getName());
169             if(m[i].getName().startsWith("get")
170                     && isValidRABeanConfigProperty(m[i].getReturnType())) {
171                 hMap.put(m[i].getName().substring(3), m[i].getReturnType());
172             }
173         }
174         
175         //remove Object's Class attribute.
176
hMap.remove("Class");
177         return hMap;
178     }
179
180     /**
181      * A valid resource adapter java bean property should either be one of the
182      * following
183      * 1. A Java primitive or a primitve wrapper
184      * 2. A String
185      */

186     public static boolean isValidRABeanConfigProperty(Class JavaDoc clz) {
187         return (clz.isPrimitive() || clz.equals(String JavaDoc.class)
188                         || isPrimitiveWrapper(clz));
189     }
190
191     /**
192      * Determines if a class is one of the eight java primitive wrapper classes
193      */

194     private static boolean isPrimitiveWrapper(Class JavaDoc clz) {
195         return (clz.equals(Boolean JavaDoc.class) || clz.equals(Character JavaDoc.class)
196                  || clz.equals(Byte JavaDoc.class) || clz.equals(Short JavaDoc.class)
197                  || clz.equals(Integer JavaDoc.class) || clz.equals(Long JavaDoc.class)
198                  || clz.equals(Float JavaDoc.class) || clz.equals(Double JavaDoc.class));
199     }
200
201     
202     private static void printClassDetails(Class JavaDoc c) {
203         Method JavaDoc[] m = c.getMethods();
204         _logger.finer("Methods in " + c.getName());
205         for (int i = 0; i < m.length; i++) {
206             _logger.finer(m[i].toString());
207         }
208     }
209     
210    /**
211      * Prepares the name/value pairs for ActivationSpec. <p>
212      * Rule: <p>
213      * 1. The name/value pairs are the union of activation-config on
214      * standard DD (message-driven) and runtime DD (mdb-resource-adapter)
215      * 2. If there are duplicate property settings, the value in runtime
216      * activation-config will overwrite the one in the standard
217      * activation-config.
218      */

219     public static Set JavaDoc getMergedActivationConfigProperties(EjbMessageBeanDescriptor msgDesc) {
220         
221         Set JavaDoc mergedProps = new HashSet JavaDoc();
222         Set JavaDoc runtimePropNames = new HashSet JavaDoc();
223         
224         Set JavaDoc runtimeProps = msgDesc.getRuntimeActivationConfigProperties();
225         if(runtimeProps != null){
226             Iterator JavaDoc iter = runtimeProps.iterator();
227             while (iter.hasNext()) {
228                 EnvironmentProperty entry = (EnvironmentProperty) iter.next();
229                 mergedProps.add(entry);
230                 String JavaDoc propName = (String JavaDoc) entry.getName();
231                 runtimePropNames.add(propName);
232             }
233         }
234         
235         Set JavaDoc standardProps = msgDesc.getActivationConfigProperties();
236         if(standardProps != null){
237             Iterator JavaDoc iter = standardProps.iterator();
238             while (iter.hasNext()) {
239                 EnvironmentProperty entry = (EnvironmentProperty) iter.next();
240                 String JavaDoc propName = (String JavaDoc) entry.getName();
241                 if (runtimePropNames.contains(propName))
242                     continue;
243                 mergedProps.add(entry);
244             }
245         }
246         
247         return mergedProps;
248         
249     }
250     
251 }
252
Popular Tags