KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > common > EnclosingBean


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  * Superclass for most s2b beans that provides useful methods for creating and finding beans
22  * in bean graph.
23  *
24  * @author Milan Kuchtiak
25  */

26
27 package org.netbeans.modules.j2ee.dd.impl.common;
28
29 import java.io.OutputStream JavaDoc;
30 import org.netbeans.modules.schema2beans.Version;
31 import org.netbeans.modules.schema2beans.BaseBean;
32 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean;
33 import org.netbeans.modules.j2ee.dd.api.common.NameAlreadyUsedException;
34 import org.netbeans.modules.j2ee.dd.api.common.CreateCapability;
35 import org.netbeans.modules.j2ee.dd.api.common.FindCapability;
36 import org.netbeans.modules.j2ee.dd.api.common.RootInterface;
37 import org.openide.filesystems.FileLock;
38
39 public abstract class EnclosingBean extends BaseBean implements CommonDDBean, CreateCapability, FindCapability {
40     
41     private Object JavaDoc original = this;
42     
43     /** Creates a new instance of EnclosingBean */
44     public EnclosingBean(java.util.Vector JavaDoc comps, Version version) {
45         super(comps, version);
46     }
47     
48     /**
49      * Method is looking for the nested bean according to the specified property and value
50      *
51      * @param beanName e.g. "Servlet" or "ResourceRef"
52      * @param propertyName e.g. "ServletName" or ResourceRefName"
53      * @param value specific propertyName value e.g. "ControllerServlet" or "jdbc/EmployeeAppDb"
54      * @return Bean satisfying the parameter values or null if not found
55      */

56     public CommonDDBean findBeanByName(String JavaDoc beanName, String JavaDoc propertyName, String JavaDoc value) {
57         return (CommonDDBean)CommonDDAccess.findBeanByName(this, beanName, propertyName, value);
58     }
59     
60     /**
61      * An empty (not bound to schema2beans graph) bean is created corresponding to beanName
62      * regardless the Servlet Spec. version
63      * @param beanName bean name e.g. Servlet
64      * @return CommonDDBean corresponding to beanName value
65      */

66     public CommonDDBean createBean(String JavaDoc beanName) throws ClassNotFoundException JavaDoc {
67         return (CommonDDBean)CommonDDAccess.newBean(this, beanName, getPackagePostfix());
68     }
69     
70     private String JavaDoc getPackagePostfix() {
71         String JavaDoc pack = getClass().getPackage().getName();
72 // if (pack.endsWith(CommonDDAccess.SERVLET_2_4)) return CommonDDAccess.SERVLET_2_4;
73
// else if (pack.endsWith(CommonDDAccess.SERVLET_2_3)) return CommonDDAccess.SERVLET_2_3;
74
return pack;
75     }
76     
77     /**
78      * Writes the current schema2beans graph as an XML document into the output
79      * stream of given <code>fo</code>.
80      * @see BaseBean#write(OutputStream)
81      */

82     public void write(org.openide.filesystems.FileObject fo) throws java.io.IOException JavaDoc {
83         // TODO: need to be implemented with Dialog opened when the file object is locked
84
FileLock lock = fo.lock();
85         try {
86             OutputStream JavaDoc os = fo.getOutputStream(lock);
87             try {
88                 write(os);
89             } finally {
90                 os.close();
91             }
92         } finally {
93             lock.releaseLock();
94         }
95     }
96     
97     public CommonDDBean addBean(String JavaDoc beanName, String JavaDoc[] propertyNames, Object JavaDoc[] propertyValues, String JavaDoc keyProperty) throws ClassNotFoundException JavaDoc, NameAlreadyUsedException {
98         if (keyProperty!=null) {
99             Object JavaDoc keyValue = null;
100             if (propertyNames!=null)
101                 for (int i=0;i<propertyNames.length;i++) {
102                 if (keyProperty.equals(propertyNames[i])) {
103                     keyValue=propertyValues[i];
104                     break;
105                 }
106                 }
107             if (keyValue!=null && keyValue instanceof String JavaDoc) {
108                 if (findBeanByName(beanName, keyProperty,(String JavaDoc)keyValue)!=null) {
109                     throw new NameAlreadyUsedException(beanName, keyProperty, (String JavaDoc)keyValue);
110                 }
111             }
112         }
113         CommonDDBean newBean = createBean(beanName);
114         if (propertyNames!=null)
115             for (int i=0;i<propertyNames.length;i++) {
116             try {
117                 ((BaseBean)newBean).setValue(propertyNames[i],propertyValues[i]);
118             } catch (IndexOutOfBoundsException JavaDoc ex) {
119                 ((BaseBean)newBean).setValue(propertyNames[i],new Object JavaDoc[]{propertyValues[i]});
120             }
121             }
122         CommonDDAccess.addBean(this, newBean, beanName, getPackagePostfix());
123         return newBean;
124     }
125     
126     public CommonDDBean addBean(String JavaDoc beanName) throws ClassNotFoundException JavaDoc {
127         try {
128             return addBean(beanName,null,null,null);
129         } catch (NameAlreadyUsedException ex){}
130         return null;
131     }
132     
133     public Object JavaDoc getOriginal() {
134         return original;
135     }
136     
137     public Object JavaDoc clone() {
138         EnclosingBean enclosingBean = (EnclosingBean) super.clone();
139         enclosingBean.original = original;
140         return enclosingBean;
141     }
142     
143     public void merge(RootInterface root, int mode) {
144         this.merge((BaseBean)root,mode);
145     }
146     
147     public static final String JavaDoc ID = "Id"; // NOI18N
148

149     public void setId(String JavaDoc value) {
150         setAttributeValue(ID, value);
151     }
152     
153     public String JavaDoc getId() {
154         return getAttributeValue(ID);
155     }
156   
157 }
158
Popular Tags