From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../container/dataaccess/DataAccessComponent.java | 128 ++++++++ .../dataaccess/DataAccessComponentBuilder.java | 108 +++++++ .../DataAccessComponentConfiguration.java | 137 +++++++++ .../dataaccess/DataAccessComponentType.java | 58 ++++ .../dataaccess/DataAccessComponentTypeLoader.java | 130 ++++++++ .../dataaccess/DataAccessImplementation.java | 79 +++++ .../dataaccess/DataAccessImplementationLoader.java | 92 ++++++ .../container/dataaccess/DataAccessInstance.java | 30 ++ .../dataaccess/DataAccessInstanceFactory.java | 92 ++++++ .../container/dataaccess/DataAccessInvoker.java | 67 +++++ .../dataaccess/MissingSideFileException.java | 44 +++ .../container/dataaccessscript/CommandMapper.java | 44 +++ .../dataaccessscript/DataAccessEngine.java | 65 ++++ .../dataaccessscript/DataAccessInstanceImpl.java | 328 +++++++++++++++++++++ .../dataaccessscript/DataAccessUtils.java | 90 ++++++ 15 files changed, 1492 insertions(+) create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponent.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentBuilder.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentConfiguration.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentType.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentTypeLoader.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementation.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementationLoader.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstance.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstanceFactory.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInvoker.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/MissingSideFileException.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java create mode 100644 sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java (limited to 'sandbox/lresende/backup/container.das/src/main/java/org') diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponent.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponent.java new file mode 100644 index 0000000000..abd1a587d0 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponent.java @@ -0,0 +1,128 @@ +/* + * 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.container.dataaccess; + +import static org.apache.tuscany.spi.idl.java.JavaIDLUtils.findMethod; + +import java.lang.reflect.Method; +import java.util.List; + +import org.apache.tuscany.container.dataaccessscript.DataAccessInstanceImpl; +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.extension.AtomicComponentExtension; +import org.apache.tuscany.spi.model.Operation; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.TargetInvoker; +import org.apache.tuscany.spi.wire.WireService; + +/** + * The DAS component implementation. + */ +public class DataAccessComponent extends AtomicComponentExtension { + + private final List> services; + //private final Map properties; + + private DataAccessInstanceFactory factory; + + + public DataAccessComponent(String name, + DataAccessInstanceFactory factory, + /*Map propValues,*/ + List> services, + CompositeComponent parent, + ScopeContainer scopeContainer, + WireService wireService, + WorkScheduler workScheduler, + WorkContext workContext) { + super(name, parent, /*scopeContainer,*/ wireService, workContext, workScheduler, null, 0); + this.factory = factory; + this.services = services; + this.scope = scopeContainer.getScope(); + //this.properties = propValues; + } + + public DataAccessComponent(DataAccessComponentConfiguration config) { + super(config.getName(), + config.getParent(), + /*config.getScopeContainer(),*/ + config.getWireService(), + config.getWorkContext(), + config.getWorkScheduler(), + config.getMonitor(), + config.getInitLevel()); + this.factory = config.getFactory(); + this.services = config.getServices(); + this.scope = config.getScopeContainer().getScope(); + //this.properties = config.getPropertyValues(); + } + + + + public Object createInstance() throws ObjectCreationException { +// Map context = new HashMap(getProperties()); +// Object instance = dataAccessAdapter.createScriptInstance(context, dataAccessAdapter.getDataAccessType()); +// +// return instance; + + return factory.getInstance(); + } + + + public TargetInvoker createTargetInvoker(String targetName, Operation operation, InboundWire callbackWire) { + //System.out.println("operation:"+operation.getName()+operation.getMetaData()); + //return new DataAccessInvoker(operation.getName(), this, operation.getOutputType().getPhysical().getClass()); + + Method[] methods = operation.getServiceContract().getInterfaceClass().getMethods(); + Method method = findMethod(operation, methods); + return new DataAccessInvoker(method.getName(), this, operation.getOutputType().getPhysical().getClass()); + } + // TODO: move all the following up to AtomicComponentExtension? + public List> getServiceInterfaces() { + return services; + } + +// public Map getProperties() { +// return properties; +// } + + public Object getServiceInstance() throws TargetResolutionException { + return getServiceInstance(null); + } + + @SuppressWarnings("unchecked") + public Object getServiceInstance(String service) throws TargetResolutionException { + InboundWire wire = getInboundWire(service); + if (wire == null) { + throw new TargetResolutionException("Service '" + service + "'not found"); // TODO better error message + } + Class clazz = wire.getServiceContract().getInterfaceClass(); + return wireService.createProxy(clazz, wire); + } + + public DataAccessInstanceImpl getTargetInstance() throws TargetResolutionException { + return (DataAccessInstanceImpl) scopeContainer.getInstance(this); + } + +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentBuilder.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentBuilder.java new file mode 100644 index 0000000000..339ca2e233 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentBuilder.java @@ -0,0 +1,108 @@ +/* + * 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.container.dataaccess; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentBuilderExtension; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.PropertyValue; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceDefinition; + +/** + * Extension point for creating {@link DataAccessComponent}s from an assembly configuration + */ +public class DataAccessComponentBuilder extends ComponentBuilderExtension { + + protected Class getImplementationType() { + return DataAccessImplementation.class; + } + + @SuppressWarnings("unchecked") + public Component build(CompositeComponent parent, + ComponentDefinition componentDefinition, + DeploymentContext deploymentContext) throws BuilderConfigException { + + String name = componentDefinition.getName(); + DataAccessImplementation implementation = componentDefinition.getImplementation(); + DataAccessComponentType componentType = implementation.getComponentType(); + + // get list of services provided by this component + Collection collection = componentType.getServices().values(); + List> services = new ArrayList>(collection.size()); + for (ServiceDefinition serviceDefinition : collection) { + services.add(serviceDefinition.getServiceContract().getInterfaceClass()); + } + + + //TODO: have ComponentBuilderExtension pass ScopeContainer in on build method? + ScopeContainer scopeContainer; + Scope scope = componentType.getLifecycleScope(); + if (Scope.COMPOSITE == scope) { + scopeContainer = deploymentContext.getCompositeScope(); + } else { + scopeContainer = scopeRegistry.getScopeContainer(scope); + } + + + //removed per jim's comments + //Map propertyValues = new Hashtable(); + //Collection> propValueSettings = componentDefinition.getPropertyValues().values(); + //for (PropertyValue propertyValue : propValueSettings) { + // propertyValues.put(propertyValue.getName(), propertyValue.getValueFactory().getInstance()); + //} + + String dasConfig = implementation.getDasConfig(); + InputStream dasConfigStream = implementation.getDasConfigStream(); + String dataAccessType = implementation.getDataAccessType(); + ClassLoader cl = implementation.getClassLoader(); + + DataAccessInstanceFactory instanceFactory = new DataAccessInstanceFactory(dasConfig, dasConfigStream, dataAccessType, cl); + + + //get the properties for the component + for (PropertyValue propertyValue : componentDefinition.getPropertyValues().values()) { + //TODO this is not safe for since multiple instances can share mutable properties + instanceFactory.addContextObjectFactory(propertyValue.getName(), propertyValue.getValueFactory ()); + } + + + + DataAccessComponentConfiguration config = new DataAccessComponentConfiguration(); + config.setName(name); + config.setFactory(instanceFactory); + config.setServices(services); + config.setParent(parent); + config.setScopeContainer(scopeContainer); + config.setWireService(wireService); + config.setWorkContext(workContext); + config.setWorkScheduler(workScheduler); + + return new DataAccessComponent(config); + } +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentConfiguration.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentConfiguration.java new file mode 100644 index 0000000000..01ef7391f7 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentConfiguration.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.container.dataaccess; + +import java.util.List; + +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.extension.ExecutionMonitor; +import org.apache.tuscany.spi.services.work.WorkScheduler; +import org.apache.tuscany.spi.wire.WireService; + +/** + * Configuration holder for creating script components + * + * @version $Rev: 471211 $ $Date: 2006-11-04 07:06:05 -0800 (Sat, 04 Nov 2006) $ + */ +public class DataAccessComponentConfiguration { + + private String name; + private DataAccessInstanceFactory factory; + private List> services; + //private Map propertyValues; + private CompositeComponent parent; + private ScopeContainer scopeContainer; + private WireService wireService; + private WorkContext workContext; + private WorkScheduler workScheduler; + private ExecutionMonitor monitor; + private int initLevel; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public DataAccessInstanceFactory getFactory() { + return factory; + } + + public void setFactory(DataAccessInstanceFactory factory) { + this.factory = factory; + } + + public List> getServices() { + return services; + } + + public void setServices(List> services) { + this.services = services; + } + +// public Map getPropertyValues() { +// return propertyValues; +// } +// +// public void setPropertyValues(Map propValues) { +// this.propertyValues = propValues; +// } + + public CompositeComponent getParent() { + return parent; + } + + public void setParent(CompositeComponent parent) { + this.parent = parent; + } + + public ScopeContainer getScopeContainer() { + return scopeContainer; + } + + public void setScopeContainer(ScopeContainer scopeContainer) { + this.scopeContainer = scopeContainer; + } + + public WireService getWireService() { + return wireService; + } + + public void setWireService(WireService wireService) { + this.wireService = wireService; + } + + public WorkContext getWorkContext() { + return workContext; + } + + public void setWorkContext(WorkContext workContext) { + this.workContext = workContext; + } + + public WorkScheduler getWorkScheduler() { + return workScheduler; + } + + public void setWorkScheduler(WorkScheduler workScheduler) { + this.workScheduler = workScheduler; + } + + public ExecutionMonitor getMonitor() { + return monitor; + } + + public void setMonitor(ExecutionMonitor monitor) { + this.monitor = monitor; + } + + public int getInitLevel() { + return initLevel; + } + + public void setInitLevel(int initLevel) { + this.initLevel = initLevel; + } + +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentType.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentType.java new file mode 100644 index 0000000000..7b2573fb68 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentType.java @@ -0,0 +1,58 @@ +/* + * 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.container.dataaccess; + +import org.apache.tuscany.spi.model.ComponentType; +import org.apache.tuscany.spi.model.Property; +import org.apache.tuscany.spi.model.ReferenceDefinition; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.model.ServiceDefinition; + +public class DataAccessComponentType extends ComponentType> { + + private Scope lifecycleScope = Scope.COMPOSITE; + + public DataAccessComponentType() { + } + + @SuppressWarnings("unchecked") + public DataAccessComponentType(ComponentType ct) { + if(ct != null){ + setInitLevel(ct.getInitLevel()); + for (Object property : ct.getProperties().values()) { + add((Property) property); + } + for (Object reference : ct.getReferences().values()) { + add((ReferenceDefinition) reference); + } + for (Object service : ct.getServices().values()) { + add((ServiceDefinition) service); + } + } + } + + public Scope getLifecycleScope() { + return lifecycleScope; + } + + public void setLifecycleScope(Scope lifecycleScope) { + this.lifecycleScope = lifecycleScope; + } + +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentTypeLoader.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentTypeLoader.java new file mode 100644 index 0000000000..f8521eb901 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessComponentTypeLoader.java @@ -0,0 +1,130 @@ +/* + * 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.container.dataaccess; + +import java.net.URL; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentTypeLoaderExtension; +import org.apache.tuscany.spi.idl.java.JavaInterfaceProcessorRegistry; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.model.ComponentType; + +/** + * @version $Rev: 442514 $ $Date: 2006-09-12 13:44:43 +0530 (Tue, 12 Sep 2006) $ + */ +public class DataAccessComponentTypeLoader extends ComponentTypeLoaderExtension { + + //private JavaInterfaceProcessorRegistry processorRegistry; + + public DataAccessComponentTypeLoader(@Autowire JavaInterfaceProcessorRegistry processorRegistry) { + //this.processorRegistry = processorRegistry; + } + + @Override + protected Class getImplementationClass() { + return DataAccessImplementation.class; + } +/* +// protected String getResourceName(DataAccessImplementation implementation) { +// return implementation.getDataAccessAdapter().getScriptName(); +// } + + // TODO: must be possible to move all the following up in to ComponentTypeLoaderExtension + + public void load(CompositeComponent parent, + DataAccessImplementation implementation, + DeploymentContext deploymentContext) throws LoaderException { + + + DataAccessComponentType componentType; + componentType = loadFromSidefile(null, deploymentContext); + + implementation.setComponentType(componentType); + } + + protected DataAccessComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext) throws LoaderException { + ComponentType ct = loaderRegistry.load(null, null, + url, + ComponentType.class, + deploymentContext); + DataAccessComponentType dasCt = new DataAccessComponentType(ct); + return dasCt; + } + +// private String getSideFileName(DataAccessImplementation implementation) { +// String baseName = getResourceName(implementation); +// int lastDot = baseName.lastIndexOf('.'); +// if (lastDot != -1) { +// baseName = baseName.substring(0,lastDot); +// } +// return baseName + ".componentType"; +// } +*/ + + protected String getResourceName(DataAccessImplementation implementation) { + //System.out.println("config file name:"+implementation.getDasConfig()); + return implementation.getDasConfig(); + } + + // TODO: must be possible to move all the following up in to ComponentTypeLoaderExtension + + public void load(CompositeComponent parent, + DataAccessImplementation implementation, + DeploymentContext deploymentContext) throws LoaderException { + + String sideFile = getSideFileName(implementation); + URL resource = implementation.getClassLoader().getResource(sideFile); + DataAccessComponentType componentType; + + if (resource == null) { + throw new MissingSideFileException("Component type side file not found : '" + sideFile + "'"); + // TODO: or else implement introspection + } else { + componentType = loadFromSidefile(resource, deploymentContext); + } + + implementation.setComponentType(componentType); + } + + protected DataAccessComponentType loadFromSidefile(URL url, DeploymentContext deploymentContext) throws LoaderException { + //Amita + DataAccessComponentType compType = new DataAccessComponentType(); + ComponentType ct = loaderRegistry.load(null, + compType, + url, + ComponentType.class, + deploymentContext); + return new DataAccessComponentType(ct); + } + + private String getSideFileName(DataAccessImplementation implementation) { + String baseName = getResourceName(implementation); + int lastDot = baseName.lastIndexOf('.'); + if (lastDot != -1) { + baseName = baseName.substring(0, + lastDot); + } + return baseName + ".componentType"; + } + + +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementation.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementation.java new file mode 100644 index 0000000000..6ed98b88be --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementation.java @@ -0,0 +1,79 @@ +/* + * 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.container.dataaccess; + +import java.io.InputStream; + +import org.apache.tuscany.spi.model.AtomicImplementation; + +/** + * Model object for a DataAccess implementation. + */ +public class DataAccessImplementation extends AtomicImplementation { + private String dasConfig; + private InputStream dasConfigStream; + private String dataAccessType; + private ClassLoader classLoader; + + /** + * @return the dasConfig + */ + public String getDasConfig() { + return dasConfig; + } + /** + * @param dasConfig the dasConfig to set + */ + public void setDasConfig(String dasConfig) { + this.dasConfig = dasConfig; + } + + /** + * @return the dasConfigStream + */ + public InputStream getDasConfigStream() { + return dasConfigStream; + } + /** + * @param dasConfigStream the dasConfigStream to set + */ + public void setDasConfigStream(InputStream dasConfigStream) { + this.dasConfigStream = dasConfigStream; + } + /** + * @return the dataAccessType + */ + public String getDataAccessType() { + return dataAccessType; + } + /** + * @param dataAccessType the dataAccessType to set + */ + public void setDataAccessType(String dataAccessType) { + this.dataAccessType = dataAccessType; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } +} \ No newline at end of file diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementationLoader.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementationLoader.java new file mode 100644 index 0000000000..01fb6ba89e --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessImplementationLoader.java @@ -0,0 +1,92 @@ +/* + * 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.container.dataaccess; + +import java.io.InputStream; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.loader.MissingResourceException; +import org.apache.tuscany.spi.model.ModelObject; +import org.osoa.sca.annotations.Constructor; + +/** + * Loader for handling JavaScript elements. + * + * Example would be as below + * + * + * + * + * + */ +public class DataAccessImplementationLoader extends LoaderExtension { + private static String XML_DATAACCESS_NAMESPACE = "http://tuscany.apache.org/xmlns/das/1.0"; + private static final QName IMPLEMENTATION_DATAACCESS = new QName(XML_DATAACCESS_NAMESPACE, "implementation.das"); + + @Constructor({"registry"}) + public DataAccessImplementationLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return IMPLEMENTATION_DATAACCESS; + } + + public DataAccessImplementation load(CompositeComponent parent, + ModelObject mo, + XMLStreamReader reader, + DeploymentContext deploymentContext) throws XMLStreamException, LoaderException { + + String dasConfig = reader.getAttributeValue(null, "config"); + String dataAccessType = reader.getAttributeValue(null, "dataAccessType"); + + if ( dasConfig == null ) { + throw new MissingResourceException("No config provided supplied"); + } + + ClassLoader cl = deploymentContext.getClassLoader(); + InputStream dasConfigStream = loadConfig(cl, dasConfig); + + LoaderUtil.skipToEndElement(reader); + + DataAccessImplementation implementation = new DataAccessImplementation(); + implementation.setDasConfig(dasConfig); + implementation.setDasConfigStream(dasConfigStream); + implementation.setDataAccessType(dataAccessType); + implementation.setClassLoader(cl); + + registry.loadComponentType(parent, implementation, deploymentContext); + return implementation; + } + + protected InputStream loadConfig(ClassLoader classLoader, String configResource) throws LoaderException { + return classLoader.getResourceAsStream(configResource); + + } +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstance.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstance.java new file mode 100644 index 0000000000..6ea3bd6613 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstance.java @@ -0,0 +1,30 @@ +/* + * 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.container.dataaccess; + + +/** + * An invokable instance of a script + * + * Basically just a wrapper around a BSF engine with an optional script class object. + */ +public interface DataAccessInstance { + + public Object invokeFunction(String functionName, Object[] args, Class returnType); +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstanceFactory.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstanceFactory.java new file mode 100644 index 0000000000..96b1deab00 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstanceFactory.java @@ -0,0 +1,92 @@ +/* + * 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.container.dataaccess; + +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.container.dataaccessscript.DataAccessEngine; +import org.apache.tuscany.container.dataaccessscript.DataAccessInstanceImpl; +import org.apache.tuscany.das.rdb.DAS; +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; + +/** + * A data access script + */ +public class DataAccessInstanceFactory implements ObjectFactory { + protected String dasConfig; + protected InputStream dasConfigStream; + protected String dataAccessType; + protected ClassLoader classLoader; + protected Map contextObjects; + + private DataAccessEngine dataAccessEngine = new DataAccessEngine(); + + /** + * Create a new DataAccess object. + * + * @param dasConfig + * the name of the Data Access configuration file to be used + * @param dataAccessType + * what type of data access mechanism is being used (e.g rdb) + */ + public DataAccessInstanceFactory(String dasConfiguration, InputStream dasConfigStream, String dataAccessType, ClassLoader classLoader) { + this.dasConfig = dasConfiguration; + this.dasConfigStream = dasConfigStream; + this.dataAccessType = dataAccessType; + this.classLoader = classLoader; + } + + /** + * Create a new invokeable instance of the script

objects to add to scope of the script instance + * + * @return a DataAccessInstance + */ + public DataAccessInstance getInstance() throws ObjectCreationException { + if (dataAccessType == null) { + return null; + } else { + //the commented one gives STREAM CLOSED exception from xerces - in JUnit??? + DAS das = dataAccessEngine.getDAS(dataAccessType, dasConfig);// factory, currently only for rdb + //DAS das = dataAccessEngine.getDAS(dataAccessType, dasConfigStream);// factory, currently only for rdb + + return new DataAccessInstanceImpl(das, this.dataAccessType); + } + } + + protected Map getResponseClasses(List services) { + Map responseClasses = new HashMap(); + if (services != null) { + for (Class s : services) { + for (Method m : s.getMethods()) { + responseClasses.put(m.getName(), m.getReturnType()); + } + } + } + return responseClasses; + } + + public void addContextObjectFactory(String name, ObjectFactory factory) { + contextObjects.put(name, factory); + } +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInvoker.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInvoker.java new file mode 100644 index 0000000000..f9d7aaf225 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInvoker.java @@ -0,0 +1,67 @@ +/* + * 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.container.dataaccess; + +import java.lang.reflect.InvocationTargetException; + +import org.apache.tuscany.container.dataaccessscript.DataAccessInstanceImpl; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.extension.TargetInvokerExtension; + +/** + * Dispatches to a DataAccessScript implementation instance + * + * @version $$Rev: 441072 $$ $$Date: 2006-09-07 18:03:39 +0530 (Thu, 07 Sep 2006) $$ + */ +public class DataAccessInvoker extends TargetInvokerExtension { + + private DataAccessComponent component; + private String functionName; + private Class returnType; + + + public DataAccessInvoker(String functionName, DataAccessComponent component, Class returnType) { + super(null,null,null); + this.functionName = functionName; + this.component = component; + this.returnType = returnType; + } + + + public Object invokeTarget(final Object payload, final short sequence) throws InvocationTargetException { + DataAccessInstanceImpl target; + + //get the component + try{ + target = (DataAccessInstanceImpl) component.getTargetInstance(); + }catch(TargetException e){ + throw new InvocationTargetException(e); + } + + //invoke it + try { + return target.invokeFunction(functionName, + (Object[]) payload, + returnType); + } catch (Exception e) { + throw new InvocationTargetException(e); + } + } + +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/MissingSideFileException.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/MissingSideFileException.java new file mode 100644 index 0000000000..f19cb91406 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/MissingSideFileException.java @@ -0,0 +1,44 @@ +/* + * 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.container.dataaccess; + +import org.apache.tuscany.spi.loader.LoaderException; + +/** + * @version $Rev: 471211 $ $Date: 2006-11-04 07:06:05 -0800 (Sat, 04 Nov 2006) $ + */ +public class MissingSideFileException extends LoaderException { + + private static final long serialVersionUID = -2616590185174691724L; + + public MissingSideFileException() { + } + + public MissingSideFileException(String message) { + super(message); + } + + public MissingSideFileException(String message, Throwable cause) { + super(message, cause); + } + + public MissingSideFileException(Throwable cause) { + super(cause); + } +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java new file mode 100644 index 0000000000..9e8565b507 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/CommandMapper.java @@ -0,0 +1,44 @@ +/* + * 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.container.dataaccessscript; + +/** + * This class maps service APIs with the DAS config commands + * the logic can be more of some code generation tool type + * At present a simple mapping is introduced + */ +public class CommandMapper { + + public static String getCommandName(String serviceAPIName) throws Exception{ + int idx = serviceAPIName.indexOf("get"); + if(idx != -1){ + return serviceAPIName.substring(idx+3); + } + else{ + //TODO better way to handle + throw new Exception("invalid service API name"); + } + } + + public static String getServiceAPIName(String commandName){ + //TODO to be implemented + return null; + } +} + diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java new file mode 100644 index 0000000000..1302315eeb --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessEngine.java @@ -0,0 +1,65 @@ +/* + * 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.container.dataaccessscript; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.apache.tuscany.das.rdb.DAS; + +public class DataAccessEngine { + public final static String SEPARATOR = ":"; + //For RDB + public final static String RDB = "rdb"; + public final static String SELECT = "select"; + public final static String DELETE = "delete"; + public final static String UPDATE = "update"; + public final static String INSERT = "insert"; + public final static String PROCEDURE = "procedure"; + + //For XQUERY + public final static String XQUERY = "xquery"; + //... + //TODO...this is the place to accomodate different data access mechanisms + + public final static String DERBY = "derby"; + public final static String MySQL = "mysql"; + //... + //TODO ...this is the place to accomodate different databases + + public DAS getDAS(String dataAccessType, String dasConfig){ + if(dataAccessType.equals(RDB)){ + return DAS.FACTORY.createDAS(new ByteArrayInputStream(dasConfig.getBytes()));//i18n? //do you need cl information here + } + else{ + //TODO ..for different mechs - need to implement + return null; + } + } + + public DAS getDAS(String dataAccessType, InputStream dasConfigStream){ + if(dataAccessType.equals(RDB)){ + return DAS.FACTORY.createDAS(dasConfigStream);//i18n? + } + else{ + //TODO ..for different mechs - need to implement + return null; + } + } +} diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java new file mode 100644 index 0000000000..22300b4e56 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessInstanceImpl.java @@ -0,0 +1,328 @@ +/* + * 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.container.dataaccessscript; + +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +import org.apache.tuscany.container.dataaccess.DataAccessInstance; +import org.apache.tuscany.das.rdb.Command; +import org.apache.tuscany.das.rdb.DAS; + +import commonj.sdo.DataObject; +/** + * An invokeable instance of a DataAccess config script. + */ +public class DataAccessInstanceImpl implements DataAccessInstance { + + private DAS dasInstance; + private String dataAccessType; + private String tableName = null; + private String genericSelectForRoot = "select * from "+ tableName; + + public DataAccessInstanceImpl(DAS das, String dataAccessType){ + this.dasInstance = das; + this.dataAccessType = dataAccessType; + } + + public Object invokeFunction(String functionName, Object[] args, Class returnType) { + Command cmd = null; + //System.out.println("functionName:"+functionName); + + Vector paramsList = null; + if(args != null){ + try{ + paramsList = (Vector)args[0]; + }catch(ClassCastException e){ + //TODO + e.printStackTrace(); + } + } + + //if any of the 3 below - its dynamic way , else , its static way + if(! "execute".equals(functionName) && + ! "executeQuery".equals(functionName) && + ! "applyChanges".equals(functionName)){ + //try for static way + try{ + String commandName = CommandMapper.getCommandName(functionName); + //System.out.println("commandName:"+commandName); + if(paramsList != null && paramsList.size()>1 && + paramsList.get(1).toString().indexOf(DataAccessEngine.SEPARATOR) == -1){ + cmd = dasInstance.getCommand(commandName);//got in static way + } + }catch(Exception e){ + //TODO + e.printStackTrace();//if command not found in config or other exception + } + } + else{//dynamic way + //upto below statement is generic DAS..but select,etc..are particular + //to RDBDAS + //TODO if no matching command is found + if(!((String)paramsList.get(0)).equals("")){ + cmd = dasInstance.getCommand((String)paramsList.get(0));//got in dynamic way + }else{ + //this can be insert or delete as applyChanges-dynamic way + } + } + + //TODO handle better + //this is not error case - as can be case for dynamic/static approach with applyChanges(), where command is + //obtained for generic select at later point. So, don't throw exception + /*if(cmd == null){ + Exception e = new Exception("No command could be found!"); + e.printStackTrace(); + }*/ + + //Particular to RDBDAS + if(this.dataAccessType.equals(DataAccessEngine.RDB)){ + DataObject dataObj = null; + dataObj = invokeRDBDASFunction(cmd, paramsList); + return dataObj; + } + + //TODO ...implement for other than RDB + return null; + } + + private DataObject invokeRDBDASSelect(Command cmd, Vector paramsList){ + DataObject dObject = null; + + if(paramsList != null && paramsList.size()>=3){ + Map inParams = new Hashtable(); + inParams = (Map)paramsList.get(2); + DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd); + } + dObject = cmd.executeQuery(); + if(dObject == null){ + System.out.println("null result returned in select"); + } + //dasInstance.releaseResources(); + return dObject; + } + + private void invokeRDBDASDeleteExecute(Command cmd, Vector paramsList){ + if(paramsList != null && paramsList.size()>=3){ + Map inParams = new Hashtable(); + inParams = (Map)paramsList.get(2); + DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd); + } + cmd.execute(); + //dasInstance.releaseResources(); + } + + private void invokeRDBDASDeleteApplyChanges(Command cmd, Vector paramsList){ + if(paramsList != null && paramsList.size()>=3){ + String paramsTableName = paramsList.get(1).toString(); + int idx = paramsTableName.indexOf(DataAccessEngine.SEPARATOR); + tableName = paramsTableName.substring(idx+1); + genericSelectForRoot = "select * from "+ tableName; + //System.out.println("genericSelectForRoot:"+genericSelectForRoot); + cmd = dasInstance.createCommand(genericSelectForRoot); + + //set WHERE params + if(paramsList.get(2) instanceof Map){ + Map whereParams = (Map)paramsList.get(2); + + String whereStr = null; + //TODO needs better way + if(whereParams != null && whereParams.size()> 0){ + for (Iterator i = whereParams.keySet().iterator(); i.hasNext();) { + String idxDel = (String)i.next(); + + Object val = whereParams.get(idxDel); + whereStr = tableName+"["+val+"]"; + //System.out.println("whereStr:"+whereStr); + } + DataObject root = cmd.executeQuery(); + DataObject dObj = root.getDataObject(whereStr); + dObj.delete(); + dasInstance.applyChanges(root); + } + } + } + + //dasInstance.releaseResources(); + } + + private void invokeRDBDASInsertExecute(Command cmd, Vector paramsList){ + if(paramsList != null && paramsList.size()>=3){ + Map inParams = new Hashtable(); + inParams = (Map)paramsList.get(2); + DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd); + } + //System.out.println("before execute insert"); + + cmd.execute(); + //dasInstance.releaseResources(); + } + + private void invokeRDBDASInsertApplyChanges(Command cmd, Vector paramsList){ + if(paramsList != null && paramsList.size()>=3){ + String paramsTableName = paramsList.get(1).toString(); + int idx = paramsTableName.indexOf(DataAccessEngine.SEPARATOR); + tableName = paramsTableName.substring(idx+1); + genericSelectForRoot = "select * from "+ tableName; + //System.out.println("genericSelectForRoot:"+genericSelectForRoot); + cmd = dasInstance.createCommand(genericSelectForRoot); + + //set INSERT params + if(paramsList.get(2) instanceof Map){ + Map insertParams = (Map)paramsList.get(2); + DataObject root = cmd.executeQuery(); + DataObject insertObj = root.createDataObject(tableName); + + if(insertParams != null && insertParams.size()> 0){ + for (Iterator i = insertParams.keySet().iterator(); i.hasNext();) { + String idxIns = (String)i.next(); + + Object val = insertParams.get(idxIns); + //System.out.println(idxIns+","+val+val.getClass().getName()); + insertObj.set(idxIns, val); + } + + } + + dasInstance.applyChanges(root); + } + } + //dasInstance.releaseResources(); + } + + private void invokeRDBDASUpdate(Command cmd, Vector paramsList){ + if(paramsList != null && paramsList.size()>=4){ + String paramsTableName = paramsList.get(1).toString(); + int idx = paramsTableName.indexOf(DataAccessEngine.SEPARATOR); + tableName = paramsTableName.substring(idx+1); + genericSelectForRoot = "select * from "+ tableName; + //System.out.println("genericSelectForRoot:"+genericSelectForRoot); + cmd = dasInstance.createCommand(genericSelectForRoot); + + //set UPD and WHERE params + if(paramsList.get(2) instanceof Map){ + Map updParams = (Map)paramsList.get(2); + + if(paramsList.get(3) instanceof Map){ + Map whereParams = (Map)paramsList.get(3); + DataObject root = DataAccessUtils.fromJavaToRDBDASNameBased(updParams, whereParams, cmd, tableName); + dasInstance.applyChanges(root); + } + } + } + //dasInstance.releaseResources(); + } + + private DataObject invokeRDBDASProcedure(Command cmd, Vector paramsList){ + DataObject dataObj = null; + if(paramsList != null && paramsList.size()>=3){ + //set IN params + if(paramsList.get(2) instanceof Map){ + Map inParams = (Map)paramsList.get(2); + DataAccessUtils.fromJavaToRDBDASPositionBased(inParams, cmd); + } + + dataObj = cmd.executeQuery(); + + if(paramsList.size()>=4 && paramsList.get(3) instanceof Map){ + try{ + Map outParams = (Map)paramsList.get(3); + DataAccessUtils.fromRDBDASToJavaPositionBased(outParams, cmd); + paramsList.add(3, outParams); + }catch(Exception e){ + //TODO what if map is not of proper type + e.printStackTrace(); + } + } + + //System.out.println("executed SP.."); + } + //dasInstance.releaseResources(); + return dataObj; // NOPMD + } + + //this method detects further what exact DAS function needs to be called e.g. applyChanges(), ... + private DataObject invokeRDBDASFunction(Command cmd, Vector paramsList){ + String commandKind = null; + commandKind = (String)paramsList.get(1);//e.g. select, update, - sim. to one in DAS config for current command + + + + //System.out.println("commandKind:"+commandKind); + //static or dynamic both - executeQuery() + if(commandKind.equals(DataAccessEngine.SELECT)){ + DataObject dataObj = invokeRDBDASSelect(cmd, paramsList); + return dataObj; //NOPMD + } + + //Delete can happen through execute() or applyChanges() + if(commandKind.startsWith(DataAccessEngine.DELETE)){ + if(paramsList.get(0).toString().equals("")){ + if(commandKind.indexOf(DataAccessEngine.SEPARATOR) != -1){//this will be applyChanges()-static/dynamic delete:tableName - cmd has no meaning here + invokeRDBDASDeleteApplyChanges(cmd, paramsList); + }else{//this will be execute() - static + invokeRDBDASDeleteExecute(cmd, paramsList); + return null; + } + }else{//this will be execute() - dynamic + invokeRDBDASDeleteExecute(cmd, paramsList); + return null; + } + + return null; + } + + //Insert can happen through execute() or applyChanges() + if(commandKind.startsWith(DataAccessEngine.INSERT)){ + if(paramsList.get(0).toString().equals("")){ + if(commandKind.indexOf(DataAccessEngine.SEPARATOR) != -1){//this will be applyChanges()-static/dynamic + invokeRDBDASInsertApplyChanges(cmd, paramsList); + } + else{//this will be execute() - static + //System.out.println("calling insert execute"); + invokeRDBDASInsertExecute(cmd, paramsList); + return null; + } + } + else{//this will be execute() - dynamic + //System.out.println("calling insert execute"); + invokeRDBDASInsertExecute(cmd, paramsList); + return null; + } + + return null; + } + + if(commandKind.startsWith(DataAccessEngine.UPDATE)){//update:tableName + invokeRDBDASUpdate(cmd, paramsList); //cmd has no meaning here + return null; + } + + if(commandKind.equals(DataAccessEngine.PROCEDURE)){//executeQuery() + DataObject dataObj = invokeRDBDASProcedure(cmd, paramsList); + return dataObj; + } + + + return null; + } +} + diff --git a/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java new file mode 100644 index 0000000000..8d28a753f1 --- /dev/null +++ b/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccessscript/DataAccessUtils.java @@ -0,0 +1,90 @@ +/* + * 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.container.dataaccessscript; + +import java.util.Iterator; +import java.util.Map; + +import org.apache.tuscany.das.rdb.Command; + +import commonj.sdo.DataObject; + +public class DataAccessUtils { + //in SP - when returning result - OUT params are populated here + public static void fromRDBDASToJavaPositionBased(Map outParams, Command cmd){ + if(outParams != null && outParams.size()> 0){ + for (Iterator i = outParams.keySet().iterator(); i.hasNext();) { + Integer odx = (Integer)i.next(); + + //result can be of any Object type + Object outRes = null; + outRes = cmd.getParameter(odx); + + outParams.put(odx, outRes); + } + } + + } + + //in SP when calling executeQuery on SP, IN params are populated here + public static void fromJavaToRDBDASPositionBased(Map inParams, Command cmd){ + if(inParams != null && inParams.size()> 0){ + for (Iterator i = inParams.keySet().iterator(); i.hasNext();) { + Integer idx = (Integer)i.next(); + + Object val = inParams.get(idx); + cmd.setParameter(idx, val); + } + } + } + + //Used in UPDATE + public static DataObject fromJavaToRDBDASNameBased(Map updParams, Map whereParams, Command cmd, String tableName){ + //currently where clause is only for PKs - by convention + String whereStr = null; + //TODO needs better way + if(whereParams != null && whereParams.size()> 0){ + for (Iterator i = whereParams.keySet().iterator(); i.hasNext();) { + String idx = (String)i.next(); + + Object val = whereParams.get(idx); + whereStr = tableName+"["+val+"]"; + //System.out.println("whereStr:"+whereStr); + } + DataObject root = cmd.executeQuery(); + DataObject dObj = root.getDataObject(whereStr); + + if(updParams != null && updParams.size()> 0){ + for (Iterator i = updParams.keySet().iterator(); i.hasNext();) { + String idx = (String)i.next(); + + Object val = updParams.get(idx); + dObj.set(idx, val); + } + + return root; + } + } + + return null; + } + + //TODO ..similarly there will be fromJavaToXQueryDAS()..and revese etc. +} + -- cgit v1.2.3