KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > ResourceReferenceDescriptor


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.deployment;
25
26 import java.util.*;
27 import javax.mail.Session JavaDoc;
28 import javax.sql.DataSource JavaDoc;
29 import java.util.logging.*;
30 import com.sun.enterprise.deployment.util.LogDomains;
31 import com.sun.enterprise.deployment.ResourcePrincipal;
32 import com.sun.enterprise.deployment.web.ResourceReference;
33 import com.sun.enterprise.util.LocalStringManagerImpl; // IASRI 4718559
34

35 /**
36  * This descriptor represents a dependency on a resource.
37  * @author Danny Coward
38  */

39
40 public class ResourceReferenceDescriptor extends EnvironmentProperty
41     implements NamedDescriptor, ResourceReference {
42     /**
43      * For database resources, this says the application will log in.
44      */

45     public static String JavaDoc APPLICATION_AUTHORIZATION = "Application";
46     /**
47      * For database resources this says the container will log in.
48      */

49     public static String JavaDoc CONTAINER_AUTHORIZATION = "Container";
50
51     // res-sharing-scope values
52
public static String JavaDoc RESOURCE_SHAREABLE = "Shareable";
53     public static String JavaDoc RESOURCE_UNSHAREABLE = "Unshareable";
54     
55     private static final String JavaDoc URL_RESOURCE_TYPE = "java.net.URL";
56     
57     //START OF IASRI 4633229
58
private static final String JavaDoc CONNECTOR_RESOURCE_TYPE = "javax.resource.cci.ConnectionFactory";
59     //END OF IASRI 4633229
60
private static final String JavaDoc MAIL_RESOURCE_TYPE = "javax.mail.Session";
61
62     // start IASRI 4734197
63
private static final String JavaDoc JDBC_RESOURCE_TYPE = "javax.sql.DataSource";
64     // end IASRI 4734197
65

66     private static final String JavaDoc ORB_RESOURCE_TYPE = "org.omg.CORBA.ORB";
67
68     private static final String JavaDoc WEBSERVICE_CONTEXT_TYPE =
69         "javax.xml.ws.WebServiceContext";
70
71     // change field name from type to rType since it's error-prone
72
// to use the same field name as its super class
73
private String JavaDoc rType;
74     private ResourcePrincipal resourcePrincipal = null;
75     private MailConfiguration mailConfiguration;
76     private String JavaDoc authorization;
77     private DataSource JavaDoc dataSource;
78     private String JavaDoc sharingScope;
79     
80     private List runtimeProps=null;
81     
82     // for cmp-resource type
83
boolean createTablesAtDeploy=false;
84     boolean dropTablesAtUndeploy=false;
85     String JavaDoc databaseVendorName = null;
86     Properties schemaGeneratorProperties = null;
87
88     // START OF IASRI 4718559
89
private static LocalStringManagerImpl localStrings =
90         new LocalStringManagerImpl(ResourceReferenceDescriptor.class);
91     // END OF IASRI 4718559
92

93     /**
94      * Construct a resource reference with the given name, description
95      * and type.
96      * @param the name of the reference
97      * @param the description
98      * @param the type of the resource reference.
99      */

100     public ResourceReferenceDescriptor(String JavaDoc name, String JavaDoc description,
101                     String JavaDoc type) {
102     super(name, "", description);
103     rType = type;
104         this.sharingScope = RESOURCE_SHAREABLE;
105     }
106     
107     /**
108      * Default constructor.
109      */

110     public ResourceReferenceDescriptor() {
111         this.sharingScope = RESOURCE_SHAREABLE;
112     }
113     
114    // Create logger object per Java SDK 1.4 to log messages
115
// introduced Santanu De, Sun Microsystems, March 2002
116

117     static Logger _logger = LogDomains.getLogger(LogDomains.DPL_LOGGER);
118     
119
120     /**
121      * Return the JNDI name of this resource reference.
122      * @return the JNDI name of the resource reference.
123      */

124     public String JavaDoc getJndiName() {
125         String JavaDoc jndiName = super.getValue();
126         return (jndiName != null && ! jndiName.equals("")) ?
127             jndiName : getMappedName();
128     }
129     
130     /**
131      * Set the JNDI name of this resource reference.
132      * @param the JNDI name of the resource reference.
133      */

134     public void setJndiName(String JavaDoc jndiName) {
135     super.setValue(jndiName);
136     }
137
138     public String JavaDoc getInjectResourceType() {
139         return rType;
140     }
141
142     public void setInjectResourceType(String JavaDoc resourceType) {
143         rType = resourceType;
144     }
145    
146     /**
147      * Return the res-sharing-scope of this resource reference.
148      * @return the sharing scope.
149      */

150     public String JavaDoc getSharingScope() {
151     if ( sharingScope == null ) {
152         return "";
153     }
154     return sharingScope;
155     }
156     
157     /**
158      * Set the res-sharing-scope of this resource reference.
159      * @param the sharing scope.
160      */

161     public void setSharingScope(String JavaDoc ss) {
162     sharingScope = ss;
163     }
164
165
166     /**
167      * Does this resource references have a JNDI name.
168      * @return true if the resource reference has a JNDI name, false otherwise
169      */

170     public boolean isResolved() {
171     return true;
172     }
173     
174     /**
175      * Return true of this resource reference is expecting the container
176      * to authorize the resource.
177      * @return true if authorization is container managed.
178      */

179     public boolean isContainerAuthorization() {
180     return this.getAuthorization().equals(CONTAINER_AUTHORIZATION);
181     }
182     
183     /**
184      * Return the authorization type of this resource. The default value
185      * is APPLICATION_AUTHORIZATION
186      * @return the authorization type of the resource.
187      */

188     public String JavaDoc getAuthorization() {
189     if (this.authorization == null) {
190         this.authorization = APPLICATION_AUTHORIZATION;
191     }
192     return this.authorization;
193     }
194
195     /**
196      * Sets the authorization type of this resource.
197      * @param the authorization type.
198      */

199     public void setAuthorization(String JavaDoc authorization) {
200     this.authorization = authorization;
201     }
202
203     /**
204      * Return the type of the resource.
205      * @return the type of the resource.
206      */

207     public String JavaDoc getType() {
208     return rType;
209     }
210     
211     /**
212      * Sets the type of this resource.
213      * @param the type of the resource.
214      */

215     public void setType(String JavaDoc type) {
216     rType = type;
217     }
218
219     /**
220      * Lookup the datasource from the namespace based on the JNDI name.
221      * @return the data source
222      */

223     public DataSource JavaDoc getJDBCDataSource()
224     {
225     if ( dataSource == null ) {
226         try {
227         // Get JDBC DataSource for database
228
javax.naming.Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
229         // cache the datasource to avoid JNDI lookup overheads
230
dataSource = (DataSource JavaDoc)ctx.lookup(getJndiName());
231         } catch ( Exception JavaDoc ex ) { }
232     }
233     return dataSource;
234     }
235
236     public boolean isWebServiceContext() {
237         return this.getType().equals(WEBSERVICE_CONTEXT_TYPE);
238     }
239     
240     public boolean isORB() {
241         return this.getType().equals(ORB_RESOURCE_TYPE);
242     }
243
244     /**
245      * Return true if this resource is to a JavaMail session object.
246      * @return true if the resource is a JavaMail session object.
247      */

248     public boolean isMailResource() {
249         //START OF IASRI 4650786
250
// return (this.getMailConfiguration() != null);
251
return this.getType().equals(MAIL_RESOURCE_TYPE);
252         //END OF IASRI 4650786
253
}
254
255     // start IASRI 4734197
256
/**
257      * @return true if the resource is a jdbc DataSource object.
258      */

259     public boolean isJDBCResource() {
260         return this.getType().equals(JDBC_RESOURCE_TYPE);
261     }
262     // end IASRI 4734197
263

264
265     /**
266      * Return true if this resource is a URL object.
267      * @return true if the resource is a URL object, false otherwise.
268      */

269     public boolean isURLResource() {
270         return this.getType().equals(URL_RESOURCE_TYPE);
271     }
272
273     /**
274      * Return true if this resource is a JMS connection factory.
275      * @return true if the resource is a JMS connection factory, false
276      * otherwise.
277      */

278     public boolean isJMSConnectionFactory() {
279         String JavaDoc myType = this.getType();
280         return
281             ( myType.equals("javax.jms.QueueConnectionFactory") ||
282               myType.equals("javax.jms.TopicConnectionFactory") );
283     }
284     
285     /**
286      * Return the identity used to authorize this resource.
287      * @return the principal.
288      */

289     public ResourcePrincipal getResourcePrincipal() {
290     return this.resourcePrincipal;
291     }
292     
293     /**
294      * Sets the identity used to authorize this resource.
295      * @param the principal.
296      */

297     public void setResourcePrincipal(ResourcePrincipal resourcePrincipal) {
298     this.resourcePrincipal = resourcePrincipal;
299     }
300     
301     /**
302      * Sets the mail configuration information for this reference.
303      * @param the mail configuration object.
304      */

305     public void setMailConfiguration(MailConfiguration mailConfiguration) {
306     this.mailConfiguration = mailConfiguration;
307     }
308     
309     /**
310      * Add a new runtime property to this cmp resource
311      */

312      public void addProperty(NameValuePairDescriptor newProp) {
313      if (runtimeProps==null) {
314          runtimeProps = new ArrayList();
315      }
316      runtimeProps.add(newProp);
317      }
318      
319      /**
320       * @return the runtime properties for this cmp resource
321       */

322      public Iterator getProperties() {
323      if (runtimeProps==null) {
324          return null;
325      }
326      return runtimeProps.iterator();
327      }
328     
329     /**
330      * Return the mail configuration details of thsi resource or null.
331      * @return the mail configuration object.
332      */

333     public MailConfiguration getMailConfiguration() {
334     return this.mailConfiguration;
335     }
336     
337     /**
338      * @return true if automatic creation of tables for the CMP Beans is
339      * done at deployment time
340      */

341     public boolean isCreateTablesAtDeploy() {
342         return createTablesAtDeploy;
343     }
344     
345     /**
346      * Sets whether if automatic creation of tables for the CMP Beans is
347      * done at deployment time
348      */

349     public void setCreateTablesAtDeploy(boolean createTablesAtDeploy) {
350         this.createTablesAtDeploy = createTablesAtDeploy;
351     }
352     
353     /**
354      * @return true if automatic creation of tables for the CMP Beans is
355      * done at deployment time
356      */

357     public boolean isDropTablesAtUndeploy() {
358         return dropTablesAtUndeploy;
359     }
360     
361     /**
362      * Sets whether if automatic creation of tables for the CMP Beans is
363      * done at deployment time
364      */

365     public void setDropTablesAtUndeploy(boolean dropTablesAtUndeploy) {
366         this.dropTablesAtUndeploy = dropTablesAtUndeploy;
367     }
368     
369     /**
370      * @return the database vendor name
371      */

372     public String JavaDoc getDatabaseVendorName() {
373         return databaseVendorName;
374     }
375     
376     /**
377      * Sets the database vendor name
378      */

379     public void setDatabaseVendorName(String JavaDoc vendorName) {
380         this.databaseVendorName = vendorName;
381     }
382     
383     /**
384      * @return the override properties for the schema generation
385      */

386     public Properties getSchemaGeneratorProperties() {
387         return schemaGeneratorProperties;
388     }
389     
390     /**
391      * Sets the override properties for the schema generation
392      */

393     public void setSchemaGeneratorProperties(Properties props) {
394         schemaGeneratorProperties = props;
395     }
396     
397     /**
398      * Equality on name.
399      */

400     public boolean equals(Object JavaDoc object) {
401     if (object instanceof ResourceReference) {
402         ResourceReference resourceReference = (ResourceReference) object;
403         return resourceReference.getName().equals(this.getName());
404     }
405     return false;
406     }
407     
408     /**
409      * Returns a formatted string representing my state.
410      */

411     public void print(StringBuffer JavaDoc toStringBuffer) {
412         StringBuffer JavaDoc sb = toStringBuffer;
413         sb.append("Res-Ref-Env-Property: ");
414         sb.append(super.getName());
415         sb.append("@");
416         sb.append(getType());
417         sb.append("@");
418         sb.append(getDescription());
419     if (this.isResolved()) {
420         sb.append(" resolved as: jndi: ");
421             sb.append(getJndiName());
422             sb.append("@res principal: ");
423             sb.append(getResourcePrincipal());
424             sb.append("@mail: ");
425             sb.append(getMailConfiguration());
426     }
427         if (runtimeProps!=null) {
428             for (Iterator itr = runtimeProps.iterator();itr.hasNext();) {
429                 sb.append("\nPropery : ");
430                 sb.append(itr.next());
431             }
432         } else {
433             sb.append("\nNo Runtime properties");
434         }
435         sb.append("\nDatabase Vendor : " + databaseVendorName);
436         sb.append("\nCreate Tables at Deploy : " + createTablesAtDeploy);
437         sb.append("\nDelete Tables at Undeploy : " + dropTablesAtUndeploy);
438         
439         if (schemaGeneratorProperties!=null) {
440             sb.append("\nSchema Generator Properties : ");
441             sb.append(schemaGeneratorProperties);
442         }
443                 
444     }
445    //START OF IASRI 4633229
446
/**
447      * Return true if this resource is a CCI connection factory.
448      * @return true if the resource is a CCI connection factory, false
449      * otherwise.
450      */

451     public boolean isResourceConnectionFactory () {
452         return this.getType().equals(CONNECTOR_RESOURCE_TYPE);
453     }
454     //END OF IASRI 4633229
455

456     // START OF IASRI 4718559, 4729298
457
/**
458     ** checks the given class type. throws an IllegalArgumentException
459     ** if bounds checking
460     ** if the class of type "type" does not exist
461     */

462     public void checkType() {
463     if (rType != null) {
464         Class JavaDoc typeClass = null;
465         // is it loadable ?
466
try {
467             // Bug fix 4850684: for resource-refs that are user-defined classes,
468
// the classloader used to load them cannot be the one associated
469
// with the application deployed, since the classloader instance
470
// would have no idea about classes not included in app
471
// for e.g connector module with res-ref that points to the
472
// ConnectionFactory class of a resource adapter
473

474             typeClass = Class.forName(rType, true,
475                       Thread.currentThread().getContextClassLoader());
476         
477         } catch (Throwable JavaDoc t) {
478             if (this.isBoundsChecking()) {
479             throw new IllegalArgumentException JavaDoc(localStrings.getLocalString(
480           "enterprise.deployment.exceptiontypenotallowedpropertytype",
481                   "{0} is not an allowed property value type",
482                   new Object JavaDoc[] {rType}));
483         } else {
484             return;
485         }
486         }
487         }
488     }
489     // END OF IASRI 4718559, 4729298
490

491 }
492
Popular Tags