KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > Resource


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.resource;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Properties JavaDoc;
28 import javax.management.AttributeList JavaDoc;
29 import javax.management.Attribute JavaDoc;
30
31 import com.sun.enterprise.config.serverbeans.ServerTags;
32
33 import static com.sun.enterprise.resource.ResourceConstants.*;
34
35 /**
36  * Class which represents the Resource.
37  */

38 public class Resource
39 {
40     protected static final String JavaDoc CUSTOM_RESOURCE = ServerTags.CUSTOM_RESOURCE;
41     protected static final String JavaDoc JDBC_CONNECTION_POOL = ServerTags.JDBC_CONNECTION_POOL;
42     protected static final String JavaDoc CONNECTOR_RESOURCE = ServerTags.CONNECTOR_RESOURCE;
43     protected static final String JavaDoc ADMIN_OBJECT_RESOURCE = ServerTags.ADMIN_OBJECT_RESOURCE;
44     protected static final String JavaDoc JDBC_RESOURCE = ServerTags.JDBC_RESOURCE;
45     protected static final String JavaDoc RESOURCE_ADAPTER_CONFIG = ServerTags.RESOURCE_ADAPTER_CONFIG;
46     protected static final String JavaDoc MAIL_RESOURCE = ServerTags.MAIL_RESOURCE;
47     protected static final String JavaDoc EXTERNAL_JNDI_RESOURCE = ServerTags.EXTERNAL_JNDI_RESOURCE;
48     protected static final String JavaDoc CONNECTOR_CONNECTION_POOL = ServerTags.CONNECTOR_CONNECTION_POOL;
49     protected static final String JavaDoc PERSISTENCE_MANAGER_FACTORY_RESOURCE = ServerTags.PERSISTENCE_MANAGER_FACTORY_RESOURCE;
50     protected static final String JavaDoc CONNECTOR_SECURITY_MAP = ServerTags.SECURITY_MAP;
51     
52     private String JavaDoc resType;
53     private AttributeList JavaDoc attrList = new AttributeList JavaDoc();
54     private Properties JavaDoc props = new Properties JavaDoc();
55     private String JavaDoc sDescription = null;
56
57     public Resource()
58     {
59     }
60     
61     public Resource(String JavaDoc type)
62     {
63        resType = type;
64     }
65     
66     public String JavaDoc getType()
67     {
68         return resType;
69     }
70     
71     public void setType(String JavaDoc type)
72     {
73         resType = type;
74     }
75     
76     public AttributeList JavaDoc getAttributes()
77     {
78         return attrList;
79     }
80     
81     public void setAttribute(String JavaDoc name, String JavaDoc value)
82     {
83         attrList.add(new Attribute JavaDoc(name, value));
84     }
85     
86     public void setAttribute(String JavaDoc name, String JavaDoc[] value)
87     {
88         attrList.add(new Attribute JavaDoc(name, value));
89     }
90
91     public void setDescription(String JavaDoc sDescription)
92     {
93         this.sDescription = sDescription;
94     }
95     
96     public String JavaDoc getDescription()
97     {
98        return sDescription;
99     }
100     
101     public void setProperty(String JavaDoc name, String JavaDoc value)
102     {
103         props.setProperty(name, value);
104     }
105
106     public void setProperty(String JavaDoc name, String JavaDoc value, String JavaDoc desc)
107     {
108         // TO DO:
109
}
110
111     public Properties JavaDoc getProperties()
112     {
113         return props;
114     }
115     
116     //Used to figure out duplicates in a List<Resource>
117
@Override JavaDoc
118     public boolean equals(Object JavaDoc obj) {
119         if (this == obj) return true;
120         if ( !(obj instanceof Resource) ) return false;
121         
122         Resource r = (Resource)obj;
123         return r.getType().equals(this.getType()) &&
124                 r.getDescription().equals(this.getDescription()) &&
125                 r.getProperties().equals(this.getProperties()) &&
126                 r.getAttributes().equals(this.getAttributes());
127     }
128     
129     //when a class overrides equals, override hashCode as well.
130
@Override JavaDoc
131     public int hashCode() {
132         return this.getAttributes().hashCode() +
133         this.getProperties().hashCode() +
134         this.getType().hashCode() +
135         this.getDescription().hashCode();
136     }
137     
138     //Used to figure out conflicts in a List<Resource>
139
//A Resource is said to be in conflict with another Resource if the two
140
//Resources have the same Identity [attributes that uniquely identify a Resource]
141
//but different properties
142
public boolean isAConflict(Resource r) {
143         //If the two resources are equal [duplicates], then there is no
144
//conflict
145
if (r.equals(this)) return true;
146         
147         //If the two resource have the same identity
148
if (hasSameIdentity(r)) {
149             //and the properties or attributes of the two resources
150
//are different, then we have a conflict
151
boolean propsNotEqual = (!(this.getProperties().equals(
152                                                     r.getProperties())));
153             boolean attrsNotEqual = (!(this.getAttributes().equals(
154                                                     r.getAttributes())));
155             if (propsNotEqual || attrsNotEqual) return true;
156         }
157         
158         return false;
159     }
160
161     /**
162      * Checks if the specified resource has the same identity as
163      * this resource.
164      */

165     private boolean hasSameIdentity(Resource r) {
166         //For two resources to have the same identity, atleast their types should match
167
if (r.getType() != this.getType()) {
168             return false;
169         }
170         String JavaDoc rType = r.getType();
171         
172         //For all resources, their identity is their "attributes"
173
if (rType.equals(CUSTOM_RESOURCE)|| rType.equals(EXTERNAL_JNDI_RESOURCE)
174              || rType.equals(JDBC_RESOURCE)|| rType.equals(PERSISTENCE_MANAGER_FACTORY_RESOURCE)
175              || rType.equals(CONNECTOR_RESOURCE)|| rType.equals(ADMIN_OBJECT_RESOURCE)) {
176             return r.getAttributes().equals(this.getAttributes());
177         }
178         
179         //For pools/mail resource and RA config, the identity is limited to
180
//a few attributes.
181
if (rType.equals(JDBC_CONNECTION_POOL)) {
182             return isEqualAttribute(r, CONNECTION_POOL_NAME) &&
183             isEqualAttribute(r, DATASOURCE_CLASS)
184             && isEqualAttribute(r, RES_TYPE);
185         }
186         
187         if (rType.equals(CONNECTOR_CONNECTION_POOL)) {
188             return isEqualAttribute(r, CONNECTION_POOL_NAME) &&
189             isEqualAttribute(r, RESOURCE_ADAPTER_CONFIG_NAME)
190             && isEqualAttribute(r, CONN_DEF_NAME);
191         }
192         
193         if (rType.equals(MAIL_RESOURCE)) {
194             return isEqualAttribute(r, JNDI_NAME);
195         }
196         
197         if (rType.equals(RESOURCE_ADAPTER_CONFIG)) {
198             return isEqualAttribute(r, RES_ADAPTER_NAME) &&
199             isEqualAttribute(r, RES_ADAPTER_CONFIG);
200         }
201         
202         return false;
203     }
204     
205     /**
206      * Compares the attribute with the specified name
207      * in this resource with the passed in resource and checks
208      * if they are <code>equal</code>
209      */

210     private boolean isEqualAttribute(Resource r, String JavaDoc name) {
211         return (getAttribute(r, name).equals(getAttribute(this, name)));
212     }
213     
214     /**
215      * Utility method to get an <code>Attribute</code> of the given name
216      * in the specified resource
217      */

218     private Attribute JavaDoc getAttribute(Resource r, String JavaDoc name) {
219         for (Iterator JavaDoc<Attribute JavaDoc> iter = r.getAttributes().iterator(); iter.hasNext();) {
220             Attribute JavaDoc elt = (Attribute JavaDoc) iter.next();
221             if (elt.getName().equals(name)) return elt;
222         }
223         return null;
224     }
225     
226 }
227
Popular Tags