KickJava   Java API By Example, From Geeks To Geeks.

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

39
40 public class CommonDDAccess {
41
42     public static final String JavaDoc SERVLET_2_3 = "2_3"; //NOI18N
43
public static final String JavaDoc SERVLET_2_4 = "2_4"; //NOI18N
44

45     public static final String JavaDoc PACKAGE_PREFIX = "org.netbeans.modules.web.dd.impl.model_"; //NOI18N
46
public static final String JavaDoc DOT = "."; //NOI18N
47

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

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

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

156     private static String JavaDoc getImplementationBeanName (CommonDDBean parent, String JavaDoc beanName, String JavaDoc version) {
157
158     if (version.equals(SERVLET_2_3)) {
159             if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; //NOI18N
160
else if ("Handler".equals(beanName) && parent instanceof ServiceRef) return "ServiceRefHandler"; //NOI18N
161
else return beanName;
162     } else {
163             return beanName;
164     }
165     }
166     
167     /**
168      * Handle special cases of version differences
169      */

170     private static String JavaDoc getNameForMethod (CommonDDBean parent, String JavaDoc beanName) {
171
172         if ("InitParam".equals(beanName) && parent instanceof WebApp) return "ContextParam"; //NOI18N
173
else if ("ServiceRefHandler".equals(beanName)) return "Handler"; //NOI18N
174
else {
175             return beanName;
176     }
177     }
178 }
179
Popular Tags