diff options
Diffstat (limited to 'das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create')
4 files changed, 580 insertions, 0 deletions
diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataGraphCreator.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataGraphCreator.java new file mode 100644 index 0000000000..bfba12d3b5 --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataGraphCreator.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.tuscany.das.ldap.emf.create; + +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.ldap.LdapContext; + +import org.apache.tuscany.das.ldap.schema.constants.AttributeTypeConstants; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.sdo.EDataGraph; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class EDataGraphCreator +implements AttributeTypeConstants +{ + public static void create( + EDataGraph eDataGraph, + LdapContext rootContext, + Map<EDataObject, String> dataObjectToRelativeDNCache) + throws NamingException + { + EDataObject rootDataObject = + (EDataObject) + eDataGraph.getRootObject(); + + EDataObjectCreator.create( + rootDataObject, + rootContext, + dataObjectToRelativeDNCache); + + EList<EObject> children = + rootDataObject.eContents(); + + if (children.size() > 0) + { + EDataGraphCreatorHelper.createChildren( + children, + rootContext, + dataObjectToRelativeDNCache); + } + } +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataGraphCreatorHelper.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataGraphCreatorHelper.java new file mode 100644 index 0000000000..ab1209224a --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataGraphCreatorHelper.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.tuscany.das.ldap.emf.create; + +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.ldap.LdapContext; + +import org.apache.tuscany.das.ldap.schema.constants.AttributeTypeConstants; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class EDataGraphCreatorHelper +implements AttributeTypeConstants +{ + /* + * TODO Make mutlithreaded + */ + public static void createChildren( + EList<EObject> children, + LdapContext rootContext, + Map<EDataObject, String> dataObjectToRelativeDNCache) + throws NamingException + { + LdapContext eContainmentFeatureContext = + null; + + for (EObject eObject : children) + { + String parentContextRDN = + dataObjectToRelativeDNCache.get(eObject.eContainer()); + + LdapContext parentContext = + (LdapContext) + rootContext. + lookup(parentContextRDN); + + EStructuralFeature eContainmentFeature = + eObject.eContainingFeature(); + + String eContainingFeatureContextName = + CN + "="+ eContainmentFeature.getName(); + + try + { + eContainmentFeatureContext = + (LdapContext) + parentContext.lookup( + eContainingFeatureContextName); + } + catch (Exception e) + { + eContainmentFeatureContext = + (LdapContext) + parentContext. + createSubcontext( + eContainingFeatureContextName); + } + + EDataObjectCreator.create(( + EDataObject) eObject, + eContainmentFeatureContext, + dataObjectToRelativeDNCache); + + children = + eObject.eContents(); + + if ( children.size()> 0) + { + createChildren( + children, + rootContext, + dataObjectToRelativeDNCache); + } + } + } +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataObjectCreator.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataObjectCreator.java new file mode 100644 index 0000000000..5d79d40fb5 --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataObjectCreator.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.tuscany.das.ldap.emf.create; + +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.BasicAttributes; +import javax.naming.ldap.LdapContext; + +import org.apache.tuscany.das.ldap.constants.DASConstants; +import org.apache.tuscany.das.ldap.util.ComplexTypeNamespaceQualifier; +import org.apache.tuscany.das.ldap.util.QualifiedNameNormalizer; +import org.apache.tuscany.das.ldap.util.SimpleTypeNamespaceQualifier; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class EDataObjectCreator +implements DASConstants +{ + /** + * Create the LDAP entry for the EDataObject argument + * + * @param eDataObject the e data object + * @param containerContext the container context + * + * @throws NamingException the naming exception + */ + public static void create( + EDataObject eDataObject, + LdapContext containerContext, + Map<EDataObject, String> dataObjectToRelativeDNCache) + throws NamingException + { + String rdn = + null; + Object dataObjectID = + null; + + EClass eClass = + eDataObject.eClass(); + + EAttribute idEAttribute = + eClass.getEIDAttribute(); + + String namespaceURI = + eClass.getEPackage().getNsURI(); + + String qualifiedEAttributeName = + null; + String normalizedEAttributeName = + null; + + Attributes attributes = + new BasicAttributes(); + + attributes = + EDataObjectCreatorHelper.processEAttributes( + attributes, + eDataObject, + eClass, + namespaceURI); + + attributes = + EDataObjectCreatorHelper.processEReferences( + attributes, + eDataObject, + eClass, + namespaceURI); + + String qualifiedEClassName = + ComplexTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName() ); + + String normalizedEClassName = + QualifiedNameNormalizer. + normalize(qualifiedEClassName); + + Attribute objectClassAttribute = + new BasicAttribute("objectClass"); + + objectClassAttribute. + add(normalizedEClassName); + + attributes.put(objectClassAttribute); + + dataObjectID = + eDataObject.eGet(idEAttribute ); + + qualifiedEAttributeName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName(), + idEAttribute.getName() ); + + normalizedEAttributeName = + QualifiedNameNormalizer. + normalize(qualifiedEAttributeName); + + rdn = + normalizedEAttributeName + "=" + dataObjectID; + + containerContext.createSubcontext( + rdn, + attributes ); + + EDataObjectCreatorHelper. + updateDataObjectToRelativeDNCache( + eDataObject, + dataObjectToRelativeDNCache, + rdn); + } +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataObjectCreatorHelper.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataObjectCreatorHelper.java new file mode 100644 index 0000000000..90ec5701ad --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/create/EDataObjectCreatorHelper.java @@ -0,0 +1,284 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.tuscany.das.ldap.emf.create; + +import java.util.List; +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttribute; + +import org.apache.tuscany.das.ldap.constants.DASConstants; +import org.apache.tuscany.das.ldap.util.QualifiedNameNormalizer; +import org.apache.tuscany.das.ldap.util.SimpleTypeNamespaceQualifier; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class EDataObjectCreatorHelper +implements DASConstants +{ + + public static void updateDataObjectToRelativeDNCache( + EDataObject eDataObject, + Map<EDataObject, String> dataObjectToRelativeDNCache, + String rdn) + { + EDataObject parentDataObject = + (EDataObject) eDataObject.eContainer(); + + if (parentDataObject !=null) + { + String parentRDN = + dataObjectToRelativeDNCache. + get(parentDataObject); + rdn = + rdn + + "," + + "cn=" + + eDataObject.eContainingFeature().getName() + + "," + + parentRDN; + dataObjectToRelativeDNCache.put(eDataObject, rdn); + } + else + { + dataObjectToRelativeDNCache.put(eDataObject, rdn); + } + } + + + /** + * Process EAttributes adding the normalized EAttribute instance's + * name and corresponding values to the attributes argument. + * + * @param attributes the attributes + * @param eDataObject the e data object + * @param eClass the e class + * @param namespaceURI the namespace URI + * + * @return the attributes + * + * @throws NamingException the naming exception + * + * Note that ApacheDS supports the Syntaxes (DataTypes) + * Boolean, Integer, String. This means that all values that + * are not Boolean or Integer are stored as Strings. + */ + public static Attributes processEAttributes( + Attributes attributes, + EDataObject eDataObject, + EClass eClass, + String namespaceURI) + throws NamingException + { + EcorePackage ecorePackage = + EcorePackage.eINSTANCE; + + EAttribute idEAttribute = + eClass.getEIDAttribute(); + + List<EAttribute> eAttributes = + eClass.getEAllAttributes(); + + String qualifiedEAttributeName = + null; + String normalizedEAttributeName = + null; + + for (EAttribute eAttribute : eAttributes) + { + Attribute attribute = + processEAttribute( + eAttribute, + namespaceURI, + eClass, eDataObject); + attributes.put(attribute); + } + return attributes; + } + + /* + * TODO This is also used in updates so consider moving to a utility + */ + public static Attribute processEAttribute( + EAttribute eAttribute, + String namespaceURI, + EClass eClass, + EDataObject eDataObject) + { + String qualifiedEAttributeName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName(), + eAttribute.getName() ); + + String normalizedEAttributeName = + QualifiedNameNormalizer. + normalize(qualifiedEAttributeName); + + Attribute attribute = + new BasicAttribute(normalizedEAttributeName); + + if (eAttribute.isMany()) + { + if ( !(eAttribute.getEType() == EcorePackage.eINSTANCE.getEBoolean() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEBooleanObject() ) ) + { + EList<Object> eAttributeValues = + ( EList<Object> ) eDataObject.eGet( eAttribute ); + + for (Object eAttributeValue : eAttributeValues) + { + attribute.add(eAttributeValue.toString() ); + } + } + else + { + EList<Boolean> eAttributeValues = + ( EList<Boolean> ) eDataObject.eGet( eAttribute ); + + for (Boolean eAttributeValue : eAttributeValues) + { + if (eAttributeValue == false) + { + attribute.add(Boolean.FALSE.toString().toUpperCase() ); + } + else + { + attribute.add(Boolean.TRUE.toString().toUpperCase() ); + } + } + } + return attribute; + } + + if ( !(eAttribute.getEType() == EcorePackage.eINSTANCE.getEBoolean() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEBooleanObject() ) ) + { + Object eAttributeValue = + eDataObject.eGet( eAttribute ); + + attribute.add(eAttributeValue.toString() ); + } + else + { + Boolean eAttributeValue = + (Boolean) eDataObject.eGet( eAttribute ); + + if (eAttributeValue == false) + { + attribute.add(Boolean.FALSE.toString().toUpperCase() ); + } + else + { + attribute.add(Boolean.TRUE.toString().toUpperCase() ); + } + } + return attribute; + } + + /** + * Process references adding the normalized EReference instance's + * name and corresponding id (IDs for multiplicity many references) + * to the attributes argument. + * + * @param attributes the attributes + * @param eDataObject the e data object + * @param eClass the e class + * @param namespaceURI the namespace URI + * + * @return the attributes + */ + public static Attributes processEReferences( + Attributes attributes, + EDataObject eDataObject, + EClass eClass, + String namespaceURI) + { + List<EReference> eReferences = + eClass.getEAllReferences(); + + for (EReference eReference : eReferences) + { + String qualifiedEReferenceName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName(), + eReference.getName() ); + + String normalizedEReferenceName = + QualifiedNameNormalizer. + normalize(qualifiedEReferenceName); + + if (eReference.isMany()) + { + List<EDataObject> referencedEDataObjects = + (List<EDataObject>) eDataObject.eGet(eReference); + + Attribute idAttribute = + new BasicAttribute(normalizedEReferenceName); + + for (EDataObject containedEDataObject : referencedEDataObjects) + { + EClass containedEDataObjectEClass = + containedEDataObject.eClass(); + + String containedEDataObjectID = + (String) containedEDataObject.eGet( + containedEDataObjectEClass. + getEIDAttribute()); + + idAttribute.add(containedEDataObjectID); + } + attributes.put(idAttribute); + } + else + { + EDataObject containedEDataObject = + (EDataObject) + eDataObject.eGet(eReference); + + if (containedEDataObject != null) + { + EClass containedEDataObjectEClass = + containedEDataObject.eClass(); + + String containedEDataObjectID = + (String) containedEDataObject.eGet( + containedEDataObjectEClass. + getEIDAttribute()); + + attributes.put( + normalizedEReferenceName, + containedEDataObjectID); + } + } + } + return attributes; + } +}
\ No newline at end of file |