KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > AttrUniqueJNDI


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.config.serverbeans.validation;
25
26 import java.util.Vector JavaDoc;
27 import java.util.logging.Level JavaDoc;
28
29 import com.sun.enterprise.config.ConfigContext;
30 import com.sun.enterprise.config.ConfigException;
31 import com.sun.enterprise.config.serverbeans.validation.tests.StaticTest;
32
33 import com.sun.enterprise.config.serverbeans.Domain;
34 import com.sun.enterprise.config.serverbeans.Resources;
35 import com.sun.enterprise.config.serverbeans.AdminObjectResource;
36 import com.sun.enterprise.config.serverbeans.ConnectorConnectionPool;
37 import com.sun.enterprise.config.serverbeans.CustomResource;
38 import com.sun.enterprise.config.serverbeans.ConnectorResource;
39 import com.sun.enterprise.config.serverbeans.ExternalJndiResource;
40 import com.sun.enterprise.config.serverbeans.JdbcConnectionPool;
41 import com.sun.enterprise.config.serverbeans.JdbcResource;
42 import com.sun.enterprise.config.serverbeans.MailResource;
43 import com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource;
44 import com.sun.enterprise.config.serverbeans.ResourceAdapterConfig;
45
46 /**
47     Class which contains Meta data for all types of attributes which is present in Validation Descriptor
48  * XML File
49  *
50  * Sample
51  * <attribute name=<Name> type="address" />
52  * <attribute name=<Name> type="integer" range="low,high" />
53  * <attribute name=<Name> type="string" max-length="length" />
54     
55     @author Srinivas Krishnan
56     @version 2.0
57 */

58
59 /* Class for attribute type Resource (to check uniqueness of jndiname across resources) */
60
61 public class AttrUniqueJNDI extends AttrType {
62     
63     public AttrUniqueJNDI(String JavaDoc name, String JavaDoc type, boolean optional) {
64         super(name,type, optional);
65     }
66     
67     public void validate(Object JavaDoc o, ValidationContext valCtx) {
68         super.validate(o, valCtx); // call to common validator first
69
String JavaDoc jndiName = null;
70         String JavaDoc choice = valCtx.choice;
71         boolean flag = false;
72         if(o == null || o.equals(""))
73             return;
74         jndiName = (String JavaDoc)o;
75         if(choice.equals("ADD") && isResourceBeingUsed(valCtx.context, jndiName)){
76             valCtx.result.failed(
77                 valCtx.smh.getLocalString(
78                     getClass().getName() + ".jndiNameBeingUsed",
79                     "Attribute({0}={1}) : name already used by some other resource",
80                     new Object JavaDoc[]{valCtx.attrName,jndiName}));
81         }
82             // apostrophes aren't allowed in jndi names - we catch
83
// this by hard coding this check because this type
84
// (AttrUniqueJNDI) doesn't take a regular expression.
85
// This is an implementation limitation and is caused by
86
// the way we handle xpath expressions - we use the
87
// apostrophe to delimit the primary key names of
88
// elements, and we don't escape the names when we get
89
// them from the user. Therefore if the name were allowed
90
// to contain an apostrophe the name would be incorrectly
91
// split into two parts. Bug 6184639 details this problem.
92
if (jndiName.indexOf("'") != -1){
93             valCtx.result.failed(
94                 valCtx.smh.getLocalString(
95                     getClass().getName() + ".invalidJndiName",
96                     "Apostrophe not allowed in jndi name: {0}",
97                     new Object JavaDoc[]{jndiName}));
98         }
99         
100     }
101     
102     public boolean isResourceBeingUsed(ConfigContext context, String JavaDoc jndi) {
103         
104         int count=0;
105         try {
106             Resources resources = ((Domain)context.getRootConfigBean()).getResources();
107             
108             AdminObjectResource[] admin = resources.getAdminObjectResource();
109             ConnectorResource[] connres = resources.getConnectorResource();
110             CustomResource[] custom = resources.getCustomResource();
111             ExternalJndiResource[] external = resources.getExternalJndiResource();
112             JdbcResource[] jdbcres = resources.getJdbcResource();
113             MailResource[] mailres = resources.getMailResource();
114             PersistenceManagerFactoryResource[] pers = resources.getPersistenceManagerFactoryResource();
115             
116             for(int i=0;i<admin.length;i++) {
117                 if(jndi.equals(admin[i].getJndiName()))
118                     return true;
119             }
120             for(int i=0;i<connres.length;i++) {
121                 if(jndi.equals(connres[i].getJndiName()))
122                     return true;
123             }
124             
125             for(int i=0;i<custom.length;i++) {
126                 if(jndi.equals(custom[i].getJndiName()))
127                     return true;
128             }
129             
130             for(int i=0;i<external.length;i++) {
131                 if(jndi.equals(external[i].getJndiName()))
132                     return true;
133             }
134             
135             for(int i=0;i<jdbcres.length;i++) {
136                 if(jndi.equals(jdbcres[i].getJndiName()))
137                     return true;
138             }
139             
140             for(int i=0;i<mailres.length;i++) {
141                 if(jndi.equals(mailres[i].getJndiName()))
142                     return true;
143             }
144             
145             for(int i=0;i<pers.length;i++) {
146                 if(jndi.equals(pers[i].getJndiName()))
147                     return true;
148             }
149             
150         } catch(ConfigException e) {
151             _logger.log(Level.FINE, "domainxmlverifier_error", e);
152         }
153         return false;
154     }
155     
156 }
157
Popular Tags