diff options
Diffstat (limited to 'das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf')
13 files changed, 2383 insertions, 0 deletions
diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/Context.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/Context.java new file mode 100644 index 0000000000..31d6d8d11e --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/Context.java @@ -0,0 +1,81 @@ + +package org.apache.tuscany.das.ldap.emf; + +import java.util.Hashtable; +import java.util.Map; + +import javax.naming.ldap.LdapContext; + +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class Context +{ + private Map<String, LdapContext> xsdNamespaceToLdapContextMap = null; + private Map<EDataObject, String> dataObjectToRelativeDNCache = null; + + + private LdapContext dasContext = null; + private LdapContext schemaContext = null; + private LdapContext dasMetaContext = null; + private LdapContext ecoreObjectClassesContext = null; + private LdapContext ecoreAttributeTypesContext = null; + + public Context() + { + xsdNamespaceToLdapContextMap = + new Hashtable<String, LdapContext>(); + + dataObjectToRelativeDNCache = + new Hashtable<EDataObject, String>(); + } + + public LdapContext getDasContext() + { + return dasContext; + } + public void setDasContext( LdapContext dasContext ) + { + this.dasContext = dasContext; + } + public LdapContext getSchemaContext() + { + return schemaContext; + } + public void setSchemaContext( LdapContext schemaContext ) + { + this.schemaContext = schemaContext; + } + public LdapContext getDasMetaContext() + { + return dasMetaContext; + } + public void setDasMetaContext( LdapContext dasMetaContext ) + { + this.dasMetaContext = dasMetaContext; + } + public LdapContext getEcoreObjectClassesContext() + { + return ecoreObjectClassesContext; + } + public void setEcoreObjectClassesContext( LdapContext ecoreObjectClassesContext ) + { + this.ecoreObjectClassesContext = ecoreObjectClassesContext; + } + public LdapContext getEcoreAttributeTypesContext() + { + return ecoreAttributeTypesContext; + } + public void setEcoreAttributeTypesContext( LdapContext ecoreAttributeTypesContext ) + { + this.ecoreAttributeTypesContext = ecoreAttributeTypesContext; + } + public Map<String, LdapContext> getXsdNamespaceToLdapContextMap() + { + return xsdNamespaceToLdapContextMap; + } + + public Map<EDataObject, String> getDataObjectToRelativeDNCache() + { + return dataObjectToRelativeDNCache; + } +} diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/LdapDAS.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/LdapDAS.java new file mode 100644 index 0000000000..43e556714f --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/LdapDAS.java @@ -0,0 +1,379 @@ +/* + * 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; + +import java.util.Hashtable; +import java.util.List; +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.ldap.LdapContext; + +import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException; +import org.apache.tuscany.das.ldap.connect.ADSEmbeddedConnection; +import org.apache.tuscany.das.ldap.connect.JNDIConnection; +import org.apache.tuscany.das.ldap.constants.DASConstants; +import org.apache.tuscany.das.ldap.create.InitialContextCreator; +import org.apache.tuscany.das.ldap.create.MetaContextCreator; +import org.apache.tuscany.das.ldap.emf.create.EDataGraphCreator; +import org.apache.tuscany.das.ldap.emf.read.EDataGraphReader; +import org.apache.tuscany.das.ldap.emf.update.EDataGraphUpdater; +import org.apache.tuscany.das.ldap.schema.emf.create.EcoreTypeSystemHelper; +import org.apache.tuscany.das.ldap.schema.emf.create.ModelTypeSystemCreator; +import org.apache.tuscany.model.Configuration; +import org.apache.tuscany.model.DASMeta; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.sdo.EDataGraph; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class LdapDAS +implements DASConstants +{ + private Map<EDataObject, String> metaDataObjectToRelativeDNCache = null; + + private Configuration configuration = null; + private DASMeta dasMeta = null; + private EDataGraph dasMetaDataGraph = null; + private Context context = null; + private ADSEmbeddedConnection adsEmbeddedConnection = null; + + + public LdapDAS(Configuration configuration) + throws NamingException + { + metaDataObjectToRelativeDNCache = + new Hashtable<EDataObject, String>(); + + + this.configuration = + configuration; + + context = + new Context(); + + JNDIConnection jndiConnection = null; + + if (configuration.isEmbedded()) + { + adsEmbeddedConnection = + new ADSEmbeddedConnection(configuration); + context. + setDasContext( + adsEmbeddedConnection. + connect( + configuration. + getDasPartitionName() )); + + context. + setSchemaContext( + adsEmbeddedConnection. + connect( + configuration. + getSchemaPartitionName() )); + } + else + { + jndiConnection = + new JNDIConnection(configuration); + + context. + setDasContext( + jndiConnection.connect( + configuration. + getDasPartitionName() )); + + context. + setSchemaContext( + jndiConnection.connect( + configuration. + getSchemaPartitionName() )); + } + + context. + setDasMetaContext( + MetaContextCreator. + create( + context. + getDasContext() )); + + context.setEcoreObjectClassesContext( + LdapDASHelper. + createEcoreObjectClassesContext( + context. + getSchemaContext() )); + + context.setEcoreAttributeTypesContext( + LdapDASHelper. + createEcoreAttributeTypesContext( + context. + getSchemaContext() )); + } + + /** + * Write. + * + * @param eDataGraph the e data graph + * @param dasContext the das context + * + * @return the map< E data object, string> + * + * @throws NamingException the naming exception + * + * First tries to find a LdapContext that matches the xsdNamespace + * of the DataGraph that is about to be written. If it finds one, then + * it know that the type system has been written, thus it can skip this + * step. + * + * If the LdapContext lookup fails, the type system may still exist + * the check: + * + * configuration.getSupportedSchemas().contains(xsdNamespace) + * + * is performed. + * + * If the supported schema list contains the xsdNamespace, the + * corresponding the LdapContext parent context of the root DataObject + * instance is created and the DataGraph written. + * + * If the xsdNamespace is not contained by the supported schemas list, + * a final check is performed to see whether the type system exists. If this + * check succeeds and exception is thrown, because it should not. This could happen + * if the configuration write that occurs as the supported schema list is updated failed, + * or the the read of the supported schemas from the DIT failed. + * + */ + public void create( + EDataGraph eDataGraph) + throws NamingException, Exception + { + + EObject rootEObject = + eDataGraph. + getERootObject(); + + EPackage ePackage = + rootEObject. + eClass(). + getEPackage(); + + String xsdNamespace = + ePackage. + getNsURI(); + + LdapContext rootContext = + context. + getXsdNamespaceToLdapContextMap(). + get(xsdNamespace); + + if (rootContext != null) + { + EDataGraphCreator.create( + eDataGraph, + rootContext, + context. + getDataObjectToRelativeDNCache()); + } + + /* + * The root context was not yet created so we check if the + * schema is written for the model instance, and if it is + * we create the root context and add it to the context map. + */ + metaDataObjectToRelativeDNCache = + new Hashtable<EDataObject, String>(); + + dasMetaDataGraph = + LdapDASHelper.readDasMeta( + context, + metaDataObjectToRelativeDNCache ); + + dasMeta = ( DASMeta ) dasMetaDataGraph.getRootObject(); + + if (LdapDASHelper.isSchemaReady(xsdNamespace, dasMeta)) + { + rootContext = + InitialContextCreator. + create( + xsdNamespace, + context. + getDasContext() ); + + context.getXsdNamespaceToLdapContextMap(). + put(xsdNamespace, rootContext); + + EDataGraphCreator.create( + eDataGraph, + rootContext, + context.getDataObjectToRelativeDNCache()); + } + + /* + * The model's schema is not yet written (We also do a validation + * check to make sure the server is not in an inconsistent state) + * so we need to write the schema and then write the model instance. + */ + + if (LdapDASHelper.isSchemaWritten( + rootEObject.eClass(), + context. + getEcoreObjectClassesContext() )) + { + throw new RuntimeException("This should not happen");//TODO better message + constant + } + + List<EClass> eClassifiers = + EcoreTypeSystemHelper. + createEClassifiersList(ePackage); + + rootContext = + InitialContextCreator. + create( + xsdNamespace, + context.getDasContext() ); + + context.getXsdNamespaceToLdapContextMap(). + put(xsdNamespace, rootContext); + + LdapContext dasModelMetaContext = + MetaContextCreator. + create(rootContext); + + ModelTypeSystemCreator.create( + dasModelMetaContext, + context.getEcoreAttributeTypesContext(), + context.getEcoreObjectClassesContext(), + eClassifiers, + TUSCANY_OID_PREFIX_VALUE); + + dasMetaDataGraph. + getChangeSummary(). + beginLogging(); + + dasMeta. + getSupportedSchemas(). + add(xsdNamespace); + + dasMetaDataGraph. + getChangeSummary(). + endLogging(); + + EDataGraphUpdater.update( + dasMetaDataGraph, + context.getDasMetaContext(), + metaDataObjectToRelativeDNCache); + + EDataGraphCreator.create( + eDataGraph, + rootContext, + context. + getDataObjectToRelativeDNCache()); + + } + + + public EDataGraph read( + EClass rootDataObjectEClass, + String id) + throws LdapNameNotFoundException, NamingException + { + String xsdNamespace = + rootDataObjectEClass. + getEPackage(). + getNsURI(); + + LdapContext rootContext = + context. + getXsdNamespaceToLdapContextMap(). + get(xsdNamespace); + + if (rootContext == null) + { + rootContext = + InitialContextCreator. + create( + xsdNamespace, + context. + getDasContext() ); + + context. + getXsdNamespaceToLdapContextMap(). + put(xsdNamespace, rootContext); + } + + return EDataGraphReader.read( + rootDataObjectEClass, + id, + rootContext, + context. + getDataObjectToRelativeDNCache()); + } + + public void update(EDataGraph eDataGraph) + throws NamingException + { + EDataObject rootDataObject = + ( EDataObject ) eDataGraph.getRootObject(); + + EClass rootDataObjectEClass = + rootDataObject.eClass(); + + String xsdNamespace = + rootDataObjectEClass. + getEPackage(). + getNsURI(); + + LdapContext rootContext = + context. + getXsdNamespaceToLdapContextMap(). + get(xsdNamespace); + + if (rootContext == null) + { + rootContext = + InitialContextCreator. + create( + xsdNamespace, + context. + getDasContext() ); + + context. + getXsdNamespaceToLdapContextMap(). + put(xsdNamespace, rootContext); + } + + EDataGraphUpdater.update( + eDataGraph, + rootContext, + context. + getDataObjectToRelativeDNCache()); + } + + public Context getContext() + { + return context; + } + + public ADSEmbeddedConnection getAdsEmbeddedConnection() + { + return adsEmbeddedConnection; + } +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/LdapDASHelper.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/LdapDASHelper.java new file mode 100644 index 0000000000..d9d2f9a0bd --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/LdapDASHelper.java @@ -0,0 +1,295 @@ +/* + * 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; + +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 javax.naming.directory.BasicAttributes; +import javax.naming.ldap.LdapContext; + +import org.apache.tuscany.das.ldap.constants.DASConstants; +import org.apache.tuscany.das.ldap.emf.create.EDataGraphCreator; +import org.apache.tuscany.das.ldap.emf.read.EDataGraphReader; +import org.apache.tuscany.das.ldap.schema.constants.AttributeTypeConstants; +import org.apache.tuscany.das.ldap.schema.constants.ObjectClassConstants; +import org.apache.tuscany.das.ldap.schema.constants.SchemaObjectClassConstants; +import org.apache.tuscany.das.ldap.schema.create.ComplexTypeRDNCreator; +import org.apache.tuscany.das.ldap.schema.emf.create.EcoreTypeSystemHelper; +import org.apache.tuscany.das.ldap.schema.emf.create.ModelTypeSystemCreator; +import org.apache.tuscany.model.ConfigurationFactory; +import org.apache.tuscany.model.DASMeta; +import org.apache.tuscany.model.impl.ConfigurationPackageImpl; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.sdo.EDataGraph; +import org.eclipse.emf.ecore.sdo.EDataObject; +import org.eclipse.emf.ecore.sdo.SDOFactory; + +public class LdapDASHelper +implements +DASConstants, +ObjectClassConstants, +AttributeTypeConstants, +SchemaObjectClassConstants +{ + public static EDataGraph readDasMeta( + Context context, + Map<EDataObject, String> metaDataObjectToRelativeDNCache) + throws Exception + { + EDataGraph dasMetaDataGraph = null; + + try + { + dasMetaDataGraph = + EDataGraphReader.read( + ConfigurationPackageImpl. + eINSTANCE.getDASMeta(), + DAS_META_ID_DEFAULT, + context. + getDasMetaContext(), + metaDataObjectToRelativeDNCache); + } + catch(NamingException e) + { + List<EClass> eClassifiers = + EcoreTypeSystemHelper. + createEClassifiersList( + ConfigurationPackageImpl. + eINSTANCE); + + ModelTypeSystemCreator. + create( + context.getDasMetaContext(), + context.getEcoreAttributeTypesContext(), + context.getEcoreObjectClassesContext(), + eClassifiers, + TUSCANY_OID_PREFIX_VALUE ); + + DASMeta dasMeta = + ConfigurationFactory. + INSTANCE. + createDASMeta(); + + dasMetaDataGraph = + SDOFactory. + eINSTANCE. + createEDataGraph(); + + dasMetaDataGraph. + setERootObject( + ( EObject ) dasMeta ); + + EDataGraphCreator.create( + dasMetaDataGraph, + context. + getDasMetaContext(), + metaDataObjectToRelativeDNCache); + } + return dasMetaDataGraph; + } + + public static boolean isSchemaReady( + String xsdNamespace, + DASMeta dasMeta) + { + return + dasMeta. + getSupportedSchemas(). + contains(xsdNamespace); + } + + public static boolean isSchemaWritten( + EClass eClass, + LdapContext ecoreObjectClassesContext) + throws Exception + { + String xsdNamespace = + eClass.getEPackage().getNsURI(); + + String eObjectClassRDN = + ComplexTypeRDNCreator. + create( + TUSCANY_OID_PREFIX_VALUE, + xsdNamespace, + eClass.getName()); + try { + ecoreObjectClassesContext. + lookup( eObjectClassRDN ); + } + catch (Exception e) + { + return false; + } + return true; + } + + + public static boolean isSchemaSupported( + String xsdNamespaceURI, + DASMeta dasMeta) + { + return + dasMeta. + getSupportedSchemas(). + contains(xsdNamespaceURI); + } + + public static void initializeSchemaContexts( + LdapContext schemaContext, + LdapContext ecoreObjectClassesContext, + LdapContext ecoreAttributeTypesContext) + throws NamingException + { + LdapContext ecoreContext = + createEcoreContext(schemaContext); + + ecoreObjectClassesContext = + createEcoreObjectClassesContext(ecoreContext); + + ecoreAttributeTypesContext = + createEcoreAttributeTypesContext( ecoreContext ); + } + + + public static LdapContext createEcoreContext( + LdapContext schemaContext) + throws NamingException + { + String ecoreContextRDN = + CN + "=" + ECORE_CONTEXT_NAME; + + try + { + return ( LdapContext ) schemaContext.lookup( ecoreContextRDN ); + } + catch (Exception e) + { + //Create the context + } + + Attributes contextAttributes = new BasicAttributes(); + + Attribute objectClassAttribute = new BasicAttribute( + OBJECT_CLASS, + TOP); + objectClassAttribute.add(META_SCHEMA); + + contextAttributes.put( objectClassAttribute ); + contextAttributes.put( CN, ECORE_CONTEXT_NAME ); + + return + ( LdapContext ) + schemaContext. + createSubcontext( + ecoreContextRDN, + contextAttributes ); + } + + public static LdapContext createEcoreObjectClassesContext( + LdapContext schemaContext) + throws NamingException + { + LdapContext ecoreContext = + createEcoreContext(schemaContext); + + String ecoreObjectClassesContextRDN = + OU + "=" + OBJECT_CLASSES_CONTEXT_NAME; + + try + { + return + ( LdapContext ) + ecoreContext. + lookup( + ecoreObjectClassesContextRDN ); + } + catch (Exception e) + { + //Create the context + } + + Attributes contextAttributes = new BasicAttributes(); + + Attribute objectClassAttribute = new BasicAttribute( + OBJECT_CLASS, + TOP); + objectClassAttribute.add(ORGANIZATIONAL_UNIT); + + Attribute ecoreAttribute = new BasicAttribute( + OU, + OBJECT_CLASSES_CONTEXT_NAME); + + contextAttributes.put( ecoreAttribute ); + contextAttributes.put( objectClassAttribute ); + + return + ( LdapContext ) + ecoreContext. + createSubcontext( + ecoreObjectClassesContextRDN, + contextAttributes ); + } + + public static LdapContext createEcoreAttributeTypesContext( + LdapContext schemaContext) + throws NamingException + { + LdapContext ecoreContext = + createEcoreContext(schemaContext); + + String ecoreAttributeTypesContextRDN = + OU + "=" + ATTRIBUTE_TYPES_CONTEXT_NAME; + + try + { + return + ( LdapContext ) + ecoreContext. + lookup( + ecoreAttributeTypesContextRDN ); + } + catch (Exception e) + { + //Create the context + } + + Attributes contextAttributes = new BasicAttributes(); + + Attribute objectClassAttribute = new BasicAttribute( + OBJECT_CLASS, + TOP); + objectClassAttribute.add(ORGANIZATIONAL_UNIT); + + contextAttributes.put( OU, ATTRIBUTE_TYPES_CONTEXT_NAME ); + contextAttributes.put( objectClassAttribute ); + + return + ( LdapContext ) + ecoreContext.createSubcontext( + ecoreAttributeTypesContextRDN, + contextAttributes ); + } +}
\ 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/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 diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataGraphReader.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataGraphReader.java new file mode 100644 index 0000000000..9add9b7677 --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataGraphReader.java @@ -0,0 +1,131 @@ +/* + * 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.read; + +import java.util.Hashtable; +import java.util.List; +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import javax.naming.ldap.LdapContext; + +import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException; +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.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.sdo.EDataGraph; +import org.eclipse.emf.ecore.sdo.EDataObject; +import org.eclipse.emf.ecore.sdo.SDOFactory; + +public class EDataGraphReader +implements DASConstants +{ + public static EDataGraph read( + EClass rootDataObjectEClass, + String id, + LdapContext rootContext, + Map<EDataObject, String> dataObjectToRelativeDNCache) + throws NamingException, LdapNameNotFoundException + { + EDataGraph eDataGraph = + SDOFactory.eINSTANCE.createEDataGraph(); + + String namespaceURI = + rootDataObjectEClass.getEPackage(). + getNsURI(); + + EAttribute idEAttribute = + rootDataObjectEClass.getEIDAttribute(); + + String qualifiedIDEAttributeName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + rootDataObjectEClass.getName(), + idEAttribute.getName() ); + + String normalizedIDEAttributeName = + QualifiedNameNormalizer. + normalize(qualifiedIDEAttributeName); + + String eDataObjectRDN = + normalizedIDEAttributeName + "=" + id; + + LdapContext eDataObjectContext = + (LdapContext) + rootContext. + lookup(eDataObjectRDN); + + Attributes attributes = + rootContext. + getAttributes(eDataObjectRDN); + + Map<EDataObject, Map<EStructuralFeature, List<String>>> crossReferenceIDCache = + new Hashtable<EDataObject, Map<EStructuralFeature, List<String>>>(); + + EDataObject rootDataObject = + EDataGraphReaderHelper. + restoreEDataObject( + crossReferenceIDCache, + rootDataObjectEClass, + namespaceURI, + attributes); + + String relativeDN = + EDataGraphReaderHelper.calculateRelativeDN( + rootContext, + eDataObjectContext); + + dataObjectToRelativeDNCache. + put(rootDataObject, relativeDN); + + List<EReference> eReferences = + rootDataObjectEClass.getEAllContainments(); + + if (rootDataObjectEClass.getEAllContainments().size() > 0) + { + EDataGraphReaderHelper. + addContainmentDataObjects( + crossReferenceIDCache, + dataObjectToRelativeDNCache, + rootDataObject, + attributes, + namespaceURI, + eDataObjectContext, + rootContext); + } + + eDataGraph.setERootObject(rootDataObject); + + Resource resource = eDataGraph.getRootResource(); + + EDataGraphReaderHelper.restoreCrossReferences( + crossReferenceIDCache, + resource); + + return eDataGraph; + } +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataGraphReaderHelper.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataGraphReaderHelper.java new file mode 100644 index 0000000000..1496d91dab --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataGraphReaderHelper.java @@ -0,0 +1,365 @@ +/* + * 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.read; + +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; + +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.ldap.LdapContext; + +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.BasicEList; +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.EStructuralFeature; +import org.eclipse.emf.ecore.impl.EClassImpl; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class EDataGraphReaderHelper +implements DASConstants +{ + public static String calculateRelativeDN( + LdapContext containerContext, + LdapContext dataObjectContext) throws NamingException + { + String containerDN = containerContext.getNameInNamespace(); + String dataObjectDN = dataObjectContext.getNameInNamespace(); + + int beginIndex = 0; + int endIndex = + dataObjectDN.length() - containerDN.length() - 1; + + String result = + dataObjectDN.substring( + beginIndex, + endIndex); + + return result; + } + + + public static EDataObject restoreEDataObject( + Map<EDataObject, Map<EStructuralFeature, List<String>>> crossReferenceIDCache, + EClass eClass, + String namespaceURI, + Attributes attributes) + throws NamingException + { + EDataObject eDataObject = + (EDataObject) + eClass.getEPackage(). + getEFactoryInstance(). + create(eClass); + + EDataObjectReaderHelper. + restoreEAttributes( + eClass, + eDataObject, + namespaceURI, + attributes ); + + EStructuralFeature[] eCrossReferences = + ((EClassImpl.FeatureSubsetSupplier) + eDataObject. + eClass(). + getEAllStructuralFeatures()). + crossReferences(); + + if (eCrossReferences != null) + { + Map<EStructuralFeature, List<String>> eReferenceToEObjectIDs = + new Hashtable<EStructuralFeature, List<String>>(); + + for (EStructuralFeature eReference : eCrossReferences) + { + String qualifiedEReferenceName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName(), + eReference.getName() ); + + String normalizedEReferenceName = + QualifiedNameNormalizer. + normalize(qualifiedEReferenceName); + + if (eReference.isMany()) + { + List<String> idList = + new ArrayList<String>(); + + Attribute eReferenceAttribute = + attributes.get(normalizedEReferenceName); + + NamingEnumeration namingEnumeration = + eReferenceAttribute.getAll(); + + while (namingEnumeration.hasMore()) + { + String id = (String) namingEnumeration.next(); + idList.add(id); + eReferenceToEObjectIDs.put(eReference, idList); + } + } + else + { + List<String> idList = + new ArrayList<String>(); + + String value = + (String) + attributes. + get(normalizedEReferenceName). + get(); + + idList.add(value); + eReferenceToEObjectIDs.put(eReference, idList); + } + crossReferenceIDCache.put( + eDataObject, + eReferenceToEObjectIDs); + } + } + return eDataObject; + } + + public static void restoreCrossReferences( + Map<EDataObject, Map<EStructuralFeature, List<String>>> crossReferenceIDCache, + Resource resource) + { + for (EDataObject eDataObject : crossReferenceIDCache.keySet()) + { + Map<EStructuralFeature, List<String>> eReferenceToIDListMap = + crossReferenceIDCache.get(eDataObject); + + for (EStructuralFeature eStructuralFeature : eReferenceToIDListMap.keySet()) + { + if (eStructuralFeature.isMany()) + { + List<String> eReferenceIDs = + eReferenceToIDListMap. + get(eStructuralFeature); + + EList<EDataObject> multiplicityManyList = null; + if (eReferenceIDs.size() > 0) + { + multiplicityManyList = + new BasicEList<EDataObject>(); + } + for (String eReferenceID : eReferenceIDs) + { + Object referencedDataObject = resource.getEObject(eReferenceID); + multiplicityManyList.add((EDataObject) referencedDataObject); + eDataObject.eSet( + eStructuralFeature, + multiplicityManyList); + } + } + else + { + String eReferenceID = + eReferenceToIDListMap. + get(eStructuralFeature). + get(0); + + Object referencedDataObject = resource.getEObject(eReferenceID); + eDataObject.eSet( + eStructuralFeature, + referencedDataObject); + } + } + } + } + + public static void addContainmentDataObjects( + Map crossReferenceIDCache, + Map<EDataObject, String> dataObjectToRelativeDNCache, + EDataObject containerDataObject, + Attributes attributes, + String namespaceURI, + LdapContext eDataObjectContext, + LdapContext rootContext) throws NamingException + { + EClass eClass = + containerDataObject.eClass(); + + List<EReference> eReferences = + eClass.getEAllContainments(); + + for (EReference eReference : eReferences) + { + String qualifiedEReferenceName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName(), + eReference.getName() ); + + String normalizedReferenceName = + QualifiedNameNormalizer. + normalize(qualifiedEReferenceName); + + Attribute attribute = + attributes. + get(normalizedReferenceName); + + if (attribute.size() > 0) + { + LdapContext eReferenceContainmentContext = + (LdapContext) + eDataObjectContext. + lookup("cn=" + eReference.getName()); + + EClass eReferenceType = + (EClass) eReference.getEType(); + + EAttribute eReferenceTypeEIDAttribute = + eReferenceType.getEIDAttribute(); + + String qualifiedEReferenceTypeIDEAttributeName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eReferenceType.getName(), + eReferenceTypeEIDAttribute.getName() ); + + String normalizedReferenceTypeEIDAttributeName = + QualifiedNameNormalizer. + normalize(qualifiedEReferenceTypeIDEAttributeName); + + if (eReference.isMany()) + { + EList<EDataObject> containmentList = + new BasicEList<EDataObject>(); + + NamingEnumeration idNamingEnumeration = + attribute.getAll(); + + while(idNamingEnumeration.hasMore()) + { + String containedDataObjectID = + (String) idNamingEnumeration.next(); + + String containedDataObjectEntryRDN = + normalizedReferenceTypeEIDAttributeName + + "=" + containedDataObjectID; + + Attributes eReferenceAttributes = + eReferenceContainmentContext. + getAttributes(containedDataObjectEntryRDN); + + + EDataObject eReferenceDataObject = + EDataGraphReaderHelper. + restoreEDataObject( + crossReferenceIDCache, + eReferenceType, + namespaceURI, + eReferenceAttributes); + + //TODO Consider using aspects + String relativeDN = + calculateRelativeDN( + rootContext, + (LdapContext) + eReferenceContainmentContext. + lookup(containedDataObjectEntryRDN)); + dataObjectToRelativeDNCache.put(eReferenceDataObject, relativeDN); + //End of Aspect + + containmentList.add(eReferenceDataObject); + + if (eReferenceDataObject.eClass().getEAllContainments().size() > 0) + { + addContainmentDataObjects( + crossReferenceIDCache, + dataObjectToRelativeDNCache, + eReferenceDataObject, + eReferenceAttributes, + namespaceURI, + (LdapContext) eReferenceContainmentContext. + lookup(containedDataObjectEntryRDN), + rootContext); + } + } + containerDataObject.eSet(eReference, containmentList); + } + else + { + String containedDataObjectID = + (String) attribute.get(); + + String containedDataObjectEntryRDN = + normalizedReferenceTypeEIDAttributeName + + "=" + containedDataObjectID; + + Attributes eReferenceAttributes = + eReferenceContainmentContext. + getAttributes(containedDataObjectEntryRDN); + + EDataObject eReferenceDataObject = + EDataGraphReaderHelper. + restoreEDataObject( + crossReferenceIDCache, + eReferenceType, + namespaceURI, + eReferenceAttributes); + + //TODO Consider using aspects + String relativeDN = + calculateRelativeDN( + rootContext, + (LdapContext) + eReferenceContainmentContext. + lookup(containedDataObjectEntryRDN)); + + dataObjectToRelativeDNCache.put(eReferenceDataObject, relativeDN); + //End of Aspect + + containerDataObject.eSet(eReference, eReferenceDataObject); + + if (eReferenceDataObject.eClass().getEAllContainments().size() > 0) + { + addContainmentDataObjects( + crossReferenceIDCache, + dataObjectToRelativeDNCache, + eReferenceDataObject, + eReferenceAttributes, + namespaceURI, + (LdapContext) eReferenceContainmentContext. + lookup(containedDataObjectEntryRDN), + rootContext); + } + } + } + } + } +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataObjectReader.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataObjectReader.java new file mode 100644 index 0000000000..0c812f4bfc --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataObjectReader.java @@ -0,0 +1,100 @@ +/* + * 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.read; + +import javax.naming.NamingException; +import javax.naming.directory.Attributes; +import javax.naming.ldap.LdapContext; + +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.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class EDataObjectReader +implements DASConstants +{ + /** + * Read. + * + * @param eClass the e class + * @param id the id + * @param containerContext the container context + * + * @return the e data object + * + * @throws NamingException the naming exception + * + * Note that this is more of a utility, as only this class's + * helper is used in restoring the EDataGraph. + */ + public static EDataObject read( + EClass eClass, + String id, + LdapContext containerContext) + throws NamingException + { + String namespaceURI = + eClass.getEPackage(). + getNsURI(); + + EAttribute idEAttribute = + eClass.getEIDAttribute(); + + String qualifiedIDEAttributeName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName(), + idEAttribute.getName() ); + + String normalizedIDEAttributeName = + QualifiedNameNormalizer. + normalize(qualifiedIDEAttributeName); + + String eDataObjectRDN = + normalizedIDEAttributeName + "=" + id; + + LdapContext eDataObjectContext = + (LdapContext) + containerContext. + lookup(eDataObjectRDN); + + Attributes attributes = + eDataObjectContext.getAttributes(""); + + EDataObject eDataObject = + (EDataObject) + eClass.getEPackage(). + getEFactoryInstance(). + create(eClass); + + EDataObjectReaderHelper. + restoreEAttributes( + eClass, + eDataObject, + namespaceURI, + attributes ); + + return eDataObject; + } +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataObjectReaderHelper.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataObjectReaderHelper.java new file mode 100644 index 0000000000..2fa6321a65 --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/read/EDataObjectReaderHelper.java @@ -0,0 +1,161 @@ +/* + * 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.read; + +import java.util.List; + +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; + +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.BasicEList; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.sdo.EDataObject; +import org.eclipse.emf.ecore.util.EcoreUtil; + +public class EDataObjectReaderHelper +implements DASConstants +{ + public static void restoreEAttributes( + EClass eClass, + EDataObject eDataObject, + String namespaceURI, + Attributes attributes) + throws NamingException + { + List<EAttribute> eAttributes = + eClass.getEAllAttributes(); + + for (EAttribute eAttribute : eAttributes) + { + String qualifiedEAttributeName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + eClass.getName(), + eAttribute.getName() ); + + String normalizedEAttributeName = + QualifiedNameNormalizer. + normalize(qualifiedEAttributeName); + + if (eAttribute.isMany()) + { + EList<Object> values = + new BasicEList<Object>(); + + Attribute attribute = + attributes. + get(normalizedEAttributeName); + + for (int i = 0; i < attribute.size(); i++) + { + String value = (String) attribute.get(i); + + values.add(EcoreUtil.createFromString( + eAttribute.getEAttributeType(), + value)); + } + + eDataObject.eSet( + eAttribute, values); + } + else + { + String value = + (String) + attributes. + get(normalizedEAttributeName). + get(); + + eDataObject.eSet( + eAttribute, + EcoreUtil.createFromString( + eAttribute.getEAttributeType(), + value)); + } + } + } + + + //TODO Test all of these verifying that we get out what we put in + //TODO Move to helper + //TODO Note that we are using eDataObject.eSet(eAttribute, EcoreUtil.createFromString(eAttribute.getEAttributeType(), value)); instead + /* + public static void setEDataObjectFeature( + EDataObject eDataObject, + String value, + EAttribute eAttribute) + { + if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEString()) + { + eDataObject.eSet(eAttribute, value); + } + else if ( + eAttribute.getEType() == EcorePackage.eINSTANCE.getEInt() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEIntegerObject()) + { + eDataObject.eSet(eAttribute, new Integer(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEBoolean() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEBooleanObject()) + { + eDataObject.eSet(eAttribute, new Boolean(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEFloat() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEFloatObject()) + { + eDataObject.eSet(eAttribute, new Float(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEDouble() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEDoubleObject()) + { + eDataObject.eSet(eAttribute, new Double(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEBigDecimal()) + { + eDataObject.eSet(eAttribute, new BigDecimal(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEBigInteger() ) + { + eDataObject.eSet(eAttribute, new BigInteger(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEByte() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEByteObject()) + { + eDataObject.eSet(eAttribute, new Byte(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEByte() || + eAttribute.getEType() == EcorePackage.eINSTANCE.getEByteObject()) + { + eDataObject.eSet(eAttribute, new Byte(value)); + } + else if (eAttribute.getEType() == EcorePackage.eINSTANCE.getEByteArray()) + { + throw new RuntimeException("Sorry - ByteArrays are not supported."); + } + } + */ +}
\ No newline at end of file diff --git a/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/update/EDataGraphUpdater.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/update/EDataGraphUpdater.java new file mode 100644 index 0000000000..4c22faf1ce --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/update/EDataGraphUpdater.java @@ -0,0 +1,63 @@ +/* + * 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.update; + +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.ldap.LdapContext; + +import org.apache.tuscany.das.ldap.constants.DASConstants; +import org.eclipse.emf.ecore.sdo.EChangeSummary; +import org.eclipse.emf.ecore.sdo.EDataGraph; +import org.eclipse.emf.ecore.sdo.EDataObject; + +public class EDataGraphUpdater +implements DASConstants +{ + public static void update( + EDataGraph eDataGraph, + LdapContext rootContext, + Map<EDataObject, String> dataObjectToRelativeDNCache ) + throws NamingException + { + EChangeSummary eChangeSummary = + (EChangeSummary) + eDataGraph.getChangeSummary(); + + EDataGraphUpdaterHelper. + processChangedDataObjects( + eChangeSummary, + rootContext, + dataObjectToRelativeDNCache); + + EDataGraphUpdaterHelper. + processCreatedDataObjects( + eChangeSummary, + rootContext, + dataObjectToRelativeDNCache); + + EDataGraphUpdaterHelper. + processDestroyedDataObjects( + eChangeSummary, + 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/update/EDataGraphUpdaterHelper.java b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/update/EDataGraphUpdaterHelper.java new file mode 100644 index 0000000000..a53b5547fb --- /dev/null +++ b/das-java/contrib/ldap/das.ldap/src/main/java/org/apache/tuscany/das/ldap/emf/update/EDataGraphUpdaterHelper.java @@ -0,0 +1,228 @@ +/* + * 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.update; + +import java.util.Map; + +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.BasicAttribute; +import javax.naming.directory.DirContext; +import javax.naming.directory.ModificationItem; +import javax.naming.ldap.LdapContext; + +import org.apache.tuscany.das.ldap.emf.create.EDataGraphCreatorHelper; +import org.apache.tuscany.das.ldap.emf.create.EDataObjectCreatorHelper; +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.common.util.EMap; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.change.FeatureChange; +import org.eclipse.emf.ecore.sdo.EChangeSummary; +import org.eclipse.emf.ecore.sdo.EDataObject; + + +public class EDataGraphUpdaterHelper +{ + public static void processDestroyedDataObjects( + EChangeSummary eChangeSummary, + LdapContext containerContext, + Map<EDataObject, String> dataObjectToRelativeDNCache) + throws NamingException + { + EList<EObject> destroyedObjects = + eChangeSummary. + getObjectsToAttach(); + + for (EObject destroyedObject : destroyedObjects) + { + String destroyedObjectRelativeDN = dataObjectToRelativeDNCache.get(destroyedObject); + containerContext.destroySubcontext(destroyedObjectRelativeDN); + dataObjectToRelativeDNCache.remove(destroyedObject); + } + } + + public static void processCreatedDataObjects( + EChangeSummary eChangeSummary, + LdapContext rootContext, + Map<EDataObject, String> dataObjectToRelativeDNCache ) + throws NamingException + { + EList<EObject> createdObjects = + eChangeSummary. + getObjectsToDetach(); + + EDataGraphCreatorHelper.createChildren( + createdObjects, + rootContext, + dataObjectToRelativeDNCache); + } + + public static void processChangedDataObjects( + EChangeSummary eChangeSummary, + LdapContext containerContext, + Map<EDataObject, String> dataObjectToRelativeDNCache ) + throws NamingException + { + EMap<EObject, EList<FeatureChange>> objectChanges = + eChangeSummary.getObjectChanges(); + + for (EObject changedDataObject : objectChanges.keySet()) + { + EList<FeatureChange> featureChanges = + objectChanges.get(changedDataObject); + + ModificationItem[] modificationItems = + new ModificationItem[featureChanges.size()]; + + EClass changedDataObjectEClass = + changedDataObject.eClass(); + + String namespaceURI = + changedDataObjectEClass. + getEPackage(). + getNsURI(); + + for (int i = 0; i < featureChanges.size(); i++) + { + FeatureChange featureChange = + featureChanges.get(i); + + EStructuralFeature eStructuralFeature = + featureChange.getFeature(); + + if (eStructuralFeature instanceof EAttribute) + { + //TODO Note that we are not checking multiplicity many on EAttributes + String qualifiedEAttributeName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + changedDataObjectEClass.getName(), + eStructuralFeature.getName() ); + + String normalizedEAttributeName = + QualifiedNameNormalizer. + normalize(qualifiedEAttributeName); + + //TODO PUt processEAttribute in a more generic class + Attribute attribute = + EDataObjectCreatorHelper.processEAttribute( + (EAttribute)eStructuralFeature, + namespaceURI, + changedDataObjectEClass, + (EDataObject) changedDataObject); + + modificationItems[i] = + new ModificationItem( + DirContext.REPLACE_ATTRIBUTE, + attribute); + } + else + { + String qualifiedEReferenceName = + SimpleTypeNamespaceQualifier. + qualify( + namespaceURI, + changedDataObjectEClass.getName(), + eStructuralFeature.getName() ); + + String normalizedEReferenceName = + QualifiedNameNormalizer. + normalize(qualifiedEReferenceName); + + Attribute attribute = + new BasicAttribute(normalizedEReferenceName); + + if (eStructuralFeature.isMany()) + { + EList<EDataObject> referenceList = + (EList<EDataObject>) changedDataObject.eGet(eStructuralFeature); + + if (referenceList.size() > 0 && referenceList!=null) + { + for (EDataObject eDataObject : referenceList) + { + attribute.add( + eDataObject. + eGet( + eDataObject. + eClass(). + getEIDAttribute())); + + modificationItems[i] = + new ModificationItem( + DirContext.REPLACE_ATTRIBUTE, + attribute); + } + } + else + { + modificationItems[i] = + new ModificationItem( + DirContext.REMOVE_ATTRIBUTE, + attribute); + } + } + else + { + EDataObject eDataObject = + (EDataObject) + changedDataObject. + eGet(eStructuralFeature); + + if (eDataObject != null) + { + attribute.add( + eDataObject. + eGet( + eDataObject. + eClass(). + getEIDAttribute())); + + modificationItems[i] = + new ModificationItem( + DirContext.REPLACE_ATTRIBUTE, + attribute); + } + else + { + modificationItems[i] = + new ModificationItem( + DirContext.REMOVE_ATTRIBUTE, + attribute); + } + } + } + } + String relativeDN = + dataObjectToRelativeDNCache. + get(changedDataObject); + + containerContext.modifyAttributes( + relativeDN, + modificationItems); + } + } +}
\ No newline at end of file |