KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > webservices > CommonDDAccess


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  * Methods for accessing schema2beans objects in a bean-independent way.
22  *
23  * @author Milan Kuchtiak
24  */

25 package org.netbeans.modules.j2ee.dd.impl.webservices;
26
27 import java.lang.reflect.*;
28 import org.openide.util.NbBundle;
29 import org.netbeans.modules.schema2beans.BaseBean;
30 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean;
31
32 /**
33  * Methods for accessing schema2beans objects in bean-independent and version-independent way.
34  *
35  * @author Milan Kuchtiak
36  */

37
38 public class CommonDDAccess {
39
40     public static final String JavaDoc WEBSERVICES_1_1 = "1_1"; //NOI18N
41

42     public static final String JavaDoc PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.webservices.model_"; //NOI18N
43
public static final String JavaDoc DOT = "."; //NOI18N
44

45     /**
46      * Return a new instance of the specified type
47      *
48      * @param parent parent bean
49      * @param beanName which bean to create
50      * @param version "2_3" or "2_4"
51      * @return BaseBean object e.g. Servlet
52      */

53
54     public static BaseBean newBean(CommonDDBean parent, String JavaDoc beanName, String JavaDoc version) throws ClassNotFoundException JavaDoc {
55     beanName = getImplementationBeanName(parent, beanName, version);
56     try {
57         Class JavaDoc beanClass = Class.forName(
58                 PACKAGE_PREFIX
59                 + version + DOT
60                 + beanName);
61         return (BaseBean) beanClass.newInstance();
62
63     } catch (Exception JavaDoc e) {
64             if (e instanceof ClassNotFoundException JavaDoc)
65                 throw (ClassNotFoundException JavaDoc)e;
66             else {
67                 // This is a programming error.
68
e.printStackTrace();
69                 throw new RuntimeException JavaDoc(
70                     NbBundle.getMessage(CommonDDAccess.class,
71                         "MSG_COMMONDDACCESS_ERROR", "newBean",
72                         ", version = " + version + ", beanName = " + beanName, e+ ": " +e.getMessage()));
73             }
74     }
75     }
76     
77     public static void addBean(CommonDDBean parent, CommonDDBean child, String JavaDoc beanName, String JavaDoc version) {
78     beanName = getImplementationBeanName(parent, beanName, version);
79     try {
80             Class JavaDoc p = parent.getClass();
81             Class JavaDoc ch = Class.forName("org.netbeans.modules.j2ee.dd.api.webservices."+beanName); //NOI18N
82
Method setter=null;
83             try {
84                 setter = p.getMethod("set" + beanName, new Class JavaDoc[]{ch}); //NOI18N
85
setter.invoke(parent, new Object JavaDoc[]{child});
86             } catch (NoSuchMethodException JavaDoc ex) {
87             }
88             if (setter==null) {
89                 setter = p.getMethod("add" + getNameForMethod(parent, beanName), new Class JavaDoc[]{ch}); //NOI18N
90
setter.invoke(parent, new Object JavaDoc[]{child});
91             }
92     } catch (Exception JavaDoc e) {
93             // This is a programming error.
94
e.printStackTrace();
95             throw new RuntimeException JavaDoc(
96                 NbBundle.getMessage(CommonDDAccess.class,
97                     "MSG_COMMONDDACCESS_ERROR", "addBean",
98                     ", version = " + version + ", beanName = " + beanName, e+ ": " +e.getMessage()));
99     }
100     }
101
102     /**
103      * Get a BaseBean object from parent BaseBean
104      *
105      * @param parent parent BaseBean
106      * @param beanProperty name of child's BaseBean object e.g. "Servlet"
107      * @param nameProperty name of property e.g. ServletName
108      * @param value e.g. "ControllerServlet"
109      */

110     public static BaseBean findBeanByName(BaseBean parent, String JavaDoc beanProperty, String JavaDoc nameProperty, String JavaDoc value) {
111     Class JavaDoc c = parent.getClass();
112     Method getter;
113     Object JavaDoc result;
114     try {
115         getter = c.getMethod("get" + getNameForMethod((CommonDDBean)parent,beanProperty), null); //NOI18N
116
result = getter.invoke(parent, null);
117         if (result == null) {
118         return null;
119         } else if (result instanceof BaseBean) {
120         return null;
121         } else {
122         BaseBean[] beans = (BaseBean[]) result;
123                 for (int i=0;i<beans.length;i++) {
124                     Class JavaDoc c1 = beans[i].getClass();
125                     Method getter1;
126                     Object JavaDoc result1;
127                     getter1 = c1.getMethod("get" + nameProperty, null); //NOI18N
128
result1 = getter1.invoke(beans[i], null);
129                     if (result1 instanceof String JavaDoc) {
130                         if (value.equals((String JavaDoc)result1)) {
131                             return beans[i];
132                         }
133                     }
134                 }
135                 return null;
136         }
137     } catch (Exception JavaDoc e) {
138         // This is a programming error
139
e.printStackTrace();
140         throw new RuntimeException JavaDoc(
141         NbBundle.getMessage(CommonDDAccess.class,
142             "MSG_COMMONDDACCESS_ERROR", "getBeanByName",
143             "parent = " + parent + ", beanProperty = " + beanProperty
144                     + ", nameProperty = " + nameProperty
145                     + ", value = " + value,
146             e+ ": " +e.getMessage()));
147     }
148     }
149     
150     /**
151      * Handle special cases of version differences
152      */

153     private static String JavaDoc getImplementationBeanName (CommonDDBean parent, String JavaDoc beanName, String JavaDoc version) {
154         if ("Webservices".equals(beanName)) {
155             return beanName;
156         } else {
157             return beanName + "Type"; //NOI18N
158
}
159     }
160     
161     /**
162      * Handle special cases of version differences
163      */

164     private static String JavaDoc getNameForMethod (CommonDDBean parent, String JavaDoc beanName) {
165         if ("Webservices".equals(beanName) ||
166             "WebserviceDescription".equals(beanName) ||
167             "PortComponent".equals(beanName)) {
168             return beanName;
169         } else {
170             return beanName + "Type"; //NOI18N
171
}
172     }
173 }
174
Popular Tags