KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > dd > impl > common > 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.common;
26
27 import java.lang.reflect.*;
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
31 import org.openide.util.NbBundle;
32 import org.netbeans.modules.schema2beans.BaseBean;
33 import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean;
34
35 /**
36  * Methods for accessing schema2beans objects in bean-independent and version-independent way.
37  *
38  * @author Milan Kuchtiak
39  */

40
41 public class CommonDDAccess {
42
43     public static final String JavaDoc DOT = "."; //NOI18N
44

45     public static final String JavaDoc COMMON_API = "org.netbeans.modules.j2ee.dd.api.common."; //NOI18N
46

47     public static final String JavaDoc SERVLET_2_3 = "2_3"; //NOI18N
48
public static final String JavaDoc SERVLET_2_4 = "2_4"; //NOI18N
49
public static final String JavaDoc WEB_API = "org.netbeans.modules.j2ee.dd.api.web."; //NOI18N
50
public static final String JavaDoc WEB_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.web.model_"; //NOI18N
51

52     public static final String JavaDoc APP_1_3 = "1_3"; //NOI18N
53
public static final String JavaDoc APP_1_4 = "1_4"; //NOI18N
54
public static final String JavaDoc APP_API = "org.netbeans.modules.j2ee.dd.api.application."; //NOI18N
55
public static final String JavaDoc APP_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.application.model_"; //NOI18N
56

57     public static final String JavaDoc EJB_2_0 = "2_0"; //NOI18N
58
public static final String JavaDoc EJB_2_1 = "2_1"; //NOI18N
59
public static final String JavaDoc EJB_1_1 = "1_1"; //NOI18N
60
public static final String JavaDoc EJB_API = "org.netbeans.modules.j2ee.dd.api.ejb."; //NOI18N
61
public static final String JavaDoc EJB_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.ejb.model_"; //NOI18N
62

63     private static Set JavaDoc COMMON_BEANS = new HashSet JavaDoc ();
64     static {
65         COMMON_BEANS.add("Icon"); //NOI18N
66
COMMON_BEANS.add("InitParam"); //NOI18N
67
COMMON_BEANS.add("EnvEntry"); //NOI18N
68
COMMON_BEANS.add("EjbRef"); //NOI18N
69
COMMON_BEANS.add("EjbLocalRef"); //NOI18N
70
COMMON_BEANS.add("ResourceRef"); //NOI18N
71
COMMON_BEANS.add("ResourceEnvRef"); //NOI18N
72
COMMON_BEANS.add("ServiceRef"); //NOI18N
73
COMMON_BEANS.add("Handler"); //NOI18N
74
COMMON_BEANS.add("PortComponentRef"); //NOI18N
75
COMMON_BEANS.add("MessageDestination"); //NOI18N
76
COMMON_BEANS.add("MessageDestinationRef"); //NOI18N
77
COMMON_BEANS.add("SecurityRole"); //NOI18N
78
COMMON_BEANS.add("SecurityRoleRef"); //NOI18N
79
}
80     
81     /**
82      * Return a new instance of the specified type
83      *
84      * @param parent parent bean
85      * @param beanName which bean to create
86      * @param pkgName implementation package name
87      * @return BaseBean object
88      */

89
90    public static BaseBean newBean(CommonDDBean parent, String JavaDoc beanName, String JavaDoc pkgName) throws ClassNotFoundException JavaDoc {
91         beanName = getImplementationBeanName(parent, beanName, pkgName);
92         try {
93         Class JavaDoc beanClass = Class.forName(
94                 pkgName
95                                 + DOT
96                 + beanName);
97         return (BaseBean) beanClass.newInstance();
98
99     } catch (Exception JavaDoc e) {
100             if (e instanceof ClassNotFoundException JavaDoc)
101                 throw (ClassNotFoundException JavaDoc)e;
102             else {
103                 // This is a programming error.
104
e.printStackTrace();
105                 throw new RuntimeException JavaDoc(
106                     NbBundle.getMessage(CommonDDAccess.class,
107                         "MSG_COMMONDDACCESS_ERROR", "newBean",
108                         ", package = " + pkgName + ", beanName = " + beanName, e+ ": " +e.getMessage()));
109             }
110     }
111    }
112     
113        
114    public static void addBean(CommonDDBean parent, CommonDDBean child, String JavaDoc beanName, String JavaDoc pkgName) {
115     beanName = getImplementationBeanName(parent, beanName, pkgName);
116         String JavaDoc apiPrefix = getAPIPrefix(beanName, pkgName);
117     try {
118             Class JavaDoc p = parent.getClass();
119             Class JavaDoc ch = Class.forName(apiPrefix + beanName); //NOI18N
120
Method setter=null;
121             try {
122                 setter = p.getMethod("set" + beanName, new Class JavaDoc[]{ch}); //NOI18N
123
setter.invoke(parent, new Object JavaDoc[]{child});
124             } catch (NoSuchMethodException JavaDoc ex) {
125             }
126             if (setter==null) {
127                 setter = p.getMethod("add" + getNameForMethod(parent, beanName), new Class JavaDoc[]{ch}); //NOI18N
128
setter.invoke(parent, new Object JavaDoc[]{child});
129             }
130     } catch (Exception JavaDoc e) {
131             // This is a programming error.
132
e.printStackTrace();
133             throw new RuntimeException JavaDoc(
134                 NbBundle.getMessage(CommonDDAccess.class,
135                     "MSG_COMMONDDACCESS_ERROR", "addBean",
136                     ", package = " + pkgName + ", beanName = " + beanName, e+ ": " +e.getMessage()));
137     }
138     }
139     
140    
141    /**
142      * Handle special cases of version differences
143      */

144     private static String JavaDoc getImplementationBeanName (CommonDDBean parent, String JavaDoc beanName, String JavaDoc pkgName) {
145     if (pkgName.equals(WEB_PACKAGE_PREFIX + SERVLET_2_3)) {
146             if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; //NOI18N
147
else return beanName;
148         } else if (beanName.equals("Session") || beanName.equals("Entity") || beanName.equals("MessageDriven")) { //NOI18N
149
return beanName + "Bean"; //NOI18N
150
} else
151             return beanName;
152     }
153     
154     private static String JavaDoc getAPIPrefix(String JavaDoc beanName, String JavaDoc pkgName){
155         if (COMMON_BEANS.contains(beanName))
156             return COMMON_API;
157         if (pkgName.startsWith(EJB_PACKAGE_PREFIX))
158             return EJB_API;
159         else if (pkgName.startsWith(WEB_PACKAGE_PREFIX))
160             return WEB_API;
161         else if (pkgName.startsWith(APP_PACKAGE_PREFIX))
162             return APP_API;
163         assert false : "Invalid package prefix:" + pkgName;
164         return ""; //NOI18N
165
}
166     
167     /**
168      * Get a BaseBean object from parent BaseBean
169      *
170      * @param parent parent BaseBean
171      * @param beanProperty name of child's BaseBean object e.g. "Servlet"
172      * @param nameProperty name of property e.g. ServletName
173      * @param value e.g. "ControllerServlet"
174      */

175     public static BaseBean findBeanByName(BaseBean parent, String JavaDoc beanProperty, String JavaDoc nameProperty, String JavaDoc value) {
176     Class JavaDoc c = parent.getClass();
177     Method getter;
178     Object JavaDoc result;
179     try {
180         getter = c.getMethod("get" + getNameForMethod((CommonDDBean)parent,beanProperty), null); //NOI18N
181
result = getter.invoke(parent, null);
182         if (result == null) {
183         return null;
184         } else if (result instanceof BaseBean) {
185         return null;
186         } else {
187         BaseBean[] beans = (BaseBean[]) result;
188                 for (int i=0;i<beans.length;i++) {
189                     Class JavaDoc c1 = beans[i].getClass();
190                     Method getter1;
191                     Object JavaDoc result1;
192                     getter1 = c1.getMethod("get" + nameProperty, null); //NOI18N
193
result1 = getter1.invoke(beans[i], null);
194                     if (result1 instanceof String JavaDoc) {
195                         if (value.equals((String JavaDoc)result1)) {
196                             return beans[i];
197                         }
198                     }
199                 }
200                 return null;
201         }
202     } catch (Exception JavaDoc e) {
203         // This is a programming error
204
e.printStackTrace();
205         throw new RuntimeException JavaDoc(
206         NbBundle.getMessage(CommonDDAccess.class,
207             "MSG_COMMONDDACCESS_ERROR", "getBeanByName",
208             "parent = " + parent + ", beanProperty = " + beanProperty
209                     + ", nameProperty = " + nameProperty
210                     + ", value = " + value,
211             e+ ": " +e.getMessage()));
212     }
213     }
214
215     /**
216      * Handle special cases of version differences
217      */

218     private static String JavaDoc getNameForMethod (CommonDDBean parent, String JavaDoc beanName) {
219
220         if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; //NOI18N
221
else if ("ServiceRefHandler".equals(beanName)) return "Handler"; //NOI18N
222
else {
223             return beanName;
224     }
225     }
226 }
227
Popular Tags