01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.apache.openejb.core.cmp;
18:
19: import java.lang.reflect.Field;
20: import java.lang.reflect.Modifier;
21:
22: import javax.ejb.EJBException;
23:
24: import org.apache.openejb.OpenEJBException;
25:
26: public abstract class AbstractKeyGenerator implements KeyGenerator {
27: public static boolean isValidPkField(Field field) {
28: int modifiers = field.getModifiers();
29: return Modifier.isPublic(modifiers)
30: && !Modifier.isStatic(modifiers);
31: }
32:
33: public static Field getField(Class clazz, String fieldName)
34: throws OpenEJBException {
35: try {
36: return clazz.getField(fieldName);
37: } catch (NoSuchFieldException e) {
38: throw new OpenEJBException(
39: "Unable to get primary key field from entity bean class: "
40: + clazz.getName(), e);
41: }
42: }
43:
44: public static Object getFieldValue(Field field, Object object)
45: throws EJBException {
46: if (field == null)
47: throw new NullPointerException("field is null");
48: if (object == null)
49: throw new NullPointerException("object is null");
50: try {
51: return field.get(object);
52: } catch (Exception e) {
53: throw new EJBException(
54: "Could not get field value for field " + field, e);
55: }
56: }
57:
58: public static void setFieldValue(Field field, Object object,
59: Object value) throws EJBException {
60: if (field == null)
61: throw new NullPointerException("field is null");
62: if (object == null)
63: throw new NullPointerException("object is null");
64: try {
65: field.set(object, value);
66: } catch (Exception e) {
67: throw new EJBException(
68: "Could not set field value for field " + field, e);
69: }
70: }
71: }
|