use of org.apache.openejb.jee.CmpField$JAXB.writeCmpField in project tomee by apache.
the class CmpJpaConversion method mapClass2x.
/**
* Generate the JPA mapping for a CMP 2.x bean. Since
* the field accessors are all defined as abstract methods
* and the fields will not be defined in the implementation
* class, we don't need to deal with mapped superclasses.
* All of the fields and concrete methods will be
* implemented by the generated subclass, so from
* a JPA standpoint, there are no mapped superclasses
* required.
*
* @param mapping The mapping information we're updating.
* @param bean The entity bean meta data
* @param classLoader The classloader for resolving class references and
* primary key classes.
*/
private Collection<MappedSuperclass> mapClass2x(final Mapping mapping, final EntityBean bean, final ClassLoader classLoader) {
final Set<String> allFields = new TreeSet<String>();
// get an acculated set of the CMP fields.
for (final CmpField cmpField : bean.getCmpField()) {
allFields.add(cmpField.getFieldName());
}
Class<?> beanClass = null;
try {
beanClass = classLoader.loadClass(bean.getEjbClass());
} catch (final ClassNotFoundException e) {
// if it does fail, just return null from here
return null;
}
// build a map from the field name to the super class that contains that field.
// If this is a strictly CMP 2.x class, this is generally an empty map. However,
// we support some migration steps toward EJB3, so this can be defined completely
// or partially as a POJO with concrete fields and accessors. This allows us to
// locate and generate the mappings
final Map<String, MappedSuperclass> superclassByField = mapFields(beanClass, allFields);
for (final Method method : beanClass.getMethods()) {
if (!Modifier.isAbstract(method.getModifiers())) {
continue;
}
if (method.getParameterTypes().length != 0) {
continue;
}
if (method.getReturnType().equals(Void.TYPE)) {
continue;
}
// Skip relationships: anything of type EJBLocalObject or Collection
if (EJBLocalObject.class.isAssignableFrom(method.getReturnType())) {
continue;
}
if (Collection.class.isAssignableFrom(method.getReturnType())) {
continue;
}
if (Map.class.isAssignableFrom(method.getReturnType())) {
continue;
}
String name = method.getName();
if (name.startsWith("get")) {
name = name.substring("get".length(), name.length());
} else if (name.startsWith("is")) {
// boolean.
if (method.getReturnType() == Boolean.TYPE) {
name = name.substring("is".length(), name.length());
} else {
// not an acceptable "is" method.
continue;
}
} else {
continue;
}
// the property needs the first character lowercased. Generally,
// we'll have this field already in our list, but it might have been
// omitted from the meta data.
name = Strings.lcfirst(name);
if (!allFields.contains(name)) {
allFields.add(name);
bean.addCmpField(name);
}
}
//
// id: the primary key
//
final Set<String> primaryKeyFields = new HashSet<String>();
if (bean.getPrimkeyField() != null) {
final String fieldName = bean.getPrimkeyField();
final MappedSuperclass superclass = superclassByField.get(fieldName);
// this need not be here...for CMP 2.x, these are generally autogenerated fields.
if (superclass != null) {
// ok, add this field to the superclass mapping
superclass.addField(new Id(fieldName));
// the direct mapping is an over ride
mapping.addField(new AttributeOverride(fieldName));
} else {
// this is a normal generated field, it will be in the main class mapping.
mapping.addField(new Id(fieldName));
}
primaryKeyFields.add(fieldName);
} else if ("java.lang.Object".equals(bean.getPrimKeyClass())) {
// the automatically generated keys use a special property name
// and will always be in the generated superclass.
final String fieldName = "OpenEJB_pk";
final Id field = new Id(fieldName);
field.setGeneratedValue(new GeneratedValue(GenerationType.AUTO));
mapping.addField(field);
primaryKeyFields.add(fieldName);
} else if (bean.getPrimKeyClass() != null) {
Class<?> pkClass = null;
try {
pkClass = classLoader.loadClass(bean.getPrimKeyClass());
MappedSuperclass idclass = null;
// to make sure everything maps correctly.
for (final Field pkField : pkClass.getFields()) {
final String pkFieldName = pkField.getName();
final int modifiers = pkField.getModifiers();
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && allFields.contains(pkFieldName)) {
// see if the bean field is concretely defined in one of the superclasses
final MappedSuperclass superclass = superclassByField.get(pkFieldName);
if (superclass != null) {
// ok, we have an override that needs to be specified at the main class level.
superclass.addField(new Id(pkFieldName));
mapping.addField(new AttributeOverride(pkFieldName));
idclass = resolveIdClass(idclass, superclass, beanClass);
} else {
// this field will be autogenerated
mapping.addField(new Id(pkFieldName));
}
primaryKeyFields.add(pkFieldName);
}
}
// if we've located an ID class, set it as such
if (idclass != null) {
idclass.setIdClass(new IdClass(bean.getPrimKeyClass()));
} else {
// do this for the toplevel mapping
mapping.setIdClass(new IdClass(bean.getPrimKeyClass()));
}
} catch (final ClassNotFoundException e) {
throw (IllegalStateException) new IllegalStateException("Could not find entity primary key class " + bean.getPrimKeyClass()).initCause(e);
}
}
//
for (final CmpField cmpField : bean.getCmpField()) {
// only add entries for cmp fields that are not part of the primary key
if (!primaryKeyFields.contains(cmpField.getFieldName())) {
final String fieldName = cmpField.getFieldName();
// this will be here if we've already processed this
final MappedSuperclass superclass = superclassByField.get(fieldName);
// we need to provide a mapping for this.
if (superclass != null) {
// we need to mark this as being in one of the superclasses
superclass.addField(new Basic(fieldName));
mapping.addField(new AttributeOverride(fieldName));
} else {
// directly generated.
mapping.addField(new Basic(fieldName));
}
}
}
// the field mappings
return new HashSet<MappedSuperclass>(superclassByField.values());
}
use of org.apache.openejb.jee.CmpField$JAXB.writeCmpField in project tomee by apache.
the class LegacyInterfaceTest method test.
public void test() throws Exception {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
final ConfigurationFactory config = new ConfigurationFactory();
final Assembler assembler = new Assembler();
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new SingletonBean(MySingletonBean.class));
ejbJar.addEnterpriseBean(new EntityBean(MyBmpBean.class, PersistenceType.BEAN));
//<entity>
// <ejb-name>License</ejb-name>
// <local-home>org.apache.openejb.test.entity.cmr.onetoone.LicenseLocalHome</local-home>
// <local>org.apache.openejb.test.entity.cmr.onetoone.LicenseLocal</local>
// <ejb-class>org.apache.openejb.test.entity.cmr.onetoone.LicenseBean</ejb-class>
// <persistence-type>Container</persistence-type>
// <prim-key-class>java.lang.Integer</prim-key-class>
// <reentrant>false</reentrant>
// <cmp-version>2.x</cmp-version>
// <abstract-schema-name>License</abstract-schema-name>
// <cmp-field>
// <field-name>id</field-name>
// </cmp-field>
// <cmp-field>
// <field-name>number</field-name>
// </cmp-field>
// <cmp-field>
// <field-name>points</field-name>
// </cmp-field>
// <cmp-field>
// <field-name>notes</field-name>
// </cmp-field>
// <primkey-field>id</primkey-field>
// <query>
// <!-- CompondPK one-to-one shares the local home interface so we need to declare this useless finder -->
// <query-method>
// <method-name>findByPrimaryKey</method-name>
// <method-params>
// <method-param>org.apache.openejb.test.entity.cmr.onetoone.LicensePk</method-param>
// </method-params>
// </query-method>
// <ejb-ql>SELECT OBJECT(DL) FROM License DL</ejb-ql>
// </query>
//</entity>
final EntityBean cmp = ejbJar.addEnterpriseBean(new EntityBean(MyCmpBean.class, PersistenceType.CONTAINER));
cmp.setPrimKeyClass(Integer.class.getName());
cmp.setPrimkeyField("id");
cmp.getCmpField().add(new CmpField("id"));
cmp.getCmpField().add(new CmpField("name"));
final Query query = new Query();
query.setQueryMethod(new QueryMethod("findByPrimaryKey", Integer.class.getName()));
query.setEjbQl("SELECT OBJECT(DL) FROM License DL");
cmp.getQuery().add(query);
final List<ContainerTransaction> transactions = ejbJar.getAssemblyDescriptor().getContainerTransaction();
//<container-transaction>
// <method>
// <ejb-name>MyBean</ejb-name>
// <method-name>*</method-name>
// </method>
// <trans-attribute>Supports</trans-attribute>
//</container-transaction>
transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyBmpBean", "*"));
transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyCmpBean", "*"));
transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MySingletonBean", "*"));
final File f = new File("test").getAbsoluteFile();
if (!f.exists() && !f.mkdirs()) {
throw new Exception("Failed to create test directory: " + f);
}
final AppModule module = new AppModule(this.getClass().getClassLoader(), f.getAbsolutePath());
module.getEjbModules().add(new EjbModule(ejbJar));
assembler.createApplication(config.configureApplication(module));
}
use of org.apache.openejb.jee.CmpField$JAXB.writeCmpField in project tomee by apache.
the class CmpJpaConversion method mapClass1x.
/**
* Create the class mapping for a CMP 1.x entity bean.
* Since the fields for 1.x persistence are defined
* in the objects directly, we need to create superclass
* mappings for each of the defined fields to identify
* which classes implement each of the managed fields.
*
* @param ejbClassName The name of the class we're processing.
* @param mapping The mappings we're going to generate.
* @param bean The bean metadata for the ejb.
* @param classLoader The classloader used to load the bean class for
* inspection.
* @return The set of mapped superclasses used in this
* bean mapping.
*/
private Collection<MappedSuperclass> mapClass1x(final String ejbClassName, final Mapping mapping, final EntityBean bean, final ClassLoader classLoader) {
final Class ejbClass = loadClass(classLoader, ejbClassName);
// build a set of all field names
final Set<String> allFields = new TreeSet<String>();
for (final CmpField cmpField : bean.getCmpField()) {
allFields.add(cmpField.getFieldName());
}
// build a map from the field name to the super class that contains that field
final Map<String, MappedSuperclass> superclassByField = mapFields(ejbClass, allFields);
//
// id: the primary key
//
final Set<String> primaryKeyFields = new HashSet<String>();
if (bean.getPrimkeyField() != null) {
final String fieldName = bean.getPrimkeyField();
final MappedSuperclass superclass = superclassByField.get(fieldName);
if (superclass == null) {
throw new IllegalStateException("Primary key field " + fieldName + " is not defined in class " + ejbClassName + " or any super classes");
}
superclass.addField(new Id(fieldName));
mapping.addField(new AttributeOverride(fieldName));
primaryKeyFields.add(fieldName);
} else if ("java.lang.Object".equals(bean.getPrimKeyClass())) {
// a primary field type of Object is an automatically generated
// pk field. Mark it as such and add it to the mapping.
final String fieldName = "OpenEJB_pk";
final Id field = new Id(fieldName);
field.setGeneratedValue(new GeneratedValue(GenerationType.AUTO));
mapping.addField(field);
} else if (bean.getPrimKeyClass() != null) {
// we have a primary key class. We need to define the mappings between the key class fields
// and the bean's managed fields.
Class<?> pkClass = null;
try {
pkClass = classLoader.loadClass(bean.getPrimKeyClass());
MappedSuperclass superclass = null;
MappedSuperclass idclass = null;
for (final Field pkField : pkClass.getFields()) {
final String fieldName = pkField.getName();
final int modifiers = pkField.getModifiers();
// AND must also exist in the class hierarchy (not enforced by mapFields());
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && allFields.contains(fieldName)) {
superclass = superclassByField.get(fieldName);
if (superclass == null) {
throw new IllegalStateException("Primary key field " + fieldName + " is not defined in class " + ejbClassName + " or any super classes");
}
// these are defined ast ID fields because they are part of the primary key
superclass.addField(new Id(fieldName));
mapping.addField(new AttributeOverride(fieldName));
primaryKeyFields.add(fieldName);
idclass = resolveIdClass(idclass, superclass, ejbClass);
}
}
// if we've located an ID class, set it as such
if (idclass != null) {
idclass.setIdClass(new IdClass(bean.getPrimKeyClass()));
}
} catch (final ClassNotFoundException e) {
throw (IllegalStateException) new IllegalStateException("Could not find entity primary key class " + bean.getPrimKeyClass()).initCause(e);
}
}
//
for (final CmpField cmpField : bean.getCmpField()) {
final String fieldName = cmpField.getFieldName();
// all of the primary key fields have been processed, so only handle whatever is left over
if (!primaryKeyFields.contains(fieldName)) {
final MappedSuperclass superclass = superclassByField.get(fieldName);
if (superclass == null) {
throw new IllegalStateException("CMP field " + fieldName + " is not defined in class " + ejbClassName + " or any super classes");
}
superclass.addField(new Basic(fieldName));
mapping.addField(new AttributeOverride(fieldName));
}
}
// the field mappings
return new HashSet<MappedSuperclass>(superclassByField.values());
}
use of org.apache.openejb.jee.CmpField$JAXB.writeCmpField in project tomee by apache.
the class SunConversion method mergeEntityMappings.
private void mergeEntityMappings(final Map<String, EntityData> entities, final String moduleId, final EjbJar ejbJar, final OpenejbJar openejbJar, final SunEjbJar sunEjbJar) {
if (openejbJar == null) {
return;
}
if (sunEjbJar == null) {
return;
}
if (sunEjbJar.getEnterpriseBeans() == null) {
return;
}
for (final Ejb ejb : sunEjbJar.getEnterpriseBeans().getEjb()) {
final Cmp cmp = ejb.getCmp();
if (cmp == null) {
// skip non cmp beans
continue;
}
// skip all non-CMP beans
final EnterpriseBean enterpriseBean = ejbJar.getEnterpriseBean(ejb.getEjbName());
if (!(enterpriseBean instanceof EntityBean) || ((EntityBean) enterpriseBean).getPersistenceType() != PersistenceType.CONTAINER) {
continue;
}
final EntityBean bean = (EntityBean) enterpriseBean;
final EntityData entityData = entities.get(moduleId + "#" + ejb.getEjbName());
if (entityData == null) {
// todo warn no such ejb in the ejb-jar.xml
continue;
}
final Collection<String> cmpFields = new ArrayList<String>(bean.getCmpField().size());
for (final CmpField cmpField : bean.getCmpField()) {
cmpFields.add(cmpField.getFieldName());
}
final OneOneFinders oneOneFinders = cmp.getOneOneFinders();
if (oneOneFinders != null) {
for (final Finder finder : oneOneFinders.getFinder()) {
final List<List<String>> params = parseQueryParamters(finder.getQueryParams());
final String queryFilter = finder.getQueryFilter();
final String ejbQl = convertToEjbQl(entityData.entity.getName(), cmpFields, finder.getQueryParams(), queryFilter);
final NamedQuery namedQuery = new NamedQuery();
final StringBuilder name = new StringBuilder();
name.append(entityData.entity.getName()).append(".").append(finder.getMethodName());
if (!params.isEmpty()) {
name.append('(');
boolean first = true;
for (final List<String> methodParam : params) {
if (!first) {
name.append(",");
}
name.append(methodParam.get(0));
first = false;
}
name.append(')');
}
namedQuery.setName(name.toString());
namedQuery.setQuery(ejbQl);
entityData.entity.getNamedQuery().add(namedQuery);
}
}
}
}
use of org.apache.openejb.jee.CmpField$JAXB.writeCmpField in project tomee by apache.
the class EjbJarInfoBuilder method initEntityBean.
private EnterpriseBeanInfo initEntityBean(final EntityBean e, final Map m) throws OpenEJBException {
final EntityBeanInfo bean = new EntityBeanInfo();
final EjbDeployment d = (EjbDeployment) m.get(e.getEjbName());
if (d == null) {
throw new OpenEJBException("No deployment information in openejb-jar.xml for bean " + e.getEjbName() + ". Please redeploy the jar");
}
bean.ejbDeploymentId = d.getDeploymentId();
bean.containerId = d.getContainerId();
final Icon icon = e.getIcon();
bean.largeIcon = icon == null ? null : icon.getLargeIcon();
bean.smallIcon = icon == null ? null : icon.getSmallIcon();
bean.description = e.getDescription();
bean.displayName = e.getDisplayName();
bean.ejbClass = e.getEjbClass();
bean.abstractSchemaName = e.getAbstractSchemaName();
bean.ejbName = e.getEjbName();
bean.home = e.getHome();
bean.remote = e.getRemote();
bean.localHome = e.getLocalHome();
bean.local = e.getLocal();
bean.transactionType = "Container";
bean.primKeyClass = e.getPrimKeyClass();
bean.primKeyField = e.getPrimkeyField();
bean.persistenceType = e.getPersistenceType().toString();
bean.reentrant = String.valueOf(e.getReentrant());
bean.properties.putAll(d.getProperties());
final CmpVersion cmpVersion = e.getCmpVersion();
if (e.getPersistenceType() == PersistenceType.CONTAINER) {
if (cmpVersion != null && cmpVersion == CmpVersion.CMP1) {
bean.cmpVersion = 1;
} else {
bean.cmpVersion = 2;
}
}
final List<CmpField> cmpFields = e.getCmpField();
for (final CmpField cmpField : cmpFields) {
bean.cmpFieldNames.add(cmpField.getFieldName());
}
if (bean.persistenceType.equalsIgnoreCase("Container")) {
for (final Query q : e.getQuery()) {
final QueryInfo query = new QueryInfo();
query.queryStatement = q.getEjbQl().trim();
final MethodInfo method = new MethodInfo();
method.ejbName = bean.ejbName;
method.className = "*";
final QueryMethod qm = q.getQueryMethod();
method.methodName = qm.getMethodName();
if (qm.getMethodParams() != null) {
method.methodParams = qm.getMethodParams().getMethodParam();
}
query.method = method;
final ResultTypeMapping resultType = q.getResultTypeMapping();
if (ResultTypeMapping.REMOTE.equals(resultType)) {
query.remoteResultType = true;
}
bean.queries.add(query);
}
for (final org.apache.openejb.jee.oejb3.Query q : d.getQuery()) {
final QueryInfo query = new QueryInfo();
query.description = q.getDescription();
query.queryStatement = q.getObjectQl().trim();
final MethodInfo method = new MethodInfo();
method.ejbName = bean.ejbName;
method.className = "*";
final org.apache.openejb.jee.oejb3.QueryMethod qm = q.getQueryMethod();
method.methodName = qm.getMethodName();
if (qm.getMethodParams() != null) {
method.methodParams = qm.getMethodParams().getMethodParam();
}
query.method = method;
bean.queries.add(query);
}
}
return bean;
}
Aggregations