001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /**
043: * Methods for accessing schema2beans objects in a bean-independent way.
044: *
045: * @author Milan Kuchtiak
046: */package org.netbeans.modules.j2ee.dd.impl.common;
047:
048: import java.lang.reflect.*;
049: import java.util.Set;
050: import java.util.HashSet;
051: import org.netbeans.modules.j2ee.dd.api.web.WebApp;
052: import org.openide.util.NbBundle;
053: import org.netbeans.modules.schema2beans.BaseBean;
054: import org.netbeans.modules.j2ee.dd.api.common.CommonDDBean;
055:
056: /**
057: * Methods for accessing schema2beans objects in bean-independent and version-independent way.
058: *
059: * @author Milan Kuchtiak
060: */
061:
062: public class CommonDDAccess {
063:
064: public static final String DOT = "."; //NOI18N
065:
066: public static final String COMMON_API = "org.netbeans.modules.j2ee.dd.api.common."; //NOI18N
067:
068: public static final String SERVLET_2_3 = "2_3"; //NOI18N
069: public static final String SERVLET_2_4 = "2_4"; //NOI18N
070: public static final String WEB_API = "org.netbeans.modules.j2ee.dd.api.web."; //NOI18N
071: public static final String WEB_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.web.model_"; //NOI18N
072:
073: public static final String APP_1_3 = "1_3"; //NOI18N
074: public static final String APP_1_4 = "1_4"; //NOI18N
075: public static final String APP_API = "org.netbeans.modules.j2ee.dd.api.application."; //NOI18N
076: public static final String APP_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.application.model_"; //NOI18N
077:
078: public static final String EJB_2_0 = "2_0"; //NOI18N
079: public static final String EJB_2_1 = "2_1"; //NOI18N
080: public static final String EJB_1_1 = "1_1"; //NOI18N
081: public static final String EJB_API = "org.netbeans.modules.j2ee.dd.api.ejb."; //NOI18N
082: public static final String EJB_PACKAGE_PREFIX = "org.netbeans.modules.j2ee.dd.impl.ejb.model_"; //NOI18N
083:
084: private static Set COMMON_BEANS = new HashSet();
085: static {
086: COMMON_BEANS.add("Icon"); //NOI18N
087: COMMON_BEANS.add("InitParam"); //NOI18N
088: COMMON_BEANS.add("EnvEntry"); //NOI18N
089: COMMON_BEANS.add("EjbRef"); //NOI18N
090: COMMON_BEANS.add("EjbLocalRef"); //NOI18N
091: COMMON_BEANS.add("ResourceRef"); //NOI18N
092: COMMON_BEANS.add("ResourceEnvRef"); //NOI18N
093: COMMON_BEANS.add("ServiceRef"); //NOI18N
094: COMMON_BEANS.add("Handler"); //NOI18N
095: COMMON_BEANS.add("PortComponentRef"); //NOI18N
096: COMMON_BEANS.add("MessageDestination"); //NOI18N
097: COMMON_BEANS.add("MessageDestinationRef"); //NOI18N
098: COMMON_BEANS.add("SecurityRole"); //NOI18N
099: COMMON_BEANS.add("SecurityRoleRef"); //NOI18N
100: }
101:
102: /**
103: * Return a new instance of the specified type
104: *
105: * @param parent parent bean
106: * @param beanName which bean to create
107: * @param pkgName implementation package name
108: * @return BaseBean object
109: */
110:
111: public static BaseBean newBean(CommonDDBean parent,
112: String beanName, String pkgName)
113: throws ClassNotFoundException {
114: beanName = getImplementationBeanName(parent, beanName, pkgName);
115: try {
116: Class beanClass = Class.forName(pkgName + DOT + beanName);
117: return (BaseBean) beanClass.newInstance();
118:
119: } catch (Exception e) {
120: if (e instanceof ClassNotFoundException)
121: throw (ClassNotFoundException) e;
122: else {
123: // This is a programming error.
124: e.printStackTrace();
125: throw new RuntimeException(NbBundle.getMessage(
126: CommonDDAccess.class,
127: "MSG_COMMONDDACCESS_ERROR", "newBean",
128: ", package = " + pkgName + ", beanName = "
129: + beanName, e + ": " + e.getMessage()));
130: }
131: }
132: }
133:
134: public static void addBean(CommonDDBean parent, CommonDDBean child,
135: String beanName, String pkgName) {
136: beanName = getImplementationBeanName(parent, beanName, pkgName);
137: String apiPrefix = getAPIPrefix(beanName, pkgName);
138: try {
139: Class p = parent.getClass();
140: Class ch = Class.forName(apiPrefix + beanName); //NOI18N
141: Method setter = null;
142: try {
143: setter = p.getMethod("set" + beanName,
144: new Class[] { ch }); //NOI18N
145: setter.invoke(parent, new Object[] { child });
146: } catch (NoSuchMethodException ex) {
147: }
148: if (setter == null) {
149: setter = p.getMethod("add"
150: + getNameForMethod(parent, beanName),
151: new Class[] { ch }); //NOI18N
152: setter.invoke(parent, new Object[] { child });
153: }
154: } catch (Exception e) {
155: // This is a programming error.
156: e.printStackTrace();
157: throw new RuntimeException(NbBundle.getMessage(
158: CommonDDAccess.class, "MSG_COMMONDDACCESS_ERROR",
159: "addBean", ", package = " + pkgName
160: + ", beanName = " + beanName, e + ": "
161: + e.getMessage()));
162: }
163: }
164:
165: /**
166: * Handle special cases of version differences
167: */
168: private static String getImplementationBeanName(
169: CommonDDBean parent, String beanName, String pkgName) {
170: if (pkgName.equals(WEB_PACKAGE_PREFIX + SERVLET_2_3)) {
171: if ("InitParam".equals(beanName)
172: && parent instanceof WebApp)
173: return "ContextParam"; //NOI18N
174: else
175: return beanName;
176: } else if (beanName.equals("Session")
177: || beanName.equals("Entity")
178: || beanName.equals("MessageDriven")) { //NOI18N
179: return beanName + "Bean"; //NOI18N
180: } else
181: return beanName;
182: }
183:
184: private static String getAPIPrefix(String beanName, String pkgName) {
185: if (COMMON_BEANS.contains(beanName))
186: return COMMON_API;
187: if (pkgName.startsWith(EJB_PACKAGE_PREFIX))
188: return EJB_API;
189: else if (pkgName.startsWith(WEB_PACKAGE_PREFIX))
190: return WEB_API;
191: else if (pkgName.startsWith(APP_PACKAGE_PREFIX))
192: return APP_API;
193: assert false : "Invalid package prefix:" + pkgName;
194: return ""; //NOI18N
195: }
196:
197: /**
198: * Get a BaseBean object from parent BaseBean
199: *
200: * @param parent parent BaseBean
201: * @param beanProperty name of child's BaseBean object e.g. "Servlet"
202: * @param nameProperty name of property e.g. ServletName
203: * @param value e.g. "ControllerServlet"
204: */
205: public static BaseBean findBeanByName(BaseBean parent,
206: String beanProperty, String nameProperty, String value) {
207: Class c = parent.getClass();
208: Method getter;
209: Object result;
210: try {
211: getter = c.getMethod("get"
212: + getNameForMethod((CommonDDBean) parent,
213: beanProperty)); //NOI18N
214: result = getter.invoke(parent);
215: if (result == null) {
216: return null;
217: } else if (result instanceof BaseBean) {
218: return null;
219: } else {
220: BaseBean[] beans = (BaseBean[]) result;
221: for (int i = 0; i < beans.length; i++) {
222: Class c1 = beans[i].getClass();
223: Method getter1;
224: Object result1;
225: getter1 = c1.getMethod("get" + nameProperty); //NOI18N
226: result1 = getter1.invoke(beans[i]);
227: if (result1 instanceof String) {
228: if (value.equals((String) result1)) {
229: return beans[i];
230: }
231: }
232: }
233: return null;
234: }
235: } catch (Exception e) {
236: // This is a programming error
237: e.printStackTrace();
238: throw new RuntimeException(NbBundle.getMessage(
239: CommonDDAccess.class, "MSG_COMMONDDACCESS_ERROR",
240: "getBeanByName", "parent = " + parent
241: + ", beanProperty = " + beanProperty
242: + ", nameProperty = " + nameProperty
243: + ", value = " + value, e + ": "
244: + e.getMessage()));
245: }
246: }
247:
248: /**
249: * Handle special cases of version differences
250: */
251: private static String getNameForMethod(CommonDDBean parent,
252: String beanName) {
253:
254: if ("InitParam".equals(beanName) && parent instanceof WebApp)
255: return "ContextParam"; //NOI18N
256: else if ("ServiceRefHandler".equals(beanName))
257: return "Handler"; //NOI18N
258: else {
259: return beanName;
260: }
261: }
262: }
|