You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2007/07/29 19:06:36 UTC
svn commit: r560760 - in /incubator/tuscany/java/sca:
modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/
modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/module/
samples/helloworld-ws-sdo/src/main/...
Author: lresende
Date: Sun Jul 29 10:06:35 2007
New Revision: 560760
URL: http://svn.apache.org/viewvc?view=rev&rev=560760
Log:
Use the contribution service to introspect SDO Static Types for populating the HelperContext
Added:
incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOPostProcessor.java
Modified:
incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/module/SDODataBindingModuleActivator.java
incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldws.composite
incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldwsclient.composite
Added: incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOPostProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOPostProcessor.java?view=auto&rev=560760
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOPostProcessor.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/ImportSDOPostProcessor.java Sun Jul 29 10:06:35 2007
@@ -0,0 +1,143 @@
+/*
+ * 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.sca.databinding.sdo;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import org.apache.tuscany.sca.contribution.Contribution;
+import org.apache.tuscany.sca.contribution.DeployedArtifact;
+import org.apache.tuscany.sca.contribution.processor.ContributionPostProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ClassReference;
+import org.apache.tuscany.sdo.api.SDOUtil;
+
+import commonj.sdo.helper.HelperContext;
+import commonj.sdo.impl.HelperProvider;
+
+/**
+ * PostProcessor resposible for identifying SDO Factories and register them with SDO Helper Context
+ *
+ * @version $Rev$ $Date$
+ */
+public class ImportSDOPostProcessor implements ContributionPostProcessor {
+ private static final String URI_SEPARATOR = "/";
+ private static final String JAVA_SEPARATOR = ".";
+
+ private HelperContextRegistry helperContextRegistry;
+
+ public ImportSDOPostProcessor(HelperContextRegistry helperContextRegistry) {
+ super();
+ this.helperContextRegistry = helperContextRegistry;
+ }
+
+ public void visit(Contribution contribution) {
+ for (DeployedArtifact artifact : contribution.getArtifacts()) {
+ String artifactURI = artifact.getURI();
+ if (artifactURI.endsWith("Factory.class")) {
+ //load the factory and prepare to register the class with SDO
+ String factoryName = getFactoryClassName(artifactURI);
+ ClassReference clazz = new ClassReference(factoryName);
+ clazz = contribution.getModelResolver().resolveModel(ClassReference.class, clazz);
+ if (clazz.getClass() != null) {
+ try {
+ //check if it's a SDO factory by introspecting INSTANCE field
+ if(isSDOFactory(clazz.getJavaClass())) {
+ register(clazz.getJavaClass(), this.getHelperContext());
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Transform class artifact URI into a java class name for proper loading by the class loader
+ * @param factoryURI
+ * @return
+ */
+ private String getFactoryClassName(String factoryURI) {
+ factoryURI = factoryURI.replace(URI_SEPARATOR, JAVA_SEPARATOR);
+ int pos = factoryURI.lastIndexOf(JAVA_SEPARATOR);
+ return factoryURI.substring(0, pos);
+ }
+
+ /**
+ * Check if a specific class is a SDO Factory by checking INSTANCE field
+ * @param factoryClass
+ * @return
+ */
+ private boolean isSDOFactory(Class factoryClass) {
+ Field field = null;
+ try {
+ field = factoryClass.getField("INSTANCE");
+ } catch (Exception e) {
+ // ignore any exception
+ }
+
+ if (field != null) {
+ return true;
+ } else {
+ return false;
+ }
+
+ }
+
+ /**
+ * Get a SDO HelperContext reference
+ * @return
+ */
+ private HelperContext getHelperContext() {
+ HelperContext helperContext = null;
+
+ // FIXME: [rfeng] How to get the enclosing composite?
+ int id = System.identityHashCode(getClass());
+ // FIXME: [rfeng] How to associate the TypeHelper with deployment
+ // context?
+ synchronized (helperContextRegistry) {
+ helperContext = helperContextRegistry.getHelperContext(id);
+ if (helperContext == null) {
+ helperContext = SDOUtil.createHelperContext();
+ helperContextRegistry.register(id, helperContext);
+ }
+ }
+
+ return helperContext;
+ }
+
+ /**
+ * Register an SDO Factory with the helper context
+ * @param factoryClass
+ * @param helperContext
+ * @throws Exception
+ */
+ private static void register(Class factoryClass, HelperContext helperContext) throws Exception {
+ Field field = factoryClass.getField("INSTANCE");
+ Object factory = field.get(null);
+ Method method = factory.getClass().getMethod("register", new Class[] {HelperContext.class});
+ method.invoke(factory, new Object[] {helperContext});
+
+ // FIXME: How do we associate the application HelperContext with the one imported by the composite
+ HelperContext defaultContext = HelperProvider.getDefaultContext();
+ method.invoke(factory, new Object[] {defaultContext});
+ }
+}
Modified: incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/module/SDODataBindingModuleActivator.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/module/SDODataBindingModuleActivator.java?view=diff&rev=560760&r1=560759&r2=560760
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/module/SDODataBindingModuleActivator.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/module/SDODataBindingModuleActivator.java Sun Jul 29 10:06:35 2007
@@ -32,7 +32,7 @@
import org.apache.tuscany.sca.databinding.sdo.HelperContextProcessor;
import org.apache.tuscany.sca.databinding.sdo.HelperContextRegistry;
import org.apache.tuscany.sca.databinding.sdo.HelperContextRegistryImpl;
-//import org.apache.tuscany.sca.databinding.sdo.ImportSDOPostProcessor;
+import org.apache.tuscany.sca.databinding.sdo.ImportSDOPostProcessor;
import org.apache.tuscany.sca.databinding.sdo.ImportSDOProcessor;
import org.apache.tuscany.sca.databinding.sdo.SDODataBinding;
import org.apache.tuscany.sca.databinding.sdo.String2DataObject;
@@ -62,8 +62,8 @@
HelperContextRegistry contextRegistry = new HelperContextRegistryImpl();
processors.addArtifactProcessor(new ImportSDOProcessor(contextRegistry));
- //ContributionPostProcessorExtensionPoint postProcessors = registry.getExtensionPoint(ContributionPostProcessorExtensionPoint.class);
- //postProcessors.addPostProcessor(new ImportSDOPostProcessor());
+ ContributionPostProcessorExtensionPoint postProcessors = registry.getExtensionPoint(ContributionPostProcessorExtensionPoint.class);
+ postProcessors.addPostProcessor(new ImportSDOPostProcessor(contextRegistry));
TransformerExtensionPoint transformers = registry.getExtensionPoint(TransformerExtensionPoint.class);
transformers.addTransformer(new DataObject2String());
Modified: incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldws.composite
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldws.composite?view=diff&rev=560760&r1=560759&r2=560760
==============================================================================
--- incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldws.composite (original)
+++ incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldws.composite Sun Jul 29 10:06:35 2007
@@ -21,7 +21,7 @@
xmlns:dbsdo="http://tuscany.apache.org/xmlns/sca/databinding/sdo/1.0" xmlns:hw="http://helloworld"
name="helloworldws">
- <dbsdo:import.sdo factory="helloworld.HelloworldFactory" />
+ <!-- dbsdo:import.sdo factory="helloworld.HelloworldFactory" / -->
<component name="HelloWorldServiceComponent">
<service name="HelloWorldService">
Modified: incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldwsclient.composite
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldwsclient.composite?view=diff&rev=560760&r1=560759&r2=560760
==============================================================================
--- incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldwsclient.composite (original)
+++ incubator/tuscany/java/sca/samples/helloworld-ws-sdo/src/main/resources/helloworldwsclient.composite Sun Jul 29 10:06:35 2007
@@ -21,7 +21,7 @@
xmlns:dbsdo="http://tuscany.apache.org/xmlns/sca/databinding/sdo/1.0" xmlns:hw="http://helloworld"
name="helloworldwsclient">
- <dbsdo:import.sdo factory="helloworld.HelloworldFactory" />
+ <!-- dbsdo:import.sdo factory="helloworld.HelloworldFactory" / -->
<component name="HelloWorldServiceComponent">
<implementation.java class="helloworld.HelloWorldServiceComponent" />
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org