summaryrefslogtreecommitdiffstats
path: root/sandbox/lresende/backup/container.das/src/main/java/org/apache/tuscany/container/dataaccess/DataAccessInstanceFactory.java
blob: 96b1deab007b4ff35a0008b48ff5a1d7315c6140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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<DataAccessInstance> {
    protected String dasConfig;
    protected InputStream dasConfigStream;
    protected String dataAccessType;
    protected ClassLoader classLoader;
    protected Map<String, ObjectFactory> 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 <p/> 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<String, Class> getResponseClasses(List<Class> services) {
        Map<String, Class> responseClasses = new HashMap<String, Class>();
        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);
    }
}