KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployapi > config > SunConfigBean


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 package com.sun.enterprise.deployapi.config;
24
25 import java.util.*;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.beans.PropertyChangeSupport JavaDoc;
28
29 import javax.enterprise.deploy.model.DDBean JavaDoc;
30 import javax.enterprise.deploy.model.XpathEvent JavaDoc;
31 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
32 import javax.enterprise.deploy.spi.DConfigBeanRoot JavaDoc;
33 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
34 import javax.enterprise.deploy.model.XpathListener JavaDoc;
35
36 import com.sun.enterprise.util.LocalStringManagerImpl;
37
38 /**
39  * Superclass for all the ConfigBeans. ConfigBeans are organized with
40  * a parent-child relationship. The parent defines xPaths and their
41  * mapping to child beans and return this mapping from the
42  * getXPathToBeanMapping method. Each bean is associated with a
43  * DOL descriptor (virtual field for this class) accessible through the
44  * getDescriptor() call.
45  *
46  * @author Jerome Dochez
47  */

48 public abstract class SunConfigBean implements DConfigBean JavaDoc, XpathListener JavaDoc {
49     
50     // Parent of this config bean. All config beans but root beans
51
// have a parent config bean
52
private SunConfigBean parent=null;
53     
54     // DDBean associated with this config beans used to get the
55
// xpath this config beans is representing. We do not use
56
// the DDBean to extract the Standard DDs since it is initialized
57
// at the DConfigBeanRoot level
58
protected DDBean JavaDoc ddBean;
59     
60     // propery change event support
61
protected PropertyChangeSupport JavaDoc propertyChange = new PropertyChangeSupport JavaDoc(this);
62     
63     protected static LocalStringManagerImpl localStrings =
64       new LocalStringManagerImpl(SunConfigBean.class);
65     
66     /**
67      * Return the JavaBean containing the server-specific deployment
68      * configuration information based upon the XML data provided
69      * by the DDBean.
70      *
71      * @return The DConfigBean to display the server-specific properties
72      * for the standard bean.
73      * @param bean The DDBean containing the XML data to be
74      * evaluated.
75      * @throws ConfigurationException reports errors in generating
76      * a configuration bean. This DDBean is considered
77      * undeployable to this server until this exception
78      * is resolved.
79      * A suitably descriptive message is required so the user
80      * can diagnose the error.
81      */

82     public DConfigBean JavaDoc getDConfigBean(DDBean JavaDoc bean)
83                throws ConfigurationException JavaDoc {
84                    
85         Map mapping = getXPathToBeanMapping();
86         if (mapping==null) {
87             return null;
88         }
89         if (mapping.containsKey(bean.getXpath())) {
90             Class JavaDoc c = (Class JavaDoc) mapping.get(bean.getXpath());
91             try {
92                 Object JavaDoc o = c.newInstance();
93                 if (o instanceof SunConfigBean) {
94                     SunConfigBean child = (SunConfigBean) o;
95                     child.setParent(this);
96                     child.setDDBean(bean);
97                     return child;
98                 }
99             } catch(Exception JavaDoc e) {
100                 e.printStackTrace();
101                 throw new ConfigurationException JavaDoc(e.getMessage());
102             }
103             
104         }
105         return null;
106     }
107     
108     /**
109     * Return the JavaBean containing the deployment
110     * descriptor XML text associated with this DConfigBean.
111     * @return The bean class containing the XML text for
112     * this DConfigBean.
113     */

114     public DDBean JavaDoc getDDBean() {
115         return ddBean;
116     }
117    
118     /**
119      * we are being set a new DDBean, we need to reevaluate
120      * ourself.
121      *
122      * @param DDBean is the new standard DDBean container
123      */

124     protected void setDDBean(DDBean JavaDoc ddBean) throws Exception JavaDoc {
125         this.ddBean = ddBean;
126         process();
127     }
128        
129     /**
130      * A notification that the DDBean provided in the
131      * event has changed and this bean or its child beans need
132      * to reevaluate themselves.
133      *
134      * @param event an event containing a reference to the
135      * DDBean which has changed.
136      */

137     public void notifyDDChange(XpathEvent JavaDoc xpathEvent) {
138     }
139     
140     /**
141      * Remove a child DConfigBean from this bean.
142      *
143      * @param bean The child DConfigBean to be removed.
144      * @throws BeanNotFoundException the bean provided
145      * is not in the child list of this bean.
146      */

147     public void removeDConfigBean(DConfigBean JavaDoc dConfigBean) throws javax.enterprise.deploy.spi.exceptions.BeanNotFoundException JavaDoc {
148     }
149     
150     /**
151      * Register a property listener for this bean.
152      * @param pcl PropertyChangeListener to add
153      */

154     public void addPropertyChangeListener(PropertyChangeListener JavaDoc propertyChangeListener) {
155     }
156     
157    /**
158     * Unregister a property listener for this bean.
159     * @param pcl Listener to remove.
160     */

161     public void removePropertyChangeListener(PropertyChangeListener JavaDoc propertyChangeListener) {
162     }
163     
164     /**
165      * Notification of change from the standard DDBean
166      *
167      * @param the change event
168      */

169     public void fireXpathEvent(XpathEvent JavaDoc xpe) {
170         if (getParent()!=null) {
171             getParent().fireXpathEvent(xpe);
172             return;
173         }
174     }
175     
176     /**
177      * Return a list of XPaths designating the deployment descriptor
178      * information this DConfigBean requires.
179      *
180      * A given server vendor will need to specify some server-specific
181      * information. Each String returned by this method is an XPath
182      * describing a certain portion of the standard deployment descriptor
183      * for which there is corresponding server-specific configuration.
184      *
185      * @return a list of XPath Strings representing XML data to be retrieved
186      * or 'null' if there are none.
187      */

188     public String JavaDoc[] getXpaths() {
189         Map mapping = getXPathToBeanMapping();
190         if (mapping==null) {
191             return null;
192         }
193         
194         Set keys = mapping.keySet();
195         String JavaDoc[] xPaths = new String JavaDoc[keys.size()];
196         int i=0;
197         for (Iterator itr=keys.iterator(); itr.hasNext();) {
198             String JavaDoc s = (String JavaDoc) itr.next();
199             xPaths[i++]=s;
200         }
201         return xPaths;
202     }
203     
204     
205     /*
206      * set the parent config bean for this config bean
207      */

208     protected void setParent(SunConfigBean parent) {
209         this.parent = parent;
210     }
211     
212     /*
213      * @return the parent for this config bean
214      */

215     public SunConfigBean getParent() {
216         return parent;
217     }
218     
219     
220     /**
221      * Convenience method extract node value from the passed xml fragment.
222      *
223      * @param key the xml tag name
224      * @param xml fragment to extract the tag value
225      * @return the xml tag value
226      */

227     public static String JavaDoc extractTextFromXML(String JavaDoc key, String JavaDoc xmlFragment) {
228        // should we use a parser... seems heavy, we'll see if string parsing is enough
229

230        // pass opening tag
231
String JavaDoc openingTag = "<" + key + ">";
232        xmlFragment = xmlFragment.substring(xmlFragment.indexOf(openingTag)+openingTag.length());
233        return xmlFragment.substring(0, xmlFragment.indexOf("</" + key + ">"));
234     }
235         
236     /**
237      * @return the mapping from xpaths to child config beans where
238      * the map keys are the xpaths and the values are the class object
239      * for the child config beans
240      */

241     protected abstract Map getXPathToBeanMapping();
242     
243     /**
244      * evaluate a standard bean
245      */

246     protected abstract void process() throws Exception JavaDoc;
247     
248     /**
249      * @return the associated DOL Descriptor for this config beans
250      */

251     public abstract Object JavaDoc getDescriptor();
252     
253     /**
254      * @return the ConfigBeanRoot for this config bean
255      */

256     protected DConfigBeanRoot JavaDoc getDConfigBeanRoot() {
257         if (parent!=null) {
258             return parent.getDConfigBeanRoot();
259         }
260         return null;
261     }
262         
263     /**
264      * @return a meaningful string about myself
265      */

266     public String JavaDoc toString() {
267         String JavaDoc s = "DConfigBean";
268         s = s + "\nDConfigBeanRoot = " + getDConfigBeanRoot();
269         s = s + "\nParent = " + parent;
270         s = s + "\nXPath = " + ddBean.getXpath();
271         return s;
272     }
273 }
274
Popular Tags