001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Rodrigo Westrupp
028: */
029:
030: package com.caucho.amber.cfg;
031:
032: import com.caucho.amber.manager.AmberPersistenceUnit;
033: import com.caucho.amber.type.EmbeddableType;
034: import com.caucho.bytecode.*;
035: import com.caucho.config.ConfigException;
036: import com.caucho.util.L10N;
037:
038: import java.sql.SQLException;
039: import java.util.HashMap;
040: import java.util.logging.Logger;
041:
042: /**
043: * Configuration for an embeddable type
044: */
045: public class EmbeddableIntrospector extends BaseConfigIntrospector {
046: private static final L10N L = new L10N(EmbeddableIntrospector.class);
047: private static final Logger log = Logger
048: .getLogger(EmbeddableIntrospector.class.getName());
049:
050: HashMap<String, EmbeddableType> _embeddableMap = new HashMap<String, EmbeddableType>();
051:
052: /**
053: * Creates the introspector.
054: */
055: public EmbeddableIntrospector(AmberPersistenceUnit persistenceUnit) {
056: super (persistenceUnit);
057: }
058:
059: /**
060: * Returns true for embeddable type.
061: */
062: public boolean isEmbeddable(JClass type) {
063: getInternalEmbeddableConfig(type, _annotationCfg);
064: JAnnotation embeddableAnn = _annotationCfg.getAnnotation();
065: EmbeddableConfig embeddableConfig = _annotationCfg
066: .getEmbeddableConfig();
067:
068: return (!_annotationCfg.isNull());
069: }
070:
071: /**
072: * Introspects.
073: */
074: public EmbeddableType introspect(JClass type)
075: throws ConfigException, SQLException {
076: getInternalEmbeddableConfig(type, _annotationCfg);
077: JAnnotation embeddableAnn = _annotationCfg.getAnnotation();
078: EmbeddableConfig embeddableConfig = _annotationCfg
079: .getEmbeddableConfig();
080:
081: String typeName = type.getName();
082:
083: EmbeddableType embeddableType = _embeddableMap.get(typeName);
084:
085: if (embeddableType != null)
086: return embeddableType;
087:
088: try {
089: embeddableType = _persistenceUnit.createEmbeddable(
090: typeName, type);
091: _embeddableMap.put(typeName, embeddableType);
092:
093: boolean isField = isField(type, embeddableConfig);
094:
095: if (isField)
096: embeddableType.setFieldAccess(true);
097:
098: // XXX: jpa/0u21
099: JAnnotation ann = type
100: .getAnnotation(javax.persistence.Embeddable.class);
101:
102: if (ann == null) {
103: isField = true;
104: embeddableType.setIdClass(true);
105: _persistenceUnit.getAmberContainer().addEmbeddable(
106: typeName, embeddableType);
107: }
108:
109: embeddableType.setInstanceClassName(type.getName()
110: + "__ResinExt");
111: embeddableType.setEnhanced(true);
112:
113: if (isField)
114: introspectFields(_persistenceUnit, embeddableType,
115: null, type, embeddableConfig, true);
116: else
117: introspectMethods(_persistenceUnit, embeddableType,
118: null, type, embeddableConfig);
119:
120: } catch (ConfigException e) {
121: if (embeddableType != null)
122: embeddableType.setConfigException(e);
123:
124: throw e;
125: } catch (RuntimeException e) {
126: if (embeddableType != null)
127: embeddableType.setConfigException(e);
128:
129: throw e;
130: }
131:
132: return embeddableType;
133: }
134:
135: boolean isField(JClass type, AbstractEnhancedConfig typeConfig)
136: throws ConfigException {
137: for (JMethod method : type.getDeclaredMethods()) {
138: JAnnotation ann[] = method.getDeclaredAnnotations();
139:
140: for (int i = 0; ann != null && i < ann.length; i++) {
141: if (isPropertyAnnotation(ann[i].getType()))
142: return false;
143: }
144: }
145:
146: return true;
147: }
148:
149: private boolean isPropertyAnnotation(String name) {
150: return ("javax.persistence.Basic".equals(name) || "javax.persistence.Column"
151: .equals(name));
152: }
153: }
|