summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/containers/container.java/src/main/java/org/apache/tuscany/container/java/context/JavaAtomicContext.java
blob: 27581198aafb4fa770542b9f322458e2d3a30fec (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.java.context;

import org.apache.tuscany.core.builder.ObjectFactory;
import org.apache.tuscany.core.context.AtomicContext;
import org.apache.tuscany.core.context.ContextInitException;
import org.apache.tuscany.core.context.QualifiedName;
import org.apache.tuscany.core.context.TargetException;
import org.apache.tuscany.core.context.event.InstanceCreated;
import org.apache.tuscany.core.context.impl.AbstractContext;
import org.apache.tuscany.core.injection.EventInvoker;
import org.apache.tuscany.core.injection.ObjectCallbackException;
import org.apache.tuscany.core.injection.ObjectCreationException;

/**
 * Manages Java component implementation instances
 *
 * @version $Rev$ $Date$
 */
public class JavaAtomicContext extends AbstractContext implements AtomicContext {

    private boolean eagerInit;

    private EventInvoker<Object> initInvoker;

    private EventInvoker<Object> destroyInvoker;

    private boolean stateless;

    // the cached target instance
    private Object cachedTargetInstance;

    // creates a new implementation instance with injected references and properties
    private ObjectFactory objectFactory;

    public JavaAtomicContext(String name, ObjectFactory objectFactory, boolean eagerInit, EventInvoker<Object> initInvoker,
                             EventInvoker<Object> destroyInvoker, boolean stateless) {
        super(name);
        assert (objectFactory != null) : "Object factory was null";
        if (eagerInit && initInvoker == null) {
            ContextInitException e = new ContextInitException("No intialization method found for implementation");
            e.setIdentifier(getName());
            throw e;
        }
        this.objectFactory = objectFactory;

        this.eagerInit = eagerInit;
        this.initInvoker = initInvoker;
        this.destroyInvoker = destroyInvoker;
        this.stateless = stateless;
    }

    public void setName(String name) {
        super.setName(name);
    }

    protected int type;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void init() throws TargetException {
        getInstance(null);
    }

    public void destroy() throws TargetException {
        if (cachedTargetInstance != null) {
            if (destroyInvoker != null) {
                try {
                    destroyInvoker.invokeEvent(cachedTargetInstance);
                } catch (ObjectCallbackException e) {
                    TargetException te = new TargetException(e.getCause());
                    te.setIdentifier(getName());
                    throw te;
                }
            }
        }
        lifecycleState = STARTED;
    }

    public synchronized Object getInstance(QualifiedName qName) throws TargetException {
        //TODO implement returning of proxy and wire chain for service
        if (cachedTargetInstance != null) {
            return cachedTargetInstance; // already cached, just return
        }

        if (getLifecycleState() == ERROR || getLifecycleState() == CONFIG_ERROR) {
            return null;
        }
        synchronized (this) {
            try {
                Object instance = objectFactory.getInstance();
                // handle @Init
                if (initInvoker != null) {
                    initInvoker.invokeEvent(instance);
                }
                publish(new InstanceCreated(this));
                lifecycleState = RUNNING;
                if (stateless) {
                    return instance;
                } else {
                    cachedTargetInstance = instance;  // cache the instance
                    return cachedTargetInstance;
                }
            } catch (ObjectCreationException e) {
                lifecycleState = ERROR;
                TargetException te = new TargetException("Error creating component instance", e);
                te.setIdentifier(getName());
                throw te;
            }
        }

    }

    public Object getTargetInstance() throws TargetException {
        //TODO refactor when getInstance() returns a proxy
        return getInstance(null);
    }

    public boolean isEagerInit() {
        return eagerInit;
    }

    public boolean isDestroyable() {
        return (destroyInvoker != null);
    }

    public void start() throws ContextInitException {
        if (getLifecycleState() != UNINITIALIZED && getLifecycleState() != STOPPED) {
            throw new IllegalStateException("Context must be in UNINITIALIZED state [" + getLifecycleState() + "]");
        }
        if (objectFactory == null) {
            lifecycleState = ERROR;
            ContextInitException e = new ContextInitException("Object factory not found");
            e.setIdentifier(getName());
            throw e;
        }
        lifecycleState = INITIALIZED;
    }

    public void stop() {
        lifecycleState = STOPPED;
    }

}